103 configDir.createNewFile();
104
105 try {
106 compress(natlib, natcmd, config);
107 }
108 // The bug resulted in a NPE being thrown
109 catch (NullPointerException e) {
110 // Rethrow the NPE if it ever occurs again.
111 throw (Exception) new Exception().initCause(e);
112 }
113 // Technically, we want to catch Command.Exception here,
114 // but it's package private, so let's catch the next best thing.
115 catch (Exception e) {
116 // yay! test passed.
117 }
118 }
119
120 private void testIfFileArgIsNotReadable(boolean natlib,
121 boolean natcmd, boolean config)
122 throws Exception {
123 setUp("NPE if file argument is not readable: "
124 + (natlib? " --natlib " : "")
125 + (natcmd? " --natcmd " : "")
126 + (config? " --config" : ""));
127
128 // Create directories and mark then non-readable to get the exception
129 if (! (natlibDir.mkdir() && natlibDir.setReadable(false) &&
130 natcmdDir.mkdir() && natcmdDir.setReadable(false) &&
131 configDir.mkdir() && configDir.setReadable(false)))
132 throw new Exception("Can't set up test");
133
134 try {
135 compress(natlib, natcmd, config);
136 }
137 // The bug resulted in a NPE being thrown
138 catch (NullPointerException e) {
139 // Rethrow the NPE if it ever occurs again.
140 throw (Exception) new Exception().initCause(e);
141 }
142 // Technically, we want to catch Command.Exception here,
153
154 private void testIfFileArgIsEmpty(boolean natlib,
155 boolean natcmd, boolean config)
156 throws Exception {
157 setUp("IOException if file argument is an empty directory: "
158 + (natlib? " --natlib " : "")
159 + (natcmd? " --natcmd " : "")
160 + (config? " --config" : ""));
161
162 // Create empty directories for jpkg to ignore
163 if (! (natlibDir.mkdir() && natcmdDir.mkdir() && configDir.mkdir()))
164 throw new Exception("Can't set up test");
165
166 compress(natlib, natcmd, config);
167 }
168
169 private void testIfModulePathArgIsNotADirectory()
170 throws Exception {
171 setUp("Check if module path argument is not a directory");
172
173 File aFile = new File("tmp", "aFile");
174 aFile.createNewFile();
175 try {
176 String [] args = {"-m", aFile.toString(), "jmod", "hello"};
177 Packager.run(args);
178 }
179 // The bug resulted in a NPE being thrown
180 catch (NullPointerException e) {
181 // Rethrow the NPE if it ever occurs again.
182 throw (Exception) new Exception().initCause(e);
183 }
184 // Technically, we want to catch Command.Exception here,
185 // but it's package private, so let's catch the next best thing.
186 catch (Exception e) {
187 // yay! test passed.
188 return;
189 }
190 finally {
191 aFile.delete();
192 }
193 throw new Exception("Should have caught an exception");
200 try {
201 String [] args = {"-m", "no such path", "jmod", "hello"};
202 Packager.run(args);
203 }
204 // The bug resulted in a NPE being thrown
205 catch (NullPointerException e) {
206 // Rethrow the NPE if it ever occurs again.
207 throw (Exception) new Exception().initCause(e);
208 }
209 // Technically, we want to catch Command.Exception here,
210 // but it's package private, so let's catch the next best thing.
211 catch (Exception e) {
212 // yay! test passed.
213 return;
214 }
215 throw new Exception("Should have caught an exception");
216 }
217
218 private void testIfModulePathArgIsNotReadable()
219 throws Exception {
220 setUp("Check if module path argument is not readable");
221
222 File dir = new File("tmp", "notReadableDir");
223 if (! (dir.mkdir() && dir.setReadable(false)))
224 throw new Exception("Can't set up test");
225
226 try {
227 String [] args = {"-m", dir.toString(), "jmod", "hello"};
228 Packager.run(args);
229 }
230 // The bug resulted in a NPE being thrown
231 catch (NullPointerException e) {
232 // Rethrow the NPE if it ever occurs again.
233 throw (Exception) new Exception().initCause(e);
234 }
235 // Technically, we want to catch Command.Exception here,
236 // but it's package private, so let's catch the next best thing.
237 catch (Exception e) {
238 // yay! test passed.
239 return;
240 }
241 finally {
242 dir.setReadable(true);
243 dir.delete();
244 }
245 throw new Exception("Should have caught an exception");
403 private boolean deleteAll(File file) {
404 if (file.isDirectory()) {
405 for (File f: file.listFiles())
406 deleteAll(f);
407 }
408 return file.delete();
409 }
410
411 /**
412 * Report an error.
413 */
414 private void error(String msg, String... more) {
415 System.err.println("error: " + msg);
416 for (String s: more)
417 System.err.println(s);
418 errors++;
419 }
420
421 private int count;
422 private int errors;
423 // use "tmp" to help avoid accidents
424 private File srcDir = new File("tmp", "src");
425 private File classesDir = new File("tmp", "classes");
426 private File moduleDir = new File("tmp", "modules");
427 private File natlibDir = new File(srcDir, "natlib");
428 private File natcmdDir = new File(srcDir, "natcmd");
429 private File configDir = new File(srcDir, "config");
430 }
|
103 configDir.createNewFile();
104
105 try {
106 compress(natlib, natcmd, config);
107 }
108 // The bug resulted in a NPE being thrown
109 catch (NullPointerException e) {
110 // Rethrow the NPE if it ever occurs again.
111 throw (Exception) new Exception().initCause(e);
112 }
113 // Technically, we want to catch Command.Exception here,
114 // but it's package private, so let's catch the next best thing.
115 catch (Exception e) {
116 // yay! test passed.
117 }
118 }
119
120 private void testIfFileArgIsNotReadable(boolean natlib,
121 boolean natcmd, boolean config)
122 throws Exception {
123 // File readability cannot be set to false in Windows
124 if (System.getProperty("os.name").startsWith("Windows")) {
125 return;
126 }
127
128 setUp("NPE if file argument is not readable: "
129 + (natlib? " --natlib " : "")
130 + (natcmd? " --natcmd " : "")
131 + (config? " --config" : ""));
132
133 // Create directories and mark then non-readable to get the exception
134 if (! (natlibDir.mkdir() && natlibDir.setReadable(false) &&
135 natcmdDir.mkdir() && natcmdDir.setReadable(false) &&
136 configDir.mkdir() && configDir.setReadable(false)))
137 throw new Exception("Can't set up test");
138
139 try {
140 compress(natlib, natcmd, config);
141 }
142 // The bug resulted in a NPE being thrown
143 catch (NullPointerException e) {
144 // Rethrow the NPE if it ever occurs again.
145 throw (Exception) new Exception().initCause(e);
146 }
147 // Technically, we want to catch Command.Exception here,
158
159 private void testIfFileArgIsEmpty(boolean natlib,
160 boolean natcmd, boolean config)
161 throws Exception {
162 setUp("IOException if file argument is an empty directory: "
163 + (natlib? " --natlib " : "")
164 + (natcmd? " --natcmd " : "")
165 + (config? " --config" : ""));
166
167 // Create empty directories for jpkg to ignore
168 if (! (natlibDir.mkdir() && natcmdDir.mkdir() && configDir.mkdir()))
169 throw new Exception("Can't set up test");
170
171 compress(natlib, natcmd, config);
172 }
173
174 private void testIfModulePathArgIsNotADirectory()
175 throws Exception {
176 setUp("Check if module path argument is not a directory");
177
178 File aFile = new File(testDir, "aFile");
179 aFile.createNewFile();
180 try {
181 String [] args = {"-m", aFile.toString(), "jmod", "hello"};
182 Packager.run(args);
183 }
184 // The bug resulted in a NPE being thrown
185 catch (NullPointerException e) {
186 // Rethrow the NPE if it ever occurs again.
187 throw (Exception) new Exception().initCause(e);
188 }
189 // Technically, we want to catch Command.Exception here,
190 // but it's package private, so let's catch the next best thing.
191 catch (Exception e) {
192 // yay! test passed.
193 return;
194 }
195 finally {
196 aFile.delete();
197 }
198 throw new Exception("Should have caught an exception");
205 try {
206 String [] args = {"-m", "no such path", "jmod", "hello"};
207 Packager.run(args);
208 }
209 // The bug resulted in a NPE being thrown
210 catch (NullPointerException e) {
211 // Rethrow the NPE if it ever occurs again.
212 throw (Exception) new Exception().initCause(e);
213 }
214 // Technically, we want to catch Command.Exception here,
215 // but it's package private, so let's catch the next best thing.
216 catch (Exception e) {
217 // yay! test passed.
218 return;
219 }
220 throw new Exception("Should have caught an exception");
221 }
222
223 private void testIfModulePathArgIsNotReadable()
224 throws Exception {
225 // File readability cannot be set to false in Windows
226 if (System.getProperty("os.name").startsWith("Windows")) {
227 return;
228 }
229 setUp("Check if module path argument is not readable");
230
231 File dir = new File(testDir, "notReadableDir");
232 if (! (dir.mkdir() && dir.setReadable(false)))
233 throw new Exception("Can't set up test");
234 try {
235 String [] args = {"-m", dir.toString(), "jmod", "hello"};
236 Packager.run(args);
237 }
238 // The bug resulted in a NPE being thrown
239 catch (NullPointerException e) {
240 // Rethrow the NPE if it ever occurs again.
241 throw (Exception) new Exception().initCause(e);
242 }
243 // Technically, we want to catch Command.Exception here,
244 // but it's package private, so let's catch the next best thing.
245 catch (Exception e) {
246 // yay! test passed.
247 return;
248 }
249 finally {
250 dir.setReadable(true);
251 dir.delete();
252 }
253 throw new Exception("Should have caught an exception");
411 private boolean deleteAll(File file) {
412 if (file.isDirectory()) {
413 for (File f: file.listFiles())
414 deleteAll(f);
415 }
416 return file.delete();
417 }
418
419 /**
420 * Report an error.
421 */
422 private void error(String msg, String... more) {
423 System.err.println("error: " + msg);
424 for (String s: more)
425 System.err.println(s);
426 errors++;
427 }
428
429 private int count;
430 private int errors;
431 private File testDir = new File(System.getProperty("test.dir", "tmp"));
432 private File srcDir = new File(testDir, "src");
433 private File classesDir = new File(testDir, "classes");
434 private File moduleDir = new File(testDir, "modules");
435 private File natlibDir = new File(srcDir, "natlib");
436 private File natcmdDir = new File(srcDir, "natcmd");
437 private File configDir = new File(srcDir, "config");
438 }
|