Fix star import and enable checkstyle rule to prevent it.
[controller.git] / opendaylight / config / yang-jmx-generator-plugin / src / test / java / org / opendaylight / controller / config / yangjmxgenerator / plugin / JMXGeneratorTest.java
index 0c2678fd53369c65941e71fac21278a11761f3c5..282dff0ec621a50e46ba107d73627ca4152e7aba 100644 (file)
@@ -7,35 +7,12 @@
  */
 package org.opendaylight.controller.config.yangjmxgenerator.plugin;
 
-import static org.hamcrest.CoreMatchers.equalTo;
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-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.junit.matchers.JUnitMatchers.containsString;
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-
-import java.io.File;
-import java.io.IOException;
-import java.nio.charset.Charset;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+import com.google.common.io.Files;
 import org.apache.commons.io.FileUtils;
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
@@ -52,6 +29,9 @@ import org.eclipse.jdt.core.dom.PackageDeclaration;
 import org.eclipse.jdt.core.dom.TypeDeclaration;
 import org.junit.Before;
 import org.junit.Test;
+import org.opendaylight.controller.config.api.DependencyResolver;
+import org.opendaylight.controller.config.api.DependencyResolverFactory;
+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;
@@ -60,16 +40,40 @@ import org.opendaylight.controller.config.spi.ModuleFactory;
 import org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants;
 import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslatorTest;
 import org.opendaylight.controller.config.yangjmxgenerator.ServiceInterfaceEntryTest;
