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