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