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, "getIdBoolean");
396 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "isIdBoolean");
397 CompilationTestUtils.assertContainsMethod(nodesClass, BigDecimal.class, "getIdDecimal64");
398 CompilationTestUtils.assertContainsMethod(nodesClass, Empty.class, "getIdEmpty");
399 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
400 testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
401 testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
402 CompilationTestUtils.assertContainsMethod(nodesClass, Byte.class, "getId8");
403 CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getId16");
404 CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getId32");
405 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getId64");
406 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
407 CompilationTestUtils.assertContainsMethod(nodesClass, String.class, "getIdString");
408 CompilationTestUtils.assertContainsMethod(nodesClass, Uint8.class, "getIdU8");
409 CompilationTestUtils.assertContainsMethod(nodesClass, Uint16.class, "getIdU16");
410 CompilationTestUtils.assertContainsMethod(nodesClass, Uint32.class, "getIdU32");
411 CompilationTestUtils.assertContainsMethod(nodesClass, Uint64.class, "getIdU64");
412 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
414 final Object builderObj = builderClass.getDeclaredConstructor().newInstance();
416 Method method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary",
418 final List<Range<Integer>> lengthConstraints = new ArrayList<>();
419 lengthConstraints.add(Range.closed(1, 10));
420 byte[] arg = new byte[] {};
421 String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg),
423 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg);
425 method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64",
427 final List<Range<BigDecimal>> rangeConstraints = new ArrayList<>();
428 rangeConstraints.add(Range.closed(new BigDecimal("1.5"), new BigDecimal("5.5")));
429 Object arg1 = new BigDecimal("1.4");
430 expectedMsg = String.format("Invalid range: %s, expected: %s.", arg1, rangeConstraints);
431 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg1);
433 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
437 public void testGenerationContextReferenceExtension() throws IOException, URISyntaxException,
438 ClassNotFoundException {
439 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("context-reference");
440 final File compiledOutputDir = CompilationTestUtils.compilerOutput("context-reference");
441 generateTestSources("/compilation/context-reference", sourcesOutputDir);
443 // Test if all sources are generated
444 final File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
445 CompilationTestUtils.assertFilesCount(fooParent, 4);
446 assertTrue(new File(fooParent, "FooData.java").exists());
447 assertTrue(new File(fooParent, "Nodes.java").exists());
448 assertTrue(new File(fooParent, "NodesBuilder.java").exists());
450 final File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
451 CompilationTestUtils.assertFilesCount(barParent, 2);
452 assertTrue(new File(barParent, "IdentityClass.java").exists());
454 // Test if sources are compilable
455 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
457 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
458 final Class<?> nodesClass = Class.forName(CompilationTestUtils.BASE_PKG
459 + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
460 final Class<?> identityClass = Class
461 .forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
464 final Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true,
466 assertEquals(ImmutableList.of(baseIdentity), Arrays.asList(identityClass.getInterfaces()));
471 getId = nodesClass.getMethod("getId");
472 } catch (final NoSuchMethodException e) {
473 throw new AssertionError("Method getId() not found", e);
476 assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
477 .map(Annotation::annotationType).collect(Collectors.toSet()));
478 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
482 public void compilationTest() throws Exception {
483 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("yang");
484 final File compiledOutputDir = CompilationTestUtils.compilerOutput("yang");
485 generateTestSources("/yang", sourcesOutputDir);
487 // Test if sources are compilable
488 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
490 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
494 public void testBug586() throws Exception {
495 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug586");
496 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug586");
497 generateTestSources("/compilation/bug586", sourcesOutputDir);
499 // Test if sources are compilable
500 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
502 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
506 public void testBug4760() throws Exception {
507 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug4760");
508 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug4760");
509 generateTestSources("/compilation/bug4760", sourcesOutputDir);
511 // Test if sources are compilable
512 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
514 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
518 * Test handling nested uses-augmentations.
521 public void testBug1172() throws Exception {
522 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1172");
523 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1172");
524 generateTestSources("/compilation/bug1172", sourcesOutputDir);
526 // Test if sources are compilable
527 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
529 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
533 public void testBug5461() throws Exception {
534 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5461");
535 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5461");
536 generateTestSources("/compilation/bug5461", sourcesOutputDir);
538 // Test if sources are compilable
539 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
541 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
545 public void testBug5882() throws Exception {
546 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5882");
547 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5882");
548 generateTestSources("/compilation/bug5882", sourcesOutputDir);
550 // Test if sources are compilable
551 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
553 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882);
554 assertTrue(new File(parent, "FooData.java").exists());
555 assertTrue(new File(parent, "TypedefCurrent.java").exists());
556 assertTrue(new File(parent, "TypedefDeprecated.java").exists());
558 try (URLClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() })) {
559 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
560 final Class<?> cls = loader.loadClass(pkg + ".FooData");
561 final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
562 final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
563 final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
564 final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
565 final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
566 final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
567 final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
568 final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
569 assertTrue(clsTypedefDepr.getAnnotations()[0].toString().contains("Deprecated"));
570 assertTrue(clsTypedefCur.getAnnotations().length == 0);
571 assertTrue(clsGroupingDepr.getAnnotations()[0].toString().contains("Deprecated"));
572 assertTrue(clsGroupingCur.getAnnotations().length == 0);
573 assertTrue(clsTypeDef1.getAnnotations().length == 0);
574 assertTrue(clsTypeDef3.getAnnotations().length == 0);
575 assertTrue(clsTypeDef2.getAnnotations()[0].toString().contains("Deprecated"));
577 /*methods inside container*/
578 assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
579 assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
580 assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
581 assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
582 assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
583 assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
585 /*methods inside module*/
586 assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
587 assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
588 assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
589 assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
590 assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
591 assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
592 assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
595 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
599 * Test if class generated for node from grouping implements ChildOf.
602 public void testBug1377() throws Exception {
603 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1377");
604 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1377");
606 generateTestSources("/compilation/bug1377", sourcesOutputDir);
608 // Test if sources are compilable
609 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
611 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
612 final Class<?> outputActionClass = Class.forName(CompilationTestUtils.BASE_PKG
613 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
614 final Class<?> actionClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.test.foo.rev140717.Action",
617 // Test generated 'container output-action'
618 assertTrue(outputActionClass.isInterface());
619 CompilationTestUtils.assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(),
620 actionClass.getCanonicalName());
622 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
626 public void testMdsal327() throws Exception {
627 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal327");
628 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal327");
629 generateTestSources("/compilation/mdsal327", sourcesOutputDir);
630 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
631 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
635 public void testMdsal365() throws Exception {
636 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal365");
637 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal365");
638 generateTestSources("/compilation/mdsal365", sourcesOutputDir);
639 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
640 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
644 public void testMdsal395() throws Exception {
645 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal395");
646 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal395");
647 generateTestSources("/compilation/mdsal395", sourcesOutputDir);
648 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
649 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
653 public void classNamesColisionTest() throws Exception {
654 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("class-name-collision");
655 final File compiledOutputDir = CompilationTestUtils.compilerOutput("class-name-collision");
656 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
657 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
658 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
662 public void innerEnumerationNameCollisionTest() throws Exception {
663 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal321");
664 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal321");
665 generateTestSources("/compilation/mdsal321", sourcesOutputDir);
666 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
667 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
671 public void twoNestedUnionsTest() throws Exception {
672 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal320");
673 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal320");
674 generateTestSources("/compilation/mdsal320", sourcesOutputDir);
675 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
676 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
680 public void testMdsal425() throws Exception {
681 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal425");
682 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal425");
683 generateTestSources("/compilation/mdsal425", sourcesOutputDir);
684 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
685 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
689 public void testMdsal426() throws Exception {
690 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal426");
691 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal426");
692 generateTestSources("/compilation/mdsal426", sourcesOutputDir);
693 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
694 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
698 public void testMdsal529() throws Exception {
699 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal529");
700 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal529");
701 generateTestSources("/compilation/mdsal529", sourcesOutputDir);
702 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
703 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
707 public void testMdsal589() throws Exception {
708 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal589");
709 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal589");
710 generateTestSources("/compilation/mdsal589", sourcesOutputDir);
711 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
712 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
715 public void testMdsal533() throws Exception {
716 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal533");
717 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal533");
718 generateTestSources("/compilation/mdsal533", sourcesOutputDir);
719 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
720 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
723 private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
724 final String returnTypeStr) throws NoSuchMethodException {
725 Method method = clazz.getMethod(methodName);
726 assertEquals(java.lang.Class.class, method.getReturnType());
727 java.lang.reflect.Type returnType = method.getGenericReturnType();
728 assertTrue(returnType instanceof ParameterizedType);
729 final ParameterizedType pt = (ParameterizedType) returnType;
730 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
731 assertEquals(1, parameters.length);
732 final java.lang.reflect.Type parameter = parameters[0];
733 assertTrue(parameter instanceof WildcardType);
734 final WildcardType wildcardType = (WildcardType) parameter;
735 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
738 private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz,
739 final String methodName) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
740 final Method method = clazz.getMethod(methodName);
741 final Class<?> rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true,
743 assertEquals(rawReturnType, method.getReturnType());
744 final java.lang.reflect.Type returnType = method.getGenericReturnType();
745 assertTrue(returnType instanceof ParameterizedType);
746 final ParameterizedType pt = (ParameterizedType) returnType;
747 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
748 assertEquals(1, parameters.length);
749 final java.lang.reflect.Type parameter = parameters[0];
750 assertTrue(parameter instanceof WildcardType);
751 final WildcardType wildcardType = (WildcardType) parameter;
752 assertEquals("?", wildcardType.toString());