Create proper assertFileContains() 98/92798/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 29 Sep 2020 22:45:37 +0000 (00:45 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 30 Sep 2020 00:09:08 +0000 (00:09 +0000)
We want to have a proper assertion, not just assertTrue(). Change
findInFile() to assertFileContains().

Change-Id: I15d36db3423bc28df364dbed216adbdced8fc9a0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/test/Bug5151Test.java
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/mdsal/binding/java/api/generator/test/FileSearchUtil.java

index 4d7ee9002532edc962e272753759a5033231630b..d1f62d3eda7b0f84a5d4d4c57c3755175728f794 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator.test;
 
-import static junit.framework.TestCase.assertTrue;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
@@ -37,14 +36,13 @@ public class Bug5151Test extends BaseCompilationTest {
 
         final File fooContainerFile = generatedFiles.get("FooContainer.java");
         assertNotNull(fooContainerFile);
-        assertTrue(FileSearchUtil.findInFile(fooContainerFile,
-                "@return <code>java.lang.String</code> <code>fooInContainer</code>, "
-                        + "or <code>null</code> if not present"));
+        FileSearchUtil.assertFileContains(fooContainerFile,
+            "@return <code>java.lang.String</code> <code>fooInContainer</code>, or <code>null</code> if not present");
 
         final File fooDataFile = generatedFiles.get("FooData.java");
         assertNotNull(fooDataFile);
-        assertTrue(FileSearchUtil.findInFile(fooDataFile,
-            "FooContainer</code> <code>fooContainer</code>, or <code>null</code> if not present"));
+        FileSearchUtil.assertFileContains(fooDataFile,
+            "FooContainer</code> <code>fooContainer</code>, or <code>null</code> if not present");
 
         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
     }
index 19abb376152316ea42d63ba476fe951ec5aa8546..b8bfa34c950d38275ff31792ad557a1e42fa3809 100644 (file)
@@ -18,16 +18,15 @@ final class FileSearchUtil {
         // Hidden on purpose
     }
 
-    static boolean findInFile(final File file, final String searchText) throws FileNotFoundException {
+    static void assertFileContains(final File file, final String searchText) throws FileNotFoundException {
         try (Scanner scanner = new Scanner(file)) {
             while (scanner.hasNextLine()) {
-                final String nextLine = scanner.nextLine();
-                if (nextLine.contains(searchText)) {
-                    return true;
+                if (scanner.nextLine().contains(searchText)) {
+                    return;
                 }
             }
         }
-        return false;
+        throw new AssertionError("File " + file + " does not contain '" + searchText + "'");
     }
 
     static Map<String, File> getFiles(final File path) {