|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| package edu.rice.cs.drjava.model.junit; |
|
38 |
| |
|
39 |
| import java.io.*; |
|
40 |
| import java.util.jar.*; |
|
41 |
| import javax.swing.*; |
|
42 |
| import java.awt.*; |
|
43 |
| |
|
44 |
| import edu.rice.cs.drjava.DrJava; |
|
45 |
| import edu.rice.cs.drjava.config.OptionConstants; |
|
46 |
| import edu.rice.cs.util.FileOps; |
|
47 |
| import edu.rice.cs.plt.concurrent.JVMBuilder; |
|
48 |
| import edu.rice.cs.plt.lambda.Runnable1; |
|
49 |
| import edu.rice.cs.util.swing.SwingWorker; |
|
50 |
| import edu.rice.cs.util.swing.ProcessingDialog; |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| public class ConcJUnitUtils { |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
540
| protected static boolean isValidJarFile(final File f, String... checkFilesInJar) {
|
|
62 |
540
| if ((f==null) || (FileOps.NULL_FILE.equals(f)) || (!f.exists())) return false;
|
|
63 |
0
| JarFile jf = null;
|
|
64 |
0
| try {
|
|
65 |
0
| jf = new JarFile(f);
|
|
66 |
0
| for(String s: checkFilesInJar) {
|
|
67 |
0
| JarEntry je = jf.getJarEntry(s);
|
|
68 |
0
| if (je==null) return false;
|
|
69 |
| } |
|
70 |
0
| return true;
|
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| } |
|
84 |
0
| catch(IOException ioe) { return false; }
|
|
85 |
| finally { |
|
86 |
0
| try {
|
|
87 |
0
| if (jf != null) jf.close();
|
|
88 |
| } |
|
89 |
| catch(IOException ioe) { } |
|
90 |
| } |
|
91 |
| } |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
180
| public static boolean isValidJUnitFile(final File f) {
|
|
97 |
180
| return isValidJarFile(f,
|
|
98 |
| "junit/framework/Test.class", |
|
99 |
| "junit/runner/Version.class"); |
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
180
| public static boolean isValidConcJUnitFile(final File f) {
|
|
106 |
180
| return isValidJarFile(f,
|
|
107 |
| "junit/framework/Test.class", |
|
108 |
| "junit/runner/Version.class", |
|
109 |
| "junit/runner/ConcutestVersion.class", |
|
110 |
| "edu/rice/cs/cunit/concJUnit/ConcJUnitFileInstrumentorLauncher.class", |
|
111 |
| "edu/rice/cs/cunit/concJUnit/MultithreadedTestError.class", |
|
112 |
| "edu/rice/cs/cunit/concJUnit/ThreadSets.class"); |
|
113 |
| } |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
180
| public static boolean isValidRTConcJUnitFile(final File f) {
|
|
119 |
180
| return isValidJarFile(f,
|
|
120 |
| "java/lang/Object.class", |
|
121 |
| "java/lang/Thread.class", |
|
122 |
| "java/lang/String.class", |
|
123 |
| "edu/rice/cs/cunit/concJUnit/ThreadSets.class"); |
|
124 |
| } |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
0
| public static boolean isCompatibleRTConcJUnitFile(final File f) {
|
|
131 |
0
| if (!isValidRTConcJUnitFile(f)) return false;
|
|
132 |
0
| try {
|
|
133 |
0
| JarFile jf = new JarFile(f);
|
|
134 |
0
| Manifest mf = jf.getManifest();
|
|
135 |
0
| if (mf==null) return false;
|
|
136 |
0
| String vendor = mf.getMainAttributes().getValue("Edu-Rice-Cs-CUnit-JavaVersion-Vendor");
|
|
137 |
0
| String version = mf.getMainAttributes().getValue("Edu-Rice-Cs-CUnit-JavaVersion");
|
|
138 |
0
| if ((vendor==null) || (version==null)) return false;
|
|
139 |
0
| return (vendor.equals(edu.rice.cs.plt.reflect.JavaVersion.CURRENT_FULL.vendor().toString()) &&
|
|
140 |
| version.equals(edu.rice.cs.plt.reflect.JavaVersion.CURRENT_FULL.toString())); |
|
141 |
| } |
|
142 |
0
| catch(IOException ioe) { return false; }
|
|
143 |
| } |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
0
| public static boolean showIncompatibleWantToRegenerateDialog(final Frame parentFrame,
|
|
150 |
| final Runnable yesRunnable, |
|
151 |
| final Runnable noRunnable) { |
|
152 |
0
| Object[] options = {"Yes","No"};
|
|
153 |
0
| int n = JOptionPane.showOptionDialog(parentFrame,
|
|
154 |
| "The specified ConcJUnit runtime file is incompatible with the\n"+ |
|
155 |
| "current version of Java. Do you wish to regenerate the file?", |
|
156 |
| "Regenerate ConcJUnit Runtime", |
|
157 |
| JOptionPane.YES_NO_OPTION, |
|
158 |
| JOptionPane.QUESTION_MESSAGE, |
|
159 |
| null, |
|
160 |
| options, |
|
161 |
| options[1]); |
|
162 |
0
| if (n==0) {
|
|
163 |
| |
|
164 |
0
| File concJUnitJarFile = FileOps.getDrJavaFile();
|
|
165 |
0
| if (DrJava.getConfig().getSetting(OptionConstants.JUNIT_LOCATION_ENABLED)) {
|
|
166 |
0
| concJUnitJarFile = DrJava.getConfig().getSetting(OptionConstants.JUNIT_LOCATION);
|
|
167 |
| } |
|
168 |
0
| File rtFile = DrJava.getConfig().getSetting(OptionConstants.RT_CONCJUNIT_LOCATION);
|
|
169 |
0
| showGenerateRTConcJUnitJarFileDialog(parentFrame,
|
|
170 |
| rtFile, |
|
171 |
| concJUnitJarFile, |
|
172 |
| new Runnable1<File>() { |
|
173 |
0
| public void run(File targetFile) {
|
|
174 |
| |
|
175 |
0
| DrJava.getConfig().setSetting(OptionConstants.RT_CONCJUNIT_LOCATION, targetFile);
|
|
176 |
0
| yesRunnable.run();
|
|
177 |
| } |
|
178 |
| }, |
|
179 |
| new Runnable() { |
|
180 |
0
| public void run() {
|
|
181 |
| |
|
182 |
0
| if (DrJava.getConfig().getSetting(OptionConstants.CONCJUNIT_CHECKS_ENABLED).
|
|
183 |
| equals(OptionConstants.ConcJUnitCheckChoices.NO_LUCKY)) { |
|
184 |
0
| DrJava.getConfig().setSetting(OptionConstants.CONCJUNIT_CHECKS_ENABLED,
|
|
185 |
| OptionConstants.ConcJUnitCheckChoices.NO_LUCKY); |
|
186 |
| } |
|
187 |
0
| noRunnable.run();
|
|
188 |
| } }); |
|
189 |
0
| return true;
|
|
190 |
| } |
|
191 |
| else { |
|
192 |
| |
|
193 |
0
| if (DrJava.getConfig().getSetting(OptionConstants.CONCJUNIT_CHECKS_ENABLED).
|
|
194 |
| equals(OptionConstants.ConcJUnitCheckChoices.NO_LUCKY)) { |
|
195 |
0
| DrJava.getConfig().setSetting(OptionConstants.CONCJUNIT_CHECKS_ENABLED,
|
|
196 |
| OptionConstants.ConcJUnitCheckChoices.NO_LUCKY); |
|
197 |
| } |
|
198 |
0
| noRunnable.run();
|
|
199 |
0
| return false;
|
|
200 |
| } |
|
201 |
| } |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
0
| public static void showGenerateRTConcJUnitJarFileDialog(final Frame parentFrame,
|
|
212 |
| File rtFile, |
|
213 |
| final File concJUnitJarFile, |
|
214 |
| final Runnable1<File> successRunnable, |
|
215 |
| final Runnable failureRunnable) { |
|
216 |
0
| if ((rtFile == null) || (FileOps.NULL_FILE.equals(rtFile))) {
|
|
217 |
| |
|
218 |
0
| File drJavaFile = FileOps.getDrJavaApplicationFile();
|
|
219 |
0
| File parent = drJavaFile.getParentFile();
|
|
220 |
0
| if (parent == null) {
|
|
221 |
0
| parent = new File(System.getProperty("user.dir"));
|
|
222 |
| } |
|
223 |
0
| rtFile = new File(parent, "rt.concjunit.jar");
|
|
224 |
| } |
|
225 |
0
| JFileChooser saveChooser = new JFileChooser() {
|
|
226 |
0
| public void setCurrentDirectory(File dir) {
|
|
227 |
| |
|
228 |
0
| super.setCurrentDirectory(dir);
|
|
229 |
0
| setDialogTitle("Save: " + getCurrentDirectory());
|
|
230 |
| } |
|
231 |
| }; |
|
232 |
0
| saveChooser.setPreferredSize(new Dimension(650, 410));
|
|
233 |
0
| saveChooser.setSelectedFile(rtFile);
|
|
234 |
0
| saveChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
|
|
235 |
0
| public boolean accept(File f) {
|
|
236 |
0
| return f.isDirectory() ||
|
|
237 |
| f.getPath().endsWith(".jar"); |
|
238 |
| } |
|
239 |
0
| public String getDescription() {
|
|
240 |
0
| return "Java Archive Files (*.jar)";
|
|
241 |
| } |
|
242 |
| }); |
|
243 |
0
| saveChooser.setMultiSelectionEnabled(false);
|
|
244 |
0
| int rc = saveChooser.showSaveDialog(parentFrame);
|
|
245 |
0
| if (rc == JFileChooser.APPROVE_OPTION) {
|
|
246 |
0
| final File targetFile = saveChooser.getSelectedFile();
|
|
247 |
0
| int n = JOptionPane.YES_OPTION;
|
|
248 |
0
| if (targetFile.exists()) {
|
|
249 |
0
| Object[] options = {"Yes","No"};
|
|
250 |
0
| n = JOptionPane.showOptionDialog(parentFrame,
|
|
251 |
| "This file already exists. Do you wish to overwrite the file?", |
|
252 |
| "Confirm Overwrite", |
|
253 |
| JOptionPane.YES_NO_OPTION, |
|
254 |
| JOptionPane.QUESTION_MESSAGE, |
|
255 |
| null, |
|
256 |
| options, |
|
257 |
| options[1]); |
|
258 |
| } |
|
259 |
0
| if (n == JOptionPane.YES_OPTION) {
|
|
260 |
0
| if (parentFrame!=null) parentFrame.setEnabled(false);
|
|
261 |
0
| final ProcessingDialog processingDialog =
|
|
262 |
| new ProcessingDialog(parentFrame, "Creating ConcJUnit Runtime", "Processing, please wait."); |
|
263 |
0
| final JProgressBar pb = processingDialog.getProgressBar();
|
|
264 |
0
| processingDialog.setVisible(true);
|
|
265 |
0
| try {
|
|
266 |
0
| final File tmpDir = FileOps.createTempDirectory("DrJavaGenerateRTConcJUnitJar");
|
|
267 |
| |
|
268 |
0
| SwingWorker worker = new SwingWorker() {
|
|
269 |
| volatile Boolean _success = null; |
|
270 |
| Thread _processIncrementer = new Thread(new Runnable() { |
|
271 |
0
| public void run() {
|
|
272 |
0
| File tmpFile = new File(tmpDir, "rt.concjunit.jar");
|
|
273 |
0
| boolean indeterminate = true;
|
|
274 |
0
| try {
|
|
275 |
0
| while (_success == null) {
|
|
276 |
0
| Thread.sleep(1000);
|
|
277 |
0
| if (tmpFile.exists()) {
|
|
278 |
0
| if (indeterminate) {
|
|
279 |
0
| pb.setIndeterminate(false);
|
|
280 |
0
| indeterminate = false;
|
|
281 |
| } |
|
282 |
0
| pb.setValue((int)(100.0/(30*1024*1024)*tmpFile.length()));
|
|
283 |
| } |
|
284 |
| } |
|
285 |
| } |
|
286 |
| catch(InterruptedException ie) { |
|
287 |
0
| pb.setIndeterminate(true);
|
|
288 |
| } |
|
289 |
| } |
|
290 |
| }); |
|
291 |
0
| public Object construct() {
|
|
292 |
0
| _processIncrementer.start();
|
|
293 |
0
| _success = edu.rice.cs.drjava.model.junit.ConcJUnitUtils.
|
|
294 |
| generateRTConcJUnitJarFile(targetFile, concJUnitJarFile, tmpDir); |
|
295 |
0
| return null;
|
|
296 |
| } |
|
297 |
| |
|
298 |
0
| public void finished() {
|
|
299 |
0
| pb.setValue(100);
|
|
300 |
0
| processingDialog.setVisible(false);
|
|
301 |
0
| processingDialog.dispose();
|
|
302 |
0
| if (parentFrame!=null) parentFrame.setEnabled(true);
|
|
303 |
0
| if ((_success != null) && (_success)) {
|
|
304 |
0
| successRunnable.run(targetFile);
|
|
305 |
0
| JOptionPane.showMessageDialog(parentFrame,
|
|
306 |
| "Successfully generated ConcJUnit Runtime File:\n"+targetFile, |
|
307 |
| "Generation Successful", |
|
308 |
| JOptionPane.INFORMATION_MESSAGE); |
|
309 |
0
| edu.rice.cs.plt.io.IOUtil.deleteRecursively(tmpDir);
|
|
310 |
| } |
|
311 |
| else { |
|
312 |
0
| failureRunnable.run();
|
|
313 |
0
| JOptionPane.showMessageDialog(parentFrame,
|
|
314 |
| "Could not generate ConcJUnit Runtime File:\n"+targetFile, |
|
315 |
| "Could Not Generate", |
|
316 |
| JOptionPane.ERROR_MESSAGE); |
|
317 |
| } |
|
318 |
| } |
|
319 |
| }; |
|
320 |
0
| worker.start();
|
|
321 |
| } |
|
322 |
| catch(IOException ioe) { |
|
323 |
0
| JOptionPane.showMessageDialog(parentFrame,
|
|
324 |
| "Could not generate ConcJUnit Runtime file:\n"+targetFile, |
|
325 |
| "Could Not Generate", |
|
326 |
| JOptionPane.ERROR_MESSAGE); |
|
327 |
| } |
|
328 |
| } |
|
329 |
| } |
|
330 |
| else { |
|
331 |
0
| failureRunnable.run();
|
|
332 |
| } |
|
333 |
| } |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
| |
|
339 |
| |
|
340 |
0
| public static boolean generateRTConcJUnitJarFile(File rtFile, File concJUnitJarFile, File tmpDir) {
|
|
341 |
0
| if (!isValidConcJUnitFile(concJUnitJarFile)) return false;
|
|
342 |
| |
|
343 |
0
| try {
|
|
344 |
0
| JVMBuilder jvmb = new JVMBuilder(tmpDir).classPath(concJUnitJarFile);
|
|
345 |
0
| Process p = jvmb.start("edu.rice.cs.cunit.concJUnit.ConcJUnitFileInstrumentorLauncher", "-r");
|
|
346 |
0
| BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
|
347 |
0
| String line;
|
|
348 |
0
| while((line = br.readLine()) != null) { }
|
|
349 |
0
| try {
|
|
350 |
0
| p.waitFor();
|
|
351 |
| } |
|
352 |
0
| catch(InterruptedException ie) { return false; }
|
|
353 |
0
| if (p.exitValue() != 0) { return false; }
|
|
354 |
0
| edu.rice.cs.plt.io.IOUtil.copyFile(new File(tmpDir, "rt.concjunit.jar"), rtFile);
|
|
355 |
0
| return isValidRTConcJUnitFile(rtFile);
|
|
356 |
| } |
|
357 |
0
| catch(IOException ioe) { return false; }
|
|
358 |
| } |
|
359 |
| } |