diff --git a/japicmp/src/test/java/japicmp/output/html/HtmlOutputGeneratorTest.java b/japicmp/src/test/java/japicmp/output/html/HtmlOutputGeneratorTest.java index 87509501d..9a27159e1 100644 --- a/japicmp/src/test/java/japicmp/output/html/HtmlOutputGeneratorTest.java +++ b/japicmp/src/test/java/japicmp/output/html/HtmlOutputGeneratorTest.java @@ -4,9 +4,6 @@ import japicmp.cmp.JarArchiveComparatorOptions; import japicmp.config.Options; import japicmp.model.JApiClass; -import japicmp.output.xml.XmlOutput; -import japicmp.output.xml.XmlOutputGenerator; -import japicmp.output.xml.XmlOutputGeneratorOptions; import japicmp.util.*; import javassist.CannotCompileException; import javassist.ClassPool; @@ -63,37 +60,4 @@ public List createNewClasses(ClassPool classPool) throws CannotCompileE assertThat(document.select("#meta-accessmodifier-value").text(), is("PROTECTED")); assertThat(document.select("#warning-missingclasses").text(), containsString("WARNING")); } - - @Test - public void testXmlReport() throws Exception { - JarArchiveComparatorOptions options = new JarArchiveComparatorOptions(); - options.setIncludeSynthetic(true); - List jApiClasses = ClassesHelper.compareClasses(options, new ClassesHelper.ClassesGenerator() { - @Override - public List createOldClasses(ClassPool classPool) throws CannotCompileException { - CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").addToClassPool(classPool); - CtMethodBuilder.create().publicAccess().name("toBeRemoved").returnType(CtClass.booleanType).addToClass(ctClass); - return Collections.singletonList(ctClass); - } - - @Override - public List createNewClasses(ClassPool classPool) throws CannotCompileException, NotFoundException { - CtClass newInterface = CtInterfaceBuilder.create().name("NewInterface").addToClassPool(classPool); - CtClass superclass = CtClassBuilder.create().name("japicmp.Superclass").addToClassPool(classPool); - CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").withSuperclass(superclass).implementsInterface(newInterface).addToClassPool(classPool); - CtMethodBuilder.create().publicAccess().name("newMethod").returnType(CtClass.booleanType).addToClass(ctClass); - CtFieldBuilder.create().type(CtClass.booleanType).name("bField").addToClass(ctClass); - CtConstructorBuilder.create().publicAccess().parameters(new CtClass[] {CtClass.intType, CtClass.booleanType}).exceptions(new CtClass[] {classPool.get("java.lang.Exception")}).addToClass(ctClass); - return Arrays.asList(ctClass, superclass); - } - }); - Options reportOptions = Options.newDefault(); - reportOptions.setIgnoreMissingClasses(true); - reportOptions.setXmlOutputFile(Optional.of(Paths.get(System.getProperty("user.dir"), "target", "report.xml").toString())); - XmlOutputGenerator generator = new XmlOutputGenerator(jApiClasses, reportOptions, new XmlOutputGeneratorOptions()); - - XmlOutput xmlOutput = generator.generate(); - - Files.write(Paths.get(System.getProperty("user.dir"), "target", "report.xml"), xmlOutput.getXmlOutputStream().get().toString().getBytes(StandardCharsets.UTF_8)); - } } diff --git a/japicmp/src/test/java/japicmp/output/xml/XmlOutputGeneratorTest.java b/japicmp/src/test/java/japicmp/output/xml/XmlOutputGeneratorTest.java index 21568175b..4c58882b1 100644 --- a/japicmp/src/test/java/japicmp/output/xml/XmlOutputGeneratorTest.java +++ b/japicmp/src/test/java/japicmp/output/xml/XmlOutputGeneratorTest.java @@ -1,85 +1,55 @@ package japicmp.output.xml; -import japicmp.cmp.JarArchiveComparator; +import japicmp.cmp.ClassesHelper; import japicmp.cmp.JarArchiveComparatorOptions; import japicmp.config.Options; -import japicmp.exception.JApiCmpException; -import japicmp.model.JApiChangeStatus; import japicmp.model.JApiClass; -import japicmp.model.JApiClassType; -import japicmp.util.Optional; +import japicmp.util.*; +import javassist.CannotCompileException; +import javassist.ClassPool; import javassist.CtClass; +import javassist.NotFoundException; import org.junit.Test; -import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.List; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - public class XmlOutputGeneratorTest { @Test - public void testWithHtmlStylesheetOption() throws IOException { - Path stylesheetPath = Paths.get(System.getProperty("user.dir"), "target", XmlOutputGeneratorTest.class.getSimpleName() + "_with.css"); - Path htmlReportPath = Paths.get(System.getProperty("user.dir"), "target", XmlOutputGeneratorTest.class.getSimpleName() + "_with.html"); - String stylesheetContent = "body {font-family: Monospace;}"; - Options options = Options.newDefault(); - options.setHtmlStylesheet(Optional.of(stylesheetPath.toString())); - options.setHtmlOutputFile(Optional.of(htmlReportPath.toString())); - Files.write(stylesheetPath, Collections.singletonList(stylesheetContent), Charset.forName("UTF-8")); - generateHtmlReport(options); - boolean foundStyleSheet = false; - List lines = Files.readAllLines(htmlReportPath, Charset.forName("UTF-8")); - for (String line : lines) { - if (line.contains(stylesheetContent)) { - foundStyleSheet = true; + public void testXmlReport() throws Exception { + JarArchiveComparatorOptions options = new JarArchiveComparatorOptions(); + options.setIncludeSynthetic(true); + List jApiClasses = ClassesHelper.compareClasses(options, new ClassesHelper.ClassesGenerator() { + @Override + public List createOldClasses(ClassPool classPool) throws CannotCompileException { + CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").addToClassPool(classPool); + CtMethodBuilder.create().publicAccess().name("toBeRemoved").returnType(CtClass.booleanType).addToClass(ctClass); + return Collections.singletonList(ctClass); } - } - assertThat(foundStyleSheet, is(true)); - } - @Test - public void testWithoutHtmlStylesheetOption() throws IOException { - Path htmlReportPath = Paths.get(System.getProperty("user.dir"), "target", XmlOutputGeneratorTest.class.getSimpleName() + "_without.html"); - Options options = Options.newDefault(); - options.setHtmlOutputFile(Optional.of(htmlReportPath.toString())); - generateHtmlReport(options); - boolean foundStyleSheet = false; - List lines = Files.readAllLines(htmlReportPath, Charset.forName("UTF-8")); - for (String line : lines) { - if (line.contains("font-family: Verdana;")) { - foundStyleSheet = true; + @Override + public List createNewClasses(ClassPool classPool) throws CannotCompileException, NotFoundException { + CtClass newInterface = CtInterfaceBuilder.create().name("NewInterface").addToClassPool(classPool); + CtClass superclass = CtClassBuilder.create().name("japicmp.Superclass").addToClassPool(classPool); + CtClass ctClass = CtClassBuilder.create().name("japicmp.Test").withSuperclass(superclass).implementsInterface(newInterface).addToClassPool(classPool); + CtMethodBuilder.create().publicAccess().name("newMethod").returnType(CtClass.booleanType).addToClass(ctClass); + CtFieldBuilder.create().type(CtClass.booleanType).name("bField").addToClass(ctClass); + CtConstructorBuilder.create().publicAccess().parameters(new CtClass[] {CtClass.intType, CtClass.booleanType}).exceptions(new CtClass[] {classPool.get("java.lang.Exception")}).addToClass(ctClass); + return Arrays.asList(ctClass, superclass); } - } - assertThat(foundStyleSheet, is(true)); - } + }); + Options reportOptions = Options.newDefault(); + reportOptions.setIgnoreMissingClasses(true); + reportOptions.setXmlOutputFile(Optional.of(Paths.get(System.getProperty("user.dir"), "target", "report.xml").toString())); + XmlOutputGenerator generator = new XmlOutputGenerator(jApiClasses, reportOptions, new XmlOutputGeneratorOptions()); - @Test(expected = JApiCmpException.class) - public void testWithNotExistingHtmlStylesheetOption() throws IOException { - Path stylesheetPath = Paths.get(System.getProperty("user.dir"), "target", XmlOutputGeneratorTest.class.getSimpleName() + "_not_existing.css"); - Path htmlReportPath = Paths.get(System.getProperty("user.dir"), "target", XmlOutputGeneratorTest.class.getSimpleName() + "_with.html"); - Options options = Options.newDefault(); - options.setHtmlStylesheet(Optional.of(stylesheetPath.toString())); - options.setHtmlOutputFile(Optional.of(htmlReportPath.toString())); - generateHtmlReport(options); - } - - private void generateHtmlReport(Options options) { - List jApiClasses = new ArrayList<>(); - JarArchiveComparatorOptions jarArchiveComparatorOptions = JarArchiveComparatorOptions.of(options); - JApiClassType classType = new JApiClassType(Optional.absent(), Optional.absent(), JApiChangeStatus.REMOVED); - jApiClasses.add(new JApiClass(new JarArchiveComparator(jarArchiveComparatorOptions), "japicmp.Test", Optional.absent(), Optional.absent(), JApiChangeStatus.NEW, classType)); - XmlOutputGeneratorOptions xmlOutputGeneratorOptions = new XmlOutputGeneratorOptions(); - xmlOutputGeneratorOptions.setCreateSchemaFile(true); - XmlOutputGenerator generator = new XmlOutputGenerator(jApiClasses, options, xmlOutputGeneratorOptions); XmlOutput xmlOutput = generator.generate(); - XmlOutputGenerator.writeToFiles(options, xmlOutput); + + Files.write(Paths.get(System.getProperty("user.dir"), "target", "report.xml"), xmlOutput.getXmlOutputStream().get().toString().getBytes(StandardCharsets.UTF_8)); } }