|
| 1 | +/*BEGIN_COPYRIGHT_BLOCK |
| 2 | + * |
| 3 | + * This file is part of DrJava. Download the current version of this project: |
| 4 | + * http://sourceforge.net/projects/drjava/ or http://www.drjava.org/ |
| 5 | + * |
| 6 | + * DrJava Open Source License |
| 7 | + * |
| 8 | + * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu) |
| 9 | + * All rights reserved. |
| 10 | + * |
| 11 | + * Developed by: Java Programming Languages Team |
| 12 | + * Rice University |
| 13 | + * http://www.cs.rice.edu/~javaplt/ |
| 14 | + * |
| 15 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 16 | + * copy of this software and associated documentation files (the "Software"), |
| 17 | + * to deal with the Software without restriction, including without |
| 18 | + * limitation the rights to use, copy, modify, merge, publish, distribute, |
| 19 | + * sublicense, and/or sell copies of the Software, and to permit persons to |
| 20 | + * whom the Software is furnished to do so, subject to the following |
| 21 | + * conditions: |
| 22 | + * |
| 23 | + * - Redistributions of source code must retain the above copyright |
| 24 | + * notice, this list of conditions and the following disclaimers. |
| 25 | + * - Redistributions in binary form must reproduce the above copyright |
| 26 | + * notice, this list of conditions and the following disclaimers in the |
| 27 | + * documentation and/or other materials provided with the distribution. |
| 28 | + * - Neither the names of DrJava, the JavaPLT, Rice University, nor the |
| 29 | + * names of its contributors may be used to endorse or promote products |
| 30 | + * derived from this Software without specific prior written permission. |
| 31 | + * - Products derived from this software may not be called "DrJava" nor |
| 32 | + * use the term "DrJava" as part of their names without prior written |
| 33 | + * permission from the JavaPLT group. For permission, write to |
| 34 | + * javaplt@rice.edu. |
| 35 | + * |
| 36 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 37 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 38 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 39 | + * THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 40 | + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 41 | + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 42 | + * OTHER DEALINGS WITH THE SOFTWARE. |
| 43 | + * |
| 44 | +END_COPYRIGHT_BLOCK*/ |
| 45 | + |
| 46 | +package edu.rice.cs.javalanglevels; |
| 47 | +import edu.rice.cs.javalanglevels.parser.*; |
| 48 | +import edu.rice.cs.javalanglevels.tree.*; |
| 49 | +import junit.framework.TestCase; |
| 50 | +import java.util.*; |
| 51 | +import java.io.*; |
| 52 | + |
| 53 | +/** |
| 54 | + * This is a high-level test to make sure that taking an Advanced Level file from |
| 55 | + * source file to augmented file has the correct behavior, does not throw errors when |
| 56 | + * it should not, throws errors when it should, and results in the correct augmented code. |
| 57 | + * Files that should be successfully tested are placed in the testFiles/forAdvancedLevelTest folder |
| 58 | + * as .dj2 files, and the expected augmented files asre also placed in the testFiles/forAdvancedLevelTest |
| 59 | + * folder with the same name, but a .expected extension. Files that are expected to generate errors are |
| 60 | + * placed in the testFiles/forAdvancedLevelTest/shouldBreak folder, as .dj2 files. |
| 61 | + * Other subdirectories are used for other tests. |
| 62 | + */public class AdvancedLevelTest extends TestCase { |
| 63 | + File directory; |
| 64 | + |
| 65 | + //Iterable _iterable; |
| 66 | + //Appendable _appendable; |
| 67 | + //EnumConstantNotPresentException _ecnp; |
| 68 | + //ProcessBuilder _pb; |
| 69 | + |
| 70 | + public void setUp() { |
| 71 | + directory = new File("testFiles/forAdvancedLevelTest"); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Try some example files and make sure they can be converted without errors and that the resulting conversions are correct. |
| 76 | + */ |
| 77 | + public void testSuccessful() { |
| 78 | + File[] testFiles = directory.listFiles(new FileFilter() { |
| 79 | + public boolean accept(File pathName) { |
| 80 | + return pathName.getAbsolutePath().endsWith(".dj2"); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + |
| 85 | + LanguageLevelConverter llc = new LanguageLevelConverter("1.5"); |
| 86 | + Pair<LinkedList<JExprParseException>, LinkedList<Pair<String, JExpressionIF>>> result; |
| 87 | + result = llc.convert(testFiles); |
| 88 | + |
| 89 | + assertEquals("should be no parse exceptions", new LinkedList<JExprParseException>(), result.getFirst()); |
| 90 | + assertEquals("should be no visitor exceptions", new LinkedList<Pair<String, JExpressionIF>>(), result.getSecond()); |
| 91 | + |
| 92 | + |
| 93 | + /**Now make sure that the resulting java files are correct.*/ |
| 94 | + for(int i = 0; i < testFiles.length; i++) { |
| 95 | + File currFile = testFiles[i]; |
| 96 | + String fileName = currFile.getAbsolutePath(); |
| 97 | + fileName = fileName.substring(0, fileName.length() -4); |
| 98 | + File resultingFile = new File(fileName + ".java"); |
| 99 | + File correctFile = new File(fileName + ".expected"); |
| 100 | + |
| 101 | + if (correctFile.exists()) { |
| 102 | + try { |
| 103 | + assertEquals("File " + currFile.getName() + " should have been parsed and augmented correctly.", |
| 104 | + readFileAsString(correctFile), |
| 105 | + readFileAsString(resultingFile)); |
| 106 | + } |
| 107 | + catch (IOException ioe) { |
| 108 | + fail(ioe.getMessage()); |
| 109 | + // let JUnit throw the exception |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + //test the subdirectory files as well. |
| 115 | + File newDirectory = new File(directory.getAbsolutePath() + System.getProperty("file.separator") + "importedFiles"); |
| 116 | + testFiles = newDirectory.listFiles(new FileFilter() { |
| 117 | + public boolean accept(File pathName) { |
| 118 | + String name = pathName.getAbsolutePath(); |
| 119 | + return name.endsWith("IsItPackageAndImport.dj1") || name.endsWith("ToReference.dj1"); |
| 120 | + }}); |
| 121 | + |
| 122 | + |
| 123 | + /**Now make sure that the resulting java files are correct.*/ |
| 124 | + for(int i = 0; i < testFiles.length; i++) { |
| 125 | + File currFile = testFiles[i]; |
| 126 | + String fileName = currFile.getAbsolutePath(); |
| 127 | + fileName = fileName.substring(0, fileName.length() -4); |
| 128 | + File resultingFile = new File(fileName + ".java"); |
| 129 | + File correctFile = new File(fileName + ".expected"); |
| 130 | + |
| 131 | + if (correctFile.exists()) { |
| 132 | + try { |
| 133 | + assertEquals("File " + currFile.getName() + " should have been parsed and augmented correctly.", |
| 134 | + readFileAsString(correctFile), |
| 135 | + readFileAsString(resultingFile)); |
| 136 | + } |
| 137 | + catch (IOException ioe) { |
| 138 | + fail(ioe.getMessage()); |
| 139 | + // let JUnit throw the exception |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + //And make sure that no java file was generated for ToReference2.dj1 |
| 145 | + //(This is testing that we correctly handled what could have been an ambiguous name reference, but wasn't) |
| 146 | + File f = new File(newDirectory, "ToReference2.java"); |
| 147 | + assertFalse("ToReference2.java should not exist", f.exists()); |
| 148 | + |
| 149 | + newDirectory = new File(directory.getAbsolutePath() + System.getProperty("file.separator") + "importedFiles2"); |
| 150 | + testFiles = newDirectory.listFiles(new FileFilter() { |
| 151 | + public boolean accept(File pathName) { |
| 152 | + return pathName.getAbsolutePath().endsWith("AlsoReferenced.dj1"); |
| 153 | + }}); |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + /**Now make sure that the resulting java files are correct.*/ |
| 158 | + for(int i = 0; i < testFiles.length; i++) { |
| 159 | + File currFile = testFiles[i]; |
| 160 | + String fileName = currFile.getAbsolutePath(); |
| 161 | + fileName = fileName.substring(0, fileName.length() -4); |
| 162 | + File resultingFile = new File(fileName + ".java"); |
| 163 | + File correctFile = new File(fileName + ".expected"); |
| 164 | + |
| 165 | + if (correctFile.exists()) { |
| 166 | + try { |
| 167 | + assertEquals("File " + currFile.getName() + " should have been parsed and augmented correctly.", |
| 168 | + readFileAsString(correctFile), |
| 169 | + readFileAsString(resultingFile)); |
| 170 | + } |
| 171 | + catch (IOException ioe) { |
| 172 | + fail(ioe.getMessage()); |
| 173 | + // let JUnit throw the exception |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + //And make sure that no java file was generated for ToReference.dj1 |
| 179 | + f = new File(newDirectory, "ToReference.java"); |
| 180 | + assertFalse("ToReference.java should not exist", f.exists()); |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Test that if a package and a class have the same name, an error is given. |
| 186 | + */ |
| 187 | + public void testPackageError() { |
| 188 | + directory = new File(directory.getAbsolutePath() + "/shouldBreak/noBreak"); |
| 189 | + File[] testFiles = directory.listFiles(new FileFilter() { |
| 190 | + public boolean accept(File pathName) { |
| 191 | + return pathName.getAbsolutePath().endsWith(".dj2"); |
| 192 | + }}); |
| 193 | + LanguageLevelConverter llc = new LanguageLevelConverter("1.5"); |
| 194 | + Pair<LinkedList<JExprParseException>, LinkedList<Pair<String, JExpressionIF>>> result; |
| 195 | + for (int i = 0; i<testFiles.length; i++) { |
| 196 | + result = llc.convert(new File[]{testFiles[i]}); |
| 197 | + assertTrue("should be parse exceptions or visitor exceptions", !result.getFirst().isEmpty() || !result.getSecond().isEmpty()); |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Test a set of files that have various Advanced Level errors. See the files themselves for a description of the errors. |
| 205 | + */ |
| 206 | + public void testShouldBeErrors() { |
| 207 | + directory = new File(directory.getAbsolutePath() + "/shouldBreak"); |
| 208 | + File[] testFiles = directory.listFiles(new FileFilter() { |
| 209 | + public boolean accept(File pathName) { |
| 210 | + return pathName.getAbsolutePath().endsWith(".dj2"); |
| 211 | + }}); |
| 212 | + |
| 213 | + LanguageLevelConverter llc = new LanguageLevelConverter("1.5"); |
| 214 | + Pair<LinkedList<JExprParseException>, LinkedList<Pair<String, JExpressionIF>>> result; |
| 215 | + for (int i = 0; i<testFiles.length; i++) { |
| 216 | + LanguageLevelVisitor._errorAdded = false; |
| 217 | + result = llc.convert(new File[]{testFiles[i]}); |
| 218 | + assertTrue("should be parse exceptions or visitor exceptions", !result.getFirst().isEmpty() || !result.getSecond().isEmpty()); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + |
| 223 | + /** |
| 224 | + * This file used to have a NullPointer Exception in it because of a bug in the code. Leave the test in so that the bug |
| 225 | + * never gets reintroduced. |
| 226 | + */ |
| 227 | + public void testNoNullPointer() { |
| 228 | + directory = new File(directory.getAbsolutePath() + "/shouldBreak"); |
| 229 | + File[] testFiles = directory.listFiles(new FileFilter() { |
| 230 | + public boolean accept(File pathName) { |
| 231 | + return pathName.getAbsolutePath().endsWith("SwitchDoesntAssign.dj2"); |
| 232 | + }}); |
| 233 | + |
| 234 | + LanguageLevelConverter llc = new LanguageLevelConverter("1.5"); |
| 235 | + Pair<LinkedList<JExprParseException>, LinkedList<Pair<String, JExpressionIF>>> result; |
| 236 | + for (int i = 0; i<testFiles.length; i++) { |
| 237 | + result = llc.convert(new File[]{testFiles[i]}); |
| 238 | + assertTrue("should be parse exceptions or visitor exceptions", !result.getFirst().isEmpty() || !result.getSecond().isEmpty()); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + |
| 243 | + /**make sure that the order packaged files are compiled in does not matter. |
| 244 | + * This is to make sure that a bug that was fixed stays fixed. |
| 245 | + */ |
| 246 | + public void testPackagedOrderMatters() { |
| 247 | + File newDirectory = new File(directory.getAbsolutePath() + System.getProperty("file.separator") + "lists-dj2" + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "listFW"); |
| 248 | + File[] testFiles = new File[]{new File(newDirectory, "NEList.dj2"), |
| 249 | + new File(newDirectory, "MTList.dj2"), |
| 250 | + new File(newDirectory, "IList.dj2")}; |
| 251 | + |
| 252 | + System.out.flush(); |
| 253 | + |
| 254 | + LanguageLevelConverter llc = new LanguageLevelConverter("1.5"); |
| 255 | + Pair<LinkedList<JExprParseException>, LinkedList<Pair<String, JExpressionIF>>> result; |
| 256 | + result = llc.convert(testFiles); |
| 257 | + |
| 258 | + assertEquals("should be no parse exceptions", new LinkedList<JExprParseException>(), result.getFirst()); |
| 259 | + |
| 260 | + assertEquals("should be no visitor exceptions", new LinkedList<Pair<String, JExpressionIF>>(), result.getSecond()); |
| 261 | + |
| 262 | + //don't worry about checking the .java files for correctness...just make sure there weren't any exceptions |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Read the entire contents of a file and return them. Copied from DrJava's FileOps. |
| 267 | + */ |
| 268 | + public static String readFileAsString(final File file) throws IOException { |
| 269 | + BufferedReader reader = new BufferedReader(new FileReader(file)); |
| 270 | + StringBuffer buf = new StringBuffer(); |
| 271 | + |
| 272 | + while (reader.ready()) { |
| 273 | + String s = reader.readLine(); |
| 274 | + buf.append(s); |
| 275 | + } |
| 276 | + |
| 277 | + reader.close(); |
| 278 | + return buf.toString(); |
| 279 | + } |
| 280 | + |
| 281 | +} |
0 commit comments