< prev index next >

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


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