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;
30 import com.google.common.collect.ImmutableSet;
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.Collections;
44 import java.util.Comparator;
45 import java.util.List;
46 import org.junit.Test;
47 import org.opendaylight.yangtools.sal.binding.model.api.Type;
48 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
49 import org.opendaylight.yangtools.yang.binding.ChildOf;
50 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
51 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
54 * Test correct code generation.
57 public class CompilationTest extends BaseCompilationTest {
60 public void testListGeneration() throws Exception {
61 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "list-gen");
62 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
63 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "list-gen");
64 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
66 generateTestSources("/compilation/list-gen", sourcesOutputDir);
68 // Test if all sources are generated
69 File parent = new File(sourcesOutputDir, NS_TEST);
70 File keyArgs = new File(parent, "KeyArgs.java");
71 File links = new File(parent, "Links.java");
72 File linksBuilder = new File(parent, "LinksBuilder.java");
73 File linksKey = new File(parent, "LinksKey.java");
74 File testData = new File(parent, "TestData.java");
75 assertTrue(keyArgs.exists());
76 assertTrue(links.exists());
77 assertTrue(linksBuilder.exists());
78 assertTrue(linksKey.exists());
79 assertTrue(testData.exists());
80 assertFilesCount(parent, 6);
82 parent = new File(sourcesOutputDir, NS_TEST + FS + "links");
83 File level = new File(parent, "Level.java");
84 File linkGroup = new File(parent, "LinkGroup.java");
85 File node = new File(parent, "Node.java");
86 File nodeBuilder = new File(parent, "NodeBuilder.java");
87 File nodeList = new File(parent, "NodeList.java");
88 File nodeListBuilder = new File(parent, "NodeListBuilder.java");
89 File nodesType = new File(parent, "NodesType.java");
90 assertTrue(level.exists());
91 assertTrue(linkGroup.exists());
92 assertTrue(node.exists());
93 assertTrue(nodeBuilder.exists());
94 assertTrue(nodeList.exists());
95 assertTrue(nodeListBuilder.exists());
96 assertTrue(nodesType.exists());
97 assertFilesCount(parent, 7);
99 // Test if sources are compilable
100 testCompilation(sourcesOutputDir, compiledOutputDir);
102 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
103 Class<?> keyArgsClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.KeyArgs", true, loader);
104 Class<?> linksClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.Links", true, loader);
105 Class<?> linksKeyClass = Class.forName(BASE_PKG + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
107 // Test generated 'grouping key-args'
108 assertTrue(keyArgsClass.isInterface());
109 assertEquals(2, keyArgsClass.getDeclaredMethods().length);
110 assertContainsMethod(keyArgsClass, String.class, "getName");
111 assertContainsMethod(keyArgsClass, Integer.class, "getSize");
113 // Test generated 'list links'
114 assertTrue(linksClass.isInterface());
116 assertEquals(6, linksClass.getDeclaredMethods().length);
117 assertImplementsIfc(linksClass, keyArgsClass);
119 // Test list key constructor arguments ordering
120 assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
121 // Test serialVersionUID generation
122 Field suid = assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
123 suid.setAccessible(true);
124 assertEquals(-8829501012356283881L, suid.getLong(null));
126 cleanUp(sourcesOutputDir, compiledOutputDir);
130 public void testAugmentUnderUsesGeneration() throws Exception {
131 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "augment-under-uses");
132 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
133 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "augment-under-uses");
134 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
136 generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
138 // Test if all sources were generated from 'module foo'
139 File parent = new File(sourcesOutputDir, NS_FOO);
140 assertTrue(new File(parent, "Object.java").exists());
141 assertTrue(new File(parent, "ClosedObject.java").exists());
142 assertTrue(new File(parent, "OpenObject.java").exists());
143 assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
144 assertTrue(new File(parent, "PathKeySubobject.java").exists());
145 assertFilesCount(parent, 9);
147 parent = new File(parent, "object");
148 assertTrue(new File(parent, "Nodes.java").exists());
149 assertTrue(new File(parent, "NodesBuilder.java").exists());
150 assertFilesCount(parent, 2);
152 parent = new File(sourcesOutputDir, NS_FOO + FS + "closed");
153 assertFilesCount(parent, 1);
155 parent = new File(parent, "object");
156 assertTrue(new File(parent, "Link1.java").exists());
157 assertTrue(new File(parent, "Link1Builder.java").exists());
158 assertFilesCount(parent, 2);
160 parent = new File(sourcesOutputDir, NS_FOO + FS + "open");
161 assertFilesCount(parent, 1);
163 parent = new File(parent, "object");
164 assertTrue(new File(parent, "Nodes1.java").exists());
165 assertTrue(new File(parent, "Nodes1Builder.java").exists());
166 assertFilesCount(parent, 3);
168 parent = new File(parent, "nodes");
169 assertTrue(new File(parent, "Links.java").exists());
170 assertTrue(new File(parent, "LinksBuilder.java").exists());
171 assertFilesCount(parent, 2);
173 parent = new File(sourcesOutputDir, NS_FOO + FS + "explicit");
174 assertFilesCount(parent, 1);
175 parent = new File(parent, "route");
176 assertFilesCount(parent, 1);
177 parent = new File(parent, "object");
178 assertTrue(new File(parent, "Subobjects.java").exists());
179 assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
180 assertFilesCount(parent, 3);
182 parent = new File(parent, "subobjects");
183 assertFilesCount(parent, 1);
184 parent = new File(parent, "subobject");
185 assertFilesCount(parent, 1);
186 parent = new File(parent, "type");
187 assertTrue(new File(parent, "PathKey.java").exists());
188 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
189 assertFilesCount(parent, 3);
191 parent = new File(parent, "path");
192 assertFilesCount(parent, 1);
193 parent = new File(parent, "key");
194 assertTrue(new File(parent, "PathKey.java").exists());
195 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
196 assertFilesCount(parent, 2);
198 // Test if all sources were generated from 'module bar'
199 parent = new File(sourcesOutputDir, NS_BAR);
200 assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
201 assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
202 assertTrue(new File(parent, "RouteSubobjects.java").exists());
203 assertFilesCount(parent, 5);
205 parent = new File(parent, "route");
206 assertFilesCount(parent, 1);
207 parent = new File(new File(sourcesOutputDir, NS_BAR), "basic");
208 assertFilesCount(parent, 1);
209 parent = new File(parent, "explicit");
210 assertFilesCount(parent, 1);
211 parent = new File(parent, "route");
212 assertFilesCount(parent, 1);
214 parent = new File(parent, "subobjects");
215 assertFilesCount(parent, 2);
216 assertTrue(new File(parent, "SubobjectType.java").exists());
218 parent = new File(parent, "subobject");
219 assertFilesCount(parent, 1);
221 parent = new File(parent, "type");
222 assertTrue(new File(parent, "IpPrefix.java").exists());
223 assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
224 assertTrue(new File(parent, "Label.java").exists());
225 assertTrue(new File(parent, "LabelBuilder.java").exists());
226 assertFilesCount(parent, 4);
228 // Test if sources are compilable
229 testCompilation(sourcesOutputDir, compiledOutputDir);
231 cleanUp(sourcesOutputDir, compiledOutputDir);
235 public void testAugmentOfAugmentGeneration() throws Exception {
236 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "aug-of-aug");
237 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
238 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "aug-of-aug");
239 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
241 generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
243 // Test if all sources were generated from 'module foo'
244 File parent = new File(sourcesOutputDir, NS_FOO);
245 File fooListener = new File(parent, "FooListener.java");
246 File pathAttributes = new File(parent, "PathAttributes.java");
247 File update = new File(parent, "Update.java");
248 File updateBuilder = new File(parent, "UpdateBuilder.java");
249 assertTrue(fooListener.exists());
250 assertTrue(pathAttributes.exists());
251 assertTrue(update.exists());
252 assertTrue(updateBuilder.exists());
253 assertFilesCount(parent, 6);
255 parent = new File(sourcesOutputDir, NS_FOO + FS + "path");
256 assertFilesCount(parent, 1);
257 parent = new File(parent, "attributes");
258 assertFilesCount(parent, 2);
259 File origin = new File(parent, "Origin.java");
260 File originBuilder = new File(parent, "OriginBuilder.java");
261 assertTrue(origin.exists());
262 assertTrue(originBuilder.exists());
264 parent = new File(sourcesOutputDir, NS_FOO + FS + "update");
265 assertFilesCount(parent, 2);
266 pathAttributes = new File(parent, "PathAttributes.java");
267 File pathAttributesBuilder = new File(parent, "PathAttributesBuilder.java");
268 assertTrue(pathAttributes.exists());
269 assertTrue(pathAttributesBuilder.exists());
271 // Test if all sources were generated from 'module bar'
272 parent = new File(sourcesOutputDir, NS_BAR);
273 File destination = new File(parent, "Destination.java");
274 File pathAttributes1 = new File(parent, "PathAttributes1.java");
275 File pathAttributes1Builder = new File(parent, "PathAttributes1Builder.java");
276 assertTrue(destination.exists());
277 assertTrue(pathAttributes1.exists());
278 assertTrue(pathAttributes1Builder.exists());
279 assertFilesCount(parent, 5);
281 parent = new File(sourcesOutputDir, NS_BAR + FS + "destination");
282 assertFilesCount(parent, 2);
283 File destinationType = new File(parent, "DestinationType.java");
284 assertTrue(destinationType.exists());
286 parent = new File(parent, "destination");
287 assertFilesCount(parent, 1);
288 parent = new File(parent, "type");
289 assertFilesCount(parent, 2);
290 File destinationIpv4 = new File(parent, "DestinationIp.java");
291 File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
292 assertTrue(destinationIpv4.exists());
293 assertTrue(destinationIpv4Builder.exists());
295 parent = new File(sourcesOutputDir, NS_BAR + FS + "update");
296 assertFilesCount(parent, 1);
297 parent = new File(parent, "path");
298 assertFilesCount(parent, 1);
299 parent = new File(parent, "attributes");
300 File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
301 File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
302 assertTrue(mpUnreachNlri.exists());
303 assertTrue(mpUnreachNlriBuilder.exists());
304 assertFilesCount(parent, 3);
306 parent = new File(parent, "mp");
307 assertFilesCount(parent, 1);
308 parent = new File(parent, "unreach");
309 assertFilesCount(parent, 1);
310 parent = new File(parent, "nlri");
311 File withdrawnRoutes = new File(parent, "WithdrawnRoutes.java");
312 File withdrawnRoutesBuilder = new File(parent, "WithdrawnRoutesBuilder.java");
313 assertTrue(withdrawnRoutes.exists());
314 assertTrue(withdrawnRoutesBuilder.exists());
315 assertFilesCount(parent, 2);
317 // Test if all sources were generated from 'module baz'
318 parent = new File(sourcesOutputDir, NS_BAZ);
319 assertFilesCount(parent, 2);
320 File linkstateDestination = new File(parent, "LinkstateDestination.java");
321 assertTrue(linkstateDestination.exists());
323 parent = new File(sourcesOutputDir, NS_BAZ + FS + "update");
324 assertFilesCount(parent, 1);
325 parent = new File(parent, "path");
326 assertFilesCount(parent, 1);
327 parent = new File(parent, "attributes");
328 assertFilesCount(parent, 1);
329 parent = new File(parent, "mp");
330 assertFilesCount(parent, 1);
331 parent = new File(parent, "unreach");
332 assertFilesCount(parent, 1);
333 parent = new File(parent, "nlri");
334 assertFilesCount(parent, 1);
335 parent = new File(parent, "withdrawn");
336 assertFilesCount(parent, 1);
337 parent = new File(parent, "routes");
338 assertFilesCount(parent, 1);
339 parent = new File(parent, "destination");
340 assertFilesCount(parent, 1);
341 parent = new File(parent, "type");
342 File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
343 File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
344 assertTrue(destinationLinkstate.exists());
345 assertTrue(destinationLinkstateBuilder.exists());
346 assertFilesCount(parent, 3);
347 parent = new File(parent, "destination");
348 assertFilesCount(parent, 1);
349 parent = new File(parent, "linkstate");
350 File links = new File(parent, "Links.java");
351 File linksBuilder = new File(parent, "LinksBuilder.java");
352 assertTrue(links.exists());
353 assertTrue(linksBuilder.exists());
354 assertFilesCount(parent, 3);
355 parent = new File(parent, "links");
356 File source = new File(parent, "Source.java");
357 File sourceBuilder = new File(parent, "SourceBuilder.java");
358 assertTrue(source.exists());
359 assertTrue(sourceBuilder.exists());
360 assertFilesCount(parent, 3);
361 parent = new File(parent, "source");
362 File address = new File(parent, "Address.java");
363 File addressBuilder = new File(parent, "AddressBuilder.java");
364 assertTrue(address.exists());
365 assertTrue(addressBuilder.exists());
366 assertFilesCount(parent, 2);
368 // Test if sources are compilable
369 testCompilation(sourcesOutputDir, compiledOutputDir);
371 cleanUp(sourcesOutputDir, compiledOutputDir);
375 public void testLeafReturnTypes() throws Exception {
376 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "leaf-return-types");
377 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
378 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "leaf-return-types");
379 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
381 generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
383 File parent = new File(sourcesOutputDir, NS_TEST);
384 assertTrue(new File(parent, "TestData.java").exists());
385 assertTrue(new File(parent, "Nodes.java").exists());
386 assertTrue(new File(parent, "NodesBuilder.java").exists());
387 assertTrue(new File(parent, "Alg.java").exists());
388 assertTrue(new File(parent, "IdUnionBuilder.java").exists());
389 assertFilesCount(parent, 5);
391 // Test if sources are compilable
392 testCompilation(sourcesOutputDir, compiledOutputDir);
394 String pkg = BASE_PKG + ".urn.opendaylight.test.rev131008";
395 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
396 Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
397 Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
399 // Test methods return type
400 byte[] b = new byte[] {};
401 assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
402 assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
403 assertContainsMethod(nodesClass, Boolean.class, "isIdBoolean");
404 assertContainsMethod(nodesClass, BigDecimal.class, "getIdDecimal64");
405 assertContainsMethod(nodesClass, Boolean.class, "isIdEmpty");
406 assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
407 testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
408 testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
409 assertContainsMethod(nodesClass, Byte.class, "getId8");
410 assertContainsMethod(nodesClass, Short.class, "getId16");
411 assertContainsMethod(nodesClass, Integer.class, "getId32");
412 assertContainsMethod(nodesClass, Long.class, "getId64");
413 assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
414 assertContainsMethod(nodesClass, String.class, "getIdString");
415 assertContainsMethod(nodesClass, Short.class, "getIdU8");
416 assertContainsMethod(nodesClass, Integer.class, "getIdU16");
417 assertContainsMethod(nodesClass, Long.class, "getIdU32");
418 assertContainsMethod(nodesClass, BigInteger.class, "getIdU64");
419 assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
421 Object builderObj = builderClass.newInstance();
423 Method m = assertContainsMethod(builderClass, builderClass, "setIdBinary", b.getClass());
424 List<Range<Integer>> lengthConstraints = new ArrayList<>();
425 lengthConstraints.add(Range.closed(1, 10));
426 Object arg = new byte[] {};
427 String expectedMsg = String.format("Invalid length: %s, expected: %s.", arg, lengthConstraints);
428 assertContainsRestrictionCheck(builderObj, m, expectedMsg, arg);
430 m = assertContainsMethod(builderClass, builderClass, "setIdDecimal64", BigDecimal.class);
431 List<Range<BigDecimal>> rangeConstraints = new ArrayList<>();
432 rangeConstraints.add(Range.closed(new BigDecimal("1.5"), new BigDecimal("5.5")));
433 arg = new BigDecimal("1.4");
434 expectedMsg = String.format("Invalid range: %s, expected: %s.", arg, rangeConstraints);
435 assertContainsRestrictionCheck(builderObj, m, expectedMsg, arg);
437 cleanUp(sourcesOutputDir, compiledOutputDir);
441 public void testGenerationContextReferenceExtension() throws Exception {
442 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "context-reference");
443 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
444 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "context-reference");
445 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
447 generateTestSources("/compilation/context-reference", sourcesOutputDir);
449 // Test if all sources are generated
450 File fooParent = new File(sourcesOutputDir, NS_FOO);
451 assertFilesCount(fooParent, 3);
452 assertTrue(new File(fooParent, "FooData.java").exists());
453 assertTrue(new File(fooParent, "Nodes.java").exists());
454 assertTrue(new File(fooParent, "NodesBuilder.java").exists());
456 File barParent = new File(sourcesOutputDir, NS_BAR);
457 assertFilesCount(barParent, 1);
458 assertTrue(new File(barParent, "IdentityClass.java").exists());
460 // Test if sources are compilable
461 testCompilation(sourcesOutputDir, compiledOutputDir);
463 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
464 Class<?> nodesClass = Class.forName(BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
465 Class<?> identityClass = Class
466 .forName(BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
470 identityClass.getConstructor();
471 Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true, loader);
472 assertEquals(baseIdentity, identityClass.getSuperclass());
473 } catch (NoSuchMethodException e) {
474 throw new AssertionError("IdentityClass must have no-arg constructor");
479 Method getId = nodesClass.getMethod("getId");
480 Annotation[] annotations = getId.getAnnotations();
481 assertEquals(1, annotations.length);
482 Annotation routingContext = annotations[0];
483 assertEquals(RoutingContext.class, routingContext.annotationType());
484 } catch (NoSuchMethodException e) {
485 throw new AssertionError("Method getId() not found");
488 cleanUp(sourcesOutputDir, compiledOutputDir);
492 public void compilationTest() throws Exception {
493 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "yang");
494 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
495 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "yang");
496 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
498 generateTestSources("/yang", sourcesOutputDir);
500 // Test if sources are compilable
501 testCompilation(sourcesOutputDir, compiledOutputDir);
503 cleanUp(sourcesOutputDir, compiledOutputDir);
507 public void testBug586() throws Exception {
508 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug586");
509 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
510 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug586");
511 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
513 generateTestSources("/compilation/bug586", sourcesOutputDir);
515 // Test if sources are compilable
516 testCompilation(sourcesOutputDir, compiledOutputDir);
518 cleanUp(sourcesOutputDir, compiledOutputDir);
522 * Test handling nested uses-augmentations.
527 public void testBug1172() throws Exception {
528 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug1172");
529 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
530 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug1172");
531 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
533 generateTestSources("/compilation/bug1172", sourcesOutputDir);
535 // Test if sources are compilable
536 testCompilation(sourcesOutputDir, compiledOutputDir);
538 cleanUp(sourcesOutputDir, compiledOutputDir);
542 * Test if class generated for node from grouping implements ChildOf.
547 public void testBug1377() throws Exception {
548 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug1377");
549 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
550 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug1377");
551 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
553 generateTestSources("/compilation/bug1377", sourcesOutputDir);
555 // Test if sources are compilable
556 testCompilation(sourcesOutputDir, compiledOutputDir);
558 ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
559 Class<?> outputActionClass = Class.forName(BASE_PKG
560 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
561 Class<?> actionClass = Class.forName(BASE_PKG + ".urn.test.foo.rev140717.Action", true, loader);
563 // Test generated 'container output-action'
564 assertTrue(outputActionClass.isInterface());
565 assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(), actionClass.getCanonicalName());
567 cleanUp(sourcesOutputDir, compiledOutputDir);
571 public void classNamesColisionTest() throws Exception {
572 final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "class-name-collision");
573 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
574 final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "class-name-collision");
575 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
577 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
578 testCompilation(sourcesOutputDir, compiledOutputDir);
579 cleanUp(sourcesOutputDir, compiledOutputDir);
582 private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
583 final List<File> sourceFiles = getSourceFiles(resourceDirPath);
584 final SchemaContext context = parser.parseFiles(sourceFiles);
585 final List<Type> types = bindingGenerator.generateTypes(context);
586 Collections.sort(types, new Comparator<Type>() {
588 public int compare(final Type o1, final Type o2) {
589 return o2.getName().compareTo(o1.getName());
592 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
593 generator.generateToFile(sourcesOutputDir);
596 private void testReturnTypeIdentityref(final Class<?> clazz, final String methodName, final String returnTypeStr) throws Exception {
598 java.lang.reflect.Type returnType;
600 method = clazz.getMethod(methodName);
601 assertEquals(java.lang.Class.class, method.getReturnType());
602 returnType = method.getGenericReturnType();
603 assertTrue(returnType instanceof ParameterizedType);
604 ParameterizedType pt = (ParameterizedType) returnType;
605 java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
606 assertEquals(1, parameters.length);
607 java.lang.reflect.Type parameter = parameters[0];
608 assertTrue(parameter instanceof WildcardType);
609 WildcardType wildcardType = (WildcardType) parameter;
610 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
611 } catch (NoSuchMethodException e) {
612 throw new AssertionError("Method '" + methodName + "' not found");
616 private void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz, final String methodName)
619 Class<?> rawReturnType;
620 java.lang.reflect.Type returnType;
622 method = clazz.getMethod(methodName);
623 rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true, loader);
624 assertEquals(rawReturnType, method.getReturnType());
625 returnType = method.getGenericReturnType();
626 assertTrue(returnType instanceof ParameterizedType);
627 ParameterizedType pt = (ParameterizedType) returnType;
628 java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
629 assertEquals(1, parameters.length);
630 java.lang.reflect.Type parameter = parameters[0];
631 assertTrue(parameter instanceof WildcardType);
632 WildcardType wildcardType = (WildcardType) parameter;
633 assertEquals("?", wildcardType.toString());
634 } catch (NoSuchMethodException e) {
635 throw new AssertionError("Method '" + methodName + "' not found");