Improve QNAME field definition
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / Bug5151Test.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator.test;
9
10 import static junit.framework.TestCase.assertTrue;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Scanner;
19 import org.junit.Test;
20
21 /**
22  * Bug5151 involves adding <code>{@literal @}return</code> annotations to accessor methods.
23  */
24 public class Bug5151Test extends BaseCompilationTest {
25
26     private static final String BUG_ID = "bug5151";
27
28     @Test
29     public void test() throws Exception {
30         final File sourcesOutputDir = CompilationTestUtils.generatorOutput(BUG_ID);
31         final File compiledOutputDir = CompilationTestUtils.compilerOutput(BUG_ID);
32         generateTestSources(CompilationTestUtils.FS + "compilation" + CompilationTestUtils.FS + BUG_ID,
33             sourcesOutputDir);
34
35         // Test if sources are compilable
36         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
37
38         final Map<String, File> generatedFiles = getFiles(sourcesOutputDir);
39         assertEquals(4, generatedFiles.size());
40
41         final File fooContainerFile = generatedFiles.get("FooContainer.java");
42         assertNotNull(fooContainerFile);
43         assertTrue(findInFile(fooContainerFile,
44                 "@return <code>java.lang.String</code> <code>fooInContainer</code>, "
45                         + "or <code>null</code> if not present"));
46
47         final File fooDataFile = generatedFiles.get("FooData.java");
48         assertNotNull(fooDataFile);
49         assertTrue(findInFile(fooDataFile, "FooContainer</code> <code>fooContainer</code>, or <code>null</code> if not present"));
50
51         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
52     }
53
54     private static boolean findInFile(final File file, final String searchText) throws FileNotFoundException {
55         try (Scanner scanner = new Scanner(file)) {
56             while (scanner.hasNextLine()) {
57                 final String nextLine = scanner.nextLine();
58                 if (nextLine.contains(searchText)) {
59                     return true;
60                 }
61             }
62         }
63         return false;
64     }
65
66     private static Map<String, File> getFiles(final File path) {
67         return getFiles(path, new HashMap<>());
68     }
69
70     private static Map<String, File> getFiles(final File path, final Map<String, File> files) {
71         final File [] dirFiles = path.listFiles();
72         for (File file : dirFiles) {
73             if (file.isDirectory()) {
74                 return getFiles(file, files);
75             }
76
77             files.put(file.getName(), file);
78         }
79         return files;
80     }
81 }