Merge "Introduce Identifiables"
[yangtools.git] / code-generator / binding-java-api-generator / src / test / java / org / opendaylight / yangtools / sal / java / api / generator / test / CompilationTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.sal.java.api.generator.test;
9
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.cleanUp;
27 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.getSourceFiles;
28 import static org.opendaylight.yangtools.sal.java.api.generator.test.CompilationTestUtils.testCompilation;
29
30 import com.google.common.collect.Range;
31 import java.io.File;
32 import java.lang.annotation.Annotation;
33 import java.lang.reflect.Field;
34 import java.lang.reflect.Method;
35 import java.lang.reflect.ParameterizedType;
36 import java.lang.reflect.WildcardType;
37 import java.math.BigDecimal;
38 import java.math.BigInteger;
39 import java.net.URL;
40 import java.net.URLClassLoader;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Set;
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.annotations.RoutingContext;
49 import org.opendaylight.yangtools.yang.model.api.Module;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51
52 /**
53  * Test correct code generation.
54  *
55  */
56 public class CompilationTest extends BaseCompilationTest {
57
58     @Test
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());
64
65         generateTestSources("/compilation/list-gen", sourcesOutputDir);
66
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);
80
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);
97
98         // Test if sources are compilable
99         testCompilation(sourcesOutputDir, compiledOutputDir);
100
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);
105
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");
111
112         // Test generated 'list links'
113         assertTrue(linksClass.isInterface());
114         // TODO: anyxml
115         assertEquals(6, linksClass.getDeclaredMethods().length);
116         assertImplementsIfc(linksClass, keyArgsClass);
117
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));
124
125         cleanUp(sourcesOutputDir, compiledOutputDir);
126     }
127
128     @Test
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());
134
135         generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
136
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);
145
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);
150
151         parent = new File(sourcesOutputDir, NS_FOO + FS + "closed");
152         assertFilesCount(parent, 1);
153
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);
158
159         parent = new File(sourcesOutputDir, NS_FOO + FS + "open");
160         assertFilesCount(parent, 1);
161
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);
166
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);
171
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);
180
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);
189
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);
196
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);
203
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);
212
213         parent = new File(parent, "subobjects");
214         assertFilesCount(parent, 2);
215         assertTrue(new File(parent, "SubobjectType.java").exists());
216
217         parent = new File(parent, "subobject");
218         assertFilesCount(parent, 1);
219
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);
226
227         // Test if sources are compilable
228         testCompilation(sourcesOutputDir, compiledOutputDir);
229
230         cleanUp(sourcesOutputDir, compiledOutputDir);
231     }
232
233     @Test
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());
239
240         generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
241
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);
253
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());
262
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());
269
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);
279
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());
284
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());
293
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);
304
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);
315
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());
321
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);
366
367         // Test if sources are compilable
368         testCompilation(sourcesOutputDir, compiledOutputDir);
369
370         cleanUp(sourcesOutputDir, compiledOutputDir);
371     }
372
373     @Test
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());
379
380         generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
381
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);
389
390         // Test if sources are compilable
391         testCompilation(sourcesOutputDir, compiledOutputDir);
392
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);
397
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);
419
420         Object builderObj = builderClass.newInstance();
421
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);
428
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);
435
436         cleanUp(sourcesOutputDir, compiledOutputDir);
437     }
438
439     @Test
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());
445
446         generateTestSources("/compilation/context-reference", sourcesOutputDir);
447
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());
454
455         File barParent = new File(sourcesOutputDir, NS_BAR);
456         assertFilesCount(barParent, 1);
457         assertTrue(new File(barParent, "IdentityClass.java").exists());
458
459         // Test if sources are compilable
460         testCompilation(sourcesOutputDir, compiledOutputDir);
461
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);
466
467         // test identity
468         try {
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");
474         }
475
476         // Test annotation
477         try {
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");
485         }
486
487         cleanUp(sourcesOutputDir, compiledOutputDir);
488     }
489
490     @Test
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());
496
497         generateTestSources("/yang", sourcesOutputDir);
498
499         // Test if sources are compilable
500         testCompilation(sourcesOutputDir, compiledOutputDir);
501
502         cleanUp(sourcesOutputDir, compiledOutputDir);
503     }
504
505     @Test
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());
511
512         generateTestSources("/compilation/bug586", sourcesOutputDir);
513
514         // Test if sources are compilable
515         testCompilation(sourcesOutputDir, compiledOutputDir);
516
517         cleanUp(sourcesOutputDir, compiledOutputDir);
518     }
519
520     /**
521      * Test handling nested uses-augmentations.
522      *
523      * @throws Exception
524      */
525     @Test
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());
531
532         generateTestSources("/compilation/bug1172", sourcesOutputDir);
533
534         // Test if sources are compilable
535         testCompilation(sourcesOutputDir, compiledOutputDir);
536
537         cleanUp(sourcesOutputDir, compiledOutputDir);
538     }
539
540     private void generateTestSources(String resourceDirPath, File sourcesOutputDir) throws Exception {
541         final List<File> sourceFiles = getSourceFiles(resourceDirPath);
542         final Set<Module> modulesToBuild = parser.parseYangModels(sourceFiles);
543         final SchemaContext context = parser.resolveSchemaContext(modulesToBuild);
544         final List<Type> types = bindingGenerator.generateTypes(context);
545         final GeneratorJavaFile generator = new GeneratorJavaFile(new HashSet<>(types));
546         generator.generateToFile(sourcesOutputDir);
547     }
548
549     private void testReturnTypeIdentityref(Class<?> clazz, String methodName, String returnTypeStr) throws Exception {
550         Method method;
551         java.lang.reflect.Type returnType;
552         try {
553             method = clazz.getMethod(methodName);
554             assertEquals(java.lang.Class.class, method.getReturnType());
555             returnType = method.getGenericReturnType();
556             assertTrue(returnType instanceof ParameterizedType);
557             ParameterizedType pt = (ParameterizedType) returnType;
558             java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
559             assertEquals(1, parameters.length);
560             java.lang.reflect.Type parameter = parameters[0];
561             assertTrue(parameter instanceof WildcardType);
562             WildcardType wildcardType = (WildcardType) parameter;
563             assertEquals("? extends " + returnTypeStr, wildcardType.toString());
564         } catch (NoSuchMethodException e) {
565             throw new AssertionError("Method '" + methodName + "' not found");
566         }
567     }
568
569     private void testReturnTypeInstanceIdentitifer(ClassLoader loader, Class<?> clazz, String methodName)
570             throws Exception {
571         Method method;
572         Class<?> rawReturnType;
573         java.lang.reflect.Type returnType;
574         try {
575             method = clazz.getMethod(methodName);
576             rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true, loader);
577             assertEquals(rawReturnType, method.getReturnType());
578             returnType = method.getGenericReturnType();
579             assertTrue(returnType instanceof ParameterizedType);
580             ParameterizedType pt = (ParameterizedType) returnType;
581             java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
582             assertEquals(1, parameters.length);
583             java.lang.reflect.Type parameter = parameters[0];
584             assertTrue(parameter instanceof WildcardType);
585             WildcardType wildcardType = (WildcardType) parameter;
586             assertEquals("?", wildcardType.toString());
587         } catch (NoSuchMethodException e) {
588             throw new AssertionError("Method '" + methodName + "' not found");
589         }
590     }
591
592 }