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.yangtools.sal.java.api.generator.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Range;
17 import java.lang.annotation.Annotation;
18 import java.lang.reflect.Field;
19 import java.lang.reflect.Method;
20 import java.lang.reflect.Modifier;
21 import java.lang.reflect.ParameterizedType;
22 import java.lang.reflect.WildcardType;
23 import java.math.BigDecimal;
24 import java.math.BigInteger;
26 import java.net.URLClassLoader;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
32 import org.junit.Test;
33 import org.opendaylight.yangtools.sal.binding.model.api.Type;
34 import org.opendaylight.yangtools.sal.java.api.generator.GeneratorJavaFile;
35 import org.opendaylight.yangtools.yang.binding.ChildOf;
36 import org.opendaylight.yangtools.yang.binding.annotations.RoutingContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 * Test correct code generation.
43 public class CompilationTest extends BaseCompilationTest {
46 * Java 8 allows JaCoCo to hook onto interfaces, as well as
47 * generating a default implementation. We only want to check
50 private static Collection<Method> abstractMethods(final Class<?> clazz) {
52 return Collections2.filter(Arrays.asList(clazz.getDeclaredMethods()), input -> Modifier.isAbstract(input.getModifiers()));
56 public void testListGeneration() throws Exception {
57 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "list-gen");
58 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
59 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "list-gen");
60 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
62 generateTestSources("/compilation/list-gen", sourcesOutputDir);
64 // Test if all sources are generated
65 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
66 final File keyArgs = new File(parent, "KeyArgs.java");
67 final File links = new File(parent, "Links.java");
68 final File linksBuilder = new File(parent, "LinksBuilder.java");
69 final File linksKey = new File(parent, "LinksKey.java");
70 final File testData = new File(parent, "TestData.java");
71 assertTrue(keyArgs.exists());
72 assertTrue(links.exists());
73 assertTrue(linksBuilder.exists());
74 assertTrue(linksKey.exists());
75 assertTrue(testData.exists());
76 CompilationTestUtils.assertFilesCount(parent, 6);
78 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST + CompilationTestUtils.FS + "links");
79 final File level = new File(parent, "Level.java");
80 final File linkGroup = new File(parent, "LinkGroup.java");
81 final File node = new File(parent, "Node.java");
82 final File nodeBuilder = new File(parent, "NodeBuilder.java");
83 final File nodeList = new File(parent, "NodeList.java");
84 final File nodeListBuilder = new File(parent, "NodeListBuilder.java");
85 final File nodesType = new File(parent, "NodesType.java");
86 assertTrue(level.exists());
87 assertTrue(linkGroup.exists());
88 assertTrue(node.exists());
89 assertTrue(nodeBuilder.exists());
90 assertTrue(nodeList.exists());
91 assertTrue(nodeListBuilder.exists());
92 assertTrue(nodesType.exists());
93 CompilationTestUtils.assertFilesCount(parent, 7);
95 // Test if sources are compilable
96 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
98 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
99 final Class<?> keyArgsClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008.KeyArgs", true, loader);
100 final Class<?> linksClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008.Links", true, loader);
101 final Class<?> linksKeyClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008.LinksKey", true, loader);
103 // Test generated 'grouping key-args'
104 assertTrue(keyArgsClass.isInterface());
105 CompilationTestUtils.assertContainsMethod(keyArgsClass, String.class, "getName");
106 CompilationTestUtils.assertContainsMethod(keyArgsClass, Integer.class, "getSize");
107 assertEquals(2, abstractMethods(keyArgsClass).size());
109 // Test generated 'list links'
110 assertTrue(linksClass.isInterface());
111 CompilationTestUtils.assertImplementsIfc(linksClass, keyArgsClass);
113 assertEquals(6, abstractMethods(linksClass).size());
115 // Test list key constructor arguments ordering
116 CompilationTestUtils.assertContainsConstructor(linksKeyClass, Byte.class, String.class, Integer.class);
117 // Test serialVersionUID generation
118 final Field suid = CompilationTestUtils.assertContainsField(linksKeyClass, "serialVersionUID", Long.TYPE);
119 suid.setAccessible(true);
120 assertEquals(-8829501012356283881L, suid.getLong(null));
122 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
126 public void testAugmentUnderUsesGeneration() throws Exception {
127 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "augment-under-uses");
128 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
129 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "augment-under-uses");
130 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
132 generateTestSources("/compilation/augment-under-uses", sourcesOutputDir);
134 // Test if all sources were generated from 'module foo'
135 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
136 assertTrue(new File(parent, "Object.java").exists());
137 assertTrue(new File(parent, "ClosedObject.java").exists());
138 assertTrue(new File(parent, "OpenObject.java").exists());
139 assertTrue(new File(parent, "ExplicitRouteObject.java").exists());
140 assertTrue(new File(parent, "PathKeySubobject.java").exists());
141 CompilationTestUtils.assertFilesCount(parent, 9);
143 parent = new File(parent, "object");
144 assertTrue(new File(parent, "Nodes.java").exists());
145 assertTrue(new File(parent, "NodesBuilder.java").exists());
146 CompilationTestUtils.assertFilesCount(parent, 2);
148 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "closed");
149 CompilationTestUtils.assertFilesCount(parent, 1);
151 parent = new File(parent, "object");
152 assertTrue(new File(parent, "Link1.java").exists());
153 assertTrue(new File(parent, "Link1Builder.java").exists());
154 CompilationTestUtils.assertFilesCount(parent, 2);
156 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "open");
157 CompilationTestUtils.assertFilesCount(parent, 1);
159 parent = new File(parent, "object");
160 assertTrue(new File(parent, "Nodes1.java").exists());
161 assertTrue(new File(parent, "Nodes1Builder.java").exists());
162 CompilationTestUtils.assertFilesCount(parent, 3);
164 parent = new File(parent, "nodes");
165 assertTrue(new File(parent, "Links.java").exists());
166 assertTrue(new File(parent, "LinksBuilder.java").exists());
167 CompilationTestUtils.assertFilesCount(parent, 2);
169 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "explicit");
170 CompilationTestUtils.assertFilesCount(parent, 1);
171 parent = new File(parent, "route");
172 CompilationTestUtils.assertFilesCount(parent, 1);
173 parent = new File(parent, "object");
174 assertTrue(new File(parent, "Subobjects.java").exists());
175 assertTrue(new File(parent, "SubobjectsBuilder.java").exists());
176 CompilationTestUtils.assertFilesCount(parent, 3);
178 parent = new File(parent, "subobjects");
179 CompilationTestUtils.assertFilesCount(parent, 1);
180 parent = new File(parent, "subobject");
181 CompilationTestUtils.assertFilesCount(parent, 1);
182 parent = new File(parent, "type");
183 assertTrue(new File(parent, "PathKey.java").exists());
184 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
185 CompilationTestUtils.assertFilesCount(parent, 3);
187 parent = new File(parent, "path");
188 CompilationTestUtils.assertFilesCount(parent, 1);
189 parent = new File(parent, "key");
190 assertTrue(new File(parent, "PathKey.java").exists());
191 assertTrue(new File(parent, "PathKeyBuilder.java").exists());
192 CompilationTestUtils.assertFilesCount(parent, 2);
194 // Test if all sources were generated from 'module bar'
195 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
196 assertTrue(new File(parent, "BasicExplicitRouteSubobjects.java").exists());
197 assertTrue(new File(parent, "ExplicitRouteSubobjects.java").exists());
198 assertTrue(new File(parent, "RouteSubobjects.java").exists());
199 CompilationTestUtils.assertFilesCount(parent, 5);
201 parent = new File(parent, "route");
202 CompilationTestUtils.assertFilesCount(parent, 1);
203 parent = new File(new File(sourcesOutputDir, CompilationTestUtils.NS_BAR), "basic");
204 CompilationTestUtils.assertFilesCount(parent, 1);
205 parent = new File(parent, "explicit");
206 CompilationTestUtils.assertFilesCount(parent, 1);
207 parent = new File(parent, "route");
208 CompilationTestUtils.assertFilesCount(parent, 1);
210 parent = new File(parent, "subobjects");
211 CompilationTestUtils.assertFilesCount(parent, 2);
212 assertTrue(new File(parent, "SubobjectType.java").exists());
214 parent = new File(parent, "subobject");
215 CompilationTestUtils.assertFilesCount(parent, 1);
217 parent = new File(parent, "type");
218 assertTrue(new File(parent, "IpPrefix.java").exists());
219 assertTrue(new File(parent, "IpPrefixBuilder.java").exists());
220 assertTrue(new File(parent, "Label.java").exists());
221 assertTrue(new File(parent, "LabelBuilder.java").exists());
222 CompilationTestUtils.assertFilesCount(parent, 4);
224 // Test if sources are compilable
225 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
227 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
231 public void testAugmentOfAugmentGeneration() throws Exception {
232 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "aug-of-aug");
233 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
234 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "aug-of-aug");
235 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
237 generateTestSources("/compilation/augment-of-augment", sourcesOutputDir);
239 // Test if all sources were generated from 'module foo'
240 File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
241 final File fooListener = new File(parent, "FooListener.java");
242 File pathAttributes = new File(parent, "PathAttributes.java");
243 final File update = new File(parent, "Update.java");
244 final File updateBuilder = new File(parent, "UpdateBuilder.java");
245 assertTrue(fooListener.exists());
246 assertTrue(pathAttributes.exists());
247 assertTrue(update.exists());
248 assertTrue(updateBuilder.exists());
249 CompilationTestUtils.assertFilesCount(parent, 6);
251 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "path");
252 CompilationTestUtils.assertFilesCount(parent, 1);
253 parent = new File(parent, "attributes");
254 CompilationTestUtils.assertFilesCount(parent, 2);
255 final File origin = new File(parent, "Origin.java");
256 final File originBuilder = new File(parent, "OriginBuilder.java");
257 assertTrue(origin.exists());
258 assertTrue(originBuilder.exists());
260 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO + CompilationTestUtils.FS + "update");
261 CompilationTestUtils.assertFilesCount(parent, 2);
262 pathAttributes = new File(parent, "PathAttributes.java");
263 final File pathAttributesBuilder = new File(parent, "PathAttributesBuilder.java");
264 assertTrue(pathAttributes.exists());
265 assertTrue(pathAttributesBuilder.exists());
267 // Test if all sources were generated from 'module bar'
268 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
269 final File destination = new File(parent, "Destination.java");
270 final File pathAttributes1 = new File(parent, "PathAttributes1.java");
271 final File pathAttributes1Builder = new File(parent, "PathAttributes1Builder.java");
272 assertTrue(destination.exists());
273 assertTrue(pathAttributes1.exists());
274 assertTrue(pathAttributes1Builder.exists());
275 CompilationTestUtils.assertFilesCount(parent, 5);
277 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "destination");
278 CompilationTestUtils.assertFilesCount(parent, 2);
279 final File destinationType = new File(parent, "DestinationType.java");
280 assertTrue(destinationType.exists());
282 parent = new File(parent, "destination");
283 CompilationTestUtils.assertFilesCount(parent, 1);
284 parent = new File(parent, "type");
285 CompilationTestUtils.assertFilesCount(parent, 2);
286 final File destinationIpv4 = new File(parent, "DestinationIp.java");
287 final File destinationIpv4Builder = new File(parent, "DestinationIpBuilder.java");
288 assertTrue(destinationIpv4.exists());
289 assertTrue(destinationIpv4Builder.exists());
291 parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR + CompilationTestUtils.FS + "update");
292 CompilationTestUtils.assertFilesCount(parent, 1);
293 parent = new File(parent, "path");
294 CompilationTestUtils.assertFilesCount(parent, 1);
295 parent = new File(parent, "attributes");
296 final File mpUnreachNlri = new File(parent, "MpUnreachNlri.java");
297 final File mpUnreachNlriBuilder = new File(parent, "MpUnreachNlriBuilder.java");
298 assertTrue(mpUnreachNlri.exists());
299 assertTrue(mpUnreachNlriBuilder.exists());
300 CompilationTestUtils.assertFilesCount(parent, 3);
302 parent = new File(parent, "mp");
303 CompilationTestUtils.assertFilesCount(parent, 1);
304 parent = new File(parent, "unreach");
305 CompilationTestUtils.assertFilesCount(parent, 1);
306 parent = new File(parent, "nlri");
307 final File withdrawnRoutes = new File(parent, "WithdrawnRoutes.java");
308 final File withdrawnRoutesBuilder = new File(parent, "WithdrawnRoutesBuilder.java");
309 assertTrue(withdrawnRoutes.exists());
310 assertTrue(withdrawnRoutesBuilder.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 CompilationTestUtils.assertFilesCount(parent, 2);
316 final File linkstateDestination = new File(parent, "LinkstateDestination.java");
317 assertTrue(linkstateDestination.exists());
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 = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "leaf-return-types");
373 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
374 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "leaf-return-types");
375 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
377 generateTestSources("/compilation/leaf-return-types", sourcesOutputDir);
379 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_TEST);
380 assertTrue(new File(parent, "TestData.java").exists());
381 assertTrue(new File(parent, "Nodes.java").exists());
382 assertTrue(new File(parent, "NodesBuilder.java").exists());
383 assertTrue(new File(parent, "Alg.java").exists());
384 assertTrue(new File(parent, "NodesIdUnionBuilder.java").exists());
385 CompilationTestUtils.assertFilesCount(parent, 5);
387 // Test if sources are compilable
388 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
390 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.opendaylight.test.rev131008";
391 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
392 final Class<?> nodesClass = Class.forName(pkg + ".Nodes", true, loader);
393 final Class<?> builderClass = Class.forName(pkg + ".NodesBuilder", true, loader);
395 // Test methods return type
396 final byte[] b = new byte[] {};
397 CompilationTestUtils.assertContainsMethod(nodesClass, b.getClass(), "getIdBinary");
398 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdBits", "getIdBits", loader);
399 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "isIdBoolean");
400 CompilationTestUtils.assertContainsMethod(nodesClass, BigDecimal.class, "getIdDecimal64");
401 CompilationTestUtils.assertContainsMethod(nodesClass, Boolean.class, "isIdEmpty");
402 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdEnumeration", "getIdEnumeration", loader);
403 testReturnTypeIdentityref(nodesClass, "getIdIdentityref", pkg + ".Alg");
404 testReturnTypeInstanceIdentitifer(loader, nodesClass, "getIdInstanceIdentifier");
405 CompilationTestUtils.assertContainsMethod(nodesClass, Byte.class, "getId8");
406 CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getId16");
407 CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getId32");
408 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getId64");
409 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdLeafref");
410 CompilationTestUtils.assertContainsMethod(nodesClass, String.class, "getIdString");
411 CompilationTestUtils.assertContainsMethod(nodesClass, Short.class, "getIdU8");
412 CompilationTestUtils.assertContainsMethod(nodesClass, Integer.class, "getIdU16");
413 CompilationTestUtils.assertContainsMethod(nodesClass, Long.class, "getIdU32");
414 CompilationTestUtils.assertContainsMethod(nodesClass, BigInteger.class, "getIdU64");
415 CompilationTestUtils.assertContainsMethod(nodesClass, pkg + ".Nodes$IdUnion", "getIdUnion", loader);
417 final Object builderObj = builderClass.newInstance();
419 Method m = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdBinary", b.getClass());
420 final List<Range<Integer>> lengthConstraints = new ArrayList<>();
421 lengthConstraints.add(Range.closed(1, 10));
422 byte[] arg = new byte[] {};
423 String expectedMsg = String.format("Invalid length: %s, expected: %s.", Arrays.toString(arg), lengthConstraints);
424 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, m, expectedMsg, arg);
426 m = CompilationTestUtils.assertContainsMethod(builderClass, builderClass, "setIdDecimal64", BigDecimal.class);
427 final List<Range<BigDecimal>> rangeConstraints = new ArrayList<>();
428 rangeConstraints.add(Range.closed(new BigDecimal("1.5"), new BigDecimal("5.5")));
429 Object arg1 = new BigDecimal("1.4");
430 expectedMsg = String.format("Invalid range: %s, expected: %s.", arg1, rangeConstraints);
431 CompilationTestUtils.assertContainsRestrictionCheck(builderObj, m, expectedMsg, arg1);
433 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
437 public void testGenerationContextReferenceExtension() throws Exception {
438 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "context-reference");
439 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
440 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "context-reference");
441 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
443 generateTestSources("/compilation/context-reference", sourcesOutputDir);
445 // Test if all sources are generated
446 final File fooParent = new File(sourcesOutputDir, CompilationTestUtils.NS_FOO);
447 CompilationTestUtils.assertFilesCount(fooParent, 3);
448 assertTrue(new File(fooParent, "FooData.java").exists());
449 assertTrue(new File(fooParent, "Nodes.java").exists());
450 assertTrue(new File(fooParent, "NodesBuilder.java").exists());
452 final File barParent = new File(sourcesOutputDir, CompilationTestUtils.NS_BAR);
453 CompilationTestUtils.assertFilesCount(barParent, 1);
454 assertTrue(new File(barParent, "IdentityClass.java").exists());
456 // Test if sources are compilable
457 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
459 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
460 final Class<?> nodesClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.foo.rev131008.Nodes", true, loader);
461 final Class<?> identityClass = Class
462 .forName(CompilationTestUtils.BASE_PKG + ".urn.opendaylight.bar.rev131008.IdentityClass", true, loader);
466 identityClass.getConstructor();
467 final Class<?> baseIdentity = Class.forName("org.opendaylight.yangtools.yang.binding.BaseIdentity", true, loader);
468 assertEquals(baseIdentity, identityClass.getSuperclass());
469 } catch (final NoSuchMethodException e) {
470 throw new AssertionError("IdentityClass must have no-arg constructor");
475 final Method getId = nodesClass.getMethod("getId");
476 final Annotation[] annotations = getId.getAnnotations();
477 assertEquals(1, annotations.length);
478 final Annotation routingContext = annotations[0];
479 assertEquals(RoutingContext.class, routingContext.annotationType());
480 } catch (final NoSuchMethodException e) {
481 throw new AssertionError("Method getId() not found");
484 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
488 public void compilationTest() throws Exception {
489 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "yang");
490 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
491 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "yang");
492 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
494 generateTestSources("/yang", sourcesOutputDir);
496 // Test if sources are compilable
497 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
499 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
503 public void testBug586() throws Exception {
504 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "bug586");
505 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
506 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "bug586");
507 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
509 generateTestSources("/compilation/bug586", sourcesOutputDir);
511 // Test if sources are compilable
512 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
514 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
518 public void testBug4760() throws Exception {
519 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "bug4760");
520 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
521 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "bug4760");
522 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
524 generateTestSources("/compilation/bug4760", sourcesOutputDir);
526 // Test if sources are compilable
527 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
529 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
533 * Test handling nested uses-augmentations.
538 public void testBug1172() throws Exception {
539 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "bug1172");
540 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
541 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "bug1172");
542 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
544 generateTestSources("/compilation/bug1172", sourcesOutputDir);
546 // Test if sources are compilable
547 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
549 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
553 public void testBug5461() throws Exception {
554 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "bug5461");
555 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
556 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "bug5461");
557 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
559 generateTestSources("/compilation/bug5461", sourcesOutputDir);
561 // Test if sources are compilable
562 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
564 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
568 public void testBug5882() throws Exception {
569 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "bug5882");
570 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
571 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "bug5882");
572 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
573 generateTestSources("/compilation/bug5882", sourcesOutputDir);
575 // Test if sources are compilable
576 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
578 final File parent = new File(sourcesOutputDir, CompilationTestUtils.NS_BUG5882);
579 assertTrue(new File(parent, "FooData.java").exists());
580 assertTrue(new File(parent, "TypedefCurrent.java").exists());
581 assertTrue(new File(parent, "TypedefDeprecated.java").exists());
583 final String pkg = CompilationTestUtils.BASE_PKG + ".urn.yang.foo.rev160102";
584 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
585 final Class<?> cls = loader.loadClass(pkg + ".FooData");
586 final Class<?> clsContainer = loader.loadClass(pkg + ".ContainerMain");
587 final Class<?> clsTypedefDepr = loader.loadClass(pkg + ".TypedefDeprecated");
588 final Class<?> clsTypedefCur = loader.loadClass(pkg + ".TypedefCurrent");
589 final Class<?> clsGroupingDepr = loader.loadClass(pkg + ".GroupingDeprecated");
590 final Class<?> clsGroupingCur = loader.loadClass(pkg + ".GroupingCurrent");
591 final Class<?> clsTypeDef1 = loader.loadClass(pkg + ".Typedef1");
592 final Class<?> clsTypeDef2 = loader.loadClass(pkg + ".Typedef2");
593 final Class<?> clsTypeDef3 = loader.loadClass(pkg + ".Typedef3");
594 assertTrue(clsTypedefDepr.getAnnotations()[0].toString().contains("Deprecated"));
595 assertTrue(clsTypedefCur.getAnnotations().length == 0);
596 assertTrue(clsGroupingDepr.getAnnotations()[0].toString().contains("Deprecated"));
597 assertTrue(clsGroupingCur.getAnnotations().length == 0);
598 assertTrue(clsTypeDef1.getAnnotations().length == 0);
599 assertTrue(clsTypeDef3.getAnnotations().length == 0);
600 assertTrue(clsTypeDef2.getAnnotations()[0].toString().contains("Deprecated"));
602 /*methods inside container*/
603 assertTrue(clsContainer.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
604 assertTrue(clsContainer.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
605 assertTrue(clsContainer.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
606 assertFalse(clsContainer.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
607 assertFalse(clsContainer.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
608 assertFalse(clsContainer.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
610 /*methods inside module*/
611 assertTrue(cls.getMethod("getContainerMainLeafDepr").isAnnotationPresent(Deprecated.class));
612 assertTrue(cls.getMethod("getContainerMainListDepr").isAnnotationPresent(Deprecated.class));
613 assertTrue(cls.getMethod("getContainerMainChoiceDepr").isAnnotationPresent(Deprecated.class));
614 assertFalse(cls.getMethod("getContainerMainLeafCurrent").isAnnotationPresent(Deprecated.class));
615 assertFalse(cls.getMethod("getContainerMainListCurrent").isAnnotationPresent(Deprecated.class));
616 assertFalse(cls.getMethod("getContainerMainChoiceCur").isAnnotationPresent(Deprecated.class));
617 assertTrue(cls.getMethod("getLeafDeprecated").isAnnotationPresent(Deprecated.class));
619 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
623 * Test if class generated for node from grouping implements ChildOf.
628 public void testBug1377() throws Exception {
629 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "bug1377");
630 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
631 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "bug1377");
632 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
634 generateTestSources("/compilation/bug1377", sourcesOutputDir);
636 // Test if sources are compilable
637 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
639 final ClassLoader loader = new URLClassLoader(new URL[] { compiledOutputDir.toURI().toURL() });
640 final Class<?> outputActionClass = Class.forName(CompilationTestUtils.BASE_PKG
641 + ".urn.test.foo.rev140717.action.action.output.action._case.OutputAction", true, loader);
642 final Class<?> actionClass = Class.forName(CompilationTestUtils.BASE_PKG + ".urn.test.foo.rev140717.Action", true, loader);
644 // Test generated 'container output-action'
645 assertTrue(outputActionClass.isInterface());
646 CompilationTestUtils.assertImplementsParameterizedIfc(outputActionClass, ChildOf.class.toString(), actionClass.getCanonicalName());
648 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
652 public void classNamesColisionTest() throws Exception {
653 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + "class-name-collision");
654 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
655 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + "class-name-collision");
656 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
658 generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
659 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
660 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
663 private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
664 final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
665 final SchemaContext context = TestUtils.parseYangSources(sourceFiles);
666 final List<Type> types = bindingGenerator.generateTypes(context);
667 Collections.sort(types, (o1, o2) -> o2.getName().compareTo(o1.getName()));
668 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
669 generator.generateToFile(sourcesOutputDir);
672 private static void testReturnTypeIdentityref(final Class<?> clazz, final String methodName, final String returnTypeStr) throws Exception {
674 java.lang.reflect.Type returnType;
676 method = clazz.getMethod(methodName);
677 assertEquals(java.lang.Class.class, method.getReturnType());
678 returnType = method.getGenericReturnType();
679 assertTrue(returnType instanceof ParameterizedType);
680 final ParameterizedType pt = (ParameterizedType) returnType;
681 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
682 assertEquals(1, parameters.length);
683 final java.lang.reflect.Type parameter = parameters[0];
684 assertTrue(parameter instanceof WildcardType);
685 final WildcardType wildcardType = (WildcardType) parameter;
686 assertEquals("? extends " + returnTypeStr, wildcardType.toString());
687 } catch (final NoSuchMethodException e) {
688 throw new AssertionError("Method '" + methodName + "' not found");
692 private static void testReturnTypeInstanceIdentitifer(final ClassLoader loader, final Class<?> clazz, final String methodName)
695 Class<?> rawReturnType;
696 java.lang.reflect.Type returnType;
698 method = clazz.getMethod(methodName);
699 rawReturnType = Class.forName("org.opendaylight.yangtools.yang.binding.InstanceIdentifier", true, loader);
700 assertEquals(rawReturnType, method.getReturnType());
701 returnType = method.getGenericReturnType();
702 assertTrue(returnType instanceof ParameterizedType);
703 final ParameterizedType pt = (ParameterizedType) returnType;
704 final java.lang.reflect.Type[] parameters = pt.getActualTypeArguments();
705 assertEquals(1, parameters.length);
706 final java.lang.reflect.Type parameter = parameters[0];
707 assertTrue(parameter instanceof WildcardType);
708 final WildcardType wildcardType = (WildcardType) parameter;
709 assertEquals("?", wildcardType.toString());
710 } catch (final NoSuchMethodException e) {
711 throw new AssertionError("Method '" + methodName + "' not found");