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