Skip to content

Commit

Permalink
fixed XmlOutputGeneratorTest
Browse files Browse the repository at this point in the history
  • Loading branch information
siom79 committed Mar 4, 2024
1 parent d6c22ee commit 37e7bed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,37 +60,4 @@ public List<CtClass> 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<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);
}

@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);
}
});
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));
}
}
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));
}
}

0 comments on commit 37e7bed

Please sign in to comment.