5ea14a51f0b5da16836cd4785e7f9ac8099ca89a
[phpeclipse.git] /
1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.core.tests.util;
12
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.ArrayList;
16
17 import junit.framework.Test;
18 import junit.framework.TestCase;
19 import junit.framework.TestSuite;
20
21 //import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
22 //import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
23
24 public class AbstractCompilerTest extends TestCase {
25
26         public static final String COMPLIANCE_1_3 = "1.3";
27         public static final String COMPLIANCE_1_4 = "1.4";
28         public static final String COMPLIANCE_1_5 = "1.5";
29
30         public static final int F_1_3 = 0x1;
31         public static final int F_1_4 = 0x2;
32         public static final int F_1_5 = 0x4;
33
34         private static int possibleComplianceLevels = -1;
35
36         protected String complianceLevel;
37
38         /*
39          * Returns the possible compliance levels this VM instance can run.
40          */
41         public static int getPossibleComplianceLevels() {
42                 if (possibleComplianceLevels == -1) {
43                         String compliance = System.getProperty("compliance");
44                         if (compliance != null) {
45                                 if (COMPLIANCE_1_3.equals(compliance)) {
46                                         possibleComplianceLevels = F_1_3;
47                                 } else if (COMPLIANCE_1_4.equals(compliance)) {
48                                         possibleComplianceLevels = F_1_4;
49                                 } else if (COMPLIANCE_1_5.equals(compliance)) {
50                                         possibleComplianceLevels = F_1_5;
51                                 } else {
52                                         System.out.println("Invalid compliance specified (" + compliance + ")");
53                                         System.out.println("Use one of " + COMPLIANCE_1_3 + ", " + COMPLIANCE_1_4 + ", " + COMPLIANCE_1_5);
54                                         System.out.println("Defaulting to all possible compliances");
55                                 }
56                         }
57                         if (possibleComplianceLevels == -1) {
58                                 possibleComplianceLevels = F_1_3;
59                                 String specVersion = System.getProperty("java.specification.version");
60                                 boolean canRun1_4 = !"1.0".equals(specVersion) && !"1.1".equals(specVersion) && !"1.2".equals(specVersion) && !"1.3".equals(specVersion);
61                                 if (canRun1_4) {
62                                         possibleComplianceLevels |= F_1_4;
63                                 }
64                                 boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
65                                 if (canRun1_5) {
66                                         possibleComplianceLevels |= F_1_5;
67                                 }
68                         }
69                 }
70                 return possibleComplianceLevels;
71         }
72
73         /*
74          * Returns a test suite including the tests defined by the given classes for all possible complianceLevels
75          * and using the given setup class (CompilerTestSetup or a subclass)
76          */
77         public static Test suite(String suiteName, Class setupClass, ArrayList testClasses) {
78                 TestSuite all = new TestSuite(suiteName);
79                 int complianceLevels = AbstractCompilerTest.getPossibleComplianceLevels();
80                 if ((complianceLevels & AbstractCompilerTest.F_1_3) != 0) {
81                         all.addTest(suiteForComplianceLevel(COMPLIANCE_1_3, setupClass, testClasses));
82                 }
83                 if ((complianceLevels & AbstractCompilerTest.F_1_4) != 0) {
84                         all.addTest(suiteForComplianceLevel(COMPLIANCE_1_4, setupClass, testClasses));
85                 }
86                 if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) {
87                         all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses));
88                 }
89                 return all;
90         }
91
92         /*
93          * Returns a test suite including the tests defined by the given classes for the given complianceLevel 
94          * (see AbstractCompilerTest for valid values) and using the given setup class (CompilerTestSetup or a subclass)
95          */
96         public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
97                 TestSuite suite;
98                 if (testClasses.size() == 1) {
99                         suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
100                 } else {
101                         suite = new TestSuite(complianceLevel);
102                         for (int i = 0, length = testClasses.size(); i < length; i++) {
103                                 Class testClass = (Class)testClasses.get(i);
104                                 TestSuite innerSuite = new TestSuite(testClass);
105                                 suite.addTest(innerSuite);
106                         }
107                 }
108
109                 // call the setup constructor with the suite and compliance level
110                 try {
111                         Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
112                         Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
113                         return setUp;
114                 } catch (IllegalAccessException e) {
115                         e.printStackTrace();
116                 } catch (InstantiationException e) {
117                         e.printStackTrace();
118                 } catch (InvocationTargetException e) {
119                         e.getTargetException().printStackTrace();
120                 } catch (NoSuchMethodException e) {
121                         e.printStackTrace();
122                 }
123
124                 return null;
125         }
126
127         public AbstractCompilerTest(String name) {
128                 super(name);
129         }
130
131 //      protected Map getCompilerOptions() {
132 //              Map options = new CompilerOptions().getMap();
133 //              if (COMPLIANCE_1_3.equals(this.complianceLevel)) {
134 //                      options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_3);
135 //                      options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_3);
136 //                      options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_1);
137 //              } else if (COMPLIANCE_1_4.equals(this.complianceLevel)) {
138 //                      options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_4);
139 //                      options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_4);
140 //                      options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_4);
141 //              } else if (COMPLIANCE_1_5.equals(this.complianceLevel)) {
142 //                      options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);
143 //                      options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
144 //                      options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5);
145 //              }
146 //              return options;
147 //      }
148
149         public String getName() {
150                 String name = super.getName();
151                 if (this.complianceLevel != null) {
152                         name = this.complianceLevel + " - " + name;
153                 }
154                 return name;
155         }
156
157 //      public void initialize(CompilerTestSetup setUp) {
158 //              this.complianceLevel = setUp.complianceLevel;
159 //      }
160 }