+import org.osgi.framework.BundleContext;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 
-import com.google.common.base.Predicate;
-import com.google.common.collect.Collections2;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-import com.google.common.io.Files;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+import static org.junit.matchers.JUnitMatchers.containsString;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
 
 public class JMXGeneratorTest extends AbstractGeneratorTest {
 
@@ -476,8 +480,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
 
         assertEquals(2, fieldDeclarations.size());
 
-        assertEquals("Incorrenct number of generated methods", 5,
-                visitor.methods.size());
+        assertFactoryMethods(visitor.methods, 8);
         assertEquals("Incorrenct number of generated method descriptions", 0,
                 visitor.methodDescriptions.size());
         assertEquals("Incorrenct number of generated method javadoc", 0,
@@ -485,6 +488,57 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
 
     }
 
+    private void assertFactoryMethods(Set<String> methods, int expectedSize) {
+
+        List<ArgumentAssertion> args = Lists.newArrayList();
+        ArgumentAssertion oldInstanceArg = new ArgumentAssertion(DynamicMBeanWithInstance.class.getCanonicalName(), "old");
+        ArgumentAssertion instanceNameArg = new ArgumentAssertion(String.class.getSimpleName(), "instanceName");
+        ArgumentAssertion dependencyResolverArg = new ArgumentAssertion(DependencyResolver.class.getCanonicalName(), "dependencyResolver");
+        ArgumentAssertion bundleContextArg = new ArgumentAssertion(BundleContext.class.getCanonicalName(), "bundleContext");
+
+        assertMethodPresent(methods, new MethodAssertion(String.class.getSimpleName(), "getImplementationName"));
+
+        args.add(instanceNameArg);
+        args.add(dependencyResolverArg);
+        args.add(bundleContextArg);
+        assertMethodPresent(methods, new MethodAssertion(Module.class.getCanonicalName(), "createModule", args));
+
+        args.add(2, oldInstanceArg);
+        assertMethodPresent(methods, new MethodAssertion(Module.class.getCanonicalName(), "createModule", args));
+
+        args.clear();
+        args.add(oldInstanceArg);
+        assertMethodPresent(methods, new MethodAssertion("org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule", "handleChangedClass", args));
+
+        args.clear();
+        args.add(instanceNameArg);
+        args.add(dependencyResolverArg);
+        args.add(bundleContextArg);
+        assertMethodPresent(methods, new MethodAssertion("org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule", "instantiateModule", args));
+
+
+        args.add(2, new ArgumentAssertion("org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule", "oldModule"));
+        args.add(3, new ArgumentAssertion(AutoCloseable.class.getCanonicalName(), "oldInstance"));
+        assertMethodPresent(methods, new MethodAssertion("org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule", "instantiateModule", args));
+
+        args.clear();
+        args.add(new ArgumentAssertion(DependencyResolverFactory.class.getCanonicalName(), "dependencyResolverFactory"));
+        args.add(bundleContextArg);
+        assertMethodPresent(methods, new MethodAssertion("java.util.Set<org.opendaylight.controller.config.threads.java.NamingThreadFactoryModule>", "getDefaultModules", args));
+
+        args.clear();
+        args.add(new ArgumentAssertion("Class<? extends org.opendaylight.controller.config.api.annotations.AbstractServiceInterface>", "serviceInterface"));
+        assertMethodPresent(methods, new MethodAssertion("boolean", "isModuleImplementingServiceInterface", args));
+
+        assertEquals(methods.size(), expectedSize);
+
+    }
+
+    private void assertMethodPresent(Set<String> methods, 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) {
         assertEquals(PackageTranslatorTest.EXPECTED_PACKAGE_PREFIX
                 + ".threads.java", visitor.packageName);
@@ -536,8 +590,8 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
                 visitor.methodDescriptions.size());
         assertEquals("Incorrenct number of generated method javadoc", 3,
                 visitor.methodJavadoc.size());
-        assertNotNull("Missing javadoc for setMaximumSize method",
-                visitor.methodJavadoc.get("setMaximumSize"));
+        assertNotNull("Missing javadoc for setMaximumSize method " + visitor.methodJavadoc,
+                visitor.methodJavadoc.get("void setMaximumSize(java.lang.Long maximumSize)"));
     }
 
     private void assertDeclaredField(Set<String> fieldDeclarations,
@@ -602,7 +656,7 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
         private String implmts;
         private final Set<String> fieldDeclarations = Sets.newHashSet();
         private final Set<String> constructors = Sets.newHashSet();
-        private final Map<String, String> methods = Maps.newHashMap();
+        private final Set<String> methods = new HashSet<String>();
         private final Map<String, String> requireIfc = Maps.newHashMap();
         private final Map<String, String> methodJavadoc = Maps.newHashMap();
 
@@ -631,10 +685,21 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
             if (node.isConstructor())
                 constructors.add(node.toString());
             else {
-                String methodName = node.getName().toString();
-                if (node.getJavadoc() != null)
-                    methodJavadoc.put(methodName, node.getJavadoc().toString());
-                methods.put(methodName, node.toString());
+                String methodSignature = node.getReturnType2() + " " + node.getName() + "(";
+                boolean first = true;
+                for (Object o : node.parameters()) {
+                    if (first){
+                        first = false;
+                    } else {
+                        methodSignature += ",";
+                    }
+                    methodSignature += o.toString();
+                }
+                methodSignature += ")";
+                methods.add(methodSignature);
+                if (node.getJavadoc() != null) {
+                    methodJavadoc.put(methodSignature, node.getJavadoc().toString());
+                }
             }
             return super.visit(node);
         }
@@ -709,4 +774,53 @@ public class JMXGeneratorTest extends AbstractGeneratorTest {
         return retVal;
     }
 
+    private static class MethodAssertion extends ArgumentAssertion{
+
+        private List<ArgumentAssertion> arguments;
+
+
+        MethodAssertion(String type, String name, List<ArgumentAssertion> arguments) {
+            super(type, name);
+            this.arguments = arguments;
+        }
+
+        MethodAssertion(String type, String name) {
+            this(type, name, Collections.<ArgumentAssertion>emptyList());
+        }
+
+        @Override
+        public String toString() {
+            final StringBuffer sb = new StringBuffer();
+            sb.append(type).append(' ');
+            sb.append(name).append('(');
+
+            int i = 0;
+            for (ArgumentAssertion argument : arguments) {
+                sb.append(argument.type).append(' ');
+                sb.append(argument.name);
+                if(++i != arguments.size())
+                    sb.append(',');
+            }
+            sb.append(')');
+            return sb.toString();
+        }
+    }
+
+    private static class ArgumentAssertion {
+
+        protected final String type, name;
+
+        private ArgumentAssertion(String type, String name) {
+            this.type = type;
+            this.name = name;
+        }
+
+        @Override
+        public String toString() {
+            final StringBuffer sb = new StringBuffer();
+            sb.append(type).append(' ');
+            sb.append(name);
+            return sb.toString();
+        }
+    }
 }