/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package testrail.testrail.JunitResults;

import hudson.FilePath;
import hudson.remoting.VirtualChannel;
import hudson.util.DirScanner;
import hudson.util.FileVisitor;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Drew on 3/24/2014.
 */
public class JUnitResults {
    private FilePath baseDir;
    private PrintStream logger;
    private String[] Files;
    private List<Testsuite> Suites;

    public JUnitResults(FilePath baseDir, String fileMatchers, PrintStream logger) throws IOException, JAXBException, InterruptedException {
        this.baseDir = baseDir;
        this.logger = logger;
        slurpTestResults(fileMatchers);
    }

    public void slurpTestResults(String fileMatchers) throws IOException, JAXBException, InterruptedException {
        Suites = new ArrayList<Testsuite>();
        JAXBContext jaxbContext = JAXBContext.newInstance(Testsuites.class);
        final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        final DirScanner scanner = new DirScanner.Glob(fileMatchers, null);
        logger.println("Scanning " + baseDir);

        baseDir.act(new FilePath.FileCallable<Void>() {
            public Void invoke(File f, VirtualChannel channel) throws IOException {
                Testsuite suite = null;
                logger.println("processing " + f.getName());
                scanner.scan(f, new FileVisitor() {
                    @Override
                    public void visit(File file, String s) throws IOException {
                        Testsuites suites = null;
                        logger.println("processing " + file.getName());
                        try {
                            suites = (Testsuites) jaxbUnmarshaller.unmarshal(file);
                        } catch (JAXBException e) {
                            e.printStackTrace();
                        }
                        if (suites.hasSuites()) {
                            for (Testsuite suite : suites.getSuites()) {
                                Suites.add(suite);
                            }
                        }
                    }
                });
                return null;
            }
        });
    }

    public List<Testsuite> getSuites() {
        return this.Suites;
    }

    public String[] getFiles() { return this.Files; }
}

@@ -1831,24 +1831,111 @@
 Context jaxb
+SuiteContext = JAXBContext.newInstance(Testsuite.class);%0A        JAXBContext jaxbSuites
 Context = JA
@@ -2002,24 +2002,29 @@
 shaller jaxb
+Suite
 Unmarshaller
@@ -2030,16 +2030,113 @@
 r = jaxb
+SuiteContext.createUnmarshaller();%0A        final Unmarshaller jaxbSuitesUnmarshaller = jaxbSuites
 Context.
@@ -2426,48 +2426,8 @@
 n %7B%0A
-                Testsuite suite = null;%0A
     
@@ -2649,58 +2649,8 @@
 n %7B%0A
-                        Testsuites suites = null;%0A
     
@@ -2775,16 +2775,27 @@
         
+Testsuites 
 suites =
@@ -2812,16 +2812,22 @@
 es) jaxb
+Suites
 Unmarsha
@@ -2876,36 +2876,123 @@
     
-%7D catch (JAXBException e) %7B%0A
+    if (suites.hasSuites()) %7B%0A                                for (Testsuite suite : suites.getSuites()) %7B%0A        
     
@@ -3019,29 +3019,35 @@
     
-e.printStackTrace();%0A
+Suites.add(suite);%0A        
     
@@ -3096,187 +3096,499 @@
     
-if (suites.hasSuites()) %7B%0A                            for (Testsuite suite : suites.getSuites()) %7B%0A                                Suites.add(suite);%0A                            %7D
+    %7D%0A                        %7D catch (ClassCastException e) %7B%0A                            try %7B%0A                                Testsuite suite = (Testsuite) jaxbSuiteUnmarshaller.unmarshal(file);%0A                                Suites.add(suite);%0A                           %7D catch (JAXBException ex) %7B%0A                               ex.printStackTrace();%0A                           %7D%0A                        %7D catch (JAXBException exc) %7B%0A                            exc.printStackTrace();
 %0A   

Add support for both Testresults and Testresult