Main.java 3.42 KB
Newer Older
Maxime Lefrançois's avatar
Maxime Lefrançois committed
package fr.emse.gitlab.saref;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Layout;
import org.apache.log4j.PatternLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fr.emse.gitlab.saref.entities.TestSuites;
import fr.emse.gitlab.saref.jobs.JobRunner;
import fr.emse.gitlab.saref.jobs.library.ReadOntologies;
import fr.emse.gitlab.saref.jobs.library.RepositoryStructureChecker;
import fr.emse.gitlab.saref.utils.SAREF;

public class Main {

	static final Logger LOG = LoggerFactory.getLogger(Main.class);
	private static final Layout LAYOUT = new PatternLayout("%d{mm:ss,SSS} %t %-5p %c:%L - %m%n");
	private static final org.apache.log4j.Logger ROOT_LOGGER = org.apache.log4j.Logger.getRootLogger();

	private static TestSuites testSuites = new TestSuites();
	private static File directory;
	private static File target;

	public static void main(String[] args) throws IOException, InterruptedException, URISyntaxException, JAXBException {
		if (args.length == 0) {
			directory = new File("").getAbsoluteFile();
		} else if (args.length > 1) {
			throw new IllegalArgumentException(
					"Expecting at most one argument: the location of the SAREF directory where to run the pipeline");
		} else {
			directory = new File(args[0]).getAbsoluteFile();
		}
		
		target = new File(directory, "target");
		FileUtils.forceDelete(target);
		FileUtils.forceMkdir(target);
		File logFile = new File(directory, "target/output.log");
		ROOT_LOGGER.addAppender(new org.apache.log4j.RollingFileAppender(LAYOUT, logFile.getAbsolutePath(), false));

		LOG.info("Starting pipeline");
		JobRunner checker = new RepositoryStructureChecker(directory);
		checker.doJob(testSuites);
		if(testSuites.getErrors() > 0) {
			reportAndExit(-1);
		}
		
		checker = new ReadOntologies(directory);
		checker.doJob(testSuites);
		
		
		reportAndExit(0);
	}

	private static void reportAndExit(int code) {
		try {
			File report = new File(target, "report_output.xml");
			JAXBContext jaxbContext = JAXBContext.newInstance(TestSuites.class);
			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			jaxbMarshaller.marshal(testSuites, report);
			jaxbMarshaller.marshal(testSuites, System.out);
			final StringWriter sw = new StringWriter();
			jaxbMarshaller.marshal(testSuites, sw);
			if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
			    Desktop.getDesktop().browse(new URI(SAREF.BASE + "report.html?report=" + URLEncoder.encode(sw.toString(), "UTF-8")));
			}
		} catch(JAXBException | URISyntaxException | IOException ex) {
			LOG.error("Exception:", ex);
			ex.printStackTrace();
		}
 		System.exit(code);
	}

	// private static void testRead() {
	// try {
	// JAXBContext jaxbContext = JAXBContext.newInstance(TestSuite.class);
	// Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
	//
	// TestCase testCase = (TestCase) jaxbUnmarshaller.unmarshal(target);
	//
	// } catch (JAXBException e) {
	// e.printStackTrace();
	// }
	// }

}