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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.core.tests.util;
13 import java.lang.reflect.Constructor;
14 import java.lang.reflect.InvocationTargetException;
15 import java.util.ArrayList;
17 import junit.framework.Test;
18 import junit.framework.TestCase;
19 import junit.framework.TestSuite;
21 //import net.sourceforge.phpdt.core.tests.junit.extension.TestCase;
22 //import net.sourceforge.phpdt.internal.compiler.impl.CompilerOptions;
24 public class AbstractCompilerTest extends TestCase {
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";
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;
34 private static int possibleComplianceLevels = -1;
36 protected String complianceLevel;
39 * Returns the possible compliance levels this VM instance can run.
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;
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");
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);
62 possibleComplianceLevels |= F_1_4;
64 boolean canRun1_5 = canRun1_4 && !"1.4".equals(specVersion);
66 possibleComplianceLevels |= F_1_5;
70 return possibleComplianceLevels;
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)
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));
83 if ((complianceLevels & AbstractCompilerTest.F_1_4) != 0) {
84 all.addTest(suiteForComplianceLevel(COMPLIANCE_1_4, setupClass, testClasses));
86 if ((complianceLevels & AbstractCompilerTest.F_1_5) != 0) {
87 all.addTest(suiteForComplianceLevel(COMPLIANCE_1_5, setupClass, testClasses));
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)
96 public static Test suiteForComplianceLevel(String complianceLevel, Class setupClass, ArrayList testClasses) {
98 if (testClasses.size() == 1) {
99 suite = new TestSuite((Class)testClasses.get(0), complianceLevel);
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);
109 // call the setup constructor with the suite and compliance level
111 Constructor constructor = setupClass.getConstructor(new Class[]{Test.class, String.class});
112 Test setUp = (Test)constructor.newInstance(new Object[]{suite, complianceLevel});
114 } catch (IllegalAccessException e) {
116 } catch (InstantiationException e) {
118 } catch (InvocationTargetException e) {
119 e.getTargetException().printStackTrace();
120 } catch (NoSuchMethodException e) {
127 public AbstractCompilerTest(String name) {
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);
149 public String getName() {
150 String name = super.getName();
151 if (this.complianceLevel != null) {
152 name = this.complianceLevel + " - " + name;
157 // public void initialize(CompilerTestSetup setUp) {
158 // this.complianceLevel = setUp.complianceLevel;