Released parse tree creation from AST tree.
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/.classpath b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/.classpath
@@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<classpath> + <classpathentry kind="src" path="src"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> + <classpathentry kind="output" path="bin"/> +</classpath>
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/JavaProjectUtil.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/JavaProjectUtil.java index 1e631e0..0ee5cbe 100644 --- a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/JavaProjectUtil.java +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/JavaProjectUtil.java
@@ -11,7 +11,7 @@ *******************************************************************************/ /* * $RCSfile: JavaProjectUtil.java,v $ - * $Revision: 1.1 $ $Date: 2003/10/27 17:25:46 $ + * $Revision: 1.2 $ $Date: 2004/01/23 22:53:36 $ */ @@ -23,6 +23,11 @@ import org.eclipse.ant.core.AntRunner; import org.eclipse.core.resources.*; import org.eclipse.core.runtime.*; +import org.eclipse.jdt.core.*; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; + +import org.eclipse.jem.internal.proxy.core.ProxyPlugin; /** * The purpose of this class is to be a utility for manipulating/populating java projects. @@ -70,6 +75,56 @@ return project; } + + /** + * Create an empty project in the workspace root. If there is a .project file already there, then use it. + * @param workspace The workspace to create projec in. + * @param projectPath The name of the project. It should only be one segment long, and that will be the name of the project. + * @param pm + * @return The project. + * @throws CoreException + * + * @since 1.0.0 + */ + public static IProject createEmptyJavaProject(IWorkspace workspace, IPath projectPath, final IProgressMonitor pm) throws CoreException { + projectPath = workspace.getRoot().getFullPath().append(projectPath); // Put it into the workspace relative. + File projectFile = new File(projectPath.toFile(), IProjectDescription.DESCRIPTION_FILE_NAME); + if (projectFile.exists()) + return createProject(workspace, projectPath, pm); // Let it be created normally. + + // create the new project operation + final IProject project = workspace.getRoot().getProject(projectPath.lastSegment()); + ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { + public void run(IProgressMonitor monitor) throws CoreException { + project.create(new SubProgressMonitor(monitor, 1000)); + project.open(new SubProgressMonitor(monitor, 1000)); + IProjectDescription description = project.getDescription(); + String[] natureids = description.getNatureIds(); + for (int i = 0; i < natureids.length; i++) { + if (natureids[i].equals("org.eclipse.jdt.core.javanature")) + return; // Already has nature. + } + String[] newNatureids = new String[natureids.length+1]; + newNatureids[0] = "org.eclipse.jdt.core.javanature"; + System.arraycopy(natureids, 0, newNatureids, 1, natureids.length); + description.setNatureIds(newNatureids); + project.setDescription(description, new SubProgressMonitor(monitor, 1000)); + // Need to put out a classfile too. We need a src and a bin directory for the classpath. + IFolder sf = project.getFolder("src"); + sf.create(true, true, new SubProgressMonitor(monitor, 1000)); + IFolder bf = project.getFolder("bin"); + bf.create(true, true, new SubProgressMonitor(monitor, 1000)); + IFile cp = project.getFile(".classpath"); + try { + cp.create(getClass().getResource(".classpath").openStream(), true, new SubProgressMonitor(monitor, 1000)); + } catch (IOException e) { + e.printStackTrace(); + } + } + }, pm); + + return project; + } /** * Delete a project and all files from the project. @@ -146,5 +201,46 @@ return result; } + + static int cfSuffix = 0; + /** + * Add a path to plugin jar to the java project's class path. + * @param plugin The plugin where the jar is located. + * @param pathToJar Path to the jar within the above plugin + * @param project java project to add to. + * + * @since 1.0.0 + */ + public static void addPluginJarToPath(Plugin plugin, String pathToJar, final IJavaProject project, IProgressMonitor pm) throws CoreException { + final IPath actualPath = new Path(ProxyPlugin.getPlugin().localizeFromPlugin(plugin, pathToJar)); + if (actualPath.isEmpty()) + return; // Didn't exist. + final IWorkspace workspace = ResourcesPlugin.getWorkspace(); + workspace.run(new IWorkspaceRunnable() { + public void run(IProgressMonitor monitor) throws CoreException { + if (actualPath.toFile().isFile()) { + // It is a jar, this will be during runtime + // Create an external jar entry. + IClasspathEntry[] raw = project.getRawClasspath(); + IClasspathEntry[] newRaw = new IClasspathEntry[raw.length+1]; + newRaw[raw.length] = JavaCore.newLibraryEntry(actualPath, null, null); + System.arraycopy(raw, 0, newRaw, 0, raw.length); + project.setRawClasspath(newRaw, new SubProgressMonitor(monitor, 100)); + } else { + // It is a path to class folder, this will be during development time. + // But classfolders MUST exist in the workspace. JDT doesn't understand them outside workspace, + // so we will link it into the project. + IFolder cf = project.getProject().getFolder("linkbin"+(++cfSuffix)); + cf.createLink(actualPath, 0, new SubProgressMonitor(monitor, 100)); + // Create class folder entry. + IClasspathEntry[] raw = project.getRawClasspath(); + IClasspathEntry[] newRaw = new IClasspathEntry[raw.length+1]; + newRaw[raw.length] = JavaCore.newLibraryEntry(cf.getFullPath(), null, null); + System.arraycopy(raw, 0, newRaw, 0, raw.length); + project.setRawClasspath(newRaw, new SubProgressMonitor(monitor, 100)); + } + } + }, pm); + } } \ No newline at end of file
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/OverloadingTest.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/OverloadingTest.java index 4e941d1..014e97d 100644 --- a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/OverloadingTest.java +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/OverloadingTest.java
@@ -11,10 +11,11 @@ *******************************************************************************/ /* * $RCSfile: OverloadingTest.java,v $ - * $Revision: 1.2 $ $Date: 2003/10/27 17:32:36 $ + * $Revision: 1.3 $ $Date: 2004/01/23 22:53:36 $ */ /** + * Test that correct overloaded method is called. * @author jmyers * * To change the template for this generated type comment go to @@ -30,19 +31,10 @@ super(name); } - public void testShort() throws Throwable { - testHelper.testInitString("(short) 3", new Short((short) 3)); + public void testOverloadString() throws Throwable { + testHelper.testInitString("new String(\"Frog\")", "Frog"); } - public void testParen1() throws Throwable { - testHelper.testInitString("(new String(\"Frog\")).length()",new Integer(4)); - } - public void testParen2() throws Throwable { - testHelper.testInitString("((new String(\"Frog\"))).length()",new Integer(4)); - } - public void testParen2b() throws Throwable { - testHelper.testInitString("((new String(\"Frog\")).length())",new Integer(4)); - } - public void testParen3() throws Throwable { - testHelper.testInitString("(((new String(\"Frog\")).length()))",new Integer(4)); + public void testOverloadShort() throws Throwable { + testHelper.testInitString("new String((short)3)", "3"); } }
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTFieldAccessTest.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTFieldAccessTest.java new file mode 100644 index 0000000..9525f14 --- /dev/null +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTFieldAccessTest.java
@@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* + * $RCSfile: ASTFieldAccessTest.java,v $ + * $Revision: 1.1 $ $Date: 2004/01/23 22:53:36 $ + */ +package org.eclipse.jem.tests.proxy.initParser.tree; + +import java.awt.Color; + +import org.eclipse.jem.tests.proxy.initParser.AbstractInitParserTestCase; + +/** + * + * @since 1.0.0 + */ +public class ASTFieldAccessTest extends AbstractInitParserTestCase { + + /** + * Create with name. + * @param name + * + * @since 1.0.0 + */ + public ASTFieldAccessTest(String name) { + super(name); + } + + protected ASTTreeInitStringParserTestHelper getTreeParser() { + return (ASTTreeInitStringParserTestHelper) testHelper; + } + + public void testNonQualifiedFieldAccess() throws Throwable { + getTreeParser().testInitString("Color.red", new String[] {"java.awt.*"}, Color.red); + } + + public void testExpressionFieldAccess() throws Throwable { + getTreeParser().testInitString("(Color.red).red", new String[] {"java.awt.*"}, Color.red); // Should not use this form, but it is valid, use valid form for expected results so no warnings. + } + + public void testNestedFieldAccess() throws Throwable { + getTreeParser().testInitString("ASTNestFieldAccessTestData.acolor.red", new String[] {"org.eclipse.jem.tests.proxy.initParser.tree.ASTNestFieldAccessTestData"}, Color.red); // Should not use this form, but it is valid, use valid form for expected results so no warnings. + } + + public void testNestedFieldExpressionAccess() throws Throwable { + getTreeParser().testInitString("new ASTNestFieldAccessTestData().acolor.red", new String[] {"org.eclipse.jem.tests.proxy.initParser.tree.ASTNestFieldAccessTestData"}, Color.red); // Should not use this form, but it is valid, use valid form for expected results so no warnings. + } + + public void testNonstaticFieldAccess() throws Throwable { + getTreeParser().testInitString("new ASTNestFieldAccessTestData().arect", new String[] {"org.eclipse.jem.tests.proxy.initParser.tree.ASTNestFieldAccessTestData"}, new ASTNestFieldAccessTestData().arect); + } + + public void testNonstaticNestedFieldAccess() throws Throwable { + getTreeParser().testInitString("new ASTNestFieldAccessTestData().arect.x", new String[] {"org.eclipse.jem.tests.proxy.initParser.tree.ASTNestFieldAccessTestData"}, new Integer(new ASTNestFieldAccessTestData().arect.x)); + } + +}
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTInnerClassAccessTest.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTInnerClassAccessTest.java new file mode 100644 index 0000000..ae789ef --- /dev/null +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTInnerClassAccessTest.java
@@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* + * $RCSfile: ASTInnerClassAccessTest.java,v $ + * $Revision: 1.1 $ $Date: 2004/01/23 22:53:36 $ + */ +package org.eclipse.jem.tests.proxy.initParser.tree; + +import org.eclipse.jem.tests.proxy.initParser.AbstractInitParserTestCase; + +/** + * This is for testing inner class access. + * @since 1.0.0 + */ +public class ASTInnerClassAccessTest extends AbstractInitParserTestCase { + + /** + * Construct with name. + * @param name + * + * @since 1.0.0 + */ + public ASTInnerClassAccessTest(String name) { + super(name); + // TODO Auto-generated constructor stub + } + + protected ASTTreeInitStringParserTestHelper getTreeParser() { + return (ASTTreeInitStringParserTestHelper) testHelper; + } + + public void testInnerFieldAccess() throws Throwable { + getTreeParser().testInitString("InnerClassTestData.InnerInnerClass.GREEN", new String[] {"org.eclipse.jem.tests.proxy.initParser.tree.*"}, InnerClassTestData.InnerInnerClass.GREEN); + } + + public void testInnerFieldAccess2() throws Throwable { + // Test where the inner class is the top level listed. + getTreeParser().testInitString("InnerInnerClass.GREEN", new String[] {"org.eclipse.jem.tests.proxy.initParser.tree.InnerClassTestData.InnerInnerClass"}, InnerClassTestData.InnerInnerClass.GREEN); + } + + public void testInnerInnerFieldAccess() throws Throwable { + getTreeParser().testInitString("InnerClassTestData.InnerInnerClass.InnerInnerInnerClass.RED", new String[] {"org.eclipse.jem.tests.proxy.initParser.tree.*"}, InnerClassTestData.InnerInnerClass.InnerInnerInnerClass.RED); + } + + public void testInnerClassCreation() throws Throwable { + // Create static inner class + getTreeParser().testInitString("new InnerClassTestData.InnerInnerClass()", new String[] {"org.eclipse.jem.tests.proxy.initParser.tree.*"}, new InnerClassTestData.InnerInnerClass()); + } + +}
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTMiscTest.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTMiscTest.java new file mode 100644 index 0000000..69f7cbb --- /dev/null +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTMiscTest.java
@@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* + * $RCSfile: ASTMiscTest.java,v $ + * $Revision: 1.1 $ $Date: 2004/01/23 22:53:36 $ + */ +package org.eclipse.jem.tests.proxy.initParser.tree; + +import org.eclipse.jem.internal.instantiation.*; +import org.eclipse.jem.internal.instantiation.InstantiationFactory; +import org.eclipse.jem.internal.instantiation.PTCharacterLiteral; +import org.eclipse.jem.tests.proxy.initParser.AbstractInitParserTestCase; + +/** + * Some misc. tests. + * @since 1.0.0 + */ +public class ASTMiscTest extends AbstractInitParserTestCase { + + /** + * Construct with name. + * @param name + * + * @since 1.0.0 + */ + public ASTMiscTest(String name) { + super(name); + } + + public void testCharLiteral() { + PTCharacterLiteral cl = InstantiationFactory.eINSTANCE.createPTCharacterLiteral(); + cl.setEscapedValue("\'a\'"); + assertEquals('a', cl.getCharValue()); + cl.setCharValue('b'); + assertEquals("\'b\'", cl.getEscapedValue()); + cl.setEscapedValue("\'\\n\'"); + assertEquals('\n', cl.getCharValue()); + cl.setCharValue('\b'); + assertEquals("\'\\b\'", cl.getEscapedValue()); + cl.setEscapedValue("\'\\u0300\'"); + assertEquals('\u0300', cl.getCharValue()); + cl.setCharValue('\u0400'); + assertEquals("\'\\u0400\'", cl.getEscapedValue()); + } + + public void testStringLiteral() { + PTStringLiteral sl = InstantiationFactory.eINSTANCE.createPTStringLiteral(); + sl.setEscapedValue("\"a\""); + assertEquals("a", sl.getLiteralValue()); + sl.setLiteralValue("\b"); + assertEquals("\"\\b\"", sl.getEscapedValue()); + sl.setEscapedValue("\"\\n\""); + assertEquals("\n", sl.getLiteralValue()); + sl.setLiteralValue("\b"); + assertEquals("\"\\b\"", sl.getEscapedValue()); + sl.setEscapedValue("\"\\u0300\""); + assertEquals("\u0300", sl.getLiteralValue()); + sl.setLiteralValue("\u0400"); + assertEquals("\"\\u0400\"", sl.getEscapedValue()); + } + +}
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTNestFieldAccessTestData.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTNestFieldAccessTestData.java new file mode 100644 index 0000000..bb0462d --- /dev/null +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTNestFieldAccessTestData.java
@@ -0,0 +1,29 @@ +package org.eclipse.jem.tests.proxy.initParser.tree; +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* + * $RCSfile: ASTNestFieldAccessTestData.java,v $ + * $Revision: 1.1 $ $Date: 2004/01/23 22:53:36 $ + */ + +import java.awt.Color; +import java.awt.Rectangle; + +/** + * This class is used in the testing of nested field access for Parse testing. + * This is when you access a field, and then access a field of that. + * In this case <code>ASTNestFieldAccessTestData.acolor.red</code> + * @since 1.0.0 + */ +public class ASTNestFieldAccessTestData { + public static Color acolor = Color.green; + public Rectangle arect = new Rectangle(10,20,30,40); +}
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTOperationsTest.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTOperationsTest.java new file mode 100644 index 0000000..7f5c734 --- /dev/null +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTOperationsTest.java
@@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* + * $RCSfile: ASTOperationsTest.java,v $ + * $Revision: 1.1 $ $Date: 2004/01/23 22:53:36 $ + */ +package org.eclipse.jem.tests.proxy.initParser.tree; + +import org.eclipse.jem.tests.proxy.initParser.AbstractInitParserTestCase; + +/** + * + * @since 1.0.0 + */ +public class ASTOperationsTest extends AbstractInitParserTestCase { + + /** + * Create with name. + * @param name + * + * @since 1.0.0 + */ + public ASTOperationsTest(String name) { + super(name); + } + + public void testTwoOpers() throws Throwable { + testHelper.testInitString("3+4", new Integer(3+4)); + } + + public void testFourOpersExtended() throws Throwable { + testHelper.testInitString("3+4+5+6", new Integer(3+4+5+6)); + } + + public void testMixedOpers() throws Throwable { + testHelper.testInitString("3+4-5", new Integer(3+4-5)); + } + + public void testMixedOpersPrecedence() throws Throwable { + testHelper.testInitString("6 | 3+4 | 8", new Integer(6 | 3+4 | 8)); + } + + public void testConditional() throws Throwable { + testHelper.testInitString("java.awt.Color.red != null ? 3 : 5", new Integer(java.awt.Color.red != null ? 3 : 5)); + } + + public void testInstanceof() throws Throwable { + testHelper.testInitString("java.awt.Color.red instanceof java.awt.Color", new Boolean(java.awt.Color.red instanceof java.awt.Color)); + } + + public void testTypeLiteral() throws Throwable { + testHelper.testInitString("Object.class", Object.class); + } + +}
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTParserSuite.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTParserSuite.java index 2d21783..51e7da5 100644 --- a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTParserSuite.java +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTParserSuite.java
@@ -11,11 +11,22 @@ *******************************************************************************/ /* * $RCSfile: ASTParserSuite.java,v $ - * $Revision: 1.1 $ $Date: 2004/01/19 22:50:22 $ + * $Revision: 1.2 $ $Date: 2004/01/23 22:53:36 $ */ +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.Path; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; + +import junit.extensions.TestSetup; +import junit.framework.*; import junit.framework.Test; import junit.framework.TestSuite; +import org.eclipse.jem.tests.JavaProjectUtil; +import org.eclipse.jem.tests.JavaTestsPlugin; import org.eclipse.jem.tests.proxy.initParser.*; /** @@ -24,44 +35,39 @@ * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ -public class ASTParserSuite extends TestSuite { +public class ASTParserSuite extends TestSetup { // Test cases to be include in the suite private static Class testsList[] = { -// NeedsCodingTest.class, // tested -// BlockTest.class, // tested + NeedsCodingTest.class, + BlockTest.class, + ASTFieldAccessTest.class, ASTArraysTest.class, -// MultiArgStaticMethodTest.class, // tested -// MultiArgConstructorTest.class, // tested -// MultiArgInstanceTest.class, // tested -// NumberTest.class, // tested -// CastTest.class, // tested -// ExceptionTest.class, -// LiteralTest.class, // tested -// StringTest.class, // tested -// CharTest.class, // tested -// JFCTest.class, -// BorderTest.class, -// OverloadingTest.class, -// SameName46376Test.class + MultiArgStaticMethodTest.class, + MultiArgConstructorTest.class, + MultiArgInstanceTest.class, + NumberTest.class, + CastTest.class, +// ExceptionTest.class, // mixes parse error w/execution errors. Can't test parse errors. Need specific test for exec errors like method not found. + LiteralTest.class, + StringTest.class, + CharTest.class, + JFCTest.class, + BorderTest.class, + OverloadingTest.class, + SameName46376Test.class, + ASTOperationsTest.class, + ASTInnerClassAccessTest.class, + ASTMiscTest.class } ; + public static String pkgName = "org.eclipse.jem.tests.proxy.initParser.tree" ; /** * Constructor for PackageSuite. */ public ASTParserSuite() { - super(); - populateSuite() ; - } - - /** - * Constructor for PackageSuite. - * @param theClass - */ - public ASTParserSuite(Class theClass) { - super(theClass); - populateSuite() ; + this("AST Parser Suite"); } /** @@ -69,19 +75,38 @@ * @param name */ public ASTParserSuite(String name) { - super(name); + super(new TestSuite(name)); populateSuite() ; } private void populateSuite () { + TestSuite suite = (TestSuite) getTest(); for (int i=0; i<testsList.length; i++) - addTestSuite(testsList[i]) ; + suite.addTestSuite(testsList[i]) ; - AbstractInitParserTestCase.initSuite(this, new ASTTreeInitStringParserTestHelper()); + try { + IProject project = JavaProjectUtil.createEmptyJavaProject(ResourcesPlugin.getWorkspace(), new Path(AST_PROJECT), null); + IJavaProject jproject = JavaCore.create(project); + // Add javatests.jar (i.e. this plugins jar) so that the classes within the tests jar are available + // to the jdt parser for referencing. + JavaProjectUtil.addPluginJarToPath(JavaTestsPlugin.getPlugin(), "javatests.jar", jproject, null); + AbstractInitParserTestCase.initSuite(suite, new ASTTreeInitStringParserTestHelper(project)); + } catch (CoreException e) { + Assert.assertNotNull(e); + } } public static Test suite() { return new ASTParserSuite("Test for: "+pkgName); } + private static final String AST_PROJECT = "/AST tests/"; + + /* (non-Javadoc) + * @see junit.extensions.TestSetup#tearDown() + */ + protected void tearDown() throws Exception { + JavaProjectUtil.deleteProject(JavaProjectUtil.getProject(AST_PROJECT)); + } + }
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTTreeInitStringParserTestHelper.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTTreeInitStringParserTestHelper.java index a5563e0..c3d752b 100644 --- a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTTreeInitStringParserTestHelper.java +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/ASTTreeInitStringParserTestHelper.java
@@ -10,7 +10,7 @@ *******************************************************************************/ /* * $RCSfile: ASTTreeInitStringParserTestHelper.java,v $ - * $Revision: 1.1 $ $Date: 2004/01/19 22:50:22 $ + * $Revision: 1.2 $ $Date: 2004/01/23 22:53:36 $ */ package org.eclipse.jem.tests.proxy.initParser.tree; @@ -18,12 +18,16 @@ import junit.framework.Assert; +import org.eclipse.core.resources.IProject; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.core.dom.*; -import org.eclipse.jdt.core.dom.AST; -import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jem.internal.instantiation.PTExpression; import org.eclipse.jem.tests.proxy.initParser.AbstractInitStringParserTestHelper; +import org.eclipse.jem.workbench.utility.ASTBoundResolver; +import org.eclipse.jem.workbench.utility.ParseTreeCreationFromAST; /** * Init String parser helper for working with AST trees. @@ -32,22 +36,66 @@ */ public class ASTTreeInitStringParserTestHelper extends AbstractInitStringParserTestHelper { - private static final String TEMPLATE_CLASS = "public class TEMPLATE '{'\n public Object test() '{'\n return {0};\n }\n}"; + private static final String TEMPLATE_CLASS = "public class TEMPLATE '{'\n public void test() '{'\n String.valueOf({0});\n }\n}"; + private static final String TEMPLATE_CLASS_IMPORTS = "{0}\npublic class TEMPLATE '{'\n public void test() '{'\n String.valueOf({1});\n }\n}"; + + private IJavaProject project; + private ParseTreeCreationFromAST parser = new ParseTreeCreationFromAST(new ASTBoundResolver()); + + public ASTTreeInitStringParserTestHelper() { + } + + public ASTTreeInitStringParserTestHelper(IProject project) { + this.project = JavaCore.create(project); + } /* (non-Javadoc) * @see org.eclipse.jem.tests.proxy.initParser.AbstractInitStringParserTestHelper#testInitString(java.lang.String, java.lang.Object, boolean, boolean) */ public void testInitString(String aString, Object expectedResult, boolean throwsException, boolean equalsOnly) throws Throwable { String testClass = MessageFormat.format(TEMPLATE_CLASS, new Object[] {aString}); - CompilationUnit cu = AST.parseCompilationUnit(testClass.toCharArray()); + CompilationUnit cu = project != null ? AST.parseCompilationUnit(testClass.toCharArray(), "TEMPLATE.java", project) : AST.parseCompilationUnit(testClass.toCharArray()); + testInitString(aString, cu, expectedResult, throwsException, equalsOnly); + } + + public void testInitString(String aString, String[] imports, Object expectedResult) throws Throwable { + testInitString(aString, imports,expectedResult, false, true); + } + + public void testInitString(String aString, String[] imports, Object expectedResult, boolean throwsException, boolean equalsOnly) throws Throwable { + StringBuffer importLines = new StringBuffer(100); + for (int i = 0; i < imports.length; i++) { + importLines.append("import "); + importLines.append(imports[i]); + importLines.append(";\n"); + } + String testClass = MessageFormat.format(TEMPLATE_CLASS_IMPORTS, new Object[] {importLines, aString}); + CompilationUnit cu = project != null ? AST.parseCompilationUnit(testClass.toCharArray(), "TEMPLATE.java", project) : AST.parseCompilationUnit(testClass.toCharArray()); + testInitString(aString, cu, expectedResult, throwsException, equalsOnly); + } + + protected void testInitString(String aString, CompilationUnit cu, Object expectedResult, boolean throwsException, boolean equalsOnly) throws Throwable { IProblem[] problems = cu.getProblems(); if (problems.length > 0) { + boolean errors = false; StringBuffer buf = new StringBuffer(100); for (int i = 0; i < problems.length; i++) { + errors = errors | problems[i].isError(); buf.append(" "+problems[i].getMessage()); } - Assert.fail("Problems with \""+aString+"\": "+ buf); + // If only warnings, try going on. Only errors should cause a failure. + if (errors) + Assert.fail("Problems with \""+aString+"\": "+ buf); + else { + // Else just log the warnings. + System.err.println("Warnings ocurred for \""+aString+"\":"+buf); + } } - Expression exp = ((ReturnStatement) ((TypeDeclaration) cu.types().get(0)).getMethods()[0].getBody().statements().get(0)).getExpression(); + + TypeDeclaration td = (TypeDeclaration) cu.types().get(0); + ExpressionStatement es = (ExpressionStatement) td.getMethods()[0].getBody().statements().get(0); + MethodInvocation mi = (MethodInvocation) es.getExpression(); + org.eclipse.jdt.core.dom.Expression exp = (org.eclipse.jdt.core.dom.Expression) mi.arguments().get(0); + PTExpression parseExp = parser.createExpression(exp); } }
diff --git a/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/InnerClassTestData.java b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/InnerClassTestData.java new file mode 100644 index 0000000..e7e5161 --- /dev/null +++ b/tests/org.eclipse.jem.tests/javatests/org/eclipse/jem/tests/proxy/initParser/tree/InnerClassTestData.java
@@ -0,0 +1,48 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +/* + * $RCSfile: InnerClassTestData.java,v $ + * $Revision: 1.1 $ $Date: 2004/01/23 22:53:36 $ + */ +package org.eclipse.jem.tests.proxy.initParser.tree; + +import java.awt.Color; + +/** + * This is for testing inner class access. + * @since 1.0.0 + */ +public class InnerClassTestData { + + /** + * + * @since 1.0.0 + */ + public static class InnerInnerClass extends Object { + public final static Color GREEN = Color.green; + + /** + * + * @since 1.0.0 + */ + public static class InnerInnerInnerClass extends Object { + public final static Color RED = Color.red; + + /** + * + * @since 1.0.0 + */ + public class InnerInnerInnerInnerClass { + + } + } + } +} \ No newline at end of file
diff --git a/tests/org.eclipse.jem.tests/proxy.jars b/tests/org.eclipse.jem.tests/proxy.jars index 87e4664..7a7029a 100644 --- a/tests/org.eclipse.jem.tests/proxy.jars +++ b/tests/org.eclipse.jem.tests/proxy.jars
@@ -1 +1,2 @@ -vm/tests.jar=/org.eclipse.jem.tests/bin/ \ No newline at end of file +vm/tests.jar=/org.eclipse.jem.tests/bin/ +javatests.jar=/org.eclipse.jem.tests/bin/ \ No newline at end of file