1 5 package polyglot.pth; 6 7 import java.io.File ; 8 import java.util.*; 9 10 import polyglot.util.ErrorInfo; 11 import polyglot.util.SilentErrorQueue; 12 13 16 public class SourceFileTest extends AbstractTest { 17 protected final List sourceFilenames; 18 protected String extensionClassname = null; 19 protected String [] extraArgs; 20 protected final SilentErrorQueue eq; 21 protected String destDir; 22 23 protected List expectedFailures; 24 25 public SourceFileTest(String filename) { 26 super(new File (filename).getName()); 27 this.sourceFilenames = Collections.singletonList(filename); 28 this.eq = new SilentErrorQueue(100, this.getName()); 29 30 } 31 32 public SourceFileTest(List filenames) { 33 super(filenames.toString()); 34 this.sourceFilenames = filenames; 35 this.eq = new SilentErrorQueue(100, this.getName()); 36 } 37 38 public SourceFileTest(String [] filenames) { 39 this(Arrays.asList(filenames).toString()); 40 } 41 42 public void setExpectedFailures(List expectedFailures) { 43 this.expectedFailures = expectedFailures; 44 } 45 46 protected boolean runTest() { 47 for (Iterator i = sourceFilenames.iterator(); i.hasNext(); ) { 48 File sourceFile = new File ((String )i.next()); 49 if (!sourceFile.exists()) { 50 setFailureMessage("File not found."); 51 return false; 52 } 53 } 54 55 try { 57 invokePolyglot(getSourceFileNames()); 58 } 59 catch (polyglot.main.Main.TerminationException e) { 60 if (e.getMessage() != null) { 61 setFailureMessage(e.getMessage()); 62 return false; 63 } 64 else { 65 if (!eq.hasErrors()) { 66 setFailureMessage("Failed to compile for unknown reasons: " + 67 e.toString()); 68 return false; 69 } 70 } 71 } 72 return checkErrorQueue(eq); 73 } 74 75 protected void postRun() { 76 output.finishTest(this, eq); 77 } 78 79 protected boolean checkErrorQueue(SilentErrorQueue eq) { 80 List errors = new ArrayList(eq.getErrors()); 81 82 boolean swallowRemainingFailures = false; 83 for (Iterator i = expectedFailures.iterator(); i.hasNext(); ) { 84 ExpectedFailure f = (ExpectedFailure)i.next(); 85 if (f instanceof AnyExpectedFailure) { 86 swallowRemainingFailures = true; 87 continue; 88 } 89 90 boolean found = false; 91 for (Iterator j = errors.iterator(); j.hasNext(); ) { 92 ErrorInfo e =(ErrorInfo)j.next(); 93 if (f.matches(e)) { 94 found = true; 96 j.remove(); 97 break; 98 } 99 } 100 if (!found) { 101 setFailureMessage("Expected to see " + f.toString()); 102 return false; 103 } 104 } 105 106 if (!errors.isEmpty() && !swallowRemainingFailures) { 108 StringBuffer sb = new StringBuffer (); 109 for (Iterator iter = errors.iterator(); iter.hasNext(); ) { 110 ErrorInfo err = (ErrorInfo)iter.next(); 111 sb.append(err.getMessage()); 112 if (err.getPosition() != null) { 113 sb.append(" ("); 114 sb.append(err.getPosition()); 115 sb.append(")"); 116 } 117 if (iter.hasNext()) sb.append("; "); 118 } 119 setFailureMessage(sb.toString()); 120 } 121 return errors.isEmpty() || swallowRemainingFailures; 122 } 123 124 protected String [] getSourceFileNames() { 125 return (String [])sourceFilenames.toArray(new String [0]); 126 } 127 128 protected void invokePolyglot(String [] files) 129 throws polyglot.main.Main.TerminationException 130 { 131 File tmpdir = new File ("pthOutput"); 132 133 int i = 1; 134 while (tmpdir.exists()) { 135 tmpdir = new File ("pthOutput." + i); 136 i++; 137 } 138 139 tmpdir.mkdir(); 140 141 setDestDir(tmpdir.getPath()); 142 143 String [] cmdLine = buildCmdLine(files); 144 polyglot.main.Main polyglotMain = new polyglot.main.Main(); 145 146 try { 147 polyglotMain.start(cmdLine, eq); 148 } 149 finally { 150 if (Main.options.deleteOutputFiles) { 151 deleteDir(tmpdir); 152 } 153 154 setDestDir(null); 155 } 156 } 157 158 protected void deleteDir(File dir) { 159 File [] list = dir.listFiles(); 160 for (int i = 0; i < list.length; i++) { 161 if (list[i].isDirectory()) { 162 deleteDir(list[i]); 163 } 164 else { 165 list[i].delete(); 166 } 167 } 168 dir.delete(); 169 } 170 171 protected String [] buildCmdLine(String [] files) { 172 ArrayList args = new ArrayList(); 173 174 String s; 175 String [] sa; 176 177 if ((s = getExtensionClassname()) != null) { 178 args.add("-extclass"); 179 args.add(s); 180 } 181 182 if ((s = getAdditionalClasspath()) != null) { 183 args.add("-cp"); 184 args.add(s); 185 } 186 187 if ((s = getDestDir()) != null) { 188 args.add("-d"); 189 args.add(s); 190 } 191 192 if ((s = getSourceDir()) != null) { 193 args.add("-sourcepath"); 194 args.add(s); 195 } 196 197 if ((s = Main.options.extraArgs) != null) { 198 sa = breakString(Main.options.extraArgs); 199 for (int i = 0; i < sa.length; i++) { 200 args.add(sa[i]); 201 } 202 } 203 204 if ((sa = getExtraCmdLineArgs()) != null) { 205 for (int i = 0; i < sa.length; i++) { 206 args.add(sa[i]); 207 } 208 } 209 210 args.addAll(Arrays.asList(files)); 211 212 return (String [])args.toArray(new String [0]); 213 } 214 215 protected String getExtensionClassname() { 216 return extensionClassname; 217 } 218 219 protected void setExtensionClassname(String extClassname) { 220 this.extensionClassname = extClassname; 221 } 222 223 protected String [] getExtraCmdLineArgs() { 224 return this.extraArgs; 225 } 226 227 protected static String [] breakString(String s) { 228 StringTokenizer st = new StringTokenizer(s); 229 ArrayList l = new ArrayList(st.countTokens()); 230 while (st.hasMoreTokens()) { 231 l.add(st.nextToken()); 232 } 233 234 return (String [])l.toArray(new String [l.size()]); 235 } 236 protected void setExtraCmdLineArgs(String args) { 237 if (args != null) { 238 this.extraArgs = breakString(args); 239 } 240 } 241 protected String getAdditionalClasspath() { 242 return Main.options.classpath; 243 } 244 protected void setDestDir(String dir) { 245 this.destDir = dir; 246 } 247 protected String getDestDir() { 248 return destDir; 249 } 250 protected String getSourceDir() { 251 return null; 252 } 253 254 } 255 | Popular Tags |