< prev index next >

test/java/lang/module/ModuleReader/ModuleReaderTest.java

Print this page




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @library /lib/testlibrary
  27  * @modules java.base/jdk.internal.misc
  28  *          jdk.compiler
  29  * @build ModuleReaderTest CompilerUtils JarUtils
  30  * @run testng ModuleReaderTest
  31  * @summary Basic tests for java.lang.module.ModuleReader
  32  */
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.lang.module.ModuleFinder;
  38 import java.lang.module.ModuleReader;
  39 import java.lang.module.ModuleReference;
  40 import java.lang.reflect.Module;
  41 import java.net.URI;
  42 import java.net.URL;
  43 import java.net.URLConnection;
  44 import java.nio.ByteBuffer;
  45 import java.nio.file.Files;
  46 import java.nio.file.Path;
  47 import java.nio.file.Paths;
  48 import java.util.Arrays;
  49 import java.util.HashSet;
  50 import java.util.List;
  51 import java.util.Optional;
  52 import java.util.Set;
  53 import java.util.stream.Collectors;
  54 import java.util.spi.ToolProvider;
  55 
  56 import jdk.internal.misc.SharedSecrets;
  57 
  58 import org.testng.annotations.BeforeTest;
  59 import org.testng.annotations.Test;
  60 import static org.testng.Assert.*;
  61 
  62 @Test
  63 public class ModuleReaderTest {
  64 
  65     private static final String TEST_SRC = System.getProperty("test.src");
  66 
  67     private static final Path USER_DIR   = Paths.get(System.getProperty("user.dir"));
  68     private static final Path SRC_DIR    = Paths.get(TEST_SRC, "src");
  69     private static final Path MODS_DIR   = Paths.get("mods");
  70 
  71     // the module name of the base module
  72     private static final String BASE_MODULE = "java.base";
  73 
  74     // the module name of the test module
  75     private static final String TEST_MODULE = "m";
  76 


 199         // jmod create --class-path mods/${TESTMODULE}  mlib/${TESTMODULE}.jmod
 200         String cp = MODS_DIR.resolve(TEST_MODULE).toString();
 201         String jmod = dir.resolve("m.jmod").toString();
 202         String[] args = { "create", "--class-path", cp, jmod };
 203         ToolProvider jmodTool = ToolProvider.findFirst("jmod")
 204             .orElseThrow(() ->
 205                 new RuntimeException("jmod tool not found")
 206             );
 207         assertEquals(jmodTool.run(System.out, System.out, args), 0);
 208 
 209         test(dir);
 210     }
 211 
 212 
 213     /**
 214      * The test module is found on the given module path. Open a ModuleReader
 215      * to the test module and test the reader.
 216      */
 217     void test(Path mp) throws IOException {
 218 
 219         ModuleFinder finder = SharedSecrets.getJavaLangModuleAccess()
 220             .newModulePath(Runtime.version(), true, mp);
 221 
 222         ModuleReference mref = finder.find(TEST_MODULE).get();
 223         ModuleReader reader = mref.open();
 224 
 225         try (reader) {
 226 
 227             // test each of the known resources in the module
 228             for (String name : TEST_RESOURCES) {
 229                 byte[] expectedBytes
 230                     = Files.readAllBytes(MODS_DIR
 231                         .resolve(TEST_MODULE)
 232                         .resolve(name.replace('/', File.separatorChar)));
 233 
 234                 testFind(reader, name, expectedBytes);
 235                 testOpen(reader, name, expectedBytes);
 236                 testRead(reader, name, expectedBytes);
 237                 testList(reader, name);
 238             }
 239 
 240             // test "not found"
 241             assertFalse(reader.find(NOT_A_RESOURCE).isPresent());




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @library /lib/testlibrary
  27  * @modules java.base/jdk.internal.module
  28  *          jdk.compiler
  29  * @build ModuleReaderTest CompilerUtils JarUtils
  30  * @run testng ModuleReaderTest
  31  * @summary Basic tests for java.lang.module.ModuleReader
  32  */
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.io.InputStream;
  37 import java.lang.module.ModuleFinder;
  38 import java.lang.module.ModuleReader;
  39 import java.lang.module.ModuleReference;
  40 import java.lang.reflect.Module;
  41 import java.net.URI;
  42 import java.net.URL;
  43 import java.net.URLConnection;
  44 import java.nio.ByteBuffer;
  45 import java.nio.file.Files;
  46 import java.nio.file.Path;
  47 import java.nio.file.Paths;
  48 import java.util.Arrays;
  49 import java.util.HashSet;
  50 import java.util.List;
  51 import java.util.Optional;
  52 import java.util.Set;
  53 import java.util.stream.Collectors;
  54 import java.util.spi.ToolProvider;
  55 
  56 import jdk.internal.module.ModulePath;
  57 
  58 import org.testng.annotations.BeforeTest;
  59 import org.testng.annotations.Test;
  60 import static org.testng.Assert.*;
  61 
  62 @Test
  63 public class ModuleReaderTest {
  64 
  65     private static final String TEST_SRC = System.getProperty("test.src");
  66 
  67     private static final Path USER_DIR   = Paths.get(System.getProperty("user.dir"));
  68     private static final Path SRC_DIR    = Paths.get(TEST_SRC, "src");
  69     private static final Path MODS_DIR   = Paths.get("mods");
  70 
  71     // the module name of the base module
  72     private static final String BASE_MODULE = "java.base";
  73 
  74     // the module name of the test module
  75     private static final String TEST_MODULE = "m";
  76 


 199         // jmod create --class-path mods/${TESTMODULE}  mlib/${TESTMODULE}.jmod
 200         String cp = MODS_DIR.resolve(TEST_MODULE).toString();
 201         String jmod = dir.resolve("m.jmod").toString();
 202         String[] args = { "create", "--class-path", cp, jmod };
 203         ToolProvider jmodTool = ToolProvider.findFirst("jmod")
 204             .orElseThrow(() ->
 205                 new RuntimeException("jmod tool not found")
 206             );
 207         assertEquals(jmodTool.run(System.out, System.out, args), 0);
 208 
 209         test(dir);
 210     }
 211 
 212 
 213     /**
 214      * The test module is found on the given module path. Open a ModuleReader
 215      * to the test module and test the reader.
 216      */
 217     void test(Path mp) throws IOException {
 218 
 219         ModuleFinder finder = new ModulePath(Runtime.version(), true, mp);


 220         ModuleReference mref = finder.find(TEST_MODULE).get();
 221         ModuleReader reader = mref.open();
 222 
 223         try (reader) {
 224 
 225             // test each of the known resources in the module
 226             for (String name : TEST_RESOURCES) {
 227                 byte[] expectedBytes
 228                     = Files.readAllBytes(MODS_DIR
 229                         .resolve(TEST_MODULE)
 230                         .resolve(name.replace('/', File.separatorChar)));
 231 
 232                 testFind(reader, name, expectedBytes);
 233                 testOpen(reader, name, expectedBytes);
 234                 testRead(reader, name, expectedBytes);
 235                 testList(reader, name);
 236             }
 237 
 238             // test "not found"
 239             assertFalse(reader.find(NOT_A_RESOURCE).isPresent());


< prev index next >