< prev index next >

jdk/test/tools/jmod/JmodNegativeTest.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 jdk.jlink/jdk.tools.jmod
  28  *          jdk.compiler
  29  * @build jdk.testlibrary.FileUtils CompilerUtils
  30  * @run testng JmodNegativeTest
  31  * @summary Negative tests for jmod
  32  */
  33 
  34 import java.io.*;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.Arrays;
  39 import java.util.List;
  40 import java.util.function.Consumer;
  41 import java.util.function.Supplier;

  42 import java.util.zip.ZipOutputStream;
  43 import jdk.testlibrary.FileUtils;
  44 import org.testng.annotations.BeforeTest;
  45 import org.testng.annotations.DataProvider;
  46 import org.testng.annotations.Test;
  47 
  48 import static java.io.File.pathSeparator;
  49 import static java.nio.charset.StandardCharsets.UTF_8;
  50 import static org.testng.Assert.assertTrue;
  51 
  52 public class JmodNegativeTest {
  53 





  54     static final String TEST_SRC = System.getProperty("test.src", ".");
  55     static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  56     static final Path EXPLODED_DIR = Paths.get("build");
  57     static final Path MODS_DIR = Paths.get("jmods");
  58 
  59     @BeforeTest
  60     public void buildExplodedModules() throws IOException {
  61         if (Files.exists(EXPLODED_DIR))
  62             FileUtils.deleteFileTreeWithRetry(EXPLODED_DIR);
  63 
  64         for (String name : new String[] { "foo"/*, "bar", "baz"*/ } ) {
  65             Path dir = EXPLODED_DIR.resolve(name);
  66             assertTrue(compileModule(name, dir.resolve("classes")));
  67         }
  68 
  69         if (Files.exists(MODS_DIR))
  70             FileUtils.deleteFileTreeWithRetry(MODS_DIR);
  71         Files.createDirectories(MODS_DIR);
  72     }
  73 


 498     }
 499 
 500     // ---
 501 
 502     static boolean compileModule(String name, Path dest) throws IOException {
 503         return CompilerUtils.compile(SRC_DIR.resolve(name), dest);
 504     }
 505 
 506     static void assertContains(String output, String subString) {
 507         if (output.contains(subString))
 508             assertTrue(true);
 509         else
 510             assertTrue(false,"Expected to find [" + subString + "], in output ["
 511                              + output + "]");
 512     }
 513 
 514     static JmodResult jmod(String... args) {
 515         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 516         PrintStream ps = new PrintStream(baos);
 517         System.out.println("jmod " + Arrays.asList(args));
 518         int ec = jdk.tools.jmod.Main.run(args, ps);
 519         return new JmodResult(ec, new String(baos.toByteArray(), UTF_8));
 520     }
 521 
 522     static class JmodResult {
 523         final int exitCode;
 524         final String output;
 525 
 526         JmodResult(int exitValue, String output) {
 527             this.exitCode = exitValue;
 528             this.output = output;
 529         }
 530         JmodResult assertFailure() { assertTrue(exitCode != 0, output); return this; }
 531         JmodResult resultChecker(Consumer<JmodResult> r) { r.accept(this); return this; }
 532     }
 533 }


   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 jdk.compiler
  28  *          jdk.jlink
  29  * @build jdk.testlibrary.FileUtils CompilerUtils
  30  * @run testng JmodNegativeTest
  31  * @summary Negative tests for jmod
  32  */
  33 
  34 import java.io.*;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.Arrays;
  39 import java.util.List;
  40 import java.util.function.Consumer;
  41 import java.util.function.Supplier;
  42 import java.util.spi.ToolProvider;
  43 import java.util.zip.ZipOutputStream;
  44 import jdk.testlibrary.FileUtils;
  45 import org.testng.annotations.BeforeTest;
  46 import org.testng.annotations.DataProvider;
  47 import org.testng.annotations.Test;
  48 
  49 import static java.io.File.pathSeparator;
  50 import static java.nio.charset.StandardCharsets.UTF_8;
  51 import static org.testng.Assert.assertTrue;
  52 
  53 public class JmodNegativeTest {
  54 
  55     static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
  56         .orElseThrow(() ->
  57             new RuntimeException("jmod tool not found")
  58         );
  59 
  60     static final String TEST_SRC = System.getProperty("test.src", ".");
  61     static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  62     static final Path EXPLODED_DIR = Paths.get("build");
  63     static final Path MODS_DIR = Paths.get("jmods");
  64 
  65     @BeforeTest
  66     public void buildExplodedModules() throws IOException {
  67         if (Files.exists(EXPLODED_DIR))
  68             FileUtils.deleteFileTreeWithRetry(EXPLODED_DIR);
  69 
  70         for (String name : new String[] { "foo"/*, "bar", "baz"*/ } ) {
  71             Path dir = EXPLODED_DIR.resolve(name);
  72             assertTrue(compileModule(name, dir.resolve("classes")));
  73         }
  74 
  75         if (Files.exists(MODS_DIR))
  76             FileUtils.deleteFileTreeWithRetry(MODS_DIR);
  77         Files.createDirectories(MODS_DIR);
  78     }
  79 


 504     }
 505 
 506     // ---
 507 
 508     static boolean compileModule(String name, Path dest) throws IOException {
 509         return CompilerUtils.compile(SRC_DIR.resolve(name), dest);
 510     }
 511 
 512     static void assertContains(String output, String subString) {
 513         if (output.contains(subString))
 514             assertTrue(true);
 515         else
 516             assertTrue(false,"Expected to find [" + subString + "], in output ["
 517                              + output + "]");
 518     }
 519 
 520     static JmodResult jmod(String... args) {
 521         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 522         PrintStream ps = new PrintStream(baos);
 523         System.out.println("jmod " + Arrays.asList(args));
 524         int ec = JMOD_TOOL.run(ps, ps, args);
 525         return new JmodResult(ec, new String(baos.toByteArray(), UTF_8));
 526     }
 527 
 528     static class JmodResult {
 529         final int exitCode;
 530         final String output;
 531 
 532         JmodResult(int exitValue, String output) {
 533             this.exitCode = exitValue;
 534             this.output = output;
 535         }
 536         JmodResult assertFailure() { assertTrue(exitCode != 0, output); return this; }
 537         JmodResult resultChecker(Consumer<JmodResult> r) { r.accept(this); return this; }
 538     }
 539 }
< prev index next >