42 43 void run() throws Exception { 44 try { 45 test(); 46 } catch (Throwable t) { 47 t.printStackTrace(); 48 errors++; 49 } 50 51 52 if (errors == 0) 53 System.out.println(count + " tests passed"); 54 else 55 throw new Exception(errors + "/" + count + " tests failed"); 56 } 57 58 void testEmptyModule() throws Exception { 59 System.err.println("Test: Empty module"); 60 count++; 61 reset(); 62 List<File> files = new ArrayList<File>(); 63 addFile(files, createFile("module-info.java", moduleinfo)); 64 compile(files); 65 compress(MNAME); 66 append(MNAME, MVER, "These bytes are not made for parsing!"); 67 try { 68 extract(MNAME, MVER); 69 } catch (IllegalArgumentException e) { 70 /* swallow expected IllegalArgumentException */ 71 if (! e.getMessage().startsWith("No SectionType")) 72 throw e; 73 } 74 } 75 76 void test() throws Exception { 77 testEmptyModule(); 78 } 79 80 /** 81 * Extract a module. 82 */ 83 void extract(String name, String version) throws Exception { 84 File module = new File(moduleDir, name + "@" + version + ".jmod"); 85 String [] args = {"extract", module.getAbsolutePath()}; 86 Librarian.run(args); 87 } 88 89 /** 90 * Append some content at the end of a module file. 91 */ 92 void append(String name, String version, String content) throws Exception { 93 String fname = moduleDir + File.separator + name + "@" + version + ".jmod"; 94 RandomAccessFile module = new RandomAccessFile(fname, "rw"); 95 module.seek(module.length()); 96 module.writeUTF(content); 97 module.close(); 98 } 99 100 /** 101 * Compress a module. 102 */ 103 void compress(String name) throws Exception { 104 compress(name, false); 105 } 106 107 void compress(String name, boolean haveNatLibs) 108 throws Exception { 109 compress(name, haveNatLibs, false); 110 } 111 112 void compress(String name, boolean haveNatLibs, 113 boolean haveNatCmds) throws Exception { 114 compress(name, haveNatLibs, haveNatCmds, false); 115 } 116 117 void compress(String name, boolean haveNatLibs, 118 boolean haveNatCmds, boolean haveConfig) 119 throws Exception { 120 List<String> args = new ArrayList<String>(); 121 args.add("-m"); 122 args.add(classesDir.getAbsolutePath()); 123 args.add("-d"); 124 args.add(moduleDir.getAbsolutePath()); 125 if (haveNatLibs) { 126 args.add("--natlib"); 127 args.add(natlibDir.toString()); 128 } 129 if (haveNatCmds) { 130 args.add("--natcmd"); 131 args.add(natcmdDir.toString()); 132 } 133 if (haveConfig) { 134 args.add("--config"); 135 args.add(configDir.toString()); 136 } 137 args.add("jmod"); 138 args.add("hello"); 139 Packager.main(args.toArray(new String[0])); 140 } 141 142 /** 143 * Compile a list of files. 144 */ 145 void compile(List<File> files) { 146 List<String> options = new ArrayList<String>(); 147 options.addAll(Arrays.asList("-source", "7", "-d", classesDir.getPath())); 148 for (File f: files) 149 options.add(f.getPath()); 150 151 String[] opts = options.toArray(new String[options.size()]); 152 StringWriter sw = new StringWriter(); 153 PrintWriter pw = new PrintWriter(sw); 154 int rc = com.sun.tools.javac.Main.compile(opts, pw); 155 pw.close(); 156 157 String out = sw.toString(); 158 if (out.trim().length() > 0) 159 System.err.println(out); 160 if (rc != 0) 161 throw new Error("compilation failed: rc=" + rc); 162 } 163 164 /** 165 * Add a file to a list if the file is not null. 166 */ 167 void addFile(List<File> files, File file) { 168 if (file != null) 169 files.add(file); 170 } 171 172 173 /** 174 * Create a test file with given content if the content is not null. 175 */ 176 File createFile(String path, String body) throws IOException { 177 if (body == null) 178 return null; 179 File file = new File(srcDir, path); 180 file.getAbsoluteFile().getParentFile().mkdirs(); 181 FileWriter out = new FileWriter(file); 182 out.write(body); 183 out.close(); 184 return file; 185 } 186 187 /** 188 * Set up empty src and classes directories for a test. 189 */ 190 void reset() { 191 resetDir(srcDir); 192 resetDir(classesDir); 193 resetDir(moduleDir); 194 resetDir(new File(MNAME)); 195 } 196 197 /** 198 * Set up an empty directory. 199 */ 200 void resetDir(File dir) { 201 if (dir.exists()) 202 deleteAll(dir); 203 dir.mkdirs(); | 42 43 void run() throws Exception { 44 try { 45 test(); 46 } catch (Throwable t) { 47 t.printStackTrace(); 48 errors++; 49 } 50 51 52 if (errors == 0) 53 System.out.println(count + " tests passed"); 54 else 55 throw new Exception(errors + "/" + count + " tests failed"); 56 } 57 58 void testEmptyModule() throws Exception { 59 System.err.println("Test: Empty module"); 60 count++; 61 reset(); 62 List<File> files = new ArrayList<>(); 63 addFile(files, createFile("module-info.java", moduleinfo)); 64 compile(files); 65 compress(MNAME); 66 append(MNAME, MVER, "These bytes are not made for parsing!"); 67 try { 68 extract(MNAME, MVER); 69 } catch (Exception e) { 70 // ## We don't need to be so strict here about the specific message 71 /* expected ModuleFileParserException wrapped by a CommandException */ 72 Throwable cause = e.getCause(); 73 if (!(cause instanceof ModuleFileParserException) || 74 ! cause.getCause().getMessage().startsWith("No SectionType")) 75 throw e; 76 } 77 } 78 79 void test() throws Exception { 80 testEmptyModule(); 81 } 82 83 /** 84 * Extract a module. 85 */ 86 void extract(String name, String version) throws Exception { 87 File module = new File(moduleDir, name + "@" + version + ".jmod"); 88 String [] args = {"extract", module.getAbsolutePath()}; 89 Librarian.run(args); 90 } 91 92 /** 93 * Append some content at the end of a module file. 94 */ 95 void append(String name, String version, String content) throws Exception { 96 String fname = moduleDir + File.separator + name + "@" + version + ".jmod"; 97 try (RandomAccessFile module = new RandomAccessFile(fname, "rw")) { 98 module.seek(module.length()); 99 module.writeUTF(content); 100 } 101 } 102 103 /** 104 * Compress a module. 105 */ 106 void compress(String name) throws Exception { 107 compress(name, false); 108 } 109 110 void compress(String name, boolean haveNatLibs) 111 throws Exception { 112 compress(name, haveNatLibs, false); 113 } 114 115 void compress(String name, boolean haveNatLibs, 116 boolean haveNatCmds) throws Exception { 117 compress(name, haveNatLibs, haveNatCmds, false); 118 } 119 120 void compress(String name, boolean haveNatLibs, 121 boolean haveNatCmds, boolean haveConfig) 122 throws Exception { 123 List<String> args = new ArrayList<>(); 124 args.add("-m"); 125 args.add(classesDir.getAbsolutePath()); 126 args.add("-d"); 127 args.add(moduleDir.getAbsolutePath()); 128 if (haveNatLibs) { 129 args.add("--natlib"); 130 args.add(natlibDir.toString()); 131 } 132 if (haveNatCmds) { 133 args.add("--natcmd"); 134 args.add(natcmdDir.toString()); 135 } 136 if (haveConfig) { 137 args.add("--config"); 138 args.add(configDir.toString()); 139 } 140 args.add("jmod"); 141 args.add("hello"); 142 Packager.main(args.toArray(new String[0])); 143 } 144 145 /** 146 * Compile a list of files. 147 */ 148 void compile(List<File> files) { 149 List<String> options = new ArrayList<>(); 150 options.addAll(Arrays.asList("-source", "8", "-d", classesDir.getPath())); 151 for (File f: files) 152 options.add(f.getPath()); 153 154 String[] opts = options.toArray(new String[options.size()]); 155 StringWriter sw = new StringWriter(); 156 int rc; 157 try (PrintWriter pw = new PrintWriter(sw)) { 158 rc = com.sun.tools.javac.Main.compile(opts, pw); 159 } 160 161 String out = sw.toString(); 162 if (out.trim().length() > 0) 163 System.err.println(out); 164 if (rc != 0) 165 throw new Error("compilation failed: rc=" + rc); 166 } 167 168 /** 169 * Add a file to a list if the file is not null. 170 */ 171 void addFile(List<File> files, File file) { 172 if (file != null) 173 files.add(file); 174 } 175 176 177 /** 178 * Create a test file with given content if the content is not null. 179 */ 180 File createFile(String path, String body) throws IOException { 181 if (body == null) 182 return null; 183 File file = new File(srcDir, path); 184 file.getAbsoluteFile().getParentFile().mkdirs(); 185 try (FileWriter out = new FileWriter(file)) { 186 out.write(body); 187 } 188 return file; 189 } 190 191 /** 192 * Set up empty src and classes directories for a test. 193 */ 194 void reset() { 195 resetDir(srcDir); 196 resetDir(classesDir); 197 resetDir(moduleDir); 198 resetDir(new File(MNAME)); 199 } 200 201 /** 202 * Set up an empty directory. 203 */ 204 void resetDir(File dir) { 205 if (dir.exists()) 206 deleteAll(dir); 207 dir.mkdirs(); |