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 CompilationTestUtils.assertFilesCount(parent, 5);
383 // Test if sources are compilable
384 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
386 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008";
387 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
388 final Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
389 final Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
391 // Test methods return type
392 final byte[] b = new byte[] {};
393 CompilationTestUtils.assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
394 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
395 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "getIdBoolean");
396 CompilationTestUtils.assertContainsMethod(nodesClass, Decimal64.class, "getIdDecimal64");
397 CompilationTestUtils.assertContainsMethod(nodesClass, Empty.class, "getIdEmpty");
398 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
399 testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
400 testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
401 CompilationTestUtils.assertContainsMethod(nodesClass, Byte.class, "getId8");
402 CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getId16");
403 CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getId32");
404 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getId64");
405 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
406 CompilationTestUtils.assertContainsMethod(nodesClass, String.class, "getIdString");
407 CompilationTestUtils.assertContainsMethod(nodesClass, Uint8.class, "getIdU8");
408 CompilationTestUtils.assertContainsMethod(nodesClass, Uint16.class, "getIdU16");
409 CompilationTestUtils.assertContainsMethod(nodesClass, Uint32.class, "getIdU32");
410 CompilationTestUtils.assertContainsMethod(nodesClass, Uint64.class, "getIdU64");
411 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
413 final Object builderObj = builderClass.getDeclaredConstructor().newInstance();
415 Method method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary",
417 final List<Range<Integer>> lengthConstraints = new ArrayList<>();
418 lengthConstraints.add(Range.closed(1, 10));
419 byte[] arg = new byte[] {};
420 String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg),
422 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg);
424 method = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64",
426 final List<Range<Decimal64>> rangeConstraints = new ArrayList<>();
427 rangeConstraints.add(Range.closed(Decimal64.valueOf("1.5"), Decimal64.valueOf("5.5")));
428 Object arg1 = Decimal64.valueOf("1.4");
429 expectedMsg = String.format("Invalid range: %s, expected: %s.", arg1, rangeConstraints);
430 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, method, expectedMsg, arg1);
432 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
436 public void testGenerationContextReferenceExtension() throws IOException, URISyntaxException,
437 ClassNotFoundException {
438 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("context-reference");
439 final File compiledOutputDir = CompilationTestUtils.compilerOutput("context-reference");
440 generateTestSources("/compilation/context-reference", sourcesOutputDir);
442 // Test if all sources are generated
443 final File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
444 CompilationTestUtils.assertFilesCount(fooParent, 4);
445 assertTrue(new File(fooParent, "FooData.java").exists());
446 assertTrue(new File(fooParent, "Nodes.java").exists());
447 assertTrue(new File(fooParent, "NodesBuilder.java").exists());
449 final File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
450 CompilationTestUtils.assertFilesCount(barParent, 3);
451 assertTrue(new File(barParent, "BarData.java").exists());
452 assertTrue(new File(barParent, "IdentityClass.java").exists());
454 // Test if sources are compilable
455 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
457 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
458 final Class<?> nodesClass = Class.forName(CompilationTestUtils.BASE_PKG
459 + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
460 final Class<?> identityClass = Class
461 .forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
464 final Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true,
466 assertEquals(ImmutableList.of(baseIdentity), Arrays.asList(identityClass.getInterfaces()));
471 getId = nodesClass.getMethod("getId");
472 } catch (final NoSuchMethodException e) {
473 throw new AssertionError("Method getId() not found", e);
476 assertEquals(ImmutableSet.of(RoutingContext.class), Arrays.stream(getId.getAnnotations())
477 .map(Annotation::annotationType).collect(Collectors.toSet()));
478 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
482 public void compilationTest() throws Exception {
483 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("yang");
484 final File compiledOutputDir = CompilationTestUtils.compilerOutput("yang");
485 generateTestSources("/yang", sourcesOutputDir);
487 // Test if sources are compilable
488 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
490 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
494 public void testBug586() throws Exception {
495 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug586");
496 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug586");
497 generateTestSources("/compilation/bug586", sourcesOutputDir);
499 // Test if sources are compilable
500 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
502 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
506 public void testBug4760() throws Exception {
507 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug4760");
508 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug4760");
509 generateTestSources("/compilation/bug4760", sourcesOutputDir);
511 // Test if sources are compilable
512 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
514 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
518 * Test handling nested uses-augmentations.
521 public void testBug1172() throws Exception {
522 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1172");
523 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1172");
524 generateTestSources("/compilation/bug1172", sourcesOutputDir);
526 // Test if sources are compilable
527 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
529 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
533 public void testBug5461() throws Exception {
534 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5461");
535 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5461");
536 generateTestSources("/compilation/bug5461", sourcesOutputDir);
538 // Test if sources are compilable
539 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
541 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
545 public void testBug5882() throws Exception {
546 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug5882");
547 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug5882");
548 generateTestSources("/compilation/bug5882", sourcesOutputDir);
550 // Test if sources are compilable
551 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
553 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882);
554 assertTrue(new File(parent, "FooData.java").exists());
555 assertTrue(new File(parent, "TypedefCurrent.java").exists());
556 assertTrue(new File(parent, "TypedefDeprecated.java").exists());
558 try (URLClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() })) {
559 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
560 final Class<?> cls = loader.loadClass(pkg + ".FooData");
561 final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
562 final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
563 final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
564 final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
565 final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
566 final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
567 final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
568 final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
569 assertEquals(1, clsTypedefDepr.getAnnotations().length);
570 assertThat(clsTypedefDepr.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
571 assertEquals(0, clsTypedefCur.getAnnotations().length);
572 assertEquals(1, clsGroupingDepr.getAnnotations().length);
573 assertThat(clsGroupingDepr.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
574 assertEquals(0, clsGroupingCur.getAnnotations().length);
575 assertEquals(0, clsTypeDef1.getAnnotations().length);
576 assertEquals(1, clsTypeDef2.getAnnotations().length);
577 assertThat(clsTypeDef2.getAnnotations()[0].toString(), startsWith("@java.lang.Deprecated"));
578 assertEquals(0, clsTypeDef3.getAnnotations().length);
580 /*methods inside container*/
581 assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
582 assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
583 assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
584 assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
585 assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
586 assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
588 /*methods inside module*/
589 assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
590 assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
591 assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
592 assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
593 assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
594 assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
595 assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
598 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
602 * Test if class generated for node from grouping implements ChildOf.
605 public void testBug1377() throws Exception {
606 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("bug1377");
607 final File compiledOutputDir = CompilationTestUtils.compilerOutput("bug1377");
609 generateTestSources("/compilation/bug1377", sourcesOutputDir);
611 // Test if sources are compilable
612 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
614 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
615 final Class<?> outputActionClass = Class.forName(CompilationTestUtils.BASE_PKG
616 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
617 final Class<?> actionClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.test.foo.rev140717.Action",
620 // Test generated 'container output-action'
621 assertTrue(outputActionClass.isInterface());
622 CompilationTestUtils.assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(),
623 actionClass.getCanonicalName());
625 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
629 public void testMdsal327() throws Exception {
630 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal327");
631 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal327");
632 generateTestSources("/compilation/mdsal327", sourcesOutputDir);
633 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
634 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
638 public void testMdsal365() throws Exception {
639 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal365");
640 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal365");
641 generateTestSources("/compilation/mdsal365", sourcesOutputDir);
642 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
643 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
647 public void testMdsal395() throws Exception {
648 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal395");
649 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal395");
650 generateTestSources("/compilation/mdsal395", sourcesOutputDir);
651 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
652 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
656 public void classNamesColisionTest() throws Exception {
657 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("class-name-collision");
658 final File compiledOutputDir = CompilationTestUtils.compilerOutput("class-name-collision");
659 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
660 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
661 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
665 public void innerEnumerationNameCollisionTest() throws Exception {
666 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal321");
667 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal321");
668 generateTestSources("/compilation/mdsal321", sourcesOutputDir);
669 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
670 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
674 public void twoNestedUnionsTest() throws Exception {
675 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal320");
676 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal320");
677 generateTestSources("/compilation/mdsal320", sourcesOutputDir);
678 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
679 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
683 public void testMdsal425() throws Exception {
684 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal425");
685 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal425");
686 generateTestSources("/compilation/mdsal425", sourcesOutputDir);
687 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
688 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
692 public void testMdsal426() throws Exception {
693 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal426");
694 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal426");
695 generateTestSources("/compilation/mdsal426", sourcesOutputDir);
696 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
697 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
701 public void testMdsal529() throws Exception {
702 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal529");
703 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal529");
704 generateTestSources("/compilation/mdsal529", sourcesOutputDir);
705 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
706 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
710 public void testMdsal589() throws Exception {
711 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal589");
712 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal589");
713 generateTestSources("/compilation/mdsal589", sourcesOutputDir);
714 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
715 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
719 public void testMdsal533() throws Exception {
720 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal533");
721 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal533");
722 generateTestSources("/compilation/mdsal533", sourcesOutputDir);
723 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
724 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
728 public void testMdsal664() throws Exception {
729 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal664");
730 final File compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal664");
731 generateTestSources("/compilation/mdsal664", sourcesOutputDir);
732 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
733 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
737 public void testUnionStringPatterns() throws Exception {
738 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("union-string-pattern");
739 final File compiledOutputDir = CompilationTestUtils.compilerOutput("union-string-pattern");
740 generateTestSources("/compilation/union-string-pattern", sourcesOutputDir);
741 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
743 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
744 final Class<?> fooClass = Class.forName(CompilationTestUtils.BASE_PKG + ".foo.norev.Foo", true, loader);
746 final Field patterns = fooClass.getDeclaredField(TypeConstants.PATTERN_CONSTANT_NAME);
747 assertEquals(List.class, patterns.getType());
749 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
752 private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName,
753 final String returnTypeStr) throws NoSuchMethodException {
754 Method method = clazz.getMethod(methodName);
755 assertEquals(java.lang.Class.class, method.getReturnType());
756 Type returnType = method.getGenericReturnType();
757 assertTrue(returnType instanceof ParameterizedType);
758 final ParameterizedType pt = (ParameterizedType) returnType;
759 final Type[] parameters = pt.getActualTypeArguments();
760 assertEquals(1, parameters.length);
761 final Type parameter = parameters[0];
762 assertTrue(parameter instanceof WildcardType);
763 final WildcardType wildcardType = (WildcardType) parameter;
764 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
767 private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz,
768 final String methodName) throws ClassNotFoundException, NoSuchMethodException, SecurityException {
769 final Method method = clazz.getMethod(methodName);
770 final Class<?> rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true,
772 assertEquals(rawReturnType, method.getReturnType());
773 final Type returnType = method.getGenericReturnType();
774 assertTrue(returnType instanceof ParameterizedType);
775 final ParameterizedType pt = (ParameterizedType) returnType;
776 final Type[] parameters = pt.getActualTypeArguments();
777 assertEquals(1, parameters.length);
778 final Type parameter = parameters[0];
779 assertTrue(parameter instanceof WildcardType);
780 final WildcardType wildcardType = (WildcardType) parameter;
781 assertEquals("?", wildcardType.toString());