Fixup BuilderTemplate decomposition
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / Bug5151Test.java
index eb00557eb32366db11f1623b797ad9525838e8a1..fc9b8279630b144d702c3f41640c15454ab61f51 100644 (file)
@@ -11,18 +11,12 @@ import static junit.framework.TestCase.assertTrue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Maps;
 import java.io.File;
 import java.io.FileNotFoundException;
-import java.util.List;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Scanner;
 import org.junit.Test;
-import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile;
-import org.opendaylight.mdsal.binding.model.api.Type;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 /**
  * Bug5151 involves adding <code>{@literal @}return</code> annotations to accessor methods.
@@ -33,18 +27,16 @@ public class Bug5151Test extends BaseCompilationTest {
 
     @Test
     public void test() throws Exception {
-        final File sourcesOutputDir = new File(CompilationTestUtils.GENERATOR_OUTPUT_PATH + CompilationTestUtils.FS + BUG_ID);
-        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
-        final File compiledOutputDir = new File(CompilationTestUtils.COMPILER_OUTPUT_PATH + CompilationTestUtils.FS + BUG_ID);
-        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
-
-        generateTestSources(CompilationTestUtils.FS + "compilation" + CompilationTestUtils.FS + BUG_ID, sourcesOutputDir);
+        final File sourcesOutputDir = CompilationTestUtils.generatorOutput(BUG_ID);
+        final File compiledOutputDir = CompilationTestUtils.compilerOutput(BUG_ID);
+        generateTestSources(CompilationTestUtils.FS + "compilation" + CompilationTestUtils.FS + BUG_ID,
+            sourcesOutputDir);
 
         // Test if sources are compilable
         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
 
         final Map<String, File> generatedFiles = getFiles(sourcesOutputDir);
-        assertEquals(3, generatedFiles.size());
+        assertEquals(4, generatedFiles.size());
 
         final File fooContainerFile = generatedFiles.get("FooContainer.java");
         assertNotNull(fooContainerFile);
@@ -60,26 +52,19 @@ public class Bug5151Test extends BaseCompilationTest {
     }
 
     private static boolean findInFile(final File file, final String searchText) throws FileNotFoundException {
-        final Scanner scanner = new Scanner(file);
-        while (scanner.hasNextLine()) {
-            final String nextLine = scanner.nextLine();
-            if (nextLine.contains(searchText)) {
-                return true;
+        try (Scanner scanner = new Scanner(file)) {
+            while (scanner.hasNextLine()) {
+                final String nextLine = scanner.nextLine();
+                if (nextLine.contains(searchText)) {
+                    return true;
+                }
             }
         }
         return false;
     }
 
-    private void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
-        final List<File> sourceFiles = CompilationTestUtils.getSourceFiles(resourceDirPath);
-        final SchemaContext context = YangParserTestUtils.parseYangSources(sourceFiles);
-        final List<Type> types = bindingGenerator.generateTypes(context);
-        final GeneratorJavaFile generator = new GeneratorJavaFile(ImmutableSet.copyOf(types));
-        generator.generateToFile(sourcesOutputDir);
-    }
-
     private static Map<String, File> getFiles(final File path) {
-        return getFiles(path, Maps.newHashMap());
+        return getFiles(path, new HashMap<>());
     }
 
     private static Map<String, File> getFiles(final File path, final Map<String, File> files) {
@@ -87,9 +72,9 @@ public class Bug5151Test extends BaseCompilationTest {
         for (File file : dirFiles) {
             if (file.isDirectory()) {
                 return getFiles(file, files);
-            } else {
-                files.put(file.getName(), file);
             }
+
+            files.put(file.getName(), file);
         }
         return files;
     }