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.Type;
28 import java.lang.reflect.WildcardType;
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.ri.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.Decimal64;
42 import org.opendaylight.yangtools.yang.common.Empty;
43 import org.opendaylight.yangtools.yang.common.Uint16;
44 import org.opendaylight.yangtools.yang.common.Uint32;
45 import org.opendaylight.yangtools.yang.common.Uint64;
46 import org.opendaylight.yangtools.yang.common.Uint8;
49 * Test correct code generation.
52 public class CompilationTest extends BaseCompilationTest {
55 * Java 8 allows JaCoCo to hook onto interfaces, as well as
56 * generating a default implementation. We only want to check
59 private static Collection<Method> abstractMethods(final Class<?> clazz) {
61 return Collections2.filter(Arrays.asList(clazz.getDeclaredMethods()),
62 input -> Modifier.isAbstract(input.getModifiers()));
66 public void testListGeneration() throws Exception {
67 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("list-gen");
68 final File compiledOutputDir = CompilationTestUtils.compilerOutput("list-gen");
69 generateTestSources("/compilation/list-gen", sourcesOutputDir);
71 // Test if all sources are generated
72 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
73 final File keyArgs = new File(parent, "KeyArgs.java");
74 final File links = new File(parent, "Links.java");
75 final File linksBuilder = new File(parent, "LinksBuilder.java");
76 final File linksKey = new File(parent, "LinksKey.java");
77 final File testData = new File(parent, "TestData.java");
78 assertTrue(keyArgs.exists());
79 assertTrue(links.exists());
80 assertTrue(linksBuilder.exists());
81 assertTrue(linksKey.exists());
82 assertTrue(testData.exists());
83 CompilationTestUtils.assertFilesCount(parent, 7);
85 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST + CompilationTestUtils.FS + "links");
86 final File level = new File(parent, "Level.java");
87 final File linkGroup = new File(parent, "LinkGroup.java");
88 final File node = new File(parent, "Node.java");
89 final File nodeBuilder = new File(parent, "NodeBuilder.java");
90 final File nodeList = new File(parent, "NodeList.java");
91 final File nodeListBuilder = new File(parent, "NodeListBuilder.java");
92 final File nodesType = new File(parent, "NodesType.java");
93 assertTrue(level.exists());
94 assertTrue(linkGroup.exists());
95 assertTrue(node.exists());
96 assertTrue(nodeBuilder.exists());
97 assertTrue(nodeList.exists());
98 assertTrue(nodeListBuilder.exists());
99 assertTrue(nodesType.exists());
100 CompilationTestUtils.assertFilesCount(parent, 8);
102 // Test if sources are compilable
103 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
105 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
106 final Class<?> keyArgsClass = Class.forName(CompilationTestUtils.BASE_PKG
107 + ".urn.opendaylight.test.rev131008.KeyArgs", true, loader);
108 final Class<?> linksClass = Class.forName(CompilationTestUtils.BASE_PKG
109 + ".urn.opendaylight.test.rev131008.Links", true, loader);
110 final Class<?> linksKeyClass = Class.forName(CompilationTestUtils.BASE_PKG
111 + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
113 // Test generated 'grouping key-args'
114 assertTrue(keyArgsClass.isInterface());
115 CompilationTestUtils.assertContainsMethod(keyArgsClass, String.class, "getName");
116 CompilationTestUtils.assertContainsMethod(keyArgsClass, Integer.class, "getSize");
117 assertEquals(3, abstractMethods(keyArgsClass).size());
119 // Test generated 'list links'
120 assertTrue(linksClass.isInterface());
121 CompilationTestUtils.assertImplementsIfc(linksClass, keyArgsClass);
122 assertEquals(7, abstractMethods(linksClass).size());
123 CompilationTestUtils.assertContainsMethod(linksClass,
124 "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.links.Text", "getText", loader);
125 CompilationTestUtils.assertContainsMethod(linksClass,
126 "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.links.Text", "requireText", loader);
128 // Test list key constructor arguments ordering
129 CompilationTestUtils.assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
130 // Test serialVersionUID generation
131 final Field suid = CompilationTestUtils.assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
132 suid.setAccessible(true);
133 assertEquals(-8829501012356283881L, suid.getLong(null));
135 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
139 public void testAugmentUnderUsesGeneration() throws Exception {
140 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("augment-under-uses");
141 final File compiledOutputDir = CompilationTestUtils.compilerOutput("augment-under-uses");
142 generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
144 // Test if all sources were generated from 'module foo'
145 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
146 assertTrue(new File(parent, "Object.java").exists());
147 assertTrue(new File(parent, "ClosedObject.java").exists());
148 assertTrue(new File(parent, "OpenObject.java").exists());
149 assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
150 assertTrue(new File(parent, "PathKeySubobject.java").exists());
151 assertTrue(new File(parent, "FooData.java").exists());
152 CompilationTestUtils.assertFilesCount(parent, 11);
154 parent = new File(parent, "object");
155 assertTrue(new File(parent, "Nodes.java").exists());
156 assertTrue(new File(parent, "NodesBuilder.java").exists());
157 CompilationTestUtils.assertFilesCount(parent, 2);
159 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "closed");
160 CompilationTestUtils.assertFilesCount(parent, 1);
162 parent = new File(parent, "object");
163 assertTrue(new File(parent, "Link1.java").exists());
164 assertTrue(new File(parent, "Link1Builder.java").exists());
165 CompilationTestUtils.assertFilesCount(parent, 2);
167 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "open");
168 CompilationTestUtils.assertFilesCount(parent, 1);
170 parent = new File(parent, "object");
171 assertTrue(new File(parent, "Nodes1.java").exists());
172 assertTrue(new File(parent, "Nodes1Builder.java").exists());
173 CompilationTestUtils.assertFilesCount(parent, 3);
175 parent = new File(parent, "nodes");
176 assertTrue(new File(parent, "Links.java").exists());
177 assertTrue(new File(parent, "LinksBuilder.java").exists());
178 CompilationTestUtils.assertFilesCount(parent, 2);
180 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "explicit");
181 CompilationTestUtils.assertFilesCount(parent, 1);
182 parent = new File(parent, "route");
183 CompilationTestUtils.assertFilesCount(parent, 1);
184 parent = new File(parent, "object");
185 assertTrue(new File(parent, "Subobjects.java").exists());
186 assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
187 CompilationTestUtils.assertFilesCount(parent, 3);
189 parent = new File(parent, "subobjects");
190 CompilationTestUtils.assertFilesCount(parent, 1);
191 parent = new File(parent, "subobject");
192 CompilationTestUtils.assertFilesCount(parent, 1);
193 parent = new File(parent, "type");
194 assertTrue(new File(parent, "PathKey.java").exists());
195 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
196 CompilationTestUtils.assertFilesCount(parent, 3);
198 parent = new File(parent, "path");
199 CompilationTestUtils.assertFilesCount(parent, 1);
200 parent = new File(parent, "key");
201 assertTrue(new File(parent, "PathKey.java").exists());
202 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
203 CompilationTestUtils.assertFilesCount(parent, 2);
205 // Test if all sources were generated from 'module bar'
206 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
207 assertTrue(new File(parent, "BarData.java").exists());
208 assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
209 assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
210 assertTrue(new File(parent, "RouteSubobjects.java").exists());
211 CompilationTestUtils.assertFilesCount(parent, 7);
213 parent = new File(parent, "route");
214 CompilationTestUtils.assertFilesCount(parent, 1);
215 parent = new File(new File(sourcesOutputDir, CompilationTestUtils.NS_BAR), "basic");
216 CompilationTestUtils.assertFilesCount(parent, 1);
217 parent = new File(parent, "explicit");
218 CompilationTestUtils.assertFilesCount(parent, 1);
219 parent = new File(parent, "route");
220 CompilationTestUtils.assertFilesCount(parent, 1);
222 parent = new File(parent, "subobjects");
223 CompilationTestUtils.assertFilesCount(parent, 2);
224 assertTrue(new File(parent, "SubobjectType.java").exists());
226 parent = new File(parent, "subobject");
227 CompilationTestUtils.assertFilesCount(parent, 1);
229 parent = new File(parent, "type");
230 assertTrue(new File(parent, "IpPrefix.java").exists());
231 assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
232 assertTrue(new File(parent, "Label.java").exists());
233 assertTrue(new File(parent, "LabelBuilder.java").exists());
234 CompilationTestUtils.assertFilesCount(parent, 4);
236 // Test if sources are compilable
237 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
239 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
243 public void testAugmentOfAugmentGeneration() throws Exception {
244 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("aug-of-aug");
245 final File compiledOutputDir = CompilationTestUtils.compilerOutput("aug-of-aug");
246 generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
248 // Test if all sources were generated from 'module foo'
249 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
250 assertTrue(new File(parent, "FooData.java").exists());
251 assertTrue(new File(parent, "FooListener.java").exists());
252 assertTrue(new File(parent, "PathAttributes.java").exists());
253 assertTrue(new File(parent, "Update.java").exists());
254 assertTrue(new File(parent, "UpdateBuilder.java").exists());
255 CompilationTestUtils.assertFilesCount(parent, 8);
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 assertTrue(new File(parent, "PathAttributes.java").exists());
269 assertTrue(new File(parent, "PathAttributesBuilder.java").exists());
271 // Test if all sources were generated from 'module bar'
272 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
273 assertTrue(new File(parent, "BarData.java").exists());
274 assertTrue(new File(parent, "Destination.java").exists());
275 assertTrue(new File(parent, "PathAttributes1.java").exists());
276 assertTrue(new File(parent, "PathAttributes1Builder.java").exists());
277 CompilationTestUtils.assertFilesCount(parent, 7);
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 assertTrue(new File(parent, "WithdrawnRoutes.java").exists());
310 assertTrue(new File(parent, "WithdrawnRoutesBuilder.java").exists());
311 CompilationTestUtils.assertFilesCount(parent, 2);
313 // Test if all sources were generated from 'module baz'
314 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ);
315 assertTrue(new File(parent, "BazData.java").exists());
316 assertTrue(new File(parent, "LinkstateDestination.java").exists());
317 CompilationTestUtils.assertFilesCount(parent, 4);
319 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ + CompilationTestUtils.FS + "update");
320 CompilationTestUtils.assertFilesCount(parent, 1);
321 parent = new File(parent, "path");
322 CompilationTestUtils.assertFilesCount(parent, 1);
323 parent = new File(parent, "attributes");
324 CompilationTestUtils.assertFilesCount(parent, 1);
325 parent = new File(parent, "mp");
326 CompilationTestUtils.assertFilesCount(parent, 1);
327 parent = new File(parent, "unreach");
328 CompilationTestUtils.assertFilesCount(parent, 1);
329 parent = new File(parent, "nlri");
330 CompilationTestUtils.assertFilesCount(parent, 1);
331 parent = new File(parent, "withdrawn");
332 CompilationTestUtils.assertFilesCount(parent, 1);
333 parent = new File(parent, "routes");
334 CompilationTestUtils.assertFilesCount(parent, 1);
335 parent = new File(parent, "destination");
336 CompilationTestUtils.assertFilesCount(parent, 1);
337 parent = new File(parent, "type");
338 final File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
339 final File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
340 assertTrue(destinationLinkstate.exists());
341 assertTrue(destinationLinkstateBuilder.exists());
342 CompilationTestUtils.assertFilesCount(parent, 3);
343 parent = new File(parent, "destination");
344 CompilationTestUtils.assertFilesCount(parent, 1);
345 parent = new File(parent, "linkstate");
346 final File links = new File(parent, "Links.java");
347 final File linksBuilder = new File(parent, "LinksBuilder.java");
348 assertTrue(links.exists());
349 assertTrue(linksBuilder.exists());
350 CompilationTestUtils.assertFilesCount(parent, 3);
351 parent = new File(parent, "links");
352 final File source = new File(parent, "Source.java");
353 final File sourceBuilder = new File(parent, "SourceBuilder.java");
354 assertTrue(source.exists());
355 assertTrue(sourceBuilder.exists());
356 CompilationTestUtils.assertFilesCount(parent, 3);
357 parent = new File(parent, "source");
358 final File address = new File(parent, "Address.java");
359 final File addressBuilder = new File(parent, "AddressBuilder.java");
360 assertTrue(address.exists());
361 assertTrue(addressBuilder.exists());
362 CompilationTestUtils.assertFilesCount(parent, 2);
364 // Test if sources are compilable
365 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
367 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
371 public void testLeafReturnTypes() throws Exception {
372 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("leaf-return-types");
373 final File compiledOutputDir = CompilationTestUtils.compilerOutput("leaf-return-types");
374 generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
376 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
377 assertTrue(new File(parent, "TestData.java").exists());
378 assertTrue(new File(parent, "Nodes.java").exists());
379 assertTrue(new File(parent, "NodesBuilder.java").exists());
380 assertTrue(new File(parent, "Alg.java").exists());
381 assertTrue(new File(parent, "NodesIdUnionBuilder.java").exists());
382 CompilationTestUtils.assertFilesCount(parent, 6);
384 // Test if sources are compilable
385 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
387 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008";
388 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
389 final Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
390 final Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
392 // Test methods return type
393 final byte[] b = new byte[] {};
394 CompilationTestUtils.assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
395 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
396 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "getIdBoolean");
397 CompilationTestUtils.assertContainsMethod(nodesClass, Decimal64.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<Decimal64>> rangeConstraints = new ArrayList<>();
428 rangeConstraints.add(Range.closed(Decimal64.valueOf("1.5"), Decimal64.valueOf("5.5")));
429 Object arg1 = Decimal64.valueOf("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, 3);
452 assertTrue(new File(barParent, "BarData.java").exists());
453 assertTrue(new File(barParent, "IdentityClass.java").exists());
455 // Test if sources are compilable
456 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
458 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
459 final Class<?> nodesClass = Class.forName(CompilationTestUtils.BASE_PKG
460 + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
461 final Class<?> identityClass = Class
462 .forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
465 final Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true,
467 assertEquals(ImmutableList.of(baseIdentity), Arrays.asList(identityClass.getInterfaces()));
472 getId = nodesClass.getMethod("getId");
473 } catch (final NoSuchMethodException e) {
474 throw new AssertionError("Method getId() not found", e);
477 assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
478 .map(Annotation::annotationType).collect(Collectors.toSet()));
479 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
483 public void compilationTest() throws Exception {
484 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("yang");
485 final File compiledOutputDir = CompilationTestUtils.compilerOutput("yang");
486 generateTestSources("/yang", sourcesOutputDir);
488 // Test if sources are compilable
489 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
491 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
495 public void testBug586() throws Exception {
496 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug586");
497 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug586");
498 generateTestSources("/compilation/bug586", sourcesOutputDir);
500 // Test if sources are compilable
501 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
503 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
507 public void testBug4760() throws Exception {
508 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug4760");
509 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug4760");
510 generateTestSources("/compilation/bug4760", sourcesOutputDir);
512 // Test if sources are compilable
513 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
515 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
519 * Test handling nested uses-augmentations.
522 public void testBug1172() throws Exception {
523 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1172");
524 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1172");
525 generateTestSources("/compilation/bug1172", sourcesOutputDir);
527 // Test if sources are compilable
528 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
530 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
534 public void testBug5461() throws Exception {
535 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5461");
536 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5461");
537 generateTestSources("/compilation/bug5461", sourcesOutputDir);
539 // Test if sources are compilable
540 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
542 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
546 public void testBug5882() throws Exception {
547 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5882");
548 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5882");
549 generateTestSources("/compilation/bug5882", sourcesOutputDir);
551 // Test if sources are compilable
552 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
554 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882);
555 assertTrue(new File(parent, "FooData.java").exists());
556 assertTrue(new File(parent, "TypedefCurrent.java").exists());
557 assertTrue(new File(parent, "TypedefDeprecated.java").exists());
559 try (URLClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() })) {
560 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
561 final Class<?> cls = loader.loadClass(pkg + ".FooData");
562 final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
563 final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
564 final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
565 final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
566 final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
567 final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
568 final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
569 final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
570 assertEquals(1, clsTypedefDepr.getAnnotations().length);
571 assertThat(clsTypedefDepr.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
572 assertEquals(0, clsTypedefCur.getAnnotations().length);
573 assertEquals(1, clsGroupingDepr.getAnnotations().length);
574 assertThat(clsGroupingDepr.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
575 assertEquals(0, clsGroupingCur.getAnnotations().length);
576 assertEquals(0, clsTypeDef1.getAnnotations().length);
577 assertEquals(1, clsTypeDef2.getAnnotations().length);
578 assertThat(clsTypeDef2.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
579 assertEquals(0, clsTypeDef3.getAnnotations().length);
581 /*methods inside container*/
582 assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
583 assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
584 assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
585 assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
586 assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
587 assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
589 /*methods inside module*/
590 assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
591 assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
592 assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
593 assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
594 assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
595 assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
596 assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
599 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
603 * Test if class generated for node from grouping implements ChildOf.
606 public void testBug1377() throws Exception {
607 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1377");
608 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1377");
610 generateTestSources("/compilation/bug1377", sourcesOutputDir);
612 // Test if sources are compilable
613 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
615 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
616 final Class<?> outputActionClass = Class.forName(CompilationTestUtils.BASE_PKG
617 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
618 final Class<?> actionClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.test.foo.rev140717.Action",
621 // Test generated 'container output-action'
622 assertTrue(outputActionClass.isInterface());
623 CompilationTestUtils.assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(),
624 actionClass.getCanonicalName());
626 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
630 public void testMdsal327() throws Exception {
631 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal327");
632 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal327");
633 generateTestSources("/compilation/mdsal327", sourcesOutputDir);
634 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
635 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
639 public void testMdsal365() throws Exception {
640 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal365");
641 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal365");
642 generateTestSources("/compilation/mdsal365", sourcesOutputDir);
643 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
644 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
648 public void testMdsal395() throws Exception {
649 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal395");
650 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal395");
651 generateTestSources("/compilation/mdsal395", sourcesOutputDir);
652 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
653 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
657 public void classNamesColisionTest() throws Exception {
658 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("class-name-collision");
659 final File compiledOutputDir = CompilationTestUtils.compilerOutput("class-name-collision");
660 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
661 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
662 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
666 public void innerEnumerationNameCollisionTest() throws Exception {
667 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal321");
668 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal321");
669 generateTestSources("/compilation/mdsal321", sourcesOutputDir);
670 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
671 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
675 public void twoNestedUnionsTest() throws Exception {
676 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal320");
677 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal320");
678 generateTestSources("/compilation/mdsal320", sourcesOutputDir);
679 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
680 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
684 public void testMdsal425() throws Exception {
685 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal425");
686 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal425");
687 generateTestSources("/compilation/mdsal425", sourcesOutputDir);
688 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
689 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
693 public void testMdsal426() throws Exception {
694 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal426");
695 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal426");
696 generateTestSources("/compilation/mdsal426", sourcesOutputDir);
697 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
698 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
702 public void testMdsal529() throws Exception {
703 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal529");
704 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal529");
705 generateTestSources("/compilation/mdsal529", sourcesOutputDir);
706 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
707 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
711 public void testMdsal589() throws Exception {
712 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal589");
713 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal589");
714 generateTestSources("/compilation/mdsal589", sourcesOutputDir);
715 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
716 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
720 public void testMdsal533() throws Exception {
721 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal533");
722 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal533");
723 generateTestSources("/compilation/mdsal533", sourcesOutputDir);
724 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
725 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
729 public void testMdsal664() throws Exception {
730 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal664");
731 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal664");
732 generateTestSources("/compilation/mdsal664", sourcesOutputDir);
733 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
734 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
738 public void testUnionStringPatterns() throws Exception {
739 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-string-pattern");
740 final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-string-pattern");
741 generateTestSources("/compilation/union-string-pattern", sourcesOutputDir);
742 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
744 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
745 final Class<?> fooClass = Class.forName(CompilationTestUtils.BASE_PKG + ".foo.norev.Foo", true, loader);
747 final Field patterns = fooClass.getDeclaredField(TypeConstants.PATTERN_CONSTANT_NAME);
748 assertEquals(List.class, patterns.getType());
750 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
753 private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
754 final String returnTypeStr) throws NoSuchMethodException {
755 Method method = clazz.getMethod(methodName);
756 assertEquals(java.lang.Class.class, method.getReturnType());
757 Type returnType = method.getGenericReturnType();
758 assertTrue(returnType instanceof ParameterizedType);
759 final ParameterizedType pt = (ParameterizedType) returnType;
760 final Type[] parameters = pt.getActualTypeArguments();
761 assertEquals(1, parameters.length);
762 final Type parameter = parameters[0];
763 assertTrue(parameter instanceof WildcardType);
764 final WildcardType wildcardType = (WildcardType) parameter;
765 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
768 private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz,
769 final String methodName) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
770 final Method method = clazz.getMethod(methodName);
771 final Class<?> rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true,
773 assertEquals(rawReturnType, method.getReturnType());
774 final Type returnType = method.getGenericReturnType();
775 assertTrue(returnType instanceof ParameterizedType);
776 final ParameterizedType pt = (ParameterizedType) returnType;
777 final Type[] parameters = pt.getActualTypeArguments();
778 assertEquals(1, parameters.length);
779 final Type parameter = parameters[0];
780 assertTrue(parameter instanceof WildcardType);
781 final WildcardType wildcardType = (WildcardType) parameter;
782 assertEquals("?", wildcardType.toString());