test/org/openjdk/jigsaw/cli/ModuleFormatTestLeftOverBytes.java

Print this page

        

@@ -57,20 +57,23 @@
 
     void testEmptyModule() throws Exception {
         System.err.println("Test: Empty module");
         count++;
         reset();
-        List<File> files = new ArrayList<File>();
+        List<File> files = new ArrayList<>();
         addFile(files, createFile("module-info.java", moduleinfo));
         compile(files);
         compress(MNAME);
         append(MNAME, MVER, "These bytes are not made for parsing!");
         try {
             extract(MNAME, MVER);
-        } catch (IllegalArgumentException e) {
-            /* swallow expected IllegalArgumentException */
-            if (! e.getMessage().startsWith("No SectionType"))
+        } catch (Exception e) {
+            // ## We don't need to be so strict here about the specific message
+            /* expected ModuleFileParserException wrapped by a CommandException */
+            Throwable cause = e.getCause();
+            if (!(cause instanceof ModuleFileParserException) ||
+                ! cause.getCause().getMessage().startsWith("No SectionType"))
                 throw e;
         }
     }
 
     void test() throws Exception {

@@ -89,15 +92,15 @@
     /**
      * Append some content at the end of a module file.
      */
     void append(String name, String version, String content) throws Exception {
         String fname = moduleDir + File.separator + name + "@" + version + ".jmod";
-        RandomAccessFile module = new RandomAccessFile(fname, "rw");
+        try (RandomAccessFile module = new RandomAccessFile(fname, "rw")) {
         module.seek(module.length());
         module.writeUTF(content);
-        module.close();
     }
+    }
 
     /**
      * Compress a module.
      */
     void compress(String name) throws Exception {

@@ -115,11 +118,11 @@
     }
 
     void compress(String name, boolean haveNatLibs,
                   boolean haveNatCmds, boolean haveConfig)
         throws Exception {
-        List<String> args = new ArrayList<String>();
+        List<String> args = new ArrayList<>();
         args.add("-m");
         args.add(classesDir.getAbsolutePath());
         args.add("-d");
         args.add(moduleDir.getAbsolutePath());
         if (haveNatLibs) {

@@ -141,20 +144,21 @@
 
     /**
      * Compile a list of files.
      */
     void compile(List<File> files) {
-        List<String> options = new ArrayList<String>();
-        options.addAll(Arrays.asList("-source", "7", "-d", classesDir.getPath()));
+        List<String> options = new ArrayList<>();
+        options.addAll(Arrays.asList("-source", "8", "-d", classesDir.getPath()));
         for (File f: files)
             options.add(f.getPath());
 
         String[] opts = options.toArray(new String[options.size()]);
         StringWriter sw = new StringWriter();
-        PrintWriter pw = new PrintWriter(sw);
-        int rc = com.sun.tools.javac.Main.compile(opts, pw);
-        pw.close();
+        int rc;
+        try (PrintWriter pw = new PrintWriter(sw)) {
+            rc = com.sun.tools.javac.Main.compile(opts, pw);
+        }
 
         String out = sw.toString();
         if (out.trim().length() > 0)
             System.err.println(out);
         if (rc != 0)

@@ -176,13 +180,13 @@
     File createFile(String path, String body) throws IOException {
         if (body == null)
             return null;
         File file = new File(srcDir, path);
         file.getAbsoluteFile().getParentFile().mkdirs();
-        FileWriter out = new FileWriter(file);
+        try (FileWriter out = new FileWriter(file)) {
         out.write(body);
-        out.close();
+        }
         return file;
     }
 
     /**
      * Set up empty src and classes directories for a test.