KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > compiler > CompilerRegistryTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  * END_COPYRIGHT_BLOCK*/

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 JavaDoc;
42 import java.util.List JavaDoc;
43
44 /** Test cases for {@link CompilerRegistry}. Here we test that the compiler registry correctly finds
45  * available compilers.
46  *
47  * @version $Id: CompilerRegistryTest.java 4044 2006-11-23 13:40:43Z dlsmith $
48  */

49 public final class CompilerRegistryTest extends DrJavaTestCase {
50   private static final CompilerRegistry _registry = CompilerRegistry.ONLY;
51   private static final String JavaDoc[][] _defaultCompilers = CompilerRegistry.DEFAULT_COMPILERS;
52
53   private static final CompilerInterface[] _allAvailableCompilers = _registry.getAvailableCompilers();
54
55   /** Stores the old state of {@link CompilerRegistry#getBaseClassLoader}, so it can be reset later. */
56   private ClassLoader JavaDoc _oldBaseLoader;
57
58   /** Constructor.
59    * @param name
60    */

61   public CompilerRegistryTest(String JavaDoc name) { super(name); }
62
63   /** Creates a test suite for JUnit to run.
64    * @return a test suite based on the methods in this class
65    */

66   public static Test suite() { return new TestSuite(CompilerRegistryTest.class); }
67
68   /** Test setup method, which saves the old base class loader. */
69   public void setUp() throws Exception JavaDoc {
70     super.setUp();
71     _oldBaseLoader = _registry.getBaseClassLoader();
72     _registry.setActiveCompiler(NoCompilerAvailable.ONLY);
73   }
74
75   public void tearDown() throws Exception JavaDoc {
76     _registry.setBaseClassLoader(_oldBaseLoader);
77     super.tearDown();
78   }
79
80   /** Confirm that one of the compiler interfaces corresponding CompilerProxy.VERSION can be loaded. */
81   public void testAtLeastOneCompiler() {
82     assertTrue("At least one version " + CompilerProxy.VERSION + " should be available",
83                  _allAvailableCompilers.length > 0);
84   }
85
86   /** Tests that list of available compilers effectively is restricted when the class is not available.
87    * Here this is done by limiting the available compilers one at a time.
88    */

89   public void testLimitOneByOne() {
90     for (int i = 0; i < _allAvailableCompilers.length; i++) {
91       //CompilerInterface[] compilers =
92
_getCompilersAfterDisablingOne(i);
93       // That method includes all the tests we need!
94
}
95   }
96
97   /** Tests that list of available compilers effectively is restricted when all default compilers are not available. */
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   /** Tests that DrJava.java can see whether CompilerRegistry has an available compiler. */
119   public void testAvailableCompilerSeenByDrJava() {
120     assertEquals("DrJava.java should have an available copmiler",
121                  _registry.getActiveCompiler() != NoCompilerAvailable.ONLY,
122                  DrJava.hasAvailableCompiler());
123   }
124
125   /** Tests that {@link CompilerRegistry#setActiveCompiler} and {@link CompilerRegistry#getActiveCompiler} work. */
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   /** Returns the list of available compilers after disabling one of them. This method includes checks for the
138    * correctness of the list after disabling one.
139    *
140    * @param i Index of default compiler to disable.
141    */

142   private CompilerInterface[] _getCompilersAfterDisablingOne(int i) {
143     return _getCompilersAfterDisablingSome(new int[] { i });
144   }
145
146   /** Returns the list of available compilers after disabling some of them. This method includes checks for the
147    * correctness of the list after disabling them.
148    * @param indices Array of ints signifying which of the default compilers to disable.
149    */

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       //System.out.println("restricting compiler: " + _allAvailableCompilers[indices[i]].getClass().getName());
156
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         // this is an index to skip.
166
indicesIndex++;
167         continue;
168       }
169
170       // Now indicesIndex is at the number of indices to skip!
171
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   /** Ensure that the active compiler in the registry cannot be set to null. */
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 JavaDoc e) {
188       // Good-- exception was thrown.
189
}
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 JavaDoc<? extends CompilerError> compile(List JavaDoc<? extends File JavaDoc> files, List JavaDoc<? extends File JavaDoc> classPath,
200                                                  List JavaDoc<? extends File JavaDoc> sourcePath, File JavaDoc destination,
201                                                  List JavaDoc<? extends File JavaDoc> bootClassPath, String JavaDoc sourceVersion, boolean showWarnings) {
202       return null;
203     }
204      public String JavaDoc getName() { return "Without"; }
205      public boolean isAvailable() { return false; }
206      public String JavaDoc toString() { return "Without"; }
207   }
208
209   /** Test that createCompiler() does successfully instantiate compilers that do not have the ONLY static field,
210    * and those that do have it.
211    */

212    public void testCreateCompiler() {
213      try { _registry.createCompiler(Without.class); }
214      catch(Throwable JavaDoc 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 JavaDoc e2) {
223         fail("testCreateCompiler: Unexpected Exception for class with ONLY field\n" + e2);
224      }
225   }
226 }
227
Popular Tags