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.math.BigDecimal;
29 import java.net.URISyntaxException;
31 import java.net.URLClassLoader;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.stream.Collectors;
37 import org.junit.Test;
38 import org.opendaylight.mdsal.binding.model.util.TypeConstants;
39 import org.opendaylight.yangtools.yang.binding.ChildOf;
40 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
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());
123 // Test list key constructor arguments ordering
124 CompilationTestUtils.assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
125 // Test serialVersionUID generation
126 final Field suid = CompilationTestUtils.assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
127 suid.setAccessible(true);
128 assertEquals(-8829501012356283881L, suid.getLong(null));
130 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
134 public void testAugmentUnderUsesGeneration() throws Exception {
135 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("augment-under-uses");
136 final File compiledOutputDir = CompilationTestUtils.compilerOutput("augment-under-uses");
137 generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
139 // Test if all sources were generated from 'module foo'
140 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
141 assertTrue(new File(parent, "Object.java").exists());
142 assertTrue(new File(parent, "ClosedObject.java").exists());
143 assertTrue(new File(parent, "OpenObject.java").exists());
144 assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
145 assertTrue(new File(parent, "PathKeySubobject.java").exists());
146 CompilationTestUtils.assertFilesCount(parent, 10);
148 parent = new File(parent, "object");
149 assertTrue(new File(parent, "Nodes.java").exists());
150 assertTrue(new File(parent, "NodesBuilder.java").exists());
151 CompilationTestUtils.assertFilesCount(parent, 2);
153 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "closed");
154 CompilationTestUtils.assertFilesCount(parent, 1);
156 parent = new File(parent, "object");
157 assertTrue(new File(parent, "Link1.java").exists());
158 assertTrue(new File(parent, "Link1Builder.java").exists());
159 CompilationTestUtils.assertFilesCount(parent, 2);
161 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "open");
162 CompilationTestUtils.assertFilesCount(parent, 1);
164 parent = new File(parent, "object");
165 assertTrue(new File(parent, "Nodes1.java").exists());
166 assertTrue(new File(parent, "Nodes1Builder.java").exists());
167 CompilationTestUtils.assertFilesCount(parent, 3);
169 parent = new File(parent, "nodes");
170 assertTrue(new File(parent, "Links.java").exists());
171 assertTrue(new File(parent, "LinksBuilder.java").exists());
172 CompilationTestUtils.assertFilesCount(parent, 2);
174 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "explicit");
175 CompilationTestUtils.assertFilesCount(parent, 1);
176 parent = new File(parent, "route");
177 CompilationTestUtils.assertFilesCount(parent, 1);
178 parent = new File(parent, "object");
179 assertTrue(new File(parent, "Subobjects.java").exists());
180 assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
181 CompilationTestUtils.assertFilesCount(parent, 3);
183 parent = new File(parent, "subobjects");
184 CompilationTestUtils.assertFilesCount(parent, 1);
185 parent = new File(parent, "subobject");
186 CompilationTestUtils.assertFilesCount(parent, 1);
187 parent = new File(parent, "type");
188 assertTrue(new File(parent, "PathKey.java").exists());
189 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
190 CompilationTestUtils.assertFilesCount(parent, 3);
192 parent = new File(parent, "path");
193 CompilationTestUtils.assertFilesCount(parent, 1);
194 parent = new File(parent, "key");
195 assertTrue(new File(parent, "PathKey.java").exists());
196 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
197 CompilationTestUtils.assertFilesCount(parent, 2);
199 // Test if all sources were generated from 'module bar'
200 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
201 assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
202 assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
203 assertTrue(new File(parent, "RouteSubobjects.java").exists());
204 CompilationTestUtils.assertFilesCount(parent, 6);
206 parent = new File(parent, "route");
207 CompilationTestUtils.assertFilesCount(parent, 1);
208 parent = new File(new File(sourcesOutputDir, CompilationTestUtils.NS_BAR), "basic");
209 CompilationTestUtils.assertFilesCount(parent, 1);
210 parent = new File(parent, "explicit");
211 CompilationTestUtils.assertFilesCount(parent, 1);
212 parent = new File(parent, "route");
213 CompilationTestUtils.assertFilesCount(parent, 1);
215 parent = new File(parent, "subobjects");
216 CompilationTestUtils.assertFilesCount(parent, 2);
217 assertTrue(new File(parent, "SubobjectType.java").exists());
219 parent = new File(parent, "subobject");
220 CompilationTestUtils.assertFilesCount(parent, 1);
222 parent = new File(parent, "type");
223 assertTrue(new File(parent, "IpPrefix.java").exists());
224 assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
225 assertTrue(new File(parent, "Label.java").exists());
226 assertTrue(new File(parent, "LabelBuilder.java").exists());
227 CompilationTestUtils.assertFilesCount(parent, 4);
229 // Test if sources are compilable
230 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
232 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
236 public void testAugmentOfAugmentGeneration() throws Exception {
237 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("aug-of-aug");
238 final File compiledOutputDir = CompilationTestUtils.compilerOutput("aug-of-aug");
239 generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
241 // Test if all sources were generated from 'module foo'
242 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
243 final File fooListener = new File(parent, "FooListener.java");
244 File pathAttributes = new File(parent, "PathAttributes.java");
245 final File update = new File(parent, "Update.java");
246 final File updateBuilder = new File(parent, "UpdateBuilder.java");
247 assertTrue(fooListener.exists());
248 assertTrue(pathAttributes.exists());
249 assertTrue(update.exists());
250 assertTrue(updateBuilder.exists());
251 CompilationTestUtils.assertFilesCount(parent, 7);
253 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "path");
254 CompilationTestUtils.assertFilesCount(parent, 1);
255 parent = new File(parent, "attributes");
256 CompilationTestUtils.assertFilesCount(parent, 2);
257 final File origin = new File(parent, "Origin.java");
258 final File originBuilder = new File(parent, "OriginBuilder.java");
259 assertTrue(origin.exists());
260 assertTrue(originBuilder.exists());
262 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "update");
263 CompilationTestUtils.assertFilesCount(parent, 2);
264 pathAttributes = new File(parent, "PathAttributes.java");
265 final File pathAttributesBuilder = new File(parent, "PathAttributesBuilder.java");
266 assertTrue(pathAttributes.exists());
267 assertTrue(pathAttributesBuilder.exists());
269 // Test if all sources were generated from 'module bar'
270 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
271 final File destination = new File(parent, "Destination.java");
272 final File pathAttributes1 = new File(parent, "PathAttributes1.java");
273 final File pathAttributes1Builder = new File(parent, "PathAttributes1Builder.java");
274 assertTrue(destination.exists());
275 assertTrue(pathAttributes1.exists());
276 assertTrue(pathAttributes1Builder.exists());
277 CompilationTestUtils.assertFilesCount(parent, 6);
279 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "destination");
280 CompilationTestUtils.assertFilesCount(parent, 2);
281 final File destinationType = new File(parent, "DestinationType.java");
282 assertTrue(destinationType.exists());
284 parent = new File(parent, "destination");
285 CompilationTestUtils.assertFilesCount(parent, 1);
286 parent = new File(parent, "type");
287 CompilationTestUtils.assertFilesCount(parent, 2);
288 final File destinationIpv4 = new File(parent, "DestinationIp.java");
289 final File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
290 assertTrue(destinationIpv4.exists());
291 assertTrue(destinationIpv4Builder.exists());
293 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "update");
294 CompilationTestUtils.assertFilesCount(parent, 1);
295 parent = new File(parent, "path");
296 CompilationTestUtils.assertFilesCount(parent, 1);
297 parent = new File(parent, "attributes");
298 final File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
299 final File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
300 assertTrue(mpUnreachNlri.exists());
301 assertTrue(mpUnreachNlriBuilder.exists());
302 CompilationTestUtils.assertFilesCount(parent, 3);
304 parent = new File(parent, "mp");
305 CompilationTestUtils.assertFilesCount(parent, 1);
306 parent = new File(parent, "unreach");
307 CompilationTestUtils.assertFilesCount(parent, 1);
308 parent = new File(parent, "nlri");
309 final File withdrawnRoutes = new File(parent, "WithdrawnRoutes.java");
310 final File withdrawnRoutesBuilder = new File(parent, "WithdrawnRoutesBuilder.java");
311 assertTrue(withdrawnRoutes.exists());
312 assertTrue(withdrawnRoutesBuilder.exists());
313 CompilationTestUtils.assertFilesCount(parent, 2);
315 // Test if all sources were generated from 'module baz'
316 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ);
317 CompilationTestUtils.assertFilesCount(parent, 3);
318 final File linkstateDestination = new File(parent, "LinkstateDestination.java");
319 assertTrue(linkstateDestination.exists());
321 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ + CompilationTestUtils.FS + "update");
322 CompilationTestUtils.assertFilesCount(parent, 1);
323 parent = new File(parent, "path");
324 CompilationTestUtils.assertFilesCount(parent, 1);
325 parent = new File(parent, "attributes");
326 CompilationTestUtils.assertFilesCount(parent, 1);
327 parent = new File(parent, "mp");
328 CompilationTestUtils.assertFilesCount(parent, 1);
329 parent = new File(parent, "unreach");
330 CompilationTestUtils.assertFilesCount(parent, 1);
331 parent = new File(parent, "nlri");
332 CompilationTestUtils.assertFilesCount(parent, 1);
333 parent = new File(parent, "withdrawn");
334 CompilationTestUtils.assertFilesCount(parent, 1);
335 parent = new File(parent, "routes");
336 CompilationTestUtils.assertFilesCount(parent, 1);
337 parent = new File(parent, "destination");
338 CompilationTestUtils.assertFilesCount(parent, 1);
339 parent = new File(parent, "type");
340 final File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
341 final File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
342 assertTrue(destinationLinkstate.exists());
343 assertTrue(destinationLinkstateBuilder.exists());
344 CompilationTestUtils.assertFilesCount(parent, 3);
345 parent = new File(parent, "destination");
346 CompilationTestUtils.assertFilesCount(parent, 1);
347 parent = new File(parent, "linkstate");
348 final File links = new File(parent, "Links.java");
349 final File linksBuilder = new File(parent, "LinksBuilder.java");
350 assertTrue(links.exists());
351 assertTrue(linksBuilder.exists());
352 CompilationTestUtils.assertFilesCount(parent, 3);
353 parent = new File(parent, "links");
354 final File source = new File(parent, "Source.java");
355 final File sourceBuilder = new File(parent, "SourceBuilder.java");
356 assertTrue(source.exists());
357 assertTrue(sourceBuilder.exists());
358 CompilationTestUtils.assertFilesCount(parent, 3);
359 parent = new File(parent, "source");
360 final File address = new File(parent, "Address.java");
361 final File addressBuilder = new File(parent, "AddressBuilder.java");
362 assertTrue(address.exists());
363 assertTrue(addressBuilder.exists());
364 CompilationTestUtils.assertFilesCount(parent, 2);
366 // Test if sources are compilable
367 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
369 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
373 public void testLeafReturnTypes() throws Exception {
374 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("leaf-return-types");
375 final File compiledOutputDir = CompilationTestUtils.compilerOutput("leaf-return-types");
376 generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
378 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
379 assertTrue(new File(parent, "TestData.java").exists());
380 assertTrue(new File(parent, "Nodes.java").exists());
381 assertTrue(new File(parent, "NodesBuilder.java").exists());
382 assertTrue(new File(parent, "Alg.java").exists());
383 assertTrue(new File(parent, "NodesIdUnionBuilder.java").exists());
384 CompilationTestUtils.assertFilesCount(parent, 6);
386 // Test if sources are compilable
387 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
389 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008";
390 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
391 final Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
392 final Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
394 // Test methods return type
395 final byte[] b = new byte[] {};
396 CompilationTestUtils.assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
397 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
398 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "getIdBoolean");
399 CompilationTestUtils.assertContainsMethod(nodesClass, BigDecimal.class, "getIdDecimal64");
400 CompilationTestUtils.assertContainsMethod(nodesClass, Empty.class, "getIdEmpty");
401 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
402 testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
403 testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
404 CompilationTestUtils.assertContainsMethod(nodesClass, Byte.class, "getId8");
405 CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getId16");
406 CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getId32");
407 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getId64");
408 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
409 CompilationTestUtils.assertContainsMethod(nodesClass, String.class, "getIdString");
410 CompilationTestUtils.assertContainsMethod(nodesClass, Uint8.class, "getIdU8");
411 CompilationTestUtils.assertContainsMethod(nodesClass, Uint16.class, "getIdU16");
412 CompilationTestUtils.assertContainsMethod(nodesClass, Uint32.class, "getIdU32");
413 CompilationTestUtils.assertContainsMethod(nodesClass, Uint64.class, "getIdU64");
414 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
416 final Object builderObj = builderClass.getDeclaredConstructor().newInstance();
418 Method method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary",
420 final List<Range<Integer>> lengthConstraints = new ArrayList<>();
421 lengthConstraints.add(Range.closed(1, 10));
422 byte[] arg = new byte[] {};
423 String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg),
425 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg);
427 method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64",
429 final List<Range<BigDecimal>> rangeConstraints = new ArrayList<>();
430 rangeConstraints.add(Range.closed(new BigDecimal("1.5"), new BigDecimal("5.5")));
431 Object arg1 = new BigDecimal("1.4");
432 expectedMsg = String.format("Invalid range: %s, expected: %s.", arg1, rangeConstraints);
433 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg1);
435 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
439 public void testGenerationContextReferenceExtension() throws IOException, URISyntaxException,
440 ClassNotFoundException {
441 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("context-reference");
442 final File compiledOutputDir = CompilationTestUtils.compilerOutput("context-reference");
443 generateTestSources("/compilation/context-reference", sourcesOutputDir);
445 // Test if all sources are generated
446 final File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
447 CompilationTestUtils.assertFilesCount(fooParent, 4);
448 assertTrue(new File(fooParent, "FooData.java").exists());
449 assertTrue(new File(fooParent, "Nodes.java").exists());
450 assertTrue(new File(fooParent, "NodesBuilder.java").exists());
452 final File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
453 CompilationTestUtils.assertFilesCount(barParent, 2);
454 assertTrue(new File(barParent, "IdentityClass.java").exists());
456 // Test if sources are compilable
457 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
459 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
460 final Class<?> nodesClass = Class.forName(CompilationTestUtils.BASE_PKG
461 + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
462 final Class<?> identityClass = Class
463 .forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
466 final Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true,
468 assertEquals(ImmutableList.of(baseIdentity), Arrays.asList(identityClass.getInterfaces()));
473 getId = nodesClass.getMethod("getId");
474 } catch (final NoSuchMethodException e) {
475 throw new AssertionError("Method getId() not found", e);
478 assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
479 .map(Annotation::annotationType).collect(Collectors.toSet()));
480 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
484 public void compilationTest() throws Exception {
485 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("yang");
486 final File compiledOutputDir = CompilationTestUtils.compilerOutput("yang");
487 generateTestSources("/yang", sourcesOutputDir);
489 // Test if sources are compilable
490 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
492 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
496 public void testBug586() throws Exception {
497 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug586");
498 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug586");
499 generateTestSources("/compilation/bug586", sourcesOutputDir);
501 // Test if sources are compilable
502 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
504 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
508 public void testBug4760() throws Exception {
509 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug4760");
510 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug4760");
511 generateTestSources("/compilation/bug4760", sourcesOutputDir);
513 // Test if sources are compilable
514 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
516 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
520 * Test handling nested uses-augmentations.
523 public void testBug1172() throws Exception {
524 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1172");
525 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1172");
526 generateTestSources("/compilation/bug1172", sourcesOutputDir);
528 // Test if sources are compilable
529 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
531 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
535 public void testBug5461() throws Exception {
536 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5461");
537 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5461");
538 generateTestSources("/compilation/bug5461", sourcesOutputDir);
540 // Test if sources are compilable
541 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
543 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
547 public void testBug5882() throws Exception {
548 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5882");
549 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5882");
550 generateTestSources("/compilation/bug5882", sourcesOutputDir);
552 // Test if sources are compilable
553 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
555 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882);
556 assertTrue(new File(parent, "FooData.java").exists());
557 assertTrue(new File(parent, "TypedefCurrent.java").exists());
558 assertTrue(new File(parent, "TypedefDeprecated.java").exists());
560 try (URLClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() })) {
561 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
562 final Class<?> cls = loader.loadClass(pkg + ".FooData");
563 final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
564 final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
565 final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
566 final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
567 final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
568 final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
569 final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
570 final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
571 assertEquals(1, clsTypedefDepr.getAnnotations().length);
572 assertThat(clsTypedefDepr.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
573 assertEquals(0, clsTypedefCur.getAnnotations().length);
574 assertEquals(1, clsGroupingDepr.getAnnotations().length);
575 assertThat(clsGroupingDepr.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
576 assertEquals(0, clsGroupingCur.getAnnotations().length);
577 assertEquals(0, clsTypeDef1.getAnnotations().length);
578 assertEquals(1, clsTypeDef2.getAnnotations().length);
579 assertThat(clsTypeDef2.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
580 assertEquals(0, clsTypeDef3.getAnnotations().length);
582 /*methods inside container*/
583 assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
584 assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
585 assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
586 assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
587 assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
588 assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
590 /*methods inside module*/
591 assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
592 assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
593 assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
594 assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
595 assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
596 assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
597 assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
600 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
604 * Test if class generated for node from grouping implements ChildOf.
607 public void testBug1377() throws Exception {
608 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1377");
609 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1377");
611 generateTestSources("/compilation/bug1377", sourcesOutputDir);
613 // Test if sources are compilable
614 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
616 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
617 final Class<?> outputActionClass = Class.forName(CompilationTestUtils.BASE_PKG
618 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
619 final Class<?> actionClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.test.foo.rev140717.Action",
622 // Test generated 'container output-action'
623 assertTrue(outputActionClass.isInterface());
624 CompilationTestUtils.assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(),
625 actionClass.getCanonicalName());
627 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
631 public void testMdsal327() throws Exception {
632 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal327");
633 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal327");
634 generateTestSources("/compilation/mdsal327", sourcesOutputDir);
635 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
636 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
640 public void testMdsal365() throws Exception {
641 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal365");
642 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal365");
643 generateTestSources("/compilation/mdsal365", sourcesOutputDir);
644 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
645 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
649 public void testMdsal395() throws Exception {
650 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal395");
651 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal395");
652 generateTestSources("/compilation/mdsal395", sourcesOutputDir);
653 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
654 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
658 public void classNamesColisionTest() throws Exception {
659 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("class-name-collision");
660 final File compiledOutputDir = CompilationTestUtils.compilerOutput("class-name-collision");
661 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
662 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
663 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
667 public void innerEnumerationNameCollisionTest() throws Exception {
668 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal321");
669 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal321");
670 generateTestSources("/compilation/mdsal321", sourcesOutputDir);
671 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
672 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
676 public void twoNestedUnionsTest() throws Exception {
677 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal320");
678 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal320");
679 generateTestSources("/compilation/mdsal320", sourcesOutputDir);
680 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
681 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
685 public void testMdsal425() throws Exception {
686 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal425");
687 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal425");
688 generateTestSources("/compilation/mdsal425", sourcesOutputDir);
689 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
690 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
694 public void testMdsal426() throws Exception {
695 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal426");
696 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal426");
697 generateTestSources("/compilation/mdsal426", sourcesOutputDir);
698 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
699 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
703 public void testMdsal529() throws Exception {
704 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal529");
705 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal529");
706 generateTestSources("/compilation/mdsal529", sourcesOutputDir);
707 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
708 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
712 public void testMdsal589() throws Exception {
713 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal589");
714 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal589");
715 generateTestSources("/compilation/mdsal589", sourcesOutputDir);
716 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
717 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
721 public void testMdsal533() throws Exception {
722 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal533");
723 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal533");
724 generateTestSources("/compilation/mdsal533", sourcesOutputDir);
725 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
726 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
730 public void testMdsal664() throws Exception {
731 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal664");
732 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal664");
733 generateTestSources("/compilation/mdsal664", sourcesOutputDir);
734 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
735 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
739 public void testUnionStringPatterns() throws Exception {
740 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-string-pattern");
741 final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-string-pattern");
742 generateTestSources("/compilation/union-string-pattern", sourcesOutputDir);
743 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
745 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
746 final Class<?> fooClass = Class.forName(CompilationTestUtils.BASE_PKG + ".foo.norev.Foo", true, loader);
748 final Field patterns = fooClass.getDeclaredField(TypeConstants.PATTERN_CONSTANT_NAME);
749 assertEquals(List.class, patterns.getType());
751 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
754 private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
755 final String returnTypeStr) throws NoSuchMethodException {
756 Method method = clazz.getMethod(methodName);
757 assertEquals(java.lang.Class.class, method.getReturnType());
758 java.lang.reflect.Type returnType = method.getGenericReturnType();
759 assertTrue(returnType instanceof ParameterizedType);
760 final ParameterizedType pt = (ParameterizedType) returnType;
761 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
762 assertEquals(1, parameters.length);
763 final java.lang.reflect.Type parameter = parameters[0];
764 assertTrue(parameter instanceof WildcardType);
765 final WildcardType wildcardType = (WildcardType) parameter;
766 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
769 private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz,
770 final String methodName) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
771 final Method method = clazz.getMethod(methodName);
772 final Class<?> rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true,
774 assertEquals(rawReturnType, method.getReturnType());
775 final java.lang.reflect.Type returnType = method.getGenericReturnType();
776 assertTrue(returnType instanceof ParameterizedType);
777 final ParameterizedType pt = (ParameterizedType) returnType;
778 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
779 assertEquals(1, parameters.length);
780 final java.lang.reflect.Type parameter = parameters[0];
781 assertTrue(parameter instanceof WildcardType);
782 final WildcardType wildcardType = (WildcardType) parameter;
783 assertEquals("?", wildcardType.toString());