From 50e47546af58254c3af07dbfc828c63cc3f6e5ab Mon Sep 17 00:00:00 2001 From: Filip Gregor Date: Fri, 13 May 2016 11:42:05 +0200 Subject: [PATCH] Bug 5882: Wrong placement of deprecated annotation Removed @Deprecated from generated Yang in javadoc added deprecated annotation for generated getters and typedefs Change-Id: I251faabd0cdc051a646ead6f4bf65a17509b90c5 Signed-off-by: Filip Gregor --- .../sal/java/api/generator/BaseTemplate.xtend | 2 +- .../java/api/generator/ClassTemplate.xtend | 9 + .../api/generator/test/CompilationTest.java | 51 ++++- .../generator/test/CompilationTestUtils.java | 3 +- .../resources/compilation/bug5882/foo.yang | 201 ++++++++++++++++++ 5 files changed, 259 insertions(+), 7 deletions(-) create mode 100644 binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5882/foo.yang diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend index eba6b3a39d..f39f7a3d1d 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend @@ -18,13 +18,13 @@ import java.util.StringTokenizer import java.util.regex.Pattern import org.opendaylight.yangtools.binding.generator.util.Types import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType +import org.opendaylight.yangtools.sal.binding.model.api.Constant import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature import org.opendaylight.yangtools.sal.binding.model.api.Restrictions import org.opendaylight.yangtools.sal.binding.model.api.Type -import org.opendaylight.yangtools.sal.binding.model.api.Constant import org.opendaylight.yangtools.yang.common.QName abstract class BaseTemplate { diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend index 8dec19fc92..6e1875a632 100644 --- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend @@ -113,6 +113,7 @@ class ClassTemplate extends BaseTemplate { */ def protected generateBody(boolean isInnerClass) ''' «wrapToDocumentation(formatDataForJavaDoc(type))» + «annotationDeclaration» «generateClassDeclaration(isInnerClass)» { «suidDeclaration» «innerClassesDeclarations» @@ -417,6 +418,14 @@ class ClassTemplate extends BaseTemplate { «ENDIF» ''' + def protected annotationDeclaration() ''' + «IF genTO.getAnnotations != null» + «FOR e : genTO.getAnnotations» + @«e.getName» + «ENDFOR» + «ENDIF» + ''' + /** * Template method which generates JAVA constants. * diff --git a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTest.java b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTest.java index 74cd9019ef..583efac18c 100644 --- a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTest.java +++ b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTest.java @@ -8,6 +8,7 @@ package org.opendaylight.yangtools.sal.java.api.generator.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import com.google.common.base.Predicate; @@ -572,17 +573,57 @@ public class CompilationTest extends BaseCompilationTest { } @Test - public void testBug5788() throws Exception { - final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "bug5788"); + public void testBug5882() throws Exception { + final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "bug5882"); assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir()); - final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "bug5788"); + final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "bug5882"); assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir()); - - generateTestSources("/compilation/bug5788", sourcesOutputDir); + generateTestSources("/compilation/bug5882", sourcesOutputDir); // Test if sources are compilable CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir); + final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882); + assertTrue(new File(parent, "FooData.java").exists()); + assertTrue(new File(parent, "TypedefCurrent.java").exists()); + assertTrue(new File(parent, "TypedefDeprecated.java").exists()); + + final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102"; + final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() }); + final Class cls = loader.loadClass(pkg + ".FooData"); + final Class clsContainer = loader.loadClass(pkg + ".ContainerMain"); + final Class clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated"); + final Class clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent"); + final Class clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated"); + final Class clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent"); + final Class clsTypeDef1 = loader.loadClass(pkg + ".Typedef1"); + final Class clsTypeDef2 = loader.loadClass(pkg + ".Typedef2"); + final Class clsTypeDef3 = loader.loadClass(pkg + ".Typedef3"); + assertTrue(clsTypedefDepr.getAnnotations()[0].toString().contains("Deprecated")); + assertTrue(clsTypedefCur.getAnnotations().length == 0); + assertTrue(clsGroupingDepr.getAnnotations()[0].toString().contains("Deprecated")); + assertTrue(clsGroupingCur.getAnnotations().length == 0); + assertTrue(clsTypeDef1.getAnnotations().length == 0); + assertTrue(clsTypeDef3.getAnnotations().length == 0); + assertTrue(clsTypeDef2.getAnnotations()[0].toString().contains("Deprecated")); + + /*methods inside container*/ + assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class)); + assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class)); + assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class)); + assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class)); + assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class)); + assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class)); + + /*methods inside module*/ + assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class)); + assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class)); + assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class)); + assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class)); + assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class)); + assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class)); + assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class)); + CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir); } diff --git a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTestUtils.java b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTestUtils.java index a442b23026..8fc981f155 100644 --- a/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTestUtils.java +++ b/binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTestUtils.java @@ -11,6 +11,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; + import java.io.File; import java.io.FileNotFoundException; import java.lang.reflect.Constructor; @@ -41,12 +42,12 @@ public class CompilationTestUtils { static final File COMPILER_OUTPUT_DIR = new File(COMPILER_OUTPUT_PATH); static final String AUGMENTATION = "interface org.opendaylight.yangtools.yang.binding.Augmentation"; - static final String BASE_PATH = "org" + FS + "opendaylight" + FS + "yang" + FS + "gen" + FS + "v1"; static final String NS_TEST = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "test" + FS + "rev131008"; static final String NS_FOO = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "foo" + FS + "rev131008"; static final String NS_BAR = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "bar" + FS + "rev131008"; static final String NS_BAZ = BASE_PATH + FS + "urn" + FS + "opendaylight" + FS + "baz" + FS + "rev131008"; + static final String NS_BUG5882 = BASE_PATH + FS + "urn" + FS + "yang" + FS + "foo" + FS + "rev160102"; /** * Method to clean resources. It is manually called at the end of each test diff --git a/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5882/foo.yang b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5882/foo.yang new file mode 100644 index 0000000000..e77ac966c5 --- /dev/null +++ b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5882/foo.yang @@ -0,0 +1,201 @@ +module foo { + namespace "urn:yang.foo"; + prefix "foo"; + + revision "2016-01-02" { + } + + typedef typedef1 { + type typedef-deprecated; + description "current"; + } + + typedef typedef2 { + status deprecated; + type typedef1; + description "deprecated"; + } + + typedef typedef3 { + type typedef2; + description "current"; + } + + typedef typedef-deprecated { + status deprecated; + type string; + description "deprecated"; + } + + typedef typedef-current { + status current; + type uint8 { + range "0 .. 100"; + } + description "current"; + } + + container container-main { + leaf container-main-leaf-depr { + status deprecated; + type string; + } + + leaf container-main-leaf-current { + type typedef-current; + } + + list container-main-list-depr { + status deprecated; + key "test"; + leaf test { + type int8; + } + } + + list container-main-list-current { + key "test"; + leaf test { + type int8; + } + } + + choice container-main-choice-depr { + status deprecated; + case depCase { + status deprecated; + leaf one { + type string; + } + } + case depLeaf { + leaf one { + status deprecated; + type string; + } + } + case cur { + leaf two { + type string; + } + } + } + + choice container-main-choice-cur { + case depCase { + status deprecated; + leaf one { + type string; + } + } + case depLeaf { + leaf one { + status deprecated; + type string; + } + } + case cur { + leaf two { + type string; + } + } + } + } + + leaf container-main-leaf-depr { + status deprecated; + type string; + } + + leaf container-main-leaf-current { + type typedef-current; + } + + list container-main-list-depr { + status deprecated; + key "test"; + leaf test { + type int8; + } + } + + list container-main-list-current { + key "test"; + leaf test { + type int8; + } + } + + choice container-main-choice-depr { + status deprecated; + case depCase { + status deprecated; + leaf one { + type string; + } + } + case depLeaf { + leaf one { + status deprecated; + type string; + } + } + case cur { + leaf two { + type string; + } + } + } + + choice container-main-choice-cur { + case depCase { + status deprecated; + leaf one { + type string; + } + } + case depLeaf { + leaf one { + status deprecated; + type string; + } + } + case cur { + leaf two { + type string; + } + } + } + + leaf leaf-deprecated { + status deprecated; + type typedef-deprecated; + } + + leaf leaf-current { + type typedef-current; + } + + grouping grouping-deprecated { + status deprecated; + leaf leaf-deprecated { + status deprecated; + type typedef-deprecated; + } + + leaf leaf-current { + type typedef-current; + } + } + + grouping grouping-current { + leaf leaf-deprecated { + status deprecated; + type typedef-deprecated; + } + + leaf leaf-current { + type typedef-current; + } + } +} \ No newline at end of file -- 2.36.6