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.test;
10 import static junit.framework.TestCase.assertTrue;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.Maps;
17 import java.io.FileNotFoundException;
18 import java.util.List;
20 import java.util.Scanner;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 * Bug5151 involves adding <code>{@literal @}return</code> annotations to accessor methods.
30 public class Bug5151Test extends BaseCompilationTest {
32 private static final String BUG_ID = "bug5151";
35 public void test() throws Exception {
36 final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + BUG_ID);
37 assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
38 final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + BUG_ID);
39 assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
41 generateTestSources(CompilationTestUtils.FS + "compilation" + CompilationTestUtils.FS + BUG_ID, sourcesOutputDir);
43 // Test if sources are compilable
44 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
46 final Map<String, File> generatedFiles = getFiles(sourcesOutputDir);
47 assertEquals(3, generatedFiles.size());
49 final File fooContainerFile = generatedFiles.get("FooContainer.java");
50 assertNotNull(fooContainerFile);
51 assertTrue(findInFile(fooContainerFile,
52 "@return <code>java.lang.String</code> <code>fooInContainer</code>, "
53 + "or <code>null</code> if not present"));
55 final File fooDataFile = generatedFiles.get("FooData.java");
56 assertNotNull(fooDataFile);
57 assertTrue(findInFile(fooDataFile, "FooContainer</code> <code>fooContainer</code>, or <code>null</code> if not present"));
59 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
62 private static boolean findInFile(final File file, final String searchText) throws FileNotFoundException {
63 final Scanner scanner = new Scanner(file);
64 while (scanner.hasNextLine()) {
65 final String nextLine = scanner.nextLine();
66 if (nextLine.contains(searchText)) {
73 private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
74 final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
75 final SchemaContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
76 final List<Type> types = bindingGenerator.generateTypes(context);
77 final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
78 generator.generateToFile(sourcesOutputDir);
81 private static Map<String, File> getFiles(final File path) {
82 return getFiles(path, Maps.newHashMap());
85 private static Map<String, File> getFiles(final File path, final Map<String, File> files) {
86 final File [] dirFiles = path.listFiles();
87 for (File file : dirFiles) {
88 if (file.isDirectory()) {
89 return getFiles(file, files);
91 files.put(file.getName(), file);