1 32 33 package edu.rice.cs.drjava.model.compiler; 34 35 import edu.rice.cs.drjava.DrJava; 36 import edu.rice.cs.drjava.DrJavaTestCase; 37 import edu.rice.cs.util.classloader.LimitingClassLoader; 38 import junit.framework.Test; 39 import junit.framework.TestSuite; 40 41 import java.io.File ; 42 import java.util.List ; 43 44 49 public final class CompilerRegistryTest extends DrJavaTestCase { 50 private static final CompilerRegistry _registry = CompilerRegistry.ONLY; 51 private static final String [][] _defaultCompilers = CompilerRegistry.DEFAULT_COMPILERS; 52 53 private static final CompilerInterface[] _allAvailableCompilers = _registry.getAvailableCompilers(); 54 55 56 private ClassLoader _oldBaseLoader; 57 58 61 public CompilerRegistryTest(String name) { super(name); } 62 63 66 public static Test suite() { return new TestSuite(CompilerRegistryTest.class); } 67 68 69 public void setUp() throws Exception { 70 super.setUp(); 71 _oldBaseLoader = _registry.getBaseClassLoader(); 72 _registry.setActiveCompiler(NoCompilerAvailable.ONLY); 73 } 74 75 public void tearDown() throws Exception { 76 _registry.setBaseClassLoader(_oldBaseLoader); 77 super.tearDown(); 78 } 79 80 81 public void testAtLeastOneCompiler() { 82 assertTrue("At least one version " + CompilerProxy.VERSION + " should be available", 83 _allAvailableCompilers.length > 0); 84 } 85 86 89 public void testLimitOneByOne() { 90 for (int i = 0; i < _allAvailableCompilers.length; i++) { 91 _getCompilersAfterDisablingOne(i); 93 } 95 } 96 97 98 public void testLimitAllAtOnce() { 99 LimitingClassLoader loader = new LimitingClassLoader(_oldBaseLoader); 100 _registry.setBaseClassLoader(loader); 101 102 for (int i = 0; i < _defaultCompilers.length; i++) { 103 for (int j = 0; j < _defaultCompilers[i].length; j++) { 104 loader.addToRestrictedList(_defaultCompilers[i][j]); 105 } 106 } 107 108 CompilerInterface[] compilers = _registry.getAvailableCompilers(); 109 assertEquals("Number of available compilers should be 1 because all real compilers are restricted.", 1, 110 compilers.length); 111 112 assertEquals("Only available compiler should be NoCompilerAvailable.ONLY", NoCompilerAvailable.ONLY, 113 compilers[0]); 114 115 assertEquals("Active compiler", NoCompilerAvailable.ONLY, _registry.getActiveCompiler()); 116 } 117 118 119 public void testAvailableCompilerSeenByDrJava() { 120 assertEquals("DrJava.java should have an available copmiler", 121 _registry.getActiveCompiler() != NoCompilerAvailable.ONLY, 122 DrJava.hasAvailableCompiler()); 123 } 124 125 126 public void testActiveCompilerAllAvailable() { 127 CompilerInterface[] compilers = _registry.getAvailableCompilers(); 128 129 assertEquals("active compiler before any setActive", compilers[0], _registry.getActiveCompiler()); 130 131 for (int i = 0; i < compilers.length; i++) { 132 _registry.setActiveCompiler(compilers[i]); 133 assertEquals("active compiler after setActive", compilers[i], _registry.getActiveCompiler()); 134 } 135 } 136 137 142 private CompilerInterface[] _getCompilersAfterDisablingOne(int i) { 143 return _getCompilersAfterDisablingSome(new int[] { i }); 144 } 145 146 150 private CompilerInterface[] _getCompilersAfterDisablingSome(int[] indices) { 151 LimitingClassLoader loader = new LimitingClassLoader(_oldBaseLoader); 152 _registry.setBaseClassLoader(loader); 153 154 for (int i = 0; i < indices.length; i++) { 155 loader.addToRestrictedList(_allAvailableCompilers[indices[i]].getClass().getName()); 157 } 158 159 CompilerInterface[] compilers = _registry.getAvailableCompilers(); 160 161 int indicesIndex = 0; 162 163 for (int j = 0; j < _allAvailableCompilers.length; j++) { 164 if ((indicesIndex < indices.length) && (j == indices[indicesIndex])) { 165 indicesIndex++; 167 continue; 168 } 169 170 int indexInAvailable = j - indicesIndex; 172 173 assertEquals("Class of available compiler #" + indexInAvailable, 174 _allAvailableCompilers[j].getClass().getName(), 175 compilers[indexInAvailable].getClass().getName()); 176 } 177 178 return compilers; 179 } 180 181 182 public void testCannotSetCompilerToNull() { 183 try { 184 _registry.setActiveCompiler(null); 185 fail("Setting active compiler to null should have caused an exception!"); 186 } 187 catch (IllegalArgumentException e) { 188 } 190 } 191 192 static class Without implements CompilerInterface { 193 public boolean testField = false; 194 public Without() 195 { 196 testField = true; 197 } 198 199 public List <? extends CompilerError> compile(List <? extends File > files, List <? extends File > classPath, 200 List <? extends File > sourcePath, File destination, 201 List <? extends File > bootClassPath, String sourceVersion, boolean showWarnings) { 202 return null; 203 } 204 public String getName() { return "Without"; } 205 public boolean isAvailable() { return false; } 206 public String toString() { return "Without"; } 207 } 208 209 212 public void testCreateCompiler() { 213 try { _registry.createCompiler(Without.class); } 214 catch(Throwable e) { 215 e.printStackTrace(); 216 fail("testCreateCompiler: Unexpected Exception for class without ONLY field\n" + e); 217 } 218 219 try { _registry.createCompiler(JavacFromClassPath.ONLY.getClass()); 220 _registry.createCompiler(JavacFromToolsJar.ONLY.getClass()); 221 } 222 catch(Throwable e2) { 223 fail("testCreateCompiler: Unexpected Exception for class with ONLY field\n" + e2); 224 } 225 } 226 } 227 | Popular Tags |