< prev index next >

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

Print this page




   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.jlink/jdk.tools.jmod
  29  *          jdk.compiler

  30  * @build ModuleReaderTest CompilerUtils JarUtils
  31  * @run testng ModuleReaderTest
  32  * @summary Basic tests for java.lang.module.ModuleReader
  33  */
  34 
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.io.InputStream;
  38 import java.lang.module.ModuleFinder;
  39 import java.lang.module.ModuleReader;
  40 import java.lang.module.ModuleReference;
  41 import java.lang.reflect.Module;
  42 import java.net.URI;
  43 import java.net.URL;
  44 import java.net.URLConnection;
  45 import java.nio.ByteBuffer;
  46 import java.nio.file.Files;
  47 import java.nio.file.Path;
  48 import java.nio.file.Paths;
  49 import java.util.Arrays;
  50 import java.util.Optional;

  51 
  52 import jdk.internal.module.ConfigurableModuleFinder;
  53 import jdk.internal.module.ConfigurableModuleFinder.Phase;
  54 
  55 import org.testng.annotations.BeforeTest;
  56 import org.testng.annotations.Test;
  57 import static org.testng.Assert.*;
  58 
  59 @Test
  60 public class ModuleReaderTest {
  61 
  62     private static final String TEST_SRC = System.getProperty("test.src");
  63 
  64     private static final Path USER_DIR   = Paths.get(System.getProperty("user.dir"));
  65     private static final Path SRC_DIR    = Paths.get(TEST_SRC, "src");
  66     private static final Path MODS_DIR   = Paths.get("mods");
  67 
  68     // the module name of the base module
  69     private static final String BASE_MODULE = "java.base";
  70 


 179         Path dir = Files.createTempDirectory(USER_DIR, "mlib");
 180 
 181         // jar cf mlib/${TESTMODULE}.jar -C mods .
 182         JarUtils.createJarFile(dir.resolve("m.jar"),
 183                                MODS_DIR.resolve(TEST_MODULE));
 184 
 185         test(dir);
 186     }
 187 
 188 
 189     /**
 190      * Test ModuleReader to JMOD
 191      */
 192     public void testJMod() throws Exception {
 193         Path dir = Files.createTempDirectory(USER_DIR, "mlib");
 194 
 195         // jmod create --class-path mods/${TESTMODULE}  mlib/${TESTMODULE}.jmod
 196         String cp = MODS_DIR.resolve(TEST_MODULE).toString();
 197         String jmod = dir.resolve("m.jmod").toString();
 198         String[] args = { "create", "--class-path", cp, jmod };
 199         jdk.tools.jmod.JmodTask task = new jdk.tools.jmod.JmodTask();
 200         assertEquals(task.run(args), 0);
 201 
 202         test(dir);
 203     }
 204 
 205 
 206     /**
 207      * The test module is found on the given module path. Open a ModuleReader
 208      * to the test module and test the reader.
 209      */
 210     void test(Path mp) throws Exception {
 211 
 212         ModuleFinder finder = ModuleFinder.of(mp);
 213         if (finder instanceof ConfigurableModuleFinder) {
 214             // need ModuleFinder to be in the phase to find JMOD files
 215             ((ConfigurableModuleFinder)finder).configurePhase(Phase.LINK_TIME);
 216         }
 217 
 218         ModuleReference mref = finder.find(TEST_MODULE).get();
 219         ModuleReader reader = mref.open();
 220 




   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  *          jdk.jlink
  30  * @build ModuleReaderTest CompilerUtils JarUtils
  31  * @run testng ModuleReaderTest
  32  * @summary Basic tests for java.lang.module.ModuleReader
  33  */
  34 
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.io.InputStream;
  38 import java.lang.module.ModuleFinder;
  39 import java.lang.module.ModuleReader;
  40 import java.lang.module.ModuleReference;
  41 import java.lang.reflect.Module;
  42 import java.net.URI;
  43 import java.net.URL;
  44 import java.net.URLConnection;
  45 import java.nio.ByteBuffer;
  46 import java.nio.file.Files;
  47 import java.nio.file.Path;
  48 import java.nio.file.Paths;
  49 import java.util.Arrays;
  50 import java.util.Optional;
  51 import java.util.spi.ToolProvider;
  52 
  53 import jdk.internal.module.ConfigurableModuleFinder;
  54 import jdk.internal.module.ConfigurableModuleFinder.Phase;
  55 
  56 import org.testng.annotations.BeforeTest;
  57 import org.testng.annotations.Test;
  58 import static org.testng.Assert.*;
  59 
  60 @Test
  61 public class ModuleReaderTest {
  62 
  63     private static final String TEST_SRC = System.getProperty("test.src");
  64 
  65     private static final Path USER_DIR   = Paths.get(System.getProperty("user.dir"));
  66     private static final Path SRC_DIR    = Paths.get(TEST_SRC, "src");
  67     private static final Path MODS_DIR   = Paths.get("mods");
  68 
  69     // the module name of the base module
  70     private static final String BASE_MODULE = "java.base";
  71 


 180         Path dir = Files.createTempDirectory(USER_DIR, "mlib");
 181 
 182         // jar cf mlib/${TESTMODULE}.jar -C mods .
 183         JarUtils.createJarFile(dir.resolve("m.jar"),
 184                                MODS_DIR.resolve(TEST_MODULE));
 185 
 186         test(dir);
 187     }
 188 
 189 
 190     /**
 191      * Test ModuleReader to JMOD
 192      */
 193     public void testJMod() throws Exception {
 194         Path dir = Files.createTempDirectory(USER_DIR, "mlib");
 195 
 196         // jmod create --class-path mods/${TESTMODULE}  mlib/${TESTMODULE}.jmod
 197         String cp = MODS_DIR.resolve(TEST_MODULE).toString();
 198         String jmod = dir.resolve("m.jmod").toString();
 199         String[] args = { "create", "--class-path", cp, jmod };
 200         ToolProvider jmodTool = ToolProvider.findFirst("jmod").get();
 201         assertEquals(jmodTool.run(System.out, System.out, args), 0);
 202 
 203         test(dir);
 204     }
 205 
 206 
 207     /**
 208      * The test module is found on the given module path. Open a ModuleReader
 209      * to the test module and test the reader.
 210      */
 211     void test(Path mp) throws Exception {
 212 
 213         ModuleFinder finder = ModuleFinder.of(mp);
 214         if (finder instanceof ConfigurableModuleFinder) {
 215             // need ModuleFinder to be in the phase to find JMOD files
 216             ((ConfigurableModuleFinder)finder).configurePhase(Phase.LINK_TIME);
 217         }
 218 
 219         ModuleReference mref = finder.find(TEST_MODULE).get();
 220         ModuleReader reader = mref.open();
 221 


< prev index next >