Fix method isSame in generated Abstract Modules. 31/3831/1
authorTomas Olvecky <tolvecky@cisco.com>
Wed, 18 Dec 2013 17:16:42 +0000 (18:16 +0100)
committerTomas Olvecky <tolvecky@cisco.com>
Wed, 18 Dec 2013 17:42:53 +0000 (18:42 +0100)
When dealing with list of dependencies, == cannot be used. Instead,
both lists must be equal, and each dependency must be == to the old one.

Change-Id: I42d489d99ff7addc9d492f4878e7a13be8d9584a
Signed-off-by: Tomas Olvecky <tolvecky@cisco.com>
opendaylight/config/config-manager/src/test/java/org/opendaylight/controller/config/manager/impl/AbstractConfigTest.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/TemplateFactory.java
opendaylight/config/yang-jmx-generator-plugin/src/main/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/ftl/model/ModuleField.java
opendaylight/config/yang-jmx-generator-plugin/src/main/resources/freeMarker/module_abs_template_new.ftl
opendaylight/config/yang-jmx-generator-plugin/src/test/java/org/opendaylight/controller/config/yangjmxgenerator/plugin/JMXGeneratorFileNamesValidationTest.java

index b0588a0..fa9e0f1 100644 (file)
@@ -115,10 +115,10 @@ public abstract class AbstractConfigTest extends
 
     protected void assertStatus(CommitStatus status, int expectedNewInstances,
             int expectedRecreatedInstances, int expectedReusedInstances) {
-        assertEquals(expectedNewInstances, status.getNewInstances().size());
-        assertEquals(expectedRecreatedInstances, status.getRecreatedInstances()
+        assertEquals("New instances mismatch in " + status, expectedNewInstances, status.getNewInstances().size());
+        assertEquals("Recreated instances mismatch in " + status, expectedRecreatedInstances, status.getRecreatedInstances()
                 .size());
-        assertEquals(expectedReusedInstances, status.getReusedInstances()
+        assertEquals("Reused instances mismatch in " + status, expectedReusedInstances, status.getReusedInstances()
                 .size());
     }
 
index 28e0256..49a20bd 100644 (file)
@@ -26,6 +26,7 @@ import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIf
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.Dependency;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.JavaAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListAttribute;
+import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.TypedAttribute;
 import org.opendaylight.controller.config.yangjmxgenerator.attribute.VoidAttribute;
@@ -623,6 +624,7 @@ public class TemplateFactory {
                 }
 
                 boolean isDependency = false;
+                boolean isListOfDependencies = false;
                 Dependency dependency = null;
                 Annotation overrideAnnotation = new Annotation("Override",
                         Collections.<Parameter> emptyList());
@@ -635,12 +637,15 @@ public class TemplateFactory {
                             .getDependency();
                     annotations.add(Annotation
                             .createRequireIfcAnnotation(dependency.getSie()));
+                    if (attributeIfc instanceof ListDependenciesAttribute) {
+                        isListOfDependencies = true;
+                    }
                 }
 
                 String varName = BindingGeneratorUtil
                         .parseToValidParamName(attrEntry.getKey());
                 moduleFields.add(new ModuleField(type, varName, attributeIfc
-                        .getUpperCaseCammelCase(), nullableDefaultWrapped, isDependency, dependency));
+                        .getUpperCaseCammelCase(), nullableDefaultWrapped, isDependency, dependency, isListOfDependencies));
 
                 String getterName = "get"
                         + attributeIfc.getUpperCaseCammelCase();
@@ -657,10 +662,16 @@ public class TemplateFactory {
                             .createDescriptionAnnotation(attributeIfc.getNullableDescription()));
                 }
 
+                String setterBody = "this." + varName + " = " + varName + ";";
+                if (isListOfDependencies) {
+                    String nullCheck = String.format("if (%s == null) throw new IllegalArgumentException(\"Null not supported\");%n",
+                            varName);
+                    setterBody = nullCheck + setterBody;
+                }
                 MethodDefinition setter = new MethodDefinition("void",
                         setterName,
                         Lists.newArrayList(new Field(type, varName)),
-                        annotations, "this." + varName + " = " + varName + ";");
+                        annotations, setterBody);
                 setter.setJavadoc(attributeIfc.getNullableDescription());
 
                 methods.add(getter);
index 5624e16..aff7af2 100644 (file)
@@ -17,12 +17,12 @@ import java.util.List;
 public class ModuleField extends Field {
 
     private final String nullableDefault, attributeName;
-    private final boolean dependent;
+    private final boolean dependent, isListOfDependencies;
     private final Dependency dependency;
 
-    public ModuleField(List<String> modifiers, String type, String name,
+    private ModuleField(List<String> modifiers, String type, String name,
             String attributeName, String nullableDefault, boolean isDependency,
-            Dependency dependency) {
+            Dependency dependency, boolean isListOfDependencies) {
         super(modifiers, type, name);
         this.dependent = isDependency;
         this.dependency = dependency;
@@ -32,12 +32,13 @@ public class ModuleField extends Field {
             nullableDefault = "new " + ArrayList.class.getName() + generics + "()";
         }
         this.nullableDefault = nullableDefault;
+        this.isListOfDependencies = isListOfDependencies;
     }
 
     public ModuleField(String type, String name, String attributeName,
-            String nullableDefault, boolean isDependency, Dependency dependency) {
+            String nullableDefault, boolean isDependency, Dependency dependency, boolean isListOfDependencies) {
         this(Collections.<String> emptyList(), type, name, attributeName,
-                nullableDefault, isDependency, dependency);
+                nullableDefault, isDependency, dependency, isListOfDependencies);
     }
 
     public Dependency getDependency() {
@@ -52,6 +53,10 @@ public class ModuleField extends Field {
         return dependent;
     }
 
+    public boolean isListOfDependencies() {
+        return isListOfDependencies;
+    }
+
     public String getAttributeName() {
         return attributeName;
     }
index b32e8bc..7192ac6 100644 (file)
@@ -149,10 +149,19 @@ package ${packageName};
             throw new IllegalArgumentException("Parameter 'other' is null");
         }
         <#list moduleFields as field>
-        <#if field.dependent==true>
+        <#if field.dependent==true && field.listOfDependencies == false>
         if (${field.name}Dependency != other.${field.name}Dependency) { // reference to dependency must be same
             return false;
         }
+        <#elseif field.listOfDependencies>
+        if (${field.name}Dependency.equals(other.${field.name}Dependency) == false) {
+            return false;
+        }
+        for (int idx = 0; idx < ${field.name}Dependency.size(); idx++) {
+            if (${field.name}Dependency.get(idx) != other.${field.name}Dependency.get(idx)) {
+                return false;
+            }
+        }
         <#else>
         if (${field.name} == null) {
             if (other.${field.name} != null) {
index b49b17e..e33f1cd 100644 (file)
@@ -42,7 +42,7 @@ public class JMXGeneratorFileNamesValidationTest extends JMXGeneratorTest {
         } catch (RuntimeException e) {
             final Throwable cause = e.getCause();
             assertNotNull(cause);
-            assertTrue(cause instanceof IllegalStateException);
+            assertTrue(cause.toString() + " is unexpected", cause instanceof IllegalStateException);
             assertThat(cause.getMessage(),
                     containsString("Name conflict in generated files"));
             assertThat(cause.getMessage(), containsString("DtoA.java"));