2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.java.api.generator.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.Range;
19 import java.io.IOException;
20 import java.lang.annotation.Annotation;
21 import java.lang.reflect.Field;
22 import java.lang.reflect.Method;
23 import java.lang.reflect.Modifier;
24 import java.lang.reflect.ParameterizedType;
25 import java.lang.reflect.WildcardType;
26 import java.math.BigDecimal;
27 import java.net.URISyntaxException;
29 import java.net.URLClassLoader;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.List;
34 import java.util.stream.Collectors;
35 import org.junit.Test;
36 import org.opendaylight.yangtools.yang.binding.ChildOf;
37 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
38 import org.opendaylight.yangtools.yang.common.Empty;
39 import org.opendaylight.yangtools.yang.common.Uint16;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41 import org.opendaylight.yangtools.yang.common.Uint64;
42 import org.opendaylight.yangtools.yang.common.Uint8;
45 * Test correct code generation.
48 public class CompilationTest extends BaseCompilationTest {
51 * Java 8 allows JaCoCo to hook onto interfaces, as well as
52 * generating a default implementation. We only want to check
55 private static Collection<Method> abstractMethods(final Class<?> clazz) {
57 return Collections2.filter(Arrays.asList(clazz.getDeclaredMethods()),
58 input -> Modifier.isAbstract(input.getModifiers()));
62 public void testListGeneration() throws Exception {
63 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("list-gen");
64 final File compiledOutputDir = CompilationTestUtils.compilerOutput("list-gen");
65 generateTestSources("/compilation/list-gen", sourcesOutputDir);
67 // Test if all sources are generated
68 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
69 final File keyArgs = new File(parent, "KeyArgs.java");
70 final File links = new File(parent, "Links.java");
71 final File linksBuilder = new File(parent, "LinksBuilder.java");
72 final File linksKey = new File(parent, "LinksKey.java");
73 final 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 CompilationTestUtils.assertFilesCount(parent, 7);
81 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST + CompilationTestUtils.FS + "links");
82 final File level = new File(parent, "Level.java");
83 final File linkGroup = new File(parent, "LinkGroup.java");
84 final File node = new File(parent, "Node.java");
85 final File nodeBuilder = new File(parent, "NodeBuilder.java");
86 final File nodeList = new File(parent, "NodeList.java");
87 final File nodeListBuilder = new File(parent, "NodeListBuilder.java");
88 final 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 CompilationTestUtils.assertFilesCount(parent, 8);
98 // Test if sources are compilable
99 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
101 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
102 final Class<?> keyArgsClass = Class.forName(CompilationTestUtils.BASE_PKG
103 + ".urn.opendaylight.test.rev131008.KeyArgs", true, loader);
104 final Class<?> linksClass = Class.forName(CompilationTestUtils.BASE_PKG
105 + ".urn.opendaylight.test.rev131008.Links", true, loader);
106 final Class<?> linksKeyClass = Class.forName(CompilationTestUtils.BASE_PKG
107 + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
109 // Test generated 'grouping key-args'
110 assertTrue(keyArgsClass.isInterface());
111 CompilationTestUtils.assertContainsMethod(keyArgsClass, String.class, "getName");
112 CompilationTestUtils.assertContainsMethod(keyArgsClass, Integer.class, "getSize");
113 assertEquals(3, abstractMethods(keyArgsClass).size());
115 // Test generated 'list links'
116 assertTrue(linksClass.isInterface());
117 CompilationTestUtils.assertImplementsIfc(linksClass, keyArgsClass);
118 assertEquals(7, abstractMethods(linksClass).size());
120 // Test list key constructor arguments ordering
121 CompilationTestUtils.assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
122 // Test serialVersionUID generation
123 final Field suid = CompilationTestUtils.assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
124 suid.setAccessible(true);
125 assertEquals(-8829501012356283881L, suid.getLong(null));
127 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
131 public void testAugmentUnderUsesGeneration() throws Exception {
132 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("augment-under-uses");
133 final File compiledOutputDir = CompilationTestUtils.compilerOutput("augment-under-uses");
134 generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
136 // Test if all sources were generated from 'module foo'
137 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
138 assertTrue(new File(parent, "Object.java").exists());
139 assertTrue(new File(parent, "ClosedObject.java").exists());
140 assertTrue(new File(parent, "OpenObject.java").exists());
141 assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
142 assertTrue(new File(parent, "PathKeySubobject.java").exists());
143 CompilationTestUtils.assertFilesCount(parent, 10);
145 parent = new File(parent, "object");
146 assertTrue(new File(parent, "Nodes.java").exists());
147 assertTrue(new File(parent, "NodesBuilder.java").exists());
148 CompilationTestUtils.assertFilesCount(parent, 2);
150 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "closed");
151 CompilationTestUtils.assertFilesCount(parent, 1);
153 parent = new File(parent, "object");
154 assertTrue(new File(parent, "Link1.java").exists());
155 assertTrue(new File(parent, "Link1Builder.java").exists());
156 CompilationTestUtils.assertFilesCount(parent, 2);
158 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "open");
159 CompilationTestUtils.assertFilesCount(parent, 1);
161 parent = new File(parent, "object");
162 assertTrue(new File(parent, "Nodes1.java").exists());
163 assertTrue(new File(parent, "Nodes1Builder.java").exists());
164 CompilationTestUtils.assertFilesCount(parent, 3);
166 parent = new File(parent, "nodes");
167 assertTrue(new File(parent, "Links.java").exists());
168 assertTrue(new File(parent, "LinksBuilder.java").exists());
169 CompilationTestUtils.assertFilesCount(parent, 2);
171 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "explicit");
172 CompilationTestUtils.assertFilesCount(parent, 1);
173 parent = new File(parent, "route");
174 CompilationTestUtils.assertFilesCount(parent, 1);
175 parent = new File(parent, "object");
176 assertTrue(new File(parent, "Subobjects.java").exists());
177 assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
178 CompilationTestUtils.assertFilesCount(parent, 3);
180 parent = new File(parent, "subobjects");
181 CompilationTestUtils.assertFilesCount(parent, 1);
182 parent = new File(parent, "subobject");
183 CompilationTestUtils.assertFilesCount(parent, 1);
184 parent = new File(parent, "type");
185 assertTrue(new File(parent, "PathKey.java").exists());
186 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
187 CompilationTestUtils.assertFilesCount(parent, 3);
189 parent = new File(parent, "path");
190 CompilationTestUtils.assertFilesCount(parent, 1);
191 parent = new File(parent, "key");
192 assertTrue(new File(parent, "PathKey.java").exists());
193 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
194 CompilationTestUtils.assertFilesCount(parent, 2);
196 // Test if all sources were generated from 'module bar'
197 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
198 assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
199 assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
200 assertTrue(new File(parent, "RouteSubobjects.java").exists());
201 CompilationTestUtils.assertFilesCount(parent, 6);
203 parent = new File(parent, "route");
204 CompilationTestUtils.assertFilesCount(parent, 1);
205 parent = new File(new File(sourcesOutputDir, CompilationTestUtils.NS_BAR), "basic");
206 CompilationTestUtils.assertFilesCount(parent, 1);
207 parent = new File(parent, "explicit");
208 CompilationTestUtils.assertFilesCount(parent, 1);
209 parent = new File(parent, "route");
210 CompilationTestUtils.assertFilesCount(parent, 1);
212 parent = new File(parent, "subobjects");
213 CompilationTestUtils.assertFilesCount(parent, 2);
214 assertTrue(new File(parent, "SubobjectType.java").exists());
216 parent = new File(parent, "subobject");
217 CompilationTestUtils.assertFilesCount(parent, 1);
219 parent = new File(parent, "type");
220 assertTrue(new File(parent, "IpPrefix.java").exists());
221 assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
222 assertTrue(new File(parent, "Label.java").exists());
223 assertTrue(new File(parent, "LabelBuilder.java").exists());
224 CompilationTestUtils.assertFilesCount(parent, 4);
226 // Test if sources are compilable
227 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
229 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
233 public void testAugmentOfAugmentGeneration() throws Exception {
234 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("aug-of-aug");
235 final File compiledOutputDir = CompilationTestUtils.compilerOutput("aug-of-aug");
236 generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
238 // Test if all sources were generated from 'module foo'
239 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
240 final File fooListener = new File(parent, "FooListener.java");
241 File pathAttributes = new File(parent, "PathAttributes.java");
242 final File update = new File(parent, "Update.java");
243 final File updateBuilder = new File(parent, "UpdateBuilder.java");
244 assertTrue(fooListener.exists());
245 assertTrue(pathAttributes.exists());
246 assertTrue(update.exists());
247 assertTrue(updateBuilder.exists());
248 CompilationTestUtils.assertFilesCount(parent, 7);
250 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "path");
251 CompilationTestUtils.assertFilesCount(parent, 1);
252 parent = new File(parent, "attributes");
253 CompilationTestUtils.assertFilesCount(parent, 2);
254 final File origin = new File(parent, "Origin.java");
255 final File originBuilder = new File(parent, "OriginBuilder.java");
256 assertTrue(origin.exists());
257 assertTrue(originBuilder.exists());
259 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "update");
260 CompilationTestUtils.assertFilesCount(parent, 2);
261 pathAttributes = new File(parent, "PathAttributes.java");
262 final File pathAttributesBuilder = new File(parent, "PathAttributesBuilder.java");
263 assertTrue(pathAttributes.exists());
264 assertTrue(pathAttributesBuilder.exists());
266 // Test if all sources were generated from 'module bar'
267 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
268 final File destination = new File(parent, "Destination.java");
269 final File pathAttributes1 = new File(parent, "PathAttributes1.java");
270 final File pathAttributes1Builder = new File(parent, "PathAttributes1Builder.java");
271 assertTrue(destination.exists());
272 assertTrue(pathAttributes1.exists());
273 assertTrue(pathAttributes1Builder.exists());
274 CompilationTestUtils.assertFilesCount(parent, 6);
276 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "destination");
277 CompilationTestUtils.assertFilesCount(parent, 2);
278 final File destinationType = new File(parent, "DestinationType.java");
279 assertTrue(destinationType.exists());
281 parent = new File(parent, "destination");
282 CompilationTestUtils.assertFilesCount(parent, 1);
283 parent = new File(parent, "type");
284 CompilationTestUtils.assertFilesCount(parent, 2);
285 final File destinationIpv4 = new File(parent, "DestinationIp.java");
286 final File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
287 assertTrue(destinationIpv4.exists());
288 assertTrue(destinationIpv4Builder.exists());
290 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "update");
291 CompilationTestUtils.assertFilesCount(parent, 1);
292 parent = new File(parent, "path");
293 CompilationTestUtils.assertFilesCount(parent, 1);
294 parent = new File(parent, "attributes");
295 final File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
296 final File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
297 assertTrue(mpUnreachNlri.exists());
298 assertTrue(mpUnreachNlriBuilder.exists());
299 CompilationTestUtils.assertFilesCount(parent, 3);
301 parent = new File(parent, "mp");
302 CompilationTestUtils.assertFilesCount(parent, 1);
303 parent = new File(parent, "unreach");
304 CompilationTestUtils.assertFilesCount(parent, 1);
305 parent = new File(parent, "nlri");
306 final File withdrawnRoutes = new File(parent, "WithdrawnRoutes.java");
307 final File withdrawnRoutesBuilder = new File(parent, "WithdrawnRoutesBuilder.java");
308 assertTrue(withdrawnRoutes.exists());
309 assertTrue(withdrawnRoutesBuilder.exists());
310 CompilationTestUtils.assertFilesCount(parent, 2);
312 // Test if all sources were generated from 'module baz'
313 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ);
314 CompilationTestUtils.assertFilesCount(parent, 3);
315 final File linkstateDestination = new File(parent, "LinkstateDestination.java");
316 assertTrue(linkstateDestination.exists());
318 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ + CompilationTestUtils.FS + "update");
319 CompilationTestUtils.assertFilesCount(parent, 1);
320 parent = new File(parent, "path");
321 CompilationTestUtils.assertFilesCount(parent, 1);
322 parent = new File(parent, "attributes");
323 CompilationTestUtils.assertFilesCount(parent, 1);
324 parent = new File(parent, "mp");
325 CompilationTestUtils.assertFilesCount(parent, 1);
326 parent = new File(parent, "unreach");
327 CompilationTestUtils.assertFilesCount(parent, 1);
328 parent = new File(parent, "nlri");
329 CompilationTestUtils.assertFilesCount(parent, 1);
330 parent = new File(parent, "withdrawn");
331 CompilationTestUtils.assertFilesCount(parent, 1);
332 parent = new File(parent, "routes");
333 CompilationTestUtils.assertFilesCount(parent, 1);
334 parent = new File(parent, "destination");
335 CompilationTestUtils.assertFilesCount(parent, 1);
336 parent = new File(parent, "type");
337 final File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
338 final File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
339 assertTrue(destinationLinkstate.exists());
340 assertTrue(destinationLinkstateBuilder.exists());
341 CompilationTestUtils.assertFilesCount(parent, 3);
342 parent = new File(parent, "destination");
343 CompilationTestUtils.assertFilesCount(parent, 1);
344 parent = new File(parent, "linkstate");
345 final File links = new File(parent, "Links.java");
346 final File linksBuilder = new File(parent, "LinksBuilder.java");
347 assertTrue(links.exists());
348 assertTrue(linksBuilder.exists());
349 CompilationTestUtils.assertFilesCount(parent, 3);
350 parent = new File(parent, "links");
351 final File source = new File(parent, "Source.java");
352 final File sourceBuilder = new File(parent, "SourceBuilder.java");
353 assertTrue(source.exists());
354 assertTrue(sourceBuilder.exists());
355 CompilationTestUtils.assertFilesCount(parent, 3);
356 parent = new File(parent, "source");
357 final File address = new File(parent, "Address.java");
358 final File addressBuilder = new File(parent, "AddressBuilder.java");
359 assertTrue(address.exists());
360 assertTrue(addressBuilder.exists());
361 CompilationTestUtils.assertFilesCount(parent, 2);
363 // Test if sources are compilable
364 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
366 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
370 public void testLeafReturnTypes() throws Exception {
371 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("leaf-return-types");
372 final File compiledOutputDir = CompilationTestUtils.compilerOutput("leaf-return-types");
373 generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
375 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
376 assertTrue(new File(parent, "TestData.java").exists());
377 assertTrue(new File(parent, "Nodes.java").exists());
378 assertTrue(new File(parent, "NodesBuilder.java").exists());
379 assertTrue(new File(parent, "Alg.java").exists());
380 assertTrue(new File(parent, "NodesIdUnionBuilder.java").exists());
381 CompilationTestUtils.assertFilesCount(parent, 6);
383 // Test if sources are compilable
384 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
386 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008";
387 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
388 final Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
389 final Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
391 // Test methods return type
392 final byte[] b = new byte[] {};
393 CompilationTestUtils.assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
394 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
395 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "isIdBoolean");
396 CompilationTestUtils.assertContainsMethod(nodesClass, BigDecimal.class, "getIdDecimal64");
397 CompilationTestUtils.assertContainsMethod(nodesClass, Empty.class, "getIdEmpty");
398 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
399 testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
400 testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
401 CompilationTestUtils.assertContainsMethod(nodesClass, Byte.class, "getId8");
402 CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getId16");
403 CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getId32");
404 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getId64");
405 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
406 CompilationTestUtils.assertContainsMethod(nodesClass, String.class, "getIdString");
407 CompilationTestUtils.assertContainsMethod(nodesClass, Uint8.class, "getIdU8");
408 CompilationTestUtils.assertContainsMethod(nodesClass, Uint16.class, "getIdU16");
409 CompilationTestUtils.assertContainsMethod(nodesClass, Uint32.class, "getIdU32");
410 CompilationTestUtils.assertContainsMethod(nodesClass, Uint64.class, "getIdU64");
411 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
413 final Object builderObj = builderClass.getDeclaredConstructor().newInstance();
415 Method method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary",
417 final List<Range<Integer>> lengthConstraints = new ArrayList<>();
418 lengthConstraints.add(Range.closed(1, 10));
419 byte[] arg = new byte[] {};
420 String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg),
422 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg);
424 method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64",
426 final List<Range<BigDecimal>> rangeConstraints = new ArrayList<>();
427 rangeConstraints.add(Range.closed(new BigDecimal("1.5"), new BigDecimal("5.5")));
428 Object arg1 = new BigDecimal("1.4");
429 expectedMsg = String.format("Invalid range: %s, expected: %s.", arg1, rangeConstraints);
430 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg1);
432 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
436 public void testGenerationContextReferenceExtension() throws IOException, URISyntaxException,
437 ClassNotFoundException {
438 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("context-reference");
439 final File compiledOutputDir = CompilationTestUtils.compilerOutput("context-reference");
440 generateTestSources("/compilation/context-reference", sourcesOutputDir);
442 // Test if all sources are generated
443 final File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
444 CompilationTestUtils.assertFilesCount(fooParent, 4);
445 assertTrue(new File(fooParent, "FooData.java").exists());
446 assertTrue(new File(fooParent, "Nodes.java").exists());
447 assertTrue(new File(fooParent, "NodesBuilder.java").exists());
449 final File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
450 CompilationTestUtils.assertFilesCount(barParent, 2);
451 assertTrue(new File(barParent, "IdentityClass.java").exists());
453 // Test if sources are compilable
454 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
456 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
457 final Class<?> nodesClass = Class.forName(CompilationTestUtils.BASE_PKG
458 + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
459 final Class<?> identityClass = Class
460 .forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
463 final Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true,
465 assertEquals(ImmutableList.of(baseIdentity), Arrays.asList(identityClass.getInterfaces()));
470 getId = nodesClass.getMethod("getId");
471 } catch (final NoSuchMethodException e) {
472 throw new AssertionError("Method getId() not found", e);
475 assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
476 .map(Annotation::annotationType).collect(Collectors.toSet()));
477 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
481 public void compilationTest() throws Exception {
482 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("yang");
483 final File compiledOutputDir = CompilationTestUtils.compilerOutput("yang");
484 generateTestSources("/yang", sourcesOutputDir);
486 // Test if sources are compilable
487 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
489 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
493 public void testBug586() throws Exception {
494 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug586");
495 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug586");
496 generateTestSources("/compilation/bug586", sourcesOutputDir);
498 // Test if sources are compilable
499 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
501 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
505 public void testBug4760() throws Exception {
506 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug4760");
507 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug4760");
508 generateTestSources("/compilation/bug4760", sourcesOutputDir);
510 // Test if sources are compilable
511 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
513 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
517 * Test handling nested uses-augmentations.
520 public void testBug1172() throws Exception {
521 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1172");
522 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1172");
523 generateTestSources("/compilation/bug1172", sourcesOutputDir);
525 // Test if sources are compilable
526 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
528 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
532 public void testBug5461() throws Exception {
533 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5461");
534 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5461");
535 generateTestSources("/compilation/bug5461", sourcesOutputDir);
537 // Test if sources are compilable
538 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
540 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
544 public void testBug5882() throws Exception {
545 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5882");
546 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5882");
547 generateTestSources("/compilation/bug5882", sourcesOutputDir);
549 // Test if sources are compilable
550 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
552 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882);
553 assertTrue(new File(parent, "FooData.java").exists());
554 assertTrue(new File(parent, "TypedefCurrent.java").exists());
555 assertTrue(new File(parent, "TypedefDeprecated.java").exists());
557 try (URLClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() })) {
558 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
559 final Class<?> cls = loader.loadClass(pkg + ".FooData");
560 final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
561 final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
562 final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
563 final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
564 final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
565 final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
566 final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
567 final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
568 assertTrue(clsTypedefDepr.getAnnotations()[0].toString().contains("Deprecated"));
569 assertTrue(clsTypedefCur.getAnnotations().length == 0);
570 assertTrue(clsGroupingDepr.getAnnotations()[0].toString().contains("Deprecated"));
571 assertTrue(clsGroupingCur.getAnnotations().length == 0);
572 assertTrue(clsTypeDef1.getAnnotations().length == 0);
573 assertTrue(clsTypeDef3.getAnnotations().length == 0);
574 assertTrue(clsTypeDef2.getAnnotations()[0].toString().contains("Deprecated"));
576 /*methods inside container*/
577 assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
578 assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
579 assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
580 assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
581 assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
582 assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
584 /*methods inside module*/
585 assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
586 assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
587 assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
588 assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
589 assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
590 assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
591 assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
594 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
598 * Test if class generated for node from grouping implements ChildOf.
601 public void testBug1377() throws Exception {
602 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1377");
603 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1377");
605 generateTestSources("/compilation/bug1377", sourcesOutputDir);
607 // Test if sources are compilable
608 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
610 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
611 final Class<?> outputActionClass = Class.forName(CompilationTestUtils.BASE_PKG
612 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
613 final Class<?> actionClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.test.foo.rev140717.Action",
616 // Test generated 'container output-action'
617 assertTrue(outputActionClass.isInterface());
618 CompilationTestUtils.assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(),
619 actionClass.getCanonicalName());
621 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
625 public void testMdsal327() throws Exception {
626 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal327");
627 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal327");
628 generateTestSources("/compilation/mdsal327", sourcesOutputDir);
629 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
630 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
634 public void testMdsal365() throws Exception {
635 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal365");
636 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal365");
637 generateTestSources("/compilation/mdsal365", sourcesOutputDir);
638 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
639 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
643 public void testMdsal395() throws Exception {
644 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal395");
645 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal395");
646 generateTestSources("/compilation/mdsal395", sourcesOutputDir);
647 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
648 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
652 public void classNamesColisionTest() throws Exception {
653 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("class-name-collision");
654 final File compiledOutputDir = CompilationTestUtils.compilerOutput("class-name-collision");
655 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
656 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
657 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
661 public void innerEnumerationNameCollisionTest() throws Exception {
662 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal321");
663 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal321");
664 generateTestSources("/compilation/mdsal321", sourcesOutputDir);
665 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
666 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
670 public void twoNestedUnionsTest() throws Exception {
671 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal320");
672 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal320");
673 generateTestSources("/compilation/mdsal320", sourcesOutputDir);
674 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
675 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
679 public void testMdsal425() throws Exception {
680 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal425");
681 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal425");
682 generateTestSources("/compilation/mdsal425", sourcesOutputDir);
683 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
684 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
688 public void testMdsal529() throws Exception {
689 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal529");
690 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal529");
691 generateTestSources("/compilation/mdsal529", sourcesOutputDir);
692 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
693 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
696 private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
697 final String returnTypeStr) throws NoSuchMethodException {
698 Method method = clazz.getMethod(methodName);
699 assertEquals(java.lang.Class.class, method.getReturnType());
700 java.lang.reflect.Type returnType = method.getGenericReturnType();
701 assertTrue(returnType instanceof ParameterizedType);
702 final ParameterizedType pt = (ParameterizedType) returnType;
703 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
704 assertEquals(1, parameters.length);
705 final java.lang.reflect.Type parameter = parameters[0];
706 assertTrue(parameter instanceof WildcardType);
707 final WildcardType wildcardType = (WildcardType) parameter;
708 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
711 private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz,
712 final String methodName) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
713 final Method method = clazz.getMethod(methodName);
714 final Class<?> rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true,
716 assertEquals(rawReturnType, method.getReturnType());
717 final java.lang.reflect.Type returnType = method.getGenericReturnType();
718 assertTrue(returnType instanceof ParameterizedType);
719 final ParameterizedType pt = (ParameterizedType) returnType;
720 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
721 assertEquals(1, parameters.length);
722 final java.lang.reflect.Type parameter = parameters[0];
723 assertTrue(parameter instanceof WildcardType);
724 final WildcardType wildcardType = (WildcardType) parameter;
725 assertEquals("?", wildcardType.toString());