2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.java.api.generator;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
16 import com.google.common.collect.Collections2;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.ImmutableSet;
19 import com.google.common.collect.Range;
21 import java.io.IOException;
22 import java.lang.annotation.Annotation;
23 import java.lang.reflect.Field;
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Modifier;
26 import java.lang.reflect.ParameterizedType;
27 import java.lang.reflect.WildcardType;
28 import java.net.URISyntaxException;
30 import java.net.URLClassLoader;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.stream.Collectors;
36 import org.junit.Test;
37 import org.opendaylight.mdsal.binding.model.ri.TypeConstants;
38 import org.opendaylight.yangtools.yang.binding.ChildOf;
39 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
40 import org.opendaylight.yangtools.yang.common.Decimal64;
41 import org.opendaylight.yangtools.yang.common.Empty;
42 import org.opendaylight.yangtools.yang.common.Uint16;
43 import org.opendaylight.yangtools.yang.common.Uint32;
44 import org.opendaylight.yangtools.yang.common.Uint64;
45 import org.opendaylight.yangtools.yang.common.Uint8;
48 * Test correct code generation.
51 public class CompilationTest extends BaseCompilationTest {
54 * Java 8 allows JaCoCo to hook onto interfaces, as well as
55 * generating a default implementation. We only want to check
58 private static Collection<Method> abstractMethods(final Class<?> clazz) {
60 return Collections2.filter(Arrays.asList(clazz.getDeclaredMethods()),
61 input -> Modifier.isAbstract(input.getModifiers()));
65 public void testListGeneration() throws Exception {
66 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("list-gen");
67 final File compiledOutputDir = CompilationTestUtils.compilerOutput("list-gen");
68 generateTestSources("/compilation/list-gen", sourcesOutputDir);
70 // Test if all sources are generated
71 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
72 final File keyArgs = new File(parent, "KeyArgs.java");
73 final File links = new File(parent, "Links.java");
74 final File linksBuilder = new File(parent, "LinksBuilder.java");
75 final File linksKey = new File(parent, "LinksKey.java");
76 final File testData = new File(parent, "TestData.java");
77 assertTrue(keyArgs.exists());
78 assertTrue(links.exists());
79 assertTrue(linksBuilder.exists());
80 assertTrue(linksKey.exists());
81 assertTrue(testData.exists());
82 CompilationTestUtils.assertFilesCount(parent, 7);
84 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST + CompilationTestUtils.FS + "links");
85 final File level = new File(parent, "Level.java");
86 final File linkGroup = new File(parent, "LinkGroup.java");
87 final File node = new File(parent, "Node.java");
88 final File nodeBuilder = new File(parent, "NodeBuilder.java");
89 final File nodeList = new File(parent, "NodeList.java");
90 final File nodeListBuilder = new File(parent, "NodeListBuilder.java");
91 final File nodesType = new File(parent, "NodesType.java");
92 assertTrue(level.exists());
93 assertTrue(linkGroup.exists());
94 assertTrue(node.exists());
95 assertTrue(nodeBuilder.exists());
96 assertTrue(nodeList.exists());
97 assertTrue(nodeListBuilder.exists());
98 assertTrue(nodesType.exists());
99 CompilationTestUtils.assertFilesCount(parent, 8);
101 // Test if sources are compilable
102 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
104 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
105 final Class<?> keyArgsClass = Class.forName(CompilationTestUtils.BASE_PKG
106 + ".urn.opendaylight.test.rev131008.KeyArgs", true, loader);
107 final Class<?> linksClass = Class.forName(CompilationTestUtils.BASE_PKG
108 + ".urn.opendaylight.test.rev131008.Links", true, loader);
109 final Class<?> linksKeyClass = Class.forName(CompilationTestUtils.BASE_PKG
110 + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
112 // Test generated 'grouping key-args'
113 assertTrue(keyArgsClass.isInterface());
114 CompilationTestUtils.assertContainsMethod(keyArgsClass, String.class, "getName");
115 CompilationTestUtils.assertContainsMethod(keyArgsClass, Integer.class, "getSize");
116 assertEquals(3, abstractMethods(keyArgsClass).size());
118 // Test generated 'list links'
119 assertTrue(linksClass.isInterface());
120 CompilationTestUtils.assertImplementsIfc(linksClass, keyArgsClass);
121 assertEquals(7, abstractMethods(linksClass).size());
122 CompilationTestUtils.assertContainsMethod(linksClass,
123 "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.links.Text", "getText", loader);
124 CompilationTestUtils.assertContainsMethod(linksClass,
125 "org.opendaylight.yang.gen.v1.urn.opendaylight.test.rev131008.links.Text", "requireText", loader);
127 // Test list key constructor arguments ordering
128 CompilationTestUtils.assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
129 // Test serialVersionUID generation
130 final Field suid = CompilationTestUtils.assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
131 suid.setAccessible(true);
132 assertEquals(-8829501012356283881L, suid.getLong(null));
134 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
138 public void testAugmentUnderUsesGeneration() throws Exception {
139 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("augment-under-uses");
140 final File compiledOutputDir = CompilationTestUtils.compilerOutput("augment-under-uses");
141 generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
143 // Test if all sources were generated from 'module foo'
144 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
145 assertTrue(new File(parent, "Object.java").exists());
146 assertTrue(new File(parent, "ClosedObject.java").exists());
147 assertTrue(new File(parent, "OpenObject.java").exists());
148 assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
149 assertTrue(new File(parent, "PathKeySubobject.java").exists());
150 assertTrue(new File(parent, "FooData.java").exists());
151 CompilationTestUtils.assertFilesCount(parent, 11);
153 parent = new File(parent, "object");
154 assertTrue(new File(parent, "Nodes.java").exists());
155 assertTrue(new File(parent, "NodesBuilder.java").exists());
156 CompilationTestUtils.assertFilesCount(parent, 2);
158 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "closed");
159 CompilationTestUtils.assertFilesCount(parent, 1);
161 parent = new File(parent, "object");
162 assertTrue(new File(parent, "Link1.java").exists());
163 assertTrue(new File(parent, "Link1Builder.java").exists());
164 CompilationTestUtils.assertFilesCount(parent, 2);
166 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "open");
167 CompilationTestUtils.assertFilesCount(parent, 1);
169 parent = new File(parent, "object");
170 assertTrue(new File(parent, "Nodes1.java").exists());
171 assertTrue(new File(parent, "Nodes1Builder.java").exists());
172 CompilationTestUtils.assertFilesCount(parent, 3);
174 parent = new File(parent, "nodes");
175 assertTrue(new File(parent, "Links.java").exists());
176 assertTrue(new File(parent, "LinksBuilder.java").exists());
177 CompilationTestUtils.assertFilesCount(parent, 2);
179 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "explicit");
180 CompilationTestUtils.assertFilesCount(parent, 1);
181 parent = new File(parent, "route");
182 CompilationTestUtils.assertFilesCount(parent, 1);
183 parent = new File(parent, "object");
184 assertTrue(new File(parent, "Subobjects.java").exists());
185 assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
186 CompilationTestUtils.assertFilesCount(parent, 3);
188 parent = new File(parent, "subobjects");
189 CompilationTestUtils.assertFilesCount(parent, 1);
190 parent = new File(parent, "subobject");
191 CompilationTestUtils.assertFilesCount(parent, 1);
192 parent = new File(parent, "type");
193 assertTrue(new File(parent, "PathKey.java").exists());
194 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
195 CompilationTestUtils.assertFilesCount(parent, 3);
197 parent = new File(parent, "path");
198 CompilationTestUtils.assertFilesCount(parent, 1);
199 parent = new File(parent, "key");
200 assertTrue(new File(parent, "PathKey.java").exists());
201 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
202 CompilationTestUtils.assertFilesCount(parent, 2);
204 // Test if all sources were generated from 'module bar'
205 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
206 assertTrue(new File(parent, "BarData.java").exists());
207 assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
208 assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
209 assertTrue(new File(parent, "RouteSubobjects.java").exists());
210 CompilationTestUtils.assertFilesCount(parent, 7);
212 parent = new File(parent, "route");
213 CompilationTestUtils.assertFilesCount(parent, 1);
214 parent = new File(new File(sourcesOutputDir, CompilationTestUtils.NS_BAR), "basic");
215 CompilationTestUtils.assertFilesCount(parent, 1);
216 parent = new File(parent, "explicit");
217 CompilationTestUtils.assertFilesCount(parent, 1);
218 parent = new File(parent, "route");
219 CompilationTestUtils.assertFilesCount(parent, 1);
221 parent = new File(parent, "subobjects");
222 CompilationTestUtils.assertFilesCount(parent, 2);
223 assertTrue(new File(parent, "SubobjectType.java").exists());
225 parent = new File(parent, "subobject");
226 CompilationTestUtils.assertFilesCount(parent, 1);
228 parent = new File(parent, "type");
229 assertTrue(new File(parent, "IpPrefix.java").exists());
230 assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
231 assertTrue(new File(parent, "Label.java").exists());
232 assertTrue(new File(parent, "LabelBuilder.java").exists());
233 CompilationTestUtils.assertFilesCount(parent, 4);
235 // Test if sources are compilable
236 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
238 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
242 public void testAugmentOfAugmentGeneration() throws Exception {
243 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("aug-of-aug");
244 final File compiledOutputDir = CompilationTestUtils.compilerOutput("aug-of-aug");
245 generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
247 // Test if all sources were generated from 'module foo'
248 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
249 assertTrue(new File(parent, "FooData.java").exists());
250 assertTrue(new File(parent, "FooListener.java").exists());
251 assertTrue(new File(parent, "PathAttributes.java").exists());
252 assertTrue(new File(parent, "Update.java").exists());
253 assertTrue(new File(parent, "UpdateBuilder.java").exists());
254 CompilationTestUtils.assertFilesCount(parent, 8);
256 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "path");
257 CompilationTestUtils.assertFilesCount(parent, 1);
258 parent = new File(parent, "attributes");
259 CompilationTestUtils.assertFilesCount(parent, 2);
260 final File origin = new File(parent, "Origin.java");
261 final File originBuilder = new File(parent, "OriginBuilder.java");
262 assertTrue(origin.exists());
263 assertTrue(originBuilder.exists());
265 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "update");
266 CompilationTestUtils.assertFilesCount(parent, 2);
267 assertTrue(new File(parent, "PathAttributes.java").exists());
268 assertTrue(new File(parent, "PathAttributesBuilder.java").exists());
270 // Test if all sources were generated from 'module bar'
271 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
272 assertTrue(new File(parent, "BarData.java").exists());
273 assertTrue(new File(parent, "Destination.java").exists());
274 assertTrue(new File(parent, "PathAttributes1.java").exists());
275 assertTrue(new File(parent, "PathAttributes1Builder.java").exists());
276 CompilationTestUtils.assertFilesCount(parent, 7);
278 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "destination");
279 CompilationTestUtils.assertFilesCount(parent, 2);
280 final File destinationType = new File(parent, "DestinationType.java");
281 assertTrue(destinationType.exists());
283 parent = new File(parent, "destination");
284 CompilationTestUtils.assertFilesCount(parent, 1);
285 parent = new File(parent, "type");
286 CompilationTestUtils.assertFilesCount(parent, 2);
287 final File destinationIpv4 = new File(parent, "DestinationIp.java");
288 final File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
289 assertTrue(destinationIpv4.exists());
290 assertTrue(destinationIpv4Builder.exists());
292 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "update");
293 CompilationTestUtils.assertFilesCount(parent, 1);
294 parent = new File(parent, "path");
295 CompilationTestUtils.assertFilesCount(parent, 1);
296 parent = new File(parent, "attributes");
297 final File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
298 final File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
299 assertTrue(mpUnreachNlri.exists());
300 assertTrue(mpUnreachNlriBuilder.exists());
301 CompilationTestUtils.assertFilesCount(parent, 3);
303 parent = new File(parent, "mp");
304 CompilationTestUtils.assertFilesCount(parent, 1);
305 parent = new File(parent, "unreach");
306 CompilationTestUtils.assertFilesCount(parent, 1);
307 parent = new File(parent, "nlri");
308 assertTrue(new File(parent, "WithdrawnRoutes.java").exists());
309 assertTrue(new File(parent, "WithdrawnRoutesBuilder.java").exists());
310 CompilationTestUtils.assertFilesCount(parent, 2);
312 // Test if all sources were generated from 'module baz'
313 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ);
314 assertTrue(new File(parent, "BazData.java").exists());
315 assertTrue(new File(parent, "LinkstateDestination.java").exists());
316 CompilationTestUtils.assertFilesCount(parent, 4);
318 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAZ + CompilationTestUtils.FS + "update");
319 CompilationTestUtils.assertFilesCount(parent, 1);
320 parent = new File(parent, "path");
321 CompilationTestUtils.assertFilesCount(parent, 1);
322 parent = new File(parent, "attributes");
323 CompilationTestUtils.assertFilesCount(parent, 1);
324 parent = new File(parent, "mp");
325 CompilationTestUtils.assertFilesCount(parent, 1);
326 parent = new File(parent, "unreach");
327 CompilationTestUtils.assertFilesCount(parent, 1);
328 parent = new File(parent, "nlri");
329 CompilationTestUtils.assertFilesCount(parent, 1);
330 parent = new File(parent, "withdrawn");
331 CompilationTestUtils.assertFilesCount(parent, 1);
332 parent = new File(parent, "routes");
333 CompilationTestUtils.assertFilesCount(parent, 1);
334 parent = new File(parent, "destination");
335 CompilationTestUtils.assertFilesCount(parent, 1);
336 parent = new File(parent, "type");
337 final File destinationLinkstate = new File(parent, "DestinationLinkstate.java");
338 final File destinationLinkstateBuilder = new File(parent, "DestinationLinkstateBuilder.java");
339 assertTrue(destinationLinkstate.exists());
340 assertTrue(destinationLinkstateBuilder.exists());
341 CompilationTestUtils.assertFilesCount(parent, 3);
342 parent = new File(parent, "destination");
343 CompilationTestUtils.assertFilesCount(parent, 1);
344 parent = new File(parent, "linkstate");
345 final File links = new File(parent, "Links.java");
346 final File linksBuilder = new File(parent, "LinksBuilder.java");
347 assertTrue(links.exists());
348 assertTrue(linksBuilder.exists());
349 CompilationTestUtils.assertFilesCount(parent, 3);
350 parent = new File(parent, "links");
351 final File source = new File(parent, "Source.java");
352 final File sourceBuilder = new File(parent, "SourceBuilder.java");
353 assertTrue(source.exists());
354 assertTrue(sourceBuilder.exists());
355 CompilationTestUtils.assertFilesCount(parent, 3);
356 parent = new File(parent, "source");
357 final File address = new File(parent, "Address.java");
358 final File addressBuilder = new File(parent, "AddressBuilder.java");
359 assertTrue(address.exists());
360 assertTrue(addressBuilder.exists());
361 CompilationTestUtils.assertFilesCount(parent, 2);
363 // Test if sources are compilable
364 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
366 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
370 public void testLeafReturnTypes() throws Exception {
371 final File sourcesOutputDir = CompilationTestUtils.generatorOutput("leaf-return-types");
372 final File compiledOutputDir = CompilationTestUtils.compilerOutput("leaf-return-types");
373 generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
375 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
376 assertTrue(new File(parent, "TestData.java").exists());
377 assertTrue(new File(parent, "Nodes.java").exists());
378 assertTrue(new File(parent, "NodesBuilder.java").exists());
379 assertTrue(new File(parent, "Alg.java").exists());
380 assertTrue(new File(parent, "NodesIdUnionBuilder.java").exists());
381 CompilationTestUtils.assertFilesCount(parent, 6);
383 // Test if sources are compilable
384 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
386 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008";
387 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
388 final Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
389 final Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
391 // Test methods return type
392 final byte[] b = new byte[] {};
393 CompilationTestUtils.assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
394 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
395 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "getIdBoolean");
396 CompilationTestUtils.assertContainsMethod(nodesClass, 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 java.lang.reflect.Type returnType = method.getGenericReturnType();
757 assertTrue(returnType instanceof ParameterizedType);
758 final ParameterizedType pt = (ParameterizedType) returnType;
759 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
760 assertEquals(1, parameters.length);
761 final java.lang.reflect.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 java.lang.reflect.Type returnType = method.getGenericReturnType();
774 assertTrue(returnType instanceof ParameterizedType);
775 final ParameterizedType pt = (ParameterizedType) returnType;
776 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
777 assertEquals(1, parameters.length);
778 final java.lang.reflect.Type parameter = parameters[0];
779 assertTrue(parameter instanceof WildcardType);
780 final WildcardType wildcardType = (WildcardType) parameter;
781 assertEquals("?", wildcardType.toString());