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;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
16 import com.google.common.collect.Collections2;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Range;
21 import java.io.IOException;
22 import java.lang.annotation.Annotation;
23 import java.lang.reflect.Field;
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Modifier;
26 import java.lang.reflect.ParameterizedType;
27 import java.lang.reflect.WildcardType;
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.mdsal.binding.model.ri.TypeConstants;
38 import org.opendaylight.yangtools.yang.binding.ChildOf;
39 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
40 import org.opendaylight.yangtools.yang.common.Decimal64;
41 import org.opendaylight.yangtools.yang.common.Empty;
42 import org.opendaylight.yangtools.yang.common.Uint16;
43 import org.opendaylight.yangtools.yang.common.Uint32;
44 import org.opendaylight.yangtools.yang.common.Uint64;
45 import org.opendaylight.yangtools.yang.common.Uint8;
48 * Test correct code generation.
51 public class CompilationTest extends BaseCompilationTest {
54 * Java 8 allows JaCoCo to hook onto interfaces, as well as
55 * generating a default implementation. We only want to check
58 private static Collection<Method> abstractMethods(final Class<?> clazz) {
60 return Collections2.filter(Arrays.asList(clazz.getDeclaredMethods()),
61 input -> Modifier.isAbstract(input.getModifiers()));
65 public void testListGeneration() throws Exception {
66 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("list-gen");
67 final File compiledOutputDir = CompilationTestUtils.compilerOutput("list-gen");
68 generateTestSources("/compilation/list-gen", sourcesOutputDir);
70 // Test if all sources are generated
71 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
72 final File keyArgs = new File(parent, "KeyArgs.java");
73 final File links = new File(parent, "Links.java");
74 final File linksBuilder = new File(parent, "LinksBuilder.java");
75 final File linksKey = new File(parent, "LinksKey.java");
76 final File testData = new File(parent, "TestData.java");
77 assertTrue(keyArgs.exists());
78 assertTrue(links.exists());
79 assertTrue(linksBuilder.exists());
80 assertTrue(linksKey.exists());
81 assertTrue(testData.exists());
82 CompilationTestUtils.assertFilesCount(parent, 7);
84 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST + CompilationTestUtils.FS + "links");
85 final File level = new File(parent, "Level.java");
86 final File linkGroup = new File(parent, "LinkGroup.java");
87 final File node = new File(parent, "Node.java");
88 final File nodeBuilder = new File(parent, "NodeBuilder.java");
89 final File nodeList = new File(parent, "NodeList.java");
90 final File nodeListBuilder = new File(parent, "NodeListBuilder.java");
91 final File nodesType = new File(parent, "NodesType.java");
92 assertTrue(level.exists());
93 assertTrue(linkGroup.exists());
94 assertTrue(node.exists());
95 assertTrue(nodeBuilder.exists());
96 assertTrue(nodeList.exists());
97 assertTrue(nodeListBuilder.exists());
98 assertTrue(nodesType.exists());
99 CompilationTestUtils.assertFilesCount(parent, 8);
101 // Test if sources are compilable
102 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
104 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
105 final Class<?> keyArgsClass = Class.forName(CompilationTestUtils.BASE_PKG
106 + ".urn.opendaylight.test.rev131008.KeyArgs", true, loader);
107 final Class<?> linksClass = Class.forName(CompilationTestUtils.BASE_PKG
108 + ".urn.opendaylight.test.rev131008.Links", true, loader);
109 final Class<?> linksKeyClass = Class.forName(CompilationTestUtils.BASE_PKG
110 + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
112 // Test generated 'grouping key-args'
113 assertTrue(keyArgsClass.isInterface());
114 CompilationTestUtils.assertContainsMethod(keyArgsClass, String.class, "getName");
115 CompilationTestUtils.assertContainsMethod(keyArgsClass, Integer.class, "getSize");
116 assertEquals(3, abstractMethods(keyArgsClass).size());
118 // Test generated 'list links'
119 assertTrue(linksClass.isInterface());
120 CompilationTestUtils.assertImplementsIfc(linksClass, keyArgsClass);
121 assertEquals(7, abstractMethods(linksClass).size());
122 CompilationTestUtils.assertContainsMethod(linksClass,
123 "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.links.Text", "getText", loader);
124 CompilationTestUtils.assertContainsMethod(linksClass,
125 "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.links.Text", "requireText", loader);
127 // Test list key constructor arguments ordering
128 CompilationTestUtils.assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
129 // Test serialVersionUID generation
130 final Field suid = CompilationTestUtils.assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
131 suid.setAccessible(true);
132 assertEquals(-8829501012356283881L, suid.getLong(null));
134 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
138 public void testAugmentUnderUsesGeneration() throws Exception {
139 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("augment-under-uses");
140 final File compiledOutputDir = CompilationTestUtils.compilerOutput("augment-under-uses");
141 generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
143 // Test if all sources were generated from 'module foo'
144 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
145 assertTrue(new File(parent, "Object.java").exists());
146 assertTrue(new File(parent, "ClosedObject.java").exists());
147 assertTrue(new File(parent, "OpenObject.java").exists());
148 assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
149 assertTrue(new File(parent, "PathKeySubobject.java").exists());
150 CompilationTestUtils.assertFilesCount(parent, 10);
152 parent = new File(parent, "object");
153 assertTrue(new File(parent, "Nodes.java").exists());
154 assertTrue(new File(parent, "NodesBuilder.java").exists());
155 CompilationTestUtils.assertFilesCount(parent, 2);
157 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "closed");
158 CompilationTestUtils.assertFilesCount(parent, 1);
160 parent = new File(parent, "object");
161 assertTrue(new File(parent, "Link1.java").exists());
162 assertTrue(new File(parent, "Link1Builder.java").exists());
163 CompilationTestUtils.assertFilesCount(parent, 2);
165 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "open");
166 CompilationTestUtils.assertFilesCount(parent, 1);
168 parent = new File(parent, "object");
169 assertTrue(new File(parent, "Nodes1.java").exists());
170 assertTrue(new File(parent, "Nodes1Builder.java").exists());
171 CompilationTestUtils.assertFilesCount(parent, 3);
173 parent = new File(parent, "nodes");
174 assertTrue(new File(parent, "Links.java").exists());
175 assertTrue(new File(parent, "LinksBuilder.java").exists());
176 CompilationTestUtils.assertFilesCount(parent, 2);
178 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "explicit");
179 CompilationTestUtils.assertFilesCount(parent, 1);
180 parent = new File(parent, "route");
181 CompilationTestUtils.assertFilesCount(parent, 1);
182 parent = new File(parent, "object");
183 assertTrue(new File(parent, "Subobjects.java").exists());
184 assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
185 CompilationTestUtils.assertFilesCount(parent, 3);
187 parent = new File(parent, "subobjects");
188 CompilationTestUtils.assertFilesCount(parent, 1);
189 parent = new File(parent, "subobject");
190 CompilationTestUtils.assertFilesCount(parent, 1);
191 parent = new File(parent, "type");
192 assertTrue(new File(parent, "PathKey.java").exists());
193 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
194 CompilationTestUtils.assertFilesCount(parent, 3);
196 parent = new File(parent, "path");
197 CompilationTestUtils.assertFilesCount(parent, 1);
198 parent = new File(parent, "key");
199 assertTrue(new File(parent, "PathKey.java").exists());
200 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
201 CompilationTestUtils.assertFilesCount(parent, 2);
203 // Test if all sources were generated from 'module bar'
204 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
205 assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
206 assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
207 assertTrue(new File(parent, "RouteSubobjects.java").exists());
208 CompilationTestUtils.assertFilesCount(parent, 6);
210 parent = new File(parent, "route");
211 CompilationTestUtils.assertFilesCount(parent, 1);
212 parent = new File(new File(sourcesOutputDir, CompilationTestUtils.NS_BAR), "basic");
213 CompilationTestUtils.assertFilesCount(parent, 1);
214 parent = new File(parent, "explicit");
215 CompilationTestUtils.assertFilesCount(parent, 1);
216 parent = new File(parent, "route");
217 CompilationTestUtils.assertFilesCount(parent, 1);
219 parent = new File(parent, "subobjects");
220 CompilationTestUtils.assertFilesCount(parent, 2);
221 assertTrue(new File(parent, "SubobjectType.java").exists());
223 parent = new File(parent, "subobject");
224 CompilationTestUtils.assertFilesCount(parent, 1);
226 parent = new File(parent, "type");
227 assertTrue(new File(parent, "IpPrefix.java").exists());
228 assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
229 assertTrue(new File(parent, "Label.java").exists());
230 assertTrue(new File(parent, "LabelBuilder.java").exists());
231 CompilationTestUtils.assertFilesCount(parent, 4);
233 // Test if sources are compilable
234 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
236 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
240 public void testAugmentOfAugmentGeneration() throws Exception {
241 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("aug-of-aug");
242 final File compiledOutputDir = CompilationTestUtils.compilerOutput("aug-of-aug");
243 generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
245 // Test if all sources were generated from 'module foo'
246 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
247 final File fooListener = new File(parent, "FooListener.java");
248 File pathAttributes = new File(parent, "PathAttributes.java");
249 final File update = new File(parent, "Update.java");
250 final File updateBuilder = new File(parent, "UpdateBuilder.java");
251 assertTrue(fooListener.exists());
252 assertTrue(pathAttributes.exists());
253 assertTrue(update.exists());
254 assertTrue(updateBuilder.exists());
255 CompilationTestUtils.assertFilesCount(parent, 7);
257 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "path");
258 CompilationTestUtils.assertFilesCount(parent, 1);
259 parent = new File(parent, "attributes");
260 CompilationTestUtils.assertFilesCount(parent, 2);
261 final File origin = new File(parent, "Origin.java");
262 final File originBuilder = new File(parent, "OriginBuilder.java");
263 assertTrue(origin.exists());
264 assertTrue(originBuilder.exists());
266 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "update");
267 CompilationTestUtils.assertFilesCount(parent, 2);
268 pathAttributes = new File(parent, "PathAttributes.java");
269 final File pathAttributesBuilder = new File(parent, "PathAttributesBuilder.java");
270 assertTrue(pathAttributes.exists());
271 assertTrue(pathAttributesBuilder.exists());
273 // Test if all sources were generated from 'module bar'
274 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
275 final File destination = new File(parent, "Destination.java");
276 final File pathAttributes1 = new File(parent, "PathAttributes1.java");
277 final File pathAttributes1Builder = new File(parent, "PathAttributes1Builder.java");
278 assertTrue(destination.exists());
279 assertTrue(pathAttributes1.exists());
280 assertTrue(pathAttributes1Builder.exists());
281 CompilationTestUtils.assertFilesCount(parent, 6);
283 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "destination");
284 CompilationTestUtils.assertFilesCount(parent, 2);
285 final File destinationType = new File(parent, "DestinationType.java");
286 assertTrue(destinationType.exists());
288 parent = new File(parent, "destination");
289 CompilationTestUtils.assertFilesCount(parent, 1);
290 parent = new File(parent, "type");
291 CompilationTestUtils.assertFilesCount(parent, 2);
292 final File destinationIpv4 = new File(parent, "DestinationIp.java");
293 final File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
294 assertTrue(destinationIpv4.exists());
295 assertTrue(destinationIpv4Builder.exists());
297 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "update");
298 CompilationTestUtils.assertFilesCount(parent, 1);
299 parent = new File(parent, "path");
300 CompilationTestUtils.assertFilesCount(parent, 1);
301 parent = new File(parent, "attributes");
302 final File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
303 final File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
304 assertTrue(mpUnreachNlri.exists());
305 assertTrue(mpUnreachNlriBuilder.exists());
306 CompilationTestUtils.assertFilesCount(parent, 3);
308 parent = new File(parent, "mp");
309 CompilationTestUtils.assertFilesCount(parent, 1);
310 parent = new File(parent, "unreach");
311 CompilationTestUtils.assertFilesCount(parent, 1);
312 parent = new File(parent, "nlri");
313 final File withdrawnRoutes = new File(parent, "WithdrawnRoutes.java");
314 final File withdrawnRoutesBuilder = new File(parent, "WithdrawnRoutesBuilder.java");
315 assertTrue(withdrawnRoutes.exists());
316 assertTrue(withdrawnRoutesBuilder.exists());
317 CompilationTestUtils.assertFilesCount(parent, 2);
319 // Test if all sources were generated from 'module baz'
320 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ);
321 CompilationTestUtils.assertFilesCount(parent, 3);
322 final File linkstateDestination = new File(parent, "LinkstateDestination.java");
323 assertTrue(linkstateDestination.exists());
325 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ + CompilationTestUtils.FS + "update");
326 CompilationTestUtils.assertFilesCount(parent, 1);
327 parent = new File(parent, "path");
328 CompilationTestUtils.assertFilesCount(parent, 1);
329 parent = new File(parent, "attributes");
330 CompilationTestUtils.assertFilesCount(parent, 1);
331 parent = new File(parent, "mp");
332 CompilationTestUtils.assertFilesCount(parent, 1);
333 parent = new File(parent, "unreach");
334 CompilationTestUtils.assertFilesCount(parent, 1);
335 parent = new File(parent, "nlri");
336 CompilationTestUtils.assertFilesCount(parent, 1);
337 parent = new File(parent, "withdrawn");
338 CompilationTestUtils.assertFilesCount(parent, 1);
339 parent = new File(parent, "routes");
340 CompilationTestUtils.assertFilesCount(parent, 1);
341 parent = new File(parent, "destination");
342 CompilationTestUtils.assertFilesCount(parent, 1);
343 parent = new File(parent, "type");
344 final File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
345 final File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
346 assertTrue(destinationLinkstate.exists());
347 assertTrue(destinationLinkstateBuilder.exists());
348 CompilationTestUtils.assertFilesCount(parent, 3);
349 parent = new File(parent, "destination");
350 CompilationTestUtils.assertFilesCount(parent, 1);
351 parent = new File(parent, "linkstate");
352 final File links = new File(parent, "Links.java");
353 final File linksBuilder = new File(parent, "LinksBuilder.java");
354 assertTrue(links.exists());
355 assertTrue(linksBuilder.exists());
356 CompilationTestUtils.assertFilesCount(parent, 3);
357 parent = new File(parent, "links");
358 final File source = new File(parent, "Source.java");
359 final File sourceBuilder = new File(parent, "SourceBuilder.java");
360 assertTrue(source.exists());
361 assertTrue(sourceBuilder.exists());
362 CompilationTestUtils.assertFilesCount(parent, 3);
363 parent = new File(parent, "source");
364 final File address = new File(parent, "Address.java");
365 final File addressBuilder = new File(parent, "AddressBuilder.java");
366 assertTrue(address.exists());
367 assertTrue(addressBuilder.exists());
368 CompilationTestUtils.assertFilesCount(parent, 2);
370 // Test if sources are compilable
371 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
373 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
377 public void testLeafReturnTypes() throws Exception {
378 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("leaf-return-types");
379 final File compiledOutputDir = CompilationTestUtils.compilerOutput("leaf-return-types");
380 generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
382 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
383 assertTrue(new File(parent, "TestData.java").exists());
384 assertTrue(new File(parent, "Nodes.java").exists());
385 assertTrue(new File(parent, "NodesBuilder.java").exists());
386 assertTrue(new File(parent, "Alg.java").exists());
387 assertTrue(new File(parent, "NodesIdUnionBuilder.java").exists());
388 CompilationTestUtils.assertFilesCount(parent, 6);
390 // Test if sources are compilable
391 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
393 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008";
394 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
395 final Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
396 final Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
398 // Test methods return type
399 final byte[] b = new byte[] {};
400 CompilationTestUtils.assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
401 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
402 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "getIdBoolean");
403 CompilationTestUtils.assertContainsMethod(nodesClass, Decimal64.class, "getIdDecimal64");
404 CompilationTestUtils.assertContainsMethod(nodesClass, Empty.class, "getIdEmpty");
405 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
406 testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
407 testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
408 CompilationTestUtils.assertContainsMethod(nodesClass, Byte.class, "getId8");
409 CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getId16");
410 CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getId32");
411 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getId64");
412 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
413 CompilationTestUtils.assertContainsMethod(nodesClass, String.class, "getIdString");
414 CompilationTestUtils.assertContainsMethod(nodesClass, Uint8.class, "getIdU8");
415 CompilationTestUtils.assertContainsMethod(nodesClass, Uint16.class, "getIdU16");
416 CompilationTestUtils.assertContainsMethod(nodesClass, Uint32.class, "getIdU32");
417 CompilationTestUtils.assertContainsMethod(nodesClass, Uint64.class, "getIdU64");
418 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
420 final Object builderObj = builderClass.getDeclaredConstructor().newInstance();
422 Method method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary",
424 final List<Range<Integer>> lengthConstraints = new ArrayList<>();
425 lengthConstraints.add(Range.closed(1, 10));
426 byte[] arg = new byte[] {};
427 String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg),
429 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg);
431 method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64",
433 final List<Range<Decimal64>> rangeConstraints = new ArrayList<>();
434 rangeConstraints.add(Range.closed(Decimal64.valueOf("1.5"), Decimal64.valueOf("5.5")));
435 Object arg1 = Decimal64.valueOf("1.4");
436 expectedMsg = String.format("Invalid range: %s, expected: %s.", arg1, rangeConstraints);
437 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg1);
439 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
443 public void testGenerationContextReferenceExtension() throws IOException, URISyntaxException,
444 ClassNotFoundException {
445 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("context-reference");
446 final File compiledOutputDir = CompilationTestUtils.compilerOutput("context-reference");
447 generateTestSources("/compilation/context-reference", sourcesOutputDir);
449 // Test if all sources are generated
450 final File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
451 CompilationTestUtils.assertFilesCount(fooParent, 4);
452 assertTrue(new File(fooParent, "FooData.java").exists());
453 assertTrue(new File(fooParent, "Nodes.java").exists());
454 assertTrue(new File(fooParent, "NodesBuilder.java").exists());
456 final File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
457 CompilationTestUtils.assertFilesCount(barParent, 2);
458 assertTrue(new File(barParent, "IdentityClass.java").exists());
460 // Test if sources are compilable
461 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
463 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
464 final Class<?> nodesClass = Class.forName(CompilationTestUtils.BASE_PKG
465 + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
466 final Class<?> identityClass = Class
467 .forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
470 final Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true,
472 assertEquals(ImmutableList.of(baseIdentity), Arrays.asList(identityClass.getInterfaces()));
477 getId = nodesClass.getMethod("getId");
478 } catch (final NoSuchMethodException e) {
479 throw new AssertionError("Method getId() not found", e);
482 assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
483 .map(Annotation::annotationType).collect(Collectors.toSet()));
484 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
488 public void compilationTest() throws Exception {
489 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("yang");
490 final File compiledOutputDir = CompilationTestUtils.compilerOutput("yang");
491 generateTestSources("/yang", sourcesOutputDir);
493 // Test if sources are compilable
494 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
496 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
500 public void testBug586() throws Exception {
501 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug586");
502 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug586");
503 generateTestSources("/compilation/bug586", sourcesOutputDir);
505 // Test if sources are compilable
506 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
508 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
512 public void testBug4760() throws Exception {
513 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug4760");
514 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug4760");
515 generateTestSources("/compilation/bug4760", sourcesOutputDir);
517 // Test if sources are compilable
518 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
520 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
524 * Test handling nested uses-augmentations.
527 public void testBug1172() throws Exception {
528 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1172");
529 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1172");
530 generateTestSources("/compilation/bug1172", sourcesOutputDir);
532 // Test if sources are compilable
533 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
535 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
539 public void testBug5461() throws Exception {
540 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5461");
541 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5461");
542 generateTestSources("/compilation/bug5461", sourcesOutputDir);
544 // Test if sources are compilable
545 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
547 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
551 public void testBug5882() throws Exception {
552 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5882");
553 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5882");
554 generateTestSources("/compilation/bug5882", sourcesOutputDir);
556 // Test if sources are compilable
557 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
559 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882);
560 assertTrue(new File(parent, "FooData.java").exists());
561 assertTrue(new File(parent, "TypedefCurrent.java").exists());
562 assertTrue(new File(parent, "TypedefDeprecated.java").exists());
564 try (URLClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() })) {
565 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
566 final Class<?> cls = loader.loadClass(pkg + ".FooData");
567 final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
568 final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
569 final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
570 final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
571 final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
572 final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
573 final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
574 final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
575 assertEquals(1, clsTypedefDepr.getAnnotations().length);
576 assertThat(clsTypedefDepr.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
577 assertEquals(0, clsTypedefCur.getAnnotations().length);
578 assertEquals(1, clsGroupingDepr.getAnnotations().length);
579 assertThat(clsGroupingDepr.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
580 assertEquals(0, clsGroupingCur.getAnnotations().length);
581 assertEquals(0, clsTypeDef1.getAnnotations().length);
582 assertEquals(1, clsTypeDef2.getAnnotations().length);
583 assertThat(clsTypeDef2.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
584 assertEquals(0, clsTypeDef3.getAnnotations().length);
586 /*methods inside container*/
587 assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
588 assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
589 assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
590 assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
591 assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
592 assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
594 /*methods inside module*/
595 assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
596 assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
597 assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
598 assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
599 assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
600 assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
601 assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
604 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
608 * Test if class generated for node from grouping implements ChildOf.
611 public void testBug1377() throws Exception {
612 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1377");
613 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1377");
615 generateTestSources("/compilation/bug1377", sourcesOutputDir);
617 // Test if sources are compilable
618 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
620 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
621 final Class<?> outputActionClass = Class.forName(CompilationTestUtils.BASE_PKG
622 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
623 final Class<?> actionClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.test.foo.rev140717.Action",
626 // Test generated 'container output-action'
627 assertTrue(outputActionClass.isInterface());
628 CompilationTestUtils.assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(),
629 actionClass.getCanonicalName());
631 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
635 public void testMdsal327() throws Exception {
636 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal327");
637 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal327");
638 generateTestSources("/compilation/mdsal327", sourcesOutputDir);
639 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
640 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
644 public void testMdsal365() throws Exception {
645 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal365");
646 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal365");
647 generateTestSources("/compilation/mdsal365", sourcesOutputDir);
648 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
649 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
653 public void testMdsal395() throws Exception {
654 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal395");
655 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal395");
656 generateTestSources("/compilation/mdsal395", sourcesOutputDir);
657 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
658 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
662 public void classNamesColisionTest() throws Exception {
663 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("class-name-collision");
664 final File compiledOutputDir = CompilationTestUtils.compilerOutput("class-name-collision");
665 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
666 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
667 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
671 public void innerEnumerationNameCollisionTest() throws Exception {
672 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal321");
673 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal321");
674 generateTestSources("/compilation/mdsal321", sourcesOutputDir);
675 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
676 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
680 public void twoNestedUnionsTest() throws Exception {
681 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal320");
682 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal320");
683 generateTestSources("/compilation/mdsal320", sourcesOutputDir);
684 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
685 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
689 public void testMdsal425() throws Exception {
690 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal425");
691 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal425");
692 generateTestSources("/compilation/mdsal425", sourcesOutputDir);
693 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
694 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
698 public void testMdsal426() throws Exception {
699 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal426");
700 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal426");
701 generateTestSources("/compilation/mdsal426", sourcesOutputDir);
702 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
703 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
707 public void testMdsal529() throws Exception {
708 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal529");
709 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal529");
710 generateTestSources("/compilation/mdsal529", sourcesOutputDir);
711 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
712 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
716 public void testMdsal589() throws Exception {
717 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal589");
718 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal589");
719 generateTestSources("/compilation/mdsal589", sourcesOutputDir);
720 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
721 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
725 public void testMdsal533() throws Exception {
726 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal533");
727 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal533");
728 generateTestSources("/compilation/mdsal533", sourcesOutputDir);
729 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
730 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
734 public void testMdsal664() throws Exception {
735 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal664");
736 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal664");
737 generateTestSources("/compilation/mdsal664", sourcesOutputDir);
738 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
739 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
743 public void testUnionStringPatterns() throws Exception {
744 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-string-pattern");
745 final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-string-pattern");
746 generateTestSources("/compilation/union-string-pattern", sourcesOutputDir);
747 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
749 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
750 final Class<?> fooClass = Class.forName(CompilationTestUtils.BASE_PKG + ".foo.norev.Foo", true, loader);
752 final Field patterns = fooClass.getDeclaredField(TypeConstants.PATTERN_CONSTANT_NAME);
753 assertEquals(List.class, patterns.getType());
755 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
758 private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
759 final String returnTypeStr) throws NoSuchMethodException {
760 Method method = clazz.getMethod(methodName);
761 assertEquals(java.lang.Class.class, method.getReturnType());
762 java.lang.reflect.Type returnType = method.getGenericReturnType();
763 assertTrue(returnType instanceof ParameterizedType);
764 final ParameterizedType pt = (ParameterizedType) returnType;
765 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
766 assertEquals(1, parameters.length);
767 final java.lang.reflect.Type parameter = parameters[0];
768 assertTrue(parameter instanceof WildcardType);
769 final WildcardType wildcardType = (WildcardType) parameter;
770 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
773 private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz,
774 final String methodName) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
775 final Method method = clazz.getMethod(methodName);
776 final Class<?> rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true,
778 assertEquals(rawReturnType, method.getReturnType());
779 final java.lang.reflect.Type returnType = method.getGenericReturnType();
780 assertTrue(returnType instanceof ParameterizedType);
781 final ParameterizedType pt = (ParameterizedType) returnType;
782 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
783 assertEquals(1, parameters.length);
784 final java.lang.reflect.Type parameter = parameters[0];
785 assertTrue(parameter instanceof WildcardType);
786 final WildcardType wildcardType = (WildcardType) parameter;
787 assertEquals("?", wildcardType.toString());