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.math.BigInteger;
28 import java.net.URISyntaxException;
30 import java.net.URLClassLoader;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.stream.Collectors;
36 import org.junit.Test;
37 import org.opendaylight.yangtools.yang.binding.ChildOf;
38 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
39 import org.opendaylight.yangtools.yang.common.Empty;
42 * Test correct code generation.
45 public class CompilationTest extends BaseCompilationTest {
48 * Java 8 allows JaCoCo to hook onto interfaces, as well as
49 * generating a default implementation. We only want to check
52 private static Collection<Method> abstractMethods(final Class<?> clazz) {
54 return Collections2.filter(Arrays.asList(clazz.getDeclaredMethods()),
55 input -> Modifier.isAbstract(input.getModifiers()));
59 public void testListGeneration() throws Exception {
60 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("list-gen");
61 final File compiledOutputDir = CompilationTestUtils.compilerOutput("list-gen");
62 generateTestSources("/compilation/list-gen", sourcesOutputDir);
64 // Test if all sources are generated
65 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
66 final File keyArgs = new File(parent, "KeyArgs.java");
67 final File links = new File(parent, "Links.java");
68 final File linksBuilder = new File(parent, "LinksBuilder.java");
69 final File linksKey = new File(parent, "LinksKey.java");
70 final File testData = new File(parent, "TestData.java");
71 assertTrue(keyArgs.exists());
72 assertTrue(links.exists());
73 assertTrue(linksBuilder.exists());
74 assertTrue(linksKey.exists());
75 assertTrue(testData.exists());
76 CompilationTestUtils.assertFilesCount(parent, 7);
78 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST + CompilationTestUtils.FS + "links");
79 final File level = new File(parent, "Level.java");
80 final File linkGroup = new File(parent, "LinkGroup.java");
81 final File node = new File(parent, "Node.java");
82 final File nodeBuilder = new File(parent, "NodeBuilder.java");
83 final File nodeList = new File(parent, "NodeList.java");
84 final File nodeListBuilder = new File(parent, "NodeListBuilder.java");
85 final File nodesType = new File(parent, "NodesType.java");
86 assertTrue(level.exists());
87 assertTrue(linkGroup.exists());
88 assertTrue(node.exists());
89 assertTrue(nodeBuilder.exists());
90 assertTrue(nodeList.exists());
91 assertTrue(nodeListBuilder.exists());
92 assertTrue(nodesType.exists());
93 CompilationTestUtils.assertFilesCount(parent, 8);
95 // Test if sources are compilable
96 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
98 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
99 final Class<?> keyArgsClass = Class.forName(CompilationTestUtils.BASE_PKG
100 + ".urn.opendaylight.test.rev131008.KeyArgs", true, loader);
101 final Class<?> linksClass = Class.forName(CompilationTestUtils.BASE_PKG
102 + ".urn.opendaylight.test.rev131008.Links", true, loader);
103 final Class<?> linksKeyClass = Class.forName(CompilationTestUtils.BASE_PKG
104 + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
106 // Test generated 'grouping key-args'
107 assertTrue(keyArgsClass.isInterface());
108 CompilationTestUtils.assertContainsMethod(keyArgsClass, String.class, "getName");
109 CompilationTestUtils.assertContainsMethod(keyArgsClass, Integer.class, "getSize");
110 assertEquals(3, abstractMethods(keyArgsClass).size());
112 // Test generated 'list links'
113 assertTrue(linksClass.isInterface());
114 CompilationTestUtils.assertImplementsIfc(linksClass, keyArgsClass);
115 assertEquals(7, abstractMethods(linksClass).size());
117 // Test list key constructor arguments ordering
118 CompilationTestUtils.assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
119 // Test serialVersionUID generation
120 final Field suid = CompilationTestUtils.assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
121 suid.setAccessible(true);
122 assertEquals(-8829501012356283881L, suid.getLong(null));
124 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
128 public void testAugmentUnderUsesGeneration() throws Exception {
129 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("augment-under-uses");
130 final File compiledOutputDir = CompilationTestUtils.compilerOutput("augment-under-uses");
131 generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
133 // Test if all sources were generated from 'module foo'
134 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
135 assertTrue(new File(parent, "Object.java").exists());
136 assertTrue(new File(parent, "ClosedObject.java").exists());
137 assertTrue(new File(parent, "OpenObject.java").exists());
138 assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
139 assertTrue(new File(parent, "PathKeySubobject.java").exists());
140 CompilationTestUtils.assertFilesCount(parent, 10);
142 parent = new File(parent, "object");
143 assertTrue(new File(parent, "Nodes.java").exists());
144 assertTrue(new File(parent, "NodesBuilder.java").exists());
145 CompilationTestUtils.assertFilesCount(parent, 2);
147 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "closed");
148 CompilationTestUtils.assertFilesCount(parent, 1);
150 parent = new File(parent, "object");
151 assertTrue(new File(parent, "Link1.java").exists());
152 assertTrue(new File(parent, "Link1Builder.java").exists());
153 CompilationTestUtils.assertFilesCount(parent, 2);
155 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "open");
156 CompilationTestUtils.assertFilesCount(parent, 1);
158 parent = new File(parent, "object");
159 assertTrue(new File(parent, "Nodes1.java").exists());
160 assertTrue(new File(parent, "Nodes1Builder.java").exists());
161 CompilationTestUtils.assertFilesCount(parent, 3);
163 parent = new File(parent, "nodes");
164 assertTrue(new File(parent, "Links.java").exists());
165 assertTrue(new File(parent, "LinksBuilder.java").exists());
166 CompilationTestUtils.assertFilesCount(parent, 2);
168 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "explicit");
169 CompilationTestUtils.assertFilesCount(parent, 1);
170 parent = new File(parent, "route");
171 CompilationTestUtils.assertFilesCount(parent, 1);
172 parent = new File(parent, "object");
173 assertTrue(new File(parent, "Subobjects.java").exists());
174 assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
175 CompilationTestUtils.assertFilesCount(parent, 3);
177 parent = new File(parent, "subobjects");
178 CompilationTestUtils.assertFilesCount(parent, 1);
179 parent = new File(parent, "subobject");
180 CompilationTestUtils.assertFilesCount(parent, 1);
181 parent = new File(parent, "type");
182 assertTrue(new File(parent, "PathKey.java").exists());
183 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
184 CompilationTestUtils.assertFilesCount(parent, 3);
186 parent = new File(parent, "path");
187 CompilationTestUtils.assertFilesCount(parent, 1);
188 parent = new File(parent, "key");
189 assertTrue(new File(parent, "PathKey.java").exists());
190 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
191 CompilationTestUtils.assertFilesCount(parent, 2);
193 // Test if all sources were generated from 'module bar'
194 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
195 assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
196 assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
197 assertTrue(new File(parent, "RouteSubobjects.java").exists());
198 CompilationTestUtils.assertFilesCount(parent, 6);
200 parent = new File(parent, "route");
201 CompilationTestUtils.assertFilesCount(parent, 1);
202 parent = new File(new File(sourcesOutputDir, CompilationTestUtils.NS_BAR), "basic");
203 CompilationTestUtils.assertFilesCount(parent, 1);
204 parent = new File(parent, "explicit");
205 CompilationTestUtils.assertFilesCount(parent, 1);
206 parent = new File(parent, "route");
207 CompilationTestUtils.assertFilesCount(parent, 1);
209 parent = new File(parent, "subobjects");
210 CompilationTestUtils.assertFilesCount(parent, 2);
211 assertTrue(new File(parent, "SubobjectType.java").exists());
213 parent = new File(parent, "subobject");
214 CompilationTestUtils.assertFilesCount(parent, 1);
216 parent = new File(parent, "type");
217 assertTrue(new File(parent, "IpPrefix.java").exists());
218 assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
219 assertTrue(new File(parent, "Label.java").exists());
220 assertTrue(new File(parent, "LabelBuilder.java").exists());
221 CompilationTestUtils.assertFilesCount(parent, 4);
223 // Test if sources are compilable
224 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
226 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
230 public void testAugmentOfAugmentGeneration() throws Exception {
231 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("aug-of-aug");
232 final File compiledOutputDir = CompilationTestUtils.compilerOutput("aug-of-aug");
233 generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
235 // Test if all sources were generated from 'module foo'
236 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
237 final File fooListener = new File(parent, "FooListener.java");
238 File pathAttributes = new File(parent, "PathAttributes.java");
239 final File update = new File(parent, "Update.java");
240 final File updateBuilder = new File(parent, "UpdateBuilder.java");
241 assertTrue(fooListener.exists());
242 assertTrue(pathAttributes.exists());
243 assertTrue(update.exists());
244 assertTrue(updateBuilder.exists());
245 CompilationTestUtils.assertFilesCount(parent, 7);
247 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "path");
248 CompilationTestUtils.assertFilesCount(parent, 1);
249 parent = new File(parent, "attributes");
250 CompilationTestUtils.assertFilesCount(parent, 2);
251 final File origin = new File(parent, "Origin.java");
252 final File originBuilder = new File(parent, "OriginBuilder.java");
253 assertTrue(origin.exists());
254 assertTrue(originBuilder.exists());
256 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "update");
257 CompilationTestUtils.assertFilesCount(parent, 2);
258 pathAttributes = new File(parent, "PathAttributes.java");
259 final File pathAttributesBuilder = new File(parent, "PathAttributesBuilder.java");
260 assertTrue(pathAttributes.exists());
261 assertTrue(pathAttributesBuilder.exists());
263 // Test if all sources were generated from 'module bar'
264 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
265 final File destination = new File(parent, "Destination.java");
266 final File pathAttributes1 = new File(parent, "PathAttributes1.java");
267 final File pathAttributes1Builder = new File(parent, "PathAttributes1Builder.java");
268 assertTrue(destination.exists());
269 assertTrue(pathAttributes1.exists());
270 assertTrue(pathAttributes1Builder.exists());
271 CompilationTestUtils.assertFilesCount(parent, 6);
273 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "destination");
274 CompilationTestUtils.assertFilesCount(parent, 2);
275 final File destinationType = new File(parent, "DestinationType.java");
276 assertTrue(destinationType.exists());
278 parent = new File(parent, "destination");
279 CompilationTestUtils.assertFilesCount(parent, 1);
280 parent = new File(parent, "type");
281 CompilationTestUtils.assertFilesCount(parent, 2);
282 final File destinationIpv4 = new File(parent, "DestinationIp.java");
283 final File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
284 assertTrue(destinationIpv4.exists());
285 assertTrue(destinationIpv4Builder.exists());
287 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "update");
288 CompilationTestUtils.assertFilesCount(parent, 1);
289 parent = new File(parent, "path");
290 CompilationTestUtils.assertFilesCount(parent, 1);
291 parent = new File(parent, "attributes");
292 final File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
293 final File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
294 assertTrue(mpUnreachNlri.exists());
295 assertTrue(mpUnreachNlriBuilder.exists());
296 CompilationTestUtils.assertFilesCount(parent, 3);
298 parent = new File(parent, "mp");
299 CompilationTestUtils.assertFilesCount(parent, 1);
300 parent = new File(parent, "unreach");
301 CompilationTestUtils.assertFilesCount(parent, 1);
302 parent = new File(parent, "nlri");
303 final File withdrawnRoutes = new File(parent, "WithdrawnRoutes.java");
304 final File withdrawnRoutesBuilder = new File(parent, "WithdrawnRoutesBuilder.java");
305 assertTrue(withdrawnRoutes.exists());
306 assertTrue(withdrawnRoutesBuilder.exists());
307 CompilationTestUtils.assertFilesCount(parent, 2);
309 // Test if all sources were generated from 'module baz'
310 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ);
311 CompilationTestUtils.assertFilesCount(parent, 3);
312 final File linkstateDestination = new File(parent, "LinkstateDestination.java");
313 assertTrue(linkstateDestination.exists());
315 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ + CompilationTestUtils.FS + "update");
316 CompilationTestUtils.assertFilesCount(parent, 1);
317 parent = new File(parent, "path");
318 CompilationTestUtils.assertFilesCount(parent, 1);
319 parent = new File(parent, "attributes");
320 CompilationTestUtils.assertFilesCount(parent, 1);
321 parent = new File(parent, "mp");
322 CompilationTestUtils.assertFilesCount(parent, 1);
323 parent = new File(parent, "unreach");
324 CompilationTestUtils.assertFilesCount(parent, 1);
325 parent = new File(parent, "nlri");
326 CompilationTestUtils.assertFilesCount(parent, 1);
327 parent = new File(parent, "withdrawn");
328 CompilationTestUtils.assertFilesCount(parent, 1);
329 parent = new File(parent, "routes");
330 CompilationTestUtils.assertFilesCount(parent, 1);
331 parent = new File(parent, "destination");
332 CompilationTestUtils.assertFilesCount(parent, 1);
333 parent = new File(parent, "type");
334 final File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
335 final File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
336 assertTrue(destinationLinkstate.exists());
337 assertTrue(destinationLinkstateBuilder.exists());
338 CompilationTestUtils.assertFilesCount(parent, 3);
339 parent = new File(parent, "destination");
340 CompilationTestUtils.assertFilesCount(parent, 1);
341 parent = new File(parent, "linkstate");
342 final File links = new File(parent, "Links.java");
343 final File linksBuilder = new File(parent, "LinksBuilder.java");
344 assertTrue(links.exists());
345 assertTrue(linksBuilder.exists());
346 CompilationTestUtils.assertFilesCount(parent, 3);
347 parent = new File(parent, "links");
348 final File source = new File(parent, "Source.java");
349 final File sourceBuilder = new File(parent, "SourceBuilder.java");
350 assertTrue(source.exists());
351 assertTrue(sourceBuilder.exists());
352 CompilationTestUtils.assertFilesCount(parent, 3);
353 parent = new File(parent, "source");
354 final File address = new File(parent, "Address.java");
355 final File addressBuilder = new File(parent, "AddressBuilder.java");
356 assertTrue(address.exists());
357 assertTrue(addressBuilder.exists());
358 CompilationTestUtils.assertFilesCount(parent, 2);
360 // Test if sources are compilable
361 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
363 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
367 public void testLeafReturnTypes() throws Exception {
368 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("leaf-return-types");
369 final File compiledOutputDir = CompilationTestUtils.compilerOutput("leaf-return-types");
370 generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
372 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
373 assertTrue(new File(parent, "TestData.java").exists());
374 assertTrue(new File(parent, "Nodes.java").exists());
375 assertTrue(new File(parent, "NodesBuilder.java").exists());
376 assertTrue(new File(parent, "Alg.java").exists());
377 assertTrue(new File(parent, "NodesIdUnionBuilder.java").exists());
378 CompilationTestUtils.assertFilesCount(parent, 6);
380 // Test if sources are compilable
381 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
383 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008";
384 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
385 final Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
386 final Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
388 // Test methods return type
389 final byte[] b = new byte[] {};
390 CompilationTestUtils.assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
391 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
392 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "isIdBoolean");
393 CompilationTestUtils.assertContainsMethod(nodesClass, BigDecimal.class, "getIdDecimal64");
394 CompilationTestUtils.assertContainsMethod(nodesClass, Empty.class, "getIdEmpty");
395 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
396 testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
397 testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
398 CompilationTestUtils.assertContainsMethod(nodesClass, Byte.class, "getId8");
399 CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getId16");
400 CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getId32");
401 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getId64");
402 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
403 CompilationTestUtils.assertContainsMethod(nodesClass, String.class, "getIdString");
404 CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getIdU8");
405 CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getIdU16");
406 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdU32");
407 CompilationTestUtils.assertContainsMethod(nodesClass, BigInteger.class, "getIdU64");
408 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
410 final Object builderObj = builderClass.getDeclaredConstructor().newInstance();
412 Method method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary",
414 final List<Range<Integer>> lengthConstraints = new ArrayList<>();
415 lengthConstraints.add(Range.closed(1, 10));
416 byte[] arg = new byte[] {};
417 String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg),
419 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg);
421 method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64",
423 final List<Range<BigDecimal>> rangeConstraints = new ArrayList<>();
424 rangeConstraints.add(Range.closed(new BigDecimal("1.5"), new BigDecimal("5.5")));
425 Object arg1 = new BigDecimal("1.4");
426 expectedMsg = String.format("Invalid range: %s, expected: %s.", arg1, rangeConstraints);
427 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg1);
429 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
433 public void testGenerationContextReferenceExtension() throws IOException, URISyntaxException,
434 ClassNotFoundException {
435 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("context-reference");
436 final File compiledOutputDir = CompilationTestUtils.compilerOutput("context-reference");
437 generateTestSources("/compilation/context-reference", sourcesOutputDir);
439 // Test if all sources are generated
440 final File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
441 CompilationTestUtils.assertFilesCount(fooParent, 4);
442 assertTrue(new File(fooParent, "FooData.java").exists());
443 assertTrue(new File(fooParent, "Nodes.java").exists());
444 assertTrue(new File(fooParent, "NodesBuilder.java").exists());
446 final File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
447 CompilationTestUtils.assertFilesCount(barParent, 2);
448 assertTrue(new File(barParent, "IdentityClass.java").exists());
450 // Test if sources are compilable
451 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
453 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
454 final Class<?> nodesClass = Class.forName(CompilationTestUtils.BASE_PKG
455 + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
456 final Class<?> identityClass = Class
457 .forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
460 final Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true,
462 assertEquals(ImmutableList.of(baseIdentity), Arrays.asList(identityClass.getInterfaces()));
467 getId = nodesClass.getMethod("getId");
468 } catch (final NoSuchMethodException e) {
469 throw new AssertionError("Method getId() not found", e);
472 assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
473 .map(Annotation::annotationType).collect(Collectors.toSet()));
474 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
478 public void compilationTest() throws Exception {
479 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("yang");
480 final File compiledOutputDir = CompilationTestUtils.compilerOutput("yang");
481 generateTestSources("/yang", sourcesOutputDir);
483 // Test if sources are compilable
484 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
486 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
490 public void testBug586() throws Exception {
491 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug586");
492 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug586");
493 generateTestSources("/compilation/bug586", sourcesOutputDir);
495 // Test if sources are compilable
496 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
498 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
502 public void testBug4760() throws Exception {
503 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug4760");
504 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug4760");
505 generateTestSources("/compilation/bug4760", sourcesOutputDir);
507 // Test if sources are compilable
508 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
510 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
514 * Test handling nested uses-augmentations.
517 public void testBug1172() throws Exception {
518 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1172");
519 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1172");
520 generateTestSources("/compilation/bug1172", sourcesOutputDir);
522 // Test if sources are compilable
523 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
525 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
529 public void testBug5461() throws Exception {
530 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5461");
531 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5461");
532 generateTestSources("/compilation/bug5461", sourcesOutputDir);
534 // Test if sources are compilable
535 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
537 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
541 public void testBug5882() throws Exception {
542 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5882");
543 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5882");
544 generateTestSources("/compilation/bug5882", sourcesOutputDir);
546 // Test if sources are compilable
547 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
549 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882);
550 assertTrue(new File(parent, "FooData.java").exists());
551 assertTrue(new File(parent, "TypedefCurrent.java").exists());
552 assertTrue(new File(parent, "TypedefDeprecated.java").exists());
554 try (URLClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() })) {
555 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
556 final Class<?> cls = loader.loadClass(pkg + ".FooData");
557 final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
558 final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
559 final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
560 final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
561 final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
562 final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
563 final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
564 final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
565 assertTrue(clsTypedefDepr.getAnnotations()[0].toString().contains("Deprecated"));
566 assertTrue(clsTypedefCur.getAnnotations().length == 0);
567 assertTrue(clsGroupingDepr.getAnnotations()[0].toString().contains("Deprecated"));
568 assertTrue(clsGroupingCur.getAnnotations().length == 0);
569 assertTrue(clsTypeDef1.getAnnotations().length == 0);
570 assertTrue(clsTypeDef3.getAnnotations().length == 0);
571 assertTrue(clsTypeDef2.getAnnotations()[0].toString().contains("Deprecated"));
573 /*methods inside container*/
574 assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
575 assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
576 assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
577 assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
578 assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
579 assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
581 /*methods inside module*/
582 assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
583 assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
584 assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
585 assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
586 assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
587 assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
588 assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
591 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
595 * Test if class generated for node from grouping implements ChildOf.
598 public void testBug1377() throws Exception {
599 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1377");
600 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1377");
602 generateTestSources("/compilation/bug1377", sourcesOutputDir);
604 // Test if sources are compilable
605 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
607 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
608 final Class<?> outputActionClass = Class.forName(CompilationTestUtils.BASE_PKG
609 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
610 final Class<?> actionClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.test.foo.rev140717.Action",
613 // Test generated 'container output-action'
614 assertTrue(outputActionClass.isInterface());
615 CompilationTestUtils.assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(),
616 actionClass.getCanonicalName());
618 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
622 public void testMdsal327() throws Exception {
623 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal327");
624 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal327");
625 generateTestSources("/compilation/mdsal327", sourcesOutputDir);
626 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
627 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
631 public void testMdsal365() throws Exception {
632 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal365");
633 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal365");
634 generateTestSources("/compilation/mdsal365", sourcesOutputDir);
635 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
636 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
640 public void testMdsal395() throws Exception {
641 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal395");
642 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal395");
643 generateTestSources("/compilation/mdsal395", sourcesOutputDir);
644 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
645 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
649 public void classNamesColisionTest() throws Exception {
650 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("class-name-collision");
651 final File compiledOutputDir = CompilationTestUtils.compilerOutput("class-name-collision");
652 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
653 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
654 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
658 public void innerEnumerationNameCollisionTest() throws Exception {
659 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal321");
660 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal321");
661 generateTestSources("/compilation/mdsal321", sourcesOutputDir);
662 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
663 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
667 public void twoNestedUnionsTest() throws Exception {
668 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal320");
669 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal320");
670 generateTestSources("/compilation/mdsal320", sourcesOutputDir);
671 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
672 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
676 public void testMdsal425() throws Exception {
677 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal425");
678 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal425");
679 generateTestSources("/compilation/mdsal425", sourcesOutputDir);
680 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
681 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
684 private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
685 final String returnTypeStr) throws NoSuchMethodException {
686 Method method = clazz.getMethod(methodName);
687 assertEquals(java.lang.Class.class, method.getReturnType());
688 java.lang.reflect.Type returnType = method.getGenericReturnType();
689 assertTrue(returnType instanceof ParameterizedType);
690 final ParameterizedType pt = (ParameterizedType) returnType;
691 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
692 assertEquals(1, parameters.length);
693 final java.lang.reflect.Type parameter = parameters[0];
694 assertTrue(parameter instanceof WildcardType);
695 final WildcardType wildcardType = (WildcardType) parameter;
696 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
699 private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz,
700 final String methodName) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
701 final Method method = clazz.getMethod(methodName);
702 final Class<?> rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true,
704 assertEquals(rawReturnType, method.getReturnType());
705 final java.lang.reflect.Type returnType = method.getGenericReturnType();
706 assertTrue(returnType instanceof ParameterizedType);
707 final ParameterizedType pt = (ParameterizedType) returnType;
708 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
709 assertEquals(1, parameters.length);
710 final java.lang.reflect.Type parameter = parameters[0];
711 assertTrue(parameter instanceof WildcardType);
712 final WildcardType wildcardType = (WildcardType) parameter;
713 assertEquals("?", wildcardType.toString());