You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2020/07/09 15:34:52 UTC

[maven-surefire] 01/02: Added tests with JPMS and resources in Surefire1733JUnitIT

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch flush
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit cdff5bd6971dbcf1bb6e0277b39d27549fa184e2
Author: tibordigana <ti...@apache.org>
AuthorDate: Sun Jun 28 03:33:25 2020 +0200

    Added tests with JPMS and resources in Surefire1733JUnitIT
---
 .../surefire/its/jiras/Surefire1733JUnitIT.java    |  4 +-
 .../src/main/java/main/Service.java                | 18 ++++++
 .../src/main/java/module-info.java                 |  4 +-
 .../src/main/resources/main/a.txt                  |  1 +
 .../src/test/java/test/MyIT.java                   | 70 +++++++++++++++++++++-
 .../src/test/java/test/MyTest.java                 | 70 +++++++++++++++++++++-
 .../src/test/resources/tests/a.txt                 |  1 +
 7 files changed, 163 insertions(+), 5 deletions(-)

diff --git a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1733JUnitIT.java b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1733JUnitIT.java
index 242fede..fe45d68 100644
--- a/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1733JUnitIT.java
+++ b/surefire-its/src/test/java/org/apache/maven/surefire/its/jiras/Surefire1733JUnitIT.java
@@ -42,7 +42,9 @@ public class Surefire1733JUnitIT extends AbstractJigsawIT
             .assertThatLogLine( containsString( "Running test.MyIT" ), is( 1 ) )
             .assertThatLogLine( containsString( "class main.Service in the module \"main\"" ), is( 2 ) )
             .assertThatLogLine( containsString( "class test.MyTest in the module \"test\"" ), is( 1 ) )
-            .assertThatLogLine( containsString( "class test.MyIT in the module \"test\"" ), is( 1 ) );
+            .assertThatLogLine( containsString( "class test.MyIT in the module \"test\"" ), is( 1 ) )
+            .assertThatLogLine( containsString( "Hi there!" ), is( 4 ) )
+            .assertThatLogLine( containsString( "Hello!" ), is( 4 ) );
     }
 
     @Override
diff --git a/surefire-its/src/test/resources/surefire-1733-junit4/src/main/java/main/Service.java b/surefire-its/src/test/resources/surefire-1733-junit4/src/main/java/main/Service.java
index 68592db..03e8340 100644
--- a/surefire-its/src/test/resources/surefire-1733-junit4/src/main/java/main/Service.java
+++ b/surefire-its/src/test/resources/surefire-1733-junit4/src/main/java/main/Service.java
@@ -19,9 +19,27 @@ package main;
  * under the License.
  */
 
+import java.io.IOException;
+import java.util.Scanner;
+
 /**
  *
  */
 public class Service
 {
+    public String getNormalResource()
+    {
+        try ( Scanner scanner = new Scanner( getClass().getResourceAsStream( "/main/a.txt" ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
+
+    public String getResourceByJPMS() throws IOException
+    {
+        try ( Scanner scanner = new Scanner( getClass().getModule().getResourceAsStream( "main/a.txt" ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
 }
diff --git a/surefire-its/src/test/resources/surefire-1733-junit4/src/main/java/module-info.java b/surefire-its/src/test/resources/surefire-1733-junit4/src/main/java/module-info.java
index 216ec8a..7eadcb4 100644
--- a/surefire-its/src/test/resources/surefire-1733-junit4/src/main/java/module-info.java
+++ b/surefire-its/src/test/resources/surefire-1733-junit4/src/main/java/module-info.java
@@ -18,9 +18,9 @@
  */
 
 /**
- *
+ * "open" for testing Class.getResourceAsStream(), a resource in main module called by test module.
  */
-module main
+open module main
 {
     exports main;
 }
diff --git a/surefire-its/src/test/resources/surefire-1733-junit4/src/main/resources/main/a.txt b/surefire-its/src/test/resources/surefire-1733-junit4/src/main/resources/main/a.txt
new file mode 100644
index 0000000..26bdedc
--- /dev/null
+++ b/surefire-its/src/test/resources/surefire-1733-junit4/src/main/resources/main/a.txt
@@ -0,0 +1 @@
+Hi there!
\ No newline at end of file
diff --git a/surefire-its/src/test/resources/surefire-1733-junit4/src/test/java/test/MyIT.java b/surefire-its/src/test/resources/surefire-1733-junit4/src/test/java/test/MyIT.java
index 3e3eb52..c36a356 100644
--- a/surefire-its/src/test/resources/surefire-1733-junit4/src/test/java/test/MyIT.java
+++ b/surefire-its/src/test/resources/surefire-1733-junit4/src/test/java/test/MyIT.java
@@ -22,6 +22,9 @@ package test;
 import main.Service;
 import org.junit.Test;
 
+import java.io.IOException;
+import java.util.Scanner;
+
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 
@@ -31,7 +34,7 @@ import static org.hamcrest.Matchers.is;
 public class MyIT
 {
     @Test
-    public void test()
+    public void test() throws Exception
     {
         Service service = new Service();
         String moduleName = service.getClass().getModule().getName();
@@ -41,5 +44,70 @@ public class MyIT
         moduleName = getClass().getModule().getName();
         System.out.println( getClass() + " in the module \"" + moduleName + "\"" );
         assertThat( moduleName, is( "test" ) );
+
+        System.out.println( service.getNormalResource() );
+        assertThat( service.getNormalResource(), is( "Hi there!" ) );
+
+        System.out.println( service.getResourceByJPMS() );
+        assertThat( service.getResourceByJPMS(), is( "Hi there!" ) );
+
+        System.out.println( getNormalResource() );
+        assertThat( getNormalResource(), is( "Hello!" ) );
+
+        System.out.println( getResourceByJPMS() );
+        assertThat( getResourceByJPMS(), is( "Hello!" ) );
+
+        Module main = ModuleLayer.boot()
+            .modules()
+            .stream()
+            .filter( m -> hasResource( m, "main/a.txt" ) )
+            .findFirst()
+            .get();
+        assertThat( getResourceByModule( main, "main/a.txt" ), is( "Hi there!" ) );
+        assertThat( getMainResource(), is( "Hi there!" ) );
+    }
+
+    private String getNormalResource()
+    {
+        try ( Scanner scanner = new Scanner( getClass().getResourceAsStream( "/tests/a.txt" ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
+
+    private String getResourceByJPMS() throws IOException
+    {
+        try ( Scanner scanner = new Scanner( getClass().getModule().getResourceAsStream( "tests/a.txt" ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
+
+    private String getResourceByModule( Module module, String resource ) throws IOException
+    {
+        try ( Scanner scanner = new Scanner( module.getResourceAsStream( resource ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
+
+    private String getMainResource()
+    {
+        try ( Scanner scanner = new Scanner( Service.class.getResourceAsStream( "/main/a.txt" ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
+
+    private static boolean hasResource( Module module, String resource )
+    {
+        try
+        {
+            return module.getResourceAsStream( resource ) != null;
+        }
+        catch ( IOException e )
+        {
+            return false;
+        }
     }
 }
diff --git a/surefire-its/src/test/resources/surefire-1733-junit4/src/test/java/test/MyTest.java b/surefire-its/src/test/resources/surefire-1733-junit4/src/test/java/test/MyTest.java
index c839350..28c190d 100644
--- a/surefire-its/src/test/resources/surefire-1733-junit4/src/test/java/test/MyTest.java
+++ b/surefire-its/src/test/resources/surefire-1733-junit4/src/test/java/test/MyTest.java
@@ -22,6 +22,9 @@ package test;
 import main.Service;
 import org.junit.Test;
 
+import java.io.IOException;
+import java.util.Scanner;
+
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
 
@@ -31,7 +34,7 @@ import static org.hamcrest.Matchers.is;
 public class MyTest
 {
     @Test
-    public void test()
+    public void test() throws Exception
     {
         Service service = new Service();
         String moduleName = service.getClass().getModule().getName();
@@ -41,5 +44,70 @@ public class MyTest
         moduleName = getClass().getModule().getName();
         System.out.println( getClass() + " in the module \"" + moduleName + "\"" );
         assertThat( moduleName, is( "test" ) );
+
+        System.out.println( service.getNormalResource() );
+        assertThat( service.getNormalResource(), is( "Hi there!" ) );
+
+        System.out.println( service.getResourceByJPMS() );
+        assertThat( service.getResourceByJPMS(), is( "Hi there!" ) );
+
+        System.out.println( getNormalResource() );
+        assertThat( getNormalResource(), is( "Hello!" ) );
+
+        System.out.println( getResourceByJPMS() );
+        assertThat( getResourceByJPMS(), is( "Hello!" ) );
+
+        Module main = ModuleLayer.boot()
+            .modules()
+            .stream()
+            .filter( m -> hasResource( m, "main/a.txt" ) )
+            .findFirst()
+            .get();
+        assertThat( getResourceByModule( main, "main/a.txt" ), is( "Hi there!" ) );
+        assertThat( getMainResource(), is( "Hi there!" ) );
+    }
+
+    private String getNormalResource()
+    {
+        try ( Scanner scanner = new Scanner( getClass().getResourceAsStream( "/tests/a.txt" ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
+
+    private String getResourceByJPMS() throws IOException
+    {
+        try ( Scanner scanner = new Scanner( getClass().getModule().getResourceAsStream( "tests/a.txt" ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
+
+    private String getResourceByModule( Module module, String resource ) throws IOException
+    {
+        try ( Scanner scanner = new Scanner( module.getResourceAsStream( resource ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
+
+    private String getMainResource()
+    {
+        try ( Scanner scanner = new Scanner( Service.class.getResourceAsStream( "/main/a.txt" ) ) )
+        {
+            return scanner.nextLine();
+        }
+    }
+
+    private static boolean hasResource( Module module, String resource )
+    {
+        try
+        {
+            return module.getResourceAsStream( resource ) != null;
+        }
+        catch ( IOException e )
+        {
+            return false;
+        }
     }
 }
diff --git a/surefire-its/src/test/resources/surefire-1733-junit4/src/test/resources/tests/a.txt b/surefire-its/src/test/resources/surefire-1733-junit4/src/test/resources/tests/a.txt
new file mode 100644
index 0000000..05a682b
--- /dev/null
+++ b/surefire-its/src/test/resources/surefire-1733-junit4/src/test/resources/tests/a.txt
@@ -0,0 +1 @@
+Hello!
\ No newline at end of file