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.io.IOException;
19 import java.net.URISyntaxException;
20 import java.util.List;
22 import java.util.Scanner;
23 import org.junit.Test;
24 import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
25 import org.opendaylight.mdsal.binding.model.api.Type;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
30 * Bug5151 involves adding <code>{@literal @}return</code> annotations to accessor methods.
32 public class Bug5151Test extends BaseCompilationTest {
34 private static final String BUG_ID = "bug5151";
37 public void test() throws Exception {
38 final File sourcesOutputDir = CompilationTestUtils.generatorOutput(BUG_ID);
39 final File compiledOutputDir = CompilationTestUtils.compilerOutput(BUG_ID);
40 generateTestSources(CompilationTestUtils.FS + "compilation" + CompilationTestUtils.FS + BUG_ID, sourcesOutputDir);
42 // Test if sources are compilable
43 CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
45 final Map<String, File> generatedFiles = getFiles(sourcesOutputDir);
46 assertEquals(3, generatedFiles.size());
48 final File fooContainerFile = generatedFiles.get("FooContainer.java");
49 assertNotNull(fooContainerFile);
50 assertTrue(findInFile(fooContainerFile,
51 "@return <code>java.lang.String</code> <code>fooInContainer</code>, "
52 + "or <code>null</code> if not present"));
54 final File fooDataFile = generatedFiles.get("FooData.java");
55 assertNotNull(fooDataFile);
56 assertTrue(findInFile(fooDataFile, "FooContainer</code> <code>fooContainer</code>, or <code>null</code> if not present"));
58 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
61 private static boolean findInFile(final File file, final String searchText) throws FileNotFoundException {
62 final Scanner scanner = new Scanner(file);
63 while (scanner.hasNextLine()) {
64 final String nextLine = scanner.nextLine();
65 if (nextLine.contains(searchText)) {
72 private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir)
73 throws IOException, URISyntaxException {
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);
92 files.put(file.getName(), file);