-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
32 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 32 additions & 62 deletions
94
japicmp/src/test/java/japicmp/output/xml/XmlOutputGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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<JApiClass> jApiClasses = ClassesHelper.compareClasses(options, new ClassesHelper.ClassesGenerator() { | ||
@Override | ||
public List<CtClass> 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<String> lines = Files.readAllLines(htmlReportPath, Charset.forName("UTF-8")); | ||
for (String line : lines) { | ||
if (line.contains("font-family: Verdana;")) { | ||
foundStyleSheet = true; | ||
@Override | ||
public List<CtClass> 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<JApiClass> jApiClasses = new ArrayList<>(); | ||
JarArchiveComparatorOptions jarArchiveComparatorOptions = JarArchiveComparatorOptions.of(options); | ||
JApiClassType classType = new JApiClassType(Optional.<JApiClassType.ClassType>absent(), Optional.<JApiClassType.ClassType>absent(), JApiChangeStatus.REMOVED); | ||
jApiClasses.add(new JApiClass(new JarArchiveComparator(jarArchiveComparatorOptions), "japicmp.Test", Optional.<CtClass>absent(), Optional.<CtClass>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)); | ||
} | ||
} |