X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fconfig%2Fyang-jmx-generator-plugin%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fyangjmxgenerator%2Fplugin%2FJMXGeneratorTest.java;h=c7a12a391738d901c174c8ac6124bf4880cdea4a;hp=8c2b5d69ebdbd9c722ebc381b69368ccead924a6;hb=b2a7e1a511100f0512095da585c2b717bb0101b5;hpb=304dec89fd7a13b42e821ca51b08bca93d7dc540 diff --git a/opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGeneratorTest.java b/opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGeneratorTest.java index 8c2b5d69eb..c7a12a3917 100644 --- a/opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGeneratorTest.java +++ b/opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGeneratorTest.java @@ -16,8 +16,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -30,6 +28,7 @@ import com.google.common.io.Files; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -41,8 +40,6 @@ import java.util.Map; import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import org.apache.commons.io.FileUtils; -import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.compiler.IProblem; @@ -63,6 +60,7 @@ import org.opendaylight.controller.config.api.DynamicMBeanWithInstance; import org.opendaylight.controller.config.api.annotations.Description; import org.opendaylight.controller.config.api.annotations.RequireInterface; import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation; +import org.opendaylight.controller.config.spi.AbstractModule; import org.opendaylight.controller.config.spi.Module; import org.opendaylight.controller.config.spi.ModuleFactory; import org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants; @@ -123,13 +121,6 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { File targetDir = new File(generatorOutputPath, "target"); generatedResourcesDir = new File(targetDir, "generated-resources"); jmxGenerator.setResourceBaseDir(generatedResourcesDir); - Log mockedLog = mock(Log.class); - doReturn(false).when(mockedLog).isDebugEnabled(); - doNothing().when(mockedLog).debug(any(CharSequence.class)); - doNothing().when(mockedLog).info(any(CharSequence.class)); - doNothing().when(mockedLog).error(any(CharSequence.class), - any(Throwable.class)); - jmxGenerator.setLog(mockedLog); MavenProject project = mock(MavenProject.class); doReturn(generatorOutputPath).when(project).getBasedir(); jmxGenerator.setMavenProject(project); @@ -157,18 +148,19 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { verifyModuleFactoryFile(false); } - private void verifyModuleFactoryFile(boolean shouldBePresent) { + private void verifyModuleFactoryFile(final boolean shouldBePresent) { File factoryFile = new File(generatedResourcesDir, "META-INF" + File.separator + "services" + File.separator + ModuleFactory.class.getName()); - if (!shouldBePresent) + if (!shouldBePresent) { assertFalse("Factory file should not be generated", factoryFile.exists()); - else + } else { assertTrue("Factory file should be generated", factoryFile.exists()); + } } - public static List toFileNames(Collection files) { + public static List toFileNames(final Collection files) { List result = new ArrayList<>(); for (File f : files) { result.add(f.getName()); @@ -278,7 +270,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { new Predicate() { @Override - public boolean apply(File input) { + public boolean apply(final File input) { return input.getName().endsWith("xml"); } }); @@ -287,7 +279,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { new Predicate() { @Override - public boolean apply(File input) { + public boolean apply(final File input) { return input.getName().endsWith("java"); } }); @@ -302,16 +294,21 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { String name = file.getName(); MbeASTVisitor visitor = new MbeASTVisitor(); verifiers.put(name, visitor); - if (name.equals("AbstractDynamicThreadPoolModule.java")) + if (name.equals("AbstractDynamicThreadPoolModule.java")) { abstractDynamicThreadPoolModuleVisitor = visitor; - if (name.equals("AsyncEventBusModuleMXBean.java")) + } + if (name.equals("AsyncEventBusModuleMXBean.java")) { asyncEventBusModuleMXBeanVisitor = visitor; - if (name.equals("AbstractNamingThreadFactoryModuleFactory.java")) + } + if (name.equals("AbstractNamingThreadFactoryModuleFactory.java")) { abstractNamingThreadFactoryModuleFactoryVisitor = visitor; - if (name.equals("AsyncEventBusModule.java")) + } + if (name.equals("AsyncEventBusModule.java")) { asyncEventBusModuleVisitor = visitor; - if (name.equals("EventBusModuleFactory.java")) + } + if (name.equals("EventBusModuleFactory.java")) { eventBusModuleFactoryVisitor = visitor; + } } processGeneratedCode(javaFiles, verifiers); @@ -328,8 +325,8 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { generatedResourcesDir, "META-INF", "services", ModuleFactory.class.getName()); assertThat(moduleFactoryFile.exists(), is(true)); - Set lines = Sets.newHashSet(FileUtils - .readLines(moduleFactoryFile)); + Set lines = Sets.newHashSet(Files + .readLines(moduleFactoryFile, StandardCharsets.UTF_8)); Set expectedLines = Sets.newHashSet(// PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.java.EventBusModuleFactory",// @@ -347,25 +344,25 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } - private void verifyXmlFiles(Collection xmlFiles) throws Exception { + private void verifyXmlFiles(final Collection xmlFiles) throws Exception { ErrorHandler errorHandler = new ErrorHandler() { @Override - public void warning(SAXParseException exception) + public void warning(final SAXParseException exception) throws SAXException { fail("Generated blueprint xml is not well formed " + exception.getMessage()); } @Override - public void fatalError(SAXParseException exception) + public void fatalError(final SAXParseException exception) throws SAXException { fail("Generated blueprint xml is not well formed " + exception.getMessage()); } @Override - public void error(SAXParseException exception) throws SAXException { + public void error(final SAXParseException exception) throws SAXException { fail("Generated blueprint xml is not well formed " + exception.getMessage()); } @@ -385,7 +382,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } - private void assertEventBusModuleFactory(MbeASTVisitor visitor) { + private void assertEventBusModuleFactory(final MbeASTVisitor visitor) { assertEquals(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.java", visitor.packageName); assertEquals("EventBusModuleFactory", visitor.type); @@ -405,7 +402,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { visitor.methodJavadoc.size()); } - private void assertAsyncEventBusModule(MbeASTVisitor visitor) { + private void assertAsyncEventBusModule(final MbeASTVisitor visitor) { assertEquals(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.java", visitor.packageName); assertEquals("AsyncEventBusModule", visitor.type); @@ -426,7 +423,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } private void assertAbstractNamingThreadFactoryModuleFactory( - MbeASTVisitor visitor) { + final MbeASTVisitor visitor) { assertEquals(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.java", visitor.packageName); assertEquals("AbstractNamingThreadFactoryModuleFactory", visitor.type); @@ -449,7 +446,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } - private void assertFactoryMethods(Set methods, int expectedSize) { + private void assertFactoryMethods(final Set methods, final int expectedSize) { List args = Lists.newArrayList(); ArgumentAssertion oldInstanceArg = new ArgumentAssertion(DynamicMBeanWithInstance.class.getCanonicalName(), "old"); @@ -495,12 +492,12 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } - private void assertMethodPresent(Set methods, MethodAssertion methodAssertion) { + private void assertMethodPresent(final Set methods, final MethodAssertion methodAssertion) { assertTrue(String.format("Generated methods did not contain %s, generated methods: %s", methodAssertion.toString(), methods), methods.contains(methodAssertion.toString())); } - private void assertAsyncEventBusModuleMXBean(MbeASTVisitor visitor) { + private void assertAsyncEventBusModuleMXBean(final MbeASTVisitor visitor) { assertEquals(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.java", visitor.packageName); assertEquals("AsyncEventBusModuleMXBean", visitor.type); @@ -510,7 +507,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } - private void assertAbstractDynamicThreadPoolModule(MbeASTVisitor visitor) { + private void assertAbstractDynamicThreadPoolModule(final MbeASTVisitor visitor) { assertEquals(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.java", visitor.packageName); assertNotNull(visitor.javadoc); @@ -520,11 +517,11 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { assertContains(visitor.implmts, PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.java.DynamicThreadPoolModuleMXBean", - Module.class.getCanonicalName(), PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.ScheduledThreadPoolServiceInterface", PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.ThreadPoolServiceInterface"); + assertContains(visitor.extnds, AbstractModule.class.getCanonicalName()); assertEquals(2, visitor.constructors.size()); Set fieldDeclarations = visitor.fieldDeclarations; assertDeclaredField(fieldDeclarations, @@ -538,7 +535,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { assertDeclaredField(fieldDeclarations, "private java.lang.Long coreSize"); assertDeclaredField(fieldDeclarations, "private byte[] binary"); - assertEquals(22, fieldDeclarations.size()); + assertEquals(17, fieldDeclarations.size()); assertEquals(1, visitor.requireIfc.size()); String reqIfc = visitor.requireIfc.get("setThreadfactory"); @@ -546,7 +543,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { assertContains(reqIfc, PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX + ".threads.ThreadFactoryServiceInterface"); - assertEquals("Incorrenct number of generated methods", 27, + assertEquals("Incorrenct number of generated methods", 26, visitor.methods.size()); assertEquals("Incorrenct number of generated method descriptions", 3, visitor.methodDescriptions.size()); @@ -556,8 +553,8 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { visitor.methodJavadoc.get("void setMaximumSize(java.lang.Long maximumSize)")); } - private void assertDeclaredField(Set fieldDeclarations, - String declaration) { + private void assertDeclaredField(final Set fieldDeclarations, + final String declaration) { assertTrue("Missing field " + declaration + ", got: " + fieldDeclarations, fieldDeclarations.contains(declaration + ";\n")); @@ -565,17 +562,17 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { private static class SieASTVisitor extends ASTVisitor { protected String packageName, descriptionAnotValue, sieAnnotValue, - sieAnnotOsgiRegistrationType, type, extnds, javadoc; + sieAnnotOsgiRegistrationType, type, extnds, javadoc; protected Map methodDescriptions = Maps.newHashMap(); @Override - public boolean visit(PackageDeclaration node) { + public boolean visit(final PackageDeclaration node) { packageName = node.getName().toString(); return super.visit(node); } @Override - public boolean visit(NormalAnnotation node) { + public boolean visit(final NormalAnnotation node) { if (node.getTypeName().toString() .equals(Description.class.getCanonicalName())) { if (node.getParent() instanceof TypeDeclaration) { @@ -603,7 +600,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } @Override - public boolean visit(TypeDeclaration node) { + public boolean visit(final TypeDeclaration node) { javadoc = node.getJavadoc() == null ? null : node.getJavadoc() .toString(); type = node.getName().toString(); @@ -623,7 +620,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { private final Map methodJavadoc = Maps.newHashMap(); @Override - public boolean visit(NormalAnnotation node) { + public boolean visit(final NormalAnnotation node) { boolean result = super.visit(node); if (node.getTypeName().toString() .equals(RequireInterface.class.getCanonicalName()) @@ -637,16 +634,16 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } @Override - public boolean visit(FieldDeclaration node) { + public boolean visit(final FieldDeclaration node) { fieldDeclarations.add(node.toString()); return super.visit(node); } @Override - public boolean visit(MethodDeclaration node) { - if (node.isConstructor()) + public boolean visit(final MethodDeclaration node) { + if (node.isConstructor()) { constructors.add(node.toString()); - else { + } else { String methodSignature = node.getReturnType2() + " " + node.getName() + "("; boolean first = true; for (Object o : node.parameters()) { @@ -667,7 +664,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } @Override - public boolean visit(TypeDeclaration node) { + public boolean visit(final TypeDeclaration node) { boolean visit = super.visit(node); List superIfcs = node.superInterfaceTypes(); implmts = superIfcs != null && !superIfcs.isEmpty() ? superIfcs @@ -679,14 +676,14 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { } - private void assertContains(String source, String... contained) { + private void assertContains(final String source, final String... contained) { for (String string : contained) { assertThat(source, containsString(string)); } } - private void processGeneratedCode(Collection files, - Map verifiers) throws IOException { + private void processGeneratedCode(final Collection files, + final Map verifiers) throws IOException { ASTParser parser = ASTParser.newParser(AST.JLS3); Map options = JavaCore.getOptions(); JavaCore.setComplianceOptions(JavaCore.VERSION_1_7, options); @@ -704,27 +701,31 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { for (IProblem c : cu.getProblems()) { // 1610613332 = Syntax error, annotations are only available if // source level is 5.0 - if (c.getID() == 1610613332) + if (c.getID() == 1610613332) { continue; + } // 1610613332 = Syntax error, parameterized types are only // available if source level is 5.0 - if (c.getID() == 1610613329) + if (c.getID() == 1610613329) { continue; - if (c.getID() == 1610613328) // 'for each' statements are only available if source level is 5.0 + } + if (c.getID() == 1610613328) { continue; + } fail("Error in generated source code " + file + ":" + c.getSourceLineNumber() + " id: " + c.getID() + " message:" + c.toString()); } ASTVisitor visitor = verifiers.get(file.getName()); - if (visitor == null) + if (visitor == null) { fail("Unknown generated file " + file.getName()); + } cu.accept(visitor); } } - public static char[] readFileAsChars(File file) throws IOException { + public static char[] readFileAsChars(final File file) throws IOException { List readLines = Files .readLines(file, Charset.forName("utf-8")); char[] retVal = new char[0]; @@ -740,15 +741,15 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { private static class MethodAssertion extends ArgumentAssertion{ - private List arguments; + private final List arguments; - MethodAssertion(String type, String name, List arguments) { + MethodAssertion(final String type, final String name, final List arguments) { super(type, name); this.arguments = arguments; } - MethodAssertion(String type, String name) { + MethodAssertion(final String type, final String name) { this(type, name, Collections.emptyList()); } @@ -762,8 +763,9 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { for (ArgumentAssertion argument : arguments) { sb.append(argument.type).append(' '); sb.append(argument.name); - if(++i != arguments.size()) + if(++i != arguments.size()) { sb.append(','); + } } sb.append(')'); return sb.toString(); @@ -774,7 +776,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest { protected final String type, name; - private ArgumentAssertion(String type, String name) { + private ArgumentAssertion(final String type, final String name) { this.type = type; this.name = name; }