2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.sal.java.api.generator.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.BASE_PKG;
13 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.COMPILER_OUTPUT_PATH;
14 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.FS;
15 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.GENERATOR_OUTPUT_PATH;
16 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.NS_BAR;
17 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.NS_BAZ;
18 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.NS_FOO;
19 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.NS_TEST;
20 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertContainsConstructor;
21 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertContainsField;
22 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertContainsMethod;
23 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertContainsRestrictionCheck;
24 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertFilesCount;
25 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertImplementsIfc;
26 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.assertImplementsParameterizedIfc;
27 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.cleanUp;
28 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
29 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
31 import com.google.common.collect.Range;
33 import java.lang.annotation.Annotation;
34 import java.lang.reflect.Field;
35 import java.lang.reflect.Method;
36 import java.lang.reflect.ParameterizedType;
37 import java.lang.reflect.WildcardType;
38 import java.math.BigDecimal;
39 import java.math.BigInteger;
41 import java.net.URLClassLoader;
42 import java.util.ArrayList;
43 import java.util.HashSet;
44 import java.util.List;
45 import org.junit.Test;
46 import org.opendaylight.yangtools.sal.binding.model.api.Type;
47 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
48 import org.opendaylight.yangtools.yang.binding.ChildOf;
49 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
53 * Test correct code generation.
56 public class CompilationTest extends BaseCompilationTest {
59 public void testListGeneration() throws Exception {
60 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "list-gen");
61 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
62 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "list-gen");
63 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
65 generateTestSources("/compilation/list-gen", sourcesOutputDir);
67 // Test if all sources are generated
68 File parent = new File(sourcesOutputDir, NS_TEST);
69 File keyArgs = new File(parent, "KeyArgs.java");
70 File links = new File(parent, "Links.java");
71 File linksBuilder = new File(parent, "LinksBuilder.java");
72 File linksKey = new File(parent, "LinksKey.java");
73 File testData = new File(parent, "TestData.java");
74 assertTrue(keyArgs.exists());
75 assertTrue(links.exists());
76 assertTrue(linksBuilder.exists());
77 assertTrue(linksKey.exists());
78 assertTrue(testData.exists());
79 assertFilesCount(parent, 6);
81 parent = new File(sourcesOutputDir, NS_TEST + FS + "links");
82 File level = new File(parent, "Level.java");
83 File linkGroup = new File(parent, "LinkGroup.java");
84 File node = new File(parent, "Node.java");
85 File nodeBuilder = new File(parent, "NodeBuilder.java");
86 File nodeList = new File(parent, "NodeList.java");
87 File nodeListBuilder = new File(parent, "NodeListBuilder.java");
88 File nodesType = new File(parent, "NodesType.java");
89 assertTrue(level.exists());
90 assertTrue(linkGroup.exists());
91 assertTrue(node.exists());
92 assertTrue(nodeBuilder.exists());
93 assertTrue(nodeList.exists());
94 assertTrue(nodeListBuilder.exists());
95 assertTrue(nodesType.exists());
96 assertFilesCount(parent, 7);
98 // Test if sources are compilable
99 testCompilation(sourcesOutputDir, compiledOutputDir);
101 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
102 Class<?> keyArgsClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.KeyArgs", true, loader);
103 Class<?> linksClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.Links", true, loader);
104 Class<?> linksKeyClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
106 // Test generated 'grouping key-args'
107 assertTrue(keyArgsClass.isInterface());
108 assertEquals(2, keyArgsClass.getDeclaredMethods().length);
109 assertContainsMethod(keyArgsClass, String.class, "getName");
110 assertContainsMethod(keyArgsClass, Integer.class, "getSize");
112 // Test generated 'list links'
113 assertTrue(linksClass.isInterface());
115 assertEquals(6, linksClass.getDeclaredMethods().length);
116 assertImplementsIfc(linksClass, keyArgsClass);
118 // Test list key constructor arguments ordering
119 assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
120 // Test serialVersionUID generation
121 Field suid = assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
122 suid.setAccessible(true);
123 assertEquals(-8829501012356283881L, suid.getLong(null));
125 cleanUp(sourcesOutputDir, compiledOutputDir);
129 public void testAugmentUnderUsesGeneration() throws Exception {
130 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "augment-under-uses");
131 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
132 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "augment-under-uses");
133 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
135 generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
137 // Test if all sources were generated from 'module foo'
138 File parent = new File(sourcesOutputDir, NS_FOO);
139 assertTrue(new File(parent, "Object.java").exists());
140 assertTrue(new File(parent, "ClosedObject.java").exists());
141 assertTrue(new File(parent, "OpenObject.java").exists());
142 assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
143 assertTrue(new File(parent, "PathKeySubobject.java").exists());
144 assertFilesCount(parent, 9);
146 parent = new File(parent, "object");
147 assertTrue(new File(parent, "Nodes.java").exists());
148 assertTrue(new File(parent, "NodesBuilder.java").exists());
149 assertFilesCount(parent, 2);
151 parent = new File(sourcesOutputDir, NS_FOO + FS + "closed");
152 assertFilesCount(parent, 1);
154 parent = new File(parent, "object");
155 assertTrue(new File(parent, "Link1.java").exists());
156 assertTrue(new File(parent, "Link1Builder.java").exists());
157 assertFilesCount(parent, 2);
159 parent = new File(sourcesOutputDir, NS_FOO + FS + "open");
160 assertFilesCount(parent, 1);
162 parent = new File(parent, "object");
163 assertTrue(new File(parent, "Nodes1.java").exists());
164 assertTrue(new File(parent, "Nodes1Builder.java").exists());
165 assertFilesCount(parent, 3);
167 parent = new File(parent, "nodes");
168 assertTrue(new File(parent, "Links.java").exists());
169 assertTrue(new File(parent, "LinksBuilder.java").exists());
170 assertFilesCount(parent, 2);
172 parent = new File(sourcesOutputDir, NS_FOO + FS + "explicit");
173 assertFilesCount(parent, 1);
174 parent = new File(parent, "route");
175 assertFilesCount(parent, 1);
176 parent = new File(parent, "object");
177 assertTrue(new File(parent, "Subobjects.java").exists());
178 assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
179 assertFilesCount(parent, 3);
181 parent = new File(parent, "subobjects");
182 assertFilesCount(parent, 1);
183 parent = new File(parent, "subobject");
184 assertFilesCount(parent, 1);
185 parent = new File(parent, "type");
186 assertTrue(new File(parent, "PathKey.java").exists());
187 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
188 assertFilesCount(parent, 3);
190 parent = new File(parent, "path");
191 assertFilesCount(parent, 1);
192 parent = new File(parent, "key");
193 assertTrue(new File(parent, "PathKey.java").exists());
194 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
195 assertFilesCount(parent, 2);
197 // Test if all sources were generated from 'module bar'
198 parent = new File(sourcesOutputDir, NS_BAR);
199 assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
200 assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
201 assertTrue(new File(parent, "RouteSubobjects.java").exists());
202 assertFilesCount(parent, 5);
204 parent = new File(parent, "route");
205 assertFilesCount(parent, 1);
206 parent = new File(new File(sourcesOutputDir, NS_BAR), "basic");
207 assertFilesCount(parent, 1);
208 parent = new File(parent, "explicit");
209 assertFilesCount(parent, 1);
210 parent = new File(parent, "route");
211 assertFilesCount(parent, 1);
213 parent = new File(parent, "subobjects");
214 assertFilesCount(parent, 2);
215 assertTrue(new File(parent, "SubobjectType.java").exists());
217 parent = new File(parent, "subobject");
218 assertFilesCount(parent, 1);
220 parent = new File(parent, "type");
221 assertTrue(new File(parent, "IpPrefix.java").exists());
222 assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
223 assertTrue(new File(parent, "Label.java").exists());
224 assertTrue(new File(parent, "LabelBuilder.java").exists());
225 assertFilesCount(parent, 4);
227 // Test if sources are compilable
228 testCompilation(sourcesOutputDir, compiledOutputDir);
230 cleanUp(sourcesOutputDir, compiledOutputDir);
234 public void testAugmentOfAugmentGeneration() throws Exception {
235 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "aug-of-aug");
236 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
237 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "aug-of-aug");
238 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
240 generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
242 // Test if all sources were generated from 'module foo'
243 File parent = new File(sourcesOutputDir, NS_FOO);
244 File fooListener = new File(parent, "FooListener.java");
245 File pathAttributes = new File(parent, "PathAttributes.java");
246 File update = new File(parent, "Update.java");
247 File updateBuilder = new File(parent, "UpdateBuilder.java");
248 assertTrue(fooListener.exists());
249 assertTrue(pathAttributes.exists());
250 assertTrue(update.exists());
251 assertTrue(updateBuilder.exists());
252 assertFilesCount(parent, 6);
254 parent = new File(sourcesOutputDir, NS_FOO + FS + "path");
255 assertFilesCount(parent, 1);
256 parent = new File(parent, "attributes");
257 assertFilesCount(parent, 2);
258 File origin = new File(parent, "Origin.java");
259 File originBuilder = new File(parent, "OriginBuilder.java");
260 assertTrue(origin.exists());
261 assertTrue(originBuilder.exists());
263 parent = new File(sourcesOutputDir, NS_FOO + FS + "update");
264 assertFilesCount(parent, 2);
265 pathAttributes = new File(parent, "PathAttributes.java");
266 File pathAttributesBuilder = new File(parent, "PathAttributesBuilder.java");
267 assertTrue(pathAttributes.exists());
268 assertTrue(pathAttributesBuilder.exists());
270 // Test if all sources were generated from 'module bar'
271 parent = new File(sourcesOutputDir, NS_BAR);
272 File destination = new File(parent, "Destination.java");
273 File pathAttributes1 = new File(parent, "PathAttributes1.java");
274 File pathAttributes1Builder = new File(parent, "PathAttributes1Builder.java");
275 assertTrue(destination.exists());
276 assertTrue(pathAttributes1.exists());
277 assertTrue(pathAttributes1Builder.exists());
278 assertFilesCount(parent, 5);
280 parent = new File(sourcesOutputDir, NS_BAR + FS + "destination");
281 assertFilesCount(parent, 2);
282 File destinationType = new File(parent, "DestinationType.java");
283 assertTrue(destinationType.exists());
285 parent = new File(parent, "destination");
286 assertFilesCount(parent, 1);
287 parent = new File(parent, "type");
288 assertFilesCount(parent, 2);
289 File destinationIpv4 = new File(parent, "DestinationIp.java");
290 File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
291 assertTrue(destinationIpv4.exists());
292 assertTrue(destinationIpv4Builder.exists());
294 parent = new File(sourcesOutputDir, NS_BAR + FS + "update");
295 assertFilesCount(parent, 1);
296 parent = new File(parent, "path");
297 assertFilesCount(parent, 1);
298 parent = new File(parent, "attributes");
299 File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
300 File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
301 assertTrue(mpUnreachNlri.exists());
302 assertTrue(mpUnreachNlriBuilder.exists());
303 assertFilesCount(parent, 3);
305 parent = new File(parent, "mp");
306 assertFilesCount(parent, 1);
307 parent = new File(parent, "unreach");
308 assertFilesCount(parent, 1);
309 parent = new File(parent, "nlri");
310 File withdrawnRoutes = new File(parent, "WithdrawnRoutes.java");
311 File withdrawnRoutesBuilder = new File(parent, "WithdrawnRoutesBuilder.java");
312 assertTrue(withdrawnRoutes.exists());
313 assertTrue(withdrawnRoutesBuilder.exists());
314 assertFilesCount(parent, 2);
316 // Test if all sources were generated from 'module baz'
317 parent = new File(sourcesOutputDir, NS_BAZ);
318 assertFilesCount(parent, 2);
319 File linkstateDestination = new File(parent, "LinkstateDestination.java");
320 assertTrue(linkstateDestination.exists());
322 parent = new File(sourcesOutputDir, NS_BAZ + FS + "update");
323 assertFilesCount(parent, 1);
324 parent = new File(parent, "path");
325 assertFilesCount(parent, 1);
326 parent = new File(parent, "attributes");
327 assertFilesCount(parent, 1);
328 parent = new File(parent, "mp");
329 assertFilesCount(parent, 1);
330 parent = new File(parent, "unreach");
331 assertFilesCount(parent, 1);
332 parent = new File(parent, "nlri");
333 assertFilesCount(parent, 1);
334 parent = new File(parent, "withdrawn");
335 assertFilesCount(parent, 1);
336 parent = new File(parent, "routes");
337 assertFilesCount(parent, 1);
338 parent = new File(parent, "destination");
339 assertFilesCount(parent, 1);
340 parent = new File(parent, "type");
341 File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
342 File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
343 assertTrue(destinationLinkstate.exists());
344 assertTrue(destinationLinkstateBuilder.exists());
345 assertFilesCount(parent, 3);
346 parent = new File(parent, "destination");
347 assertFilesCount(parent, 1);
348 parent = new File(parent, "linkstate");
349 File links = new File(parent, "Links.java");
350 File linksBuilder = new File(parent, "LinksBuilder.java");
351 assertTrue(links.exists());
352 assertTrue(linksBuilder.exists());
353 assertFilesCount(parent, 3);
354 parent = new File(parent, "links");
355 File source = new File(parent, "Source.java");
356 File sourceBuilder = new File(parent, "SourceBuilder.java");
357 assertTrue(source.exists());
358 assertTrue(sourceBuilder.exists());
359 assertFilesCount(parent, 3);
360 parent = new File(parent, "source");
361 File address = new File(parent, "Address.java");
362 File addressBuilder = new File(parent, "AddressBuilder.java");
363 assertTrue(address.exists());
364 assertTrue(addressBuilder.exists());
365 assertFilesCount(parent, 2);
367 // Test if sources are compilable
368 testCompilation(sourcesOutputDir, compiledOutputDir);
370 cleanUp(sourcesOutputDir, compiledOutputDir);
374 public void testLeafReturnTypes() throws Exception {
375 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "leaf-return-types");
376 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
377 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "leaf-return-types");
378 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
380 generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
382 File parent = new File(sourcesOutputDir, NS_TEST);
383 assertTrue(new File(parent, "TestData.java").exists());
384 assertTrue(new File(parent, "Nodes.java").exists());
385 assertTrue(new File(parent, "NodesBuilder.java").exists());
386 assertTrue(new File(parent, "Alg.java").exists());
387 assertTrue(new File(parent, "IdUnionBuilder.java").exists());
388 assertFilesCount(parent, 5);
390 // Test if sources are compilable
391 testCompilation(sourcesOutputDir, compiledOutputDir);
393 String pkg = BASE_PKG + ".urn.opendaylight.test.rev131008";
394 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
395 Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
396 Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
398 // Test methods return type
399 byte[] b = new byte[] {};
400 assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
401 assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
402 assertContainsMethod(nodesClass, Boolean.class, "isIdBoolean");
403 assertContainsMethod(nodesClass, BigDecimal.class, "getIdDecimal64");
404 assertContainsMethod(nodesClass, Boolean.class, "isIdEmpty");
405 assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
406 testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
407 testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
408 assertContainsMethod(nodesClass, Byte.class, "getId8");
409 assertContainsMethod(nodesClass, Short.class, "getId16");
410 assertContainsMethod(nodesClass, Integer.class, "getId32");
411 assertContainsMethod(nodesClass, Long.class, "getId64");
412 assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
413 assertContainsMethod(nodesClass, String.class, "getIdString");
414 assertContainsMethod(nodesClass, Short.class, "getIdU8");
415 assertContainsMethod(nodesClass, Integer.class, "getIdU16");
416 assertContainsMethod(nodesClass, Long.class, "getIdU32");
417 assertContainsMethod(nodesClass, BigInteger.class, "getIdU64");
418 assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
420 Object builderObj = builderClass.newInstance();
422 Method m = assertContainsMethod(builderClass, builderClass, "setIdBinary", b.getClass());
423 List<Range<Integer>> lengthConstraints = new ArrayList<>();
424 lengthConstraints.add(Range.closed(1, 10));
425 Object arg = new byte[] {};
426 String expectedMsg = String.format("Invalid length: %s, expected: %s.", arg, lengthConstraints);
427 assertContainsRestrictionCheck(builderObj, m, expectedMsg, arg);
429 m = assertContainsMethod(builderClass, builderClass, "setIdDecimal64", BigDecimal.class);
430 List<Range<BigDecimal>> rangeConstraints = new ArrayList<>();
431 rangeConstraints.add(Range.closed(new BigDecimal("1.5"), new BigDecimal("5.5")));
432 arg = new BigDecimal("1.4");
433 expectedMsg = String.format("Invalid range: %s, expected: %s.", arg, rangeConstraints);
434 assertContainsRestrictionCheck(builderObj, m, expectedMsg, arg);
436 cleanUp(sourcesOutputDir, compiledOutputDir);
440 public void testGenerationContextReferenceExtension() throws Exception {
441 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "context-reference");
442 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
443 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "context-reference");
444 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
446 generateTestSources("/compilation/context-reference", sourcesOutputDir);
448 // Test if all sources are generated
449 File fooParent = new File(sourcesOutputDir, NS_FOO);
450 assertFilesCount(fooParent, 3);
451 assertTrue(new File(fooParent, "FooData.java").exists());
452 assertTrue(new File(fooParent, "Nodes.java").exists());
453 assertTrue(new File(fooParent, "NodesBuilder.java").exists());
455 File barParent = new File(sourcesOutputDir, NS_BAR);
456 assertFilesCount(barParent, 1);
457 assertTrue(new File(barParent, "IdentityClass.java").exists());
459 // Test if sources are compilable
460 testCompilation(sourcesOutputDir, compiledOutputDir);
462 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
463 Class<?> nodesClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
464 Class<?> identityClass = Class
465 .forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
469 identityClass.getConstructor();
470 Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true, loader);
471 assertEquals(baseIdentity, identityClass.getSuperclass());
472 } catch (NoSuchMethodException e) {
473 throw new AssertionError("IdentityClass must have no-arg constructor");
478 Method getId = nodesClass.getMethod("getId");
479 Annotation[] annotations = getId.getAnnotations();
480 assertEquals(1, annotations.length);
481 Annotation routingContext = annotations[0];
482 assertEquals(RoutingContext.class, routingContext.annotationType());
483 } catch (NoSuchMethodException e) {
484 throw new AssertionError("Method getId() not found");
487 cleanUp(sourcesOutputDir, compiledOutputDir);
491 public void compilationTest() throws Exception {
492 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "yang");
493 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
494 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "yang");
495 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
497 generateTestSources("/yang", sourcesOutputDir);
499 // Test if sources are compilable
500 testCompilation(sourcesOutputDir, compiledOutputDir);
502 cleanUp(sourcesOutputDir, compiledOutputDir);
506 public void testBug586() throws Exception {
507 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug586");
508 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
509 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug586");
510 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
512 generateTestSources("/compilation/bug586", sourcesOutputDir);
514 // Test if sources are compilable
515 testCompilation(sourcesOutputDir, compiledOutputDir);
517 cleanUp(sourcesOutputDir, compiledOutputDir);
521 * Test handling nested uses-augmentations.
526 public void testBug1172() throws Exception {
527 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug1172");
528 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
529 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug1172");
530 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
532 generateTestSources("/compilation/bug1172", sourcesOutputDir);
534 // Test if sources are compilable
535 testCompilation(sourcesOutputDir, compiledOutputDir);
537 cleanUp(sourcesOutputDir, compiledOutputDir);
541 * Test if class generated for node from grouping implements ChildOf.
546 public void testBug1377() throws Exception {
547 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug1377");
548 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
549 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug1377");
550 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
552 generateTestSources("/compilation/bug1377", sourcesOutputDir);
554 // Test if sources are compilable
555 testCompilation(sourcesOutputDir, compiledOutputDir);
557 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
558 Class<?> outputActionClass = Class.forName(BASE_PKG
559 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
560 Class<?> actionClass = Class.forName(BASE_PKG + ".urn.test.foo.rev140717.Action", true, loader);
562 // Test generated 'container output-action'
563 assertTrue(outputActionClass.isInterface());
564 assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(), actionClass.getCanonicalName());
566 cleanUp(sourcesOutputDir, compiledOutputDir);
570 public void classNamesColisionTest() throws Exception {
571 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "class-name-collision");
572 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
573 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "class-name-collision");
574 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
576 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
577 testCompilation(sourcesOutputDir, compiledOutputDir);
578 cleanUp(sourcesOutputDir, compiledOutputDir);
581 private void generateTestSources(String resourceDirPath, File sourcesOutputDir) throws Exception {
582 final List<File> sourceFiles = getSourceFiles(resourceDirPath);
583 final SchemaContext context = parser.parseFiles(sourceFiles);
584 final List<Type> types = bindingGenerator.generateTypes(context);
585 final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
586 generator.generateToFile(sourcesOutputDir);
589 private void testReturnTypeIdentityref(Class<?> clazz, String methodName, String returnTypeStr) throws Exception {
591 java.lang.reflect.Type returnType;
593 method = clazz.getMethod(methodName);
594 assertEquals(java.lang.Class.class, method.getReturnType());
595 returnType = method.getGenericReturnType();
596 assertTrue(returnType instanceof ParameterizedType);
597 ParameterizedType pt = (ParameterizedType) returnType;
598 java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
599 assertEquals(1, parameters.length);
600 java.lang.reflect.Type parameter = parameters[0];
601 assertTrue(parameter instanceof WildcardType);
602 WildcardType wildcardType = (WildcardType) parameter;
603 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
604 } catch (NoSuchMethodException e) {
605 throw new AssertionError("Method '" + methodName + "' not found");
609 private void testReturnTypeInstanceIdentitifer(ClassLoader loader, Class<?> clazz, String methodName)
612 Class<?> rawReturnType;
613 java.lang.reflect.Type returnType;
615 method = clazz.getMethod(methodName);
616 rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true, loader);
617 assertEquals(rawReturnType, method.getReturnType());
618 returnType = method.getGenericReturnType();
619 assertTrue(returnType instanceof ParameterizedType);
620 ParameterizedType pt = (ParameterizedType) returnType;
621 java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
622 assertEquals(1, parameters.length);
623 java.lang.reflect.Type parameter = parameters[0];
624 assertTrue(parameter instanceof WildcardType);
625 WildcardType wildcardType = (WildcardType) parameter;
626 assertEquals("?", wildcardType.toString());
627 } catch (NoSuchMethodException e) {
628 throw new AssertionError("Method '" + methodName + "' not found");