summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2007-05-01 15:49:13 (EDT)
committerpelder2007-05-01 15:49:13 (EDT)
commit18f216e3b3e1418b2bfd73d09e72ada567d852ef (patch)
tree13401385241bb67adc25055f1cdce3ce1d7da469
parent87cd27e970a034a8234ba0e65739ac577bb709a4 (diff)
downloadorg.eclipse.jet-18f216e3b3e1418b2bfd73d09e72ada567d852ef.zip
org.eclipse.jet-18f216e3b3e1418b2bfd73d09e72ada567d852ef.tar.gz
org.eclipse.jet-18f216e3b3e1418b2bfd73d09e72ada567d852ef.tar.bz2
[184846] The constructor URI(String) expects special characters to be encoded. Other URI constructors will encode as appropriate. Change to these constructors, and avoid other URI methods the use this constructor (resolve(String) and create(String)). Also, platform URL handlers don't correctly decode encoded URLs. Switch from URI.toURL() to new URL(String, int, String, String)
-rw-r--r--plugins/org.eclipse.jet.core/src/org/eclipse/jet/core/parser/AbstractTemplateResolver.java10
-rw-r--r--plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateInput.java17
-rw-r--r--plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateResolverHelper.java14
-rw-r--r--plugins/org.eclipse.jet/META-INF/MANIFEST.MF1
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java2
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/ResourceTemplateInput.java15
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverBuilder.java16
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverHelperFactory.java33
-rw-r--r--tests/org.eclipse.jet.tests.core/data/templates/base1/Copy of test1.txt1
-rw-r--r--tests/org.eclipse.jet.tests.core/src/org/eclipse/jet/tests/tools/compiler/TestTemplateInput.java52
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/AllTests.java1
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/AllTests.java19
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/ResourceTemplateInputTest.java136
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/WorkspaceTemplateResolverBuilderTest.java114
-rw-r--r--tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/WorkspaceTemplateResolverHelperFactoryTest.java126
15 files changed, 524 insertions, 33 deletions
diff --git a/plugins/org.eclipse.jet.core/src/org/eclipse/jet/core/parser/AbstractTemplateResolver.java b/plugins/org.eclipse.jet.core/src/org/eclipse/jet/core/parser/AbstractTemplateResolver.java
index ef6b7bb..c56d533 100644
--- a/plugins/org.eclipse.jet.core/src/org/eclipse/jet/core/parser/AbstractTemplateResolver.java
+++ b/plugins/org.eclipse.jet.core/src/org/eclipse/jet/core/parser/AbstractTemplateResolver.java
@@ -111,11 +111,13 @@ public abstract class AbstractTemplateResolver implements ITemplateResolver {
*/
private String resolveToTemplatePath(String includePath, String templatePath) {
try {
- URI currentURI = new URI(templatePath);
- URI resolvedURI = currentURI.resolve(includePath);
- return resolvedURI.toString();
+ URI currentURI = new URI(null, templatePath, null);
+ URI resolvedURI = currentURI.resolve(new URI(null, includePath, null));
+ return resolvedURI.getSchemeSpecificPart();
} catch (URISyntaxException e) {
- throw new IllegalArgumentException();
+ IllegalArgumentException wrapper = new IllegalArgumentException();
+ wrapper.initCause(e);
+ throw wrapper;
}
}
diff --git a/plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateInput.java b/plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateInput.java
index 211d1f5..8797b56 100644
--- a/plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateInput.java
+++ b/plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateInput.java
@@ -12,9 +12,11 @@ package org.eclipse.jet.internal.core.parser;
import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.jet.core.parser.ITemplateInput;
@@ -52,12 +54,21 @@ public class DefaultTemplateInput implements ITemplateInput {
}
public Reader getReader() throws TemplateInputException {
- // TODO Auto-generated method stub
try {
- URL url = baseLocation.resolve(templatePath).toURL();
- return new BufferedReader(new InputStreamReader(url.openStream(), getEncoding()));
+ URI templateURI = new URI(null, templatePath, null);
+ URI resolvedURI = baseLocation.resolve(templateURI);
+ // Don't use URI.toURL(), platform:/ URI's don't properly transform into URLs
+ // Instead, we use:
+ URL url = new URL(resolvedURI.getScheme(), resolvedURI.getHost(), resolvedURI.getPort(), resolvedURI.getPath());
+ InputStream urlStream = url.openStream();
+ return new BufferedReader(new InputStreamReader(urlStream,
+ getEncoding()));
} catch (IOException e) {
throw new TemplateInputException(e);
+ } catch (URISyntaxException e) {
+ IllegalArgumentException wrapper = new IllegalArgumentException();
+ wrapper.initCause(e);
+ throw wrapper;
}
}
diff --git a/plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateResolverHelper.java b/plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateResolverHelper.java
index 968e7fe..08d5416 100644
--- a/plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateResolverHelper.java
+++ b/plugins/org.eclipse.jet.core/src/org/eclipse/jet/internal/core/parser/DefaultTemplateResolverHelper.java
@@ -12,7 +12,7 @@
*
* </copyright>
*
- * $Id: DefaultTemplateResolverHelper.java,v 1.1 2007/04/04 14:53:53 pelder Exp $
+ * $Id: DefaultTemplateResolverHelper.java,v 1.2 2007/05/01 19:49:13 pelder Exp $
*/
package org.eclipse.jet.internal.core.parser;
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.jet.core.parser.ITemplateInput;
@@ -61,14 +62,21 @@ public class DefaultTemplateResolverHelper implements ITemplateResolverHelper
private InputStream openStream(URI baseLocation, String templatePath) {
try {
- URL templateURL = baseLocation.resolve(templatePath).toURL();
+ URI resolvedURI = baseLocation.resolve(new URI(null, templatePath, null));
+ // Don't use URI.toURL(), platform:/ URI's don't properly transform into URLs
+ // Instead, we use
+ URL templateURL = new URL(resolvedURI.getScheme(), resolvedURI.getHost(), resolvedURI.getPort(), resolvedURI.getPath());
final InputStream input = templateURL.openStream();
return input;
} catch (MalformedURLException e) {
// ignore;
} catch (IOException e) {
// ignore;
- }
+ } catch (URISyntaxException e) {
+ IllegalArgumentException wrapper = new IllegalArgumentException();
+ wrapper.initCause(e);
+ throw wrapper;
+ }
return null;
}
diff --git a/plugins/org.eclipse.jet/META-INF/MANIFEST.MF b/plugins/org.eclipse.jet/META-INF/MANIFEST.MF
index 0ae6a89..98ec3c2 100644
--- a/plugins/org.eclipse.jet/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.jet/META-INF/MANIFEST.MF
@@ -32,6 +32,7 @@ Export-Package: org.eclipse.jet,
org.eclipse.jet.internal.l10n;x-friends:="org.eclipse.jet.tests",
org.eclipse.jet.internal.launch;x-friends:="org.eclipse.jet.tests,org.eclipse.jet.ui",
org.eclipse.jet.internal.parser;x-friends:="org.eclipse.jet.tests",
+ org.eclipse.jet.internal.parser.resources;x-friends:="org.eclipse.jet.tests",
org.eclipse.jet.internal.runtime;x-friends:="org.eclipse.jet.tests,org.eclipse.jet.ui",
org.eclipse.jet.internal.runtime.model;x-friends:="org.eclipse.jet.tests",
org.eclipse.jet.internal.taglib;x-friends:="org.eclipse.jet.tests",
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java
index e80169b..a2cd96b 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/compiler/templates/CodeGenTemplateLoader.java
@@ -66,8 +66,8 @@ public class CodeGenTemplateLoader implements JET2TemplateLoader, JET2TemplateLo
private void initPathToFactoryMap() {
pathToFactoryMap = new HashMap(10);
addTemplate("templates/writeJavaExpression.jet", "org.eclipse.jet.internal.compiler.templates.WriteJavaExpressionTemplate" ); //$NON-NLS-1$ //$NON-NLS-2$
- addTemplate("templates/xmlStart.jet", "org.eclipse.jet.internal.compiler.templates.XMLStartGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
addTemplate("templates/jet2java.jet", "org.eclipse.jet.internal.compiler.templates.JET2JavaGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
+ addTemplate("templates/xmlStart.jet", "org.eclipse.jet.internal.compiler.templates.XMLStartGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
addTemplate("templates/main.jet", "org.eclipse.jet.internal.compiler.templates.DummyMain" ); //$NON-NLS-1$ //$NON-NLS-2$
addTemplate("templates/jet2transform.jet", "org.eclipse.jet.internal.compiler.templates.JET2TransformGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
addTemplate("templates/xmlEnd.jet", "org.eclipse.jet.internal.compiler.templates.XMLEndGenerator" ); //$NON-NLS-1$ //$NON-NLS-2$
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/ResourceTemplateInput.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/ResourceTemplateInput.java
index dbaa075..7d2ef08 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/ResourceTemplateInput.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/ResourceTemplateInput.java
@@ -12,7 +12,7 @@
*
* </copyright>
*
- * $Id: ResourceTemplateInput.java,v 1.1 2007/04/04 14:59:33 pelder Exp $
+ * $Id: ResourceTemplateInput.java,v 1.2 2007/05/01 19:49:11 pelder Exp $
*/
package org.eclipse.jet.internal.parser.resources;
@@ -21,6 +21,7 @@ import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
+import java.net.URISyntaxException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
@@ -47,7 +48,17 @@ public class ResourceTemplateInput implements ITemplateInput
// The following doesn't work - we get a file:/ uri which is inconsistent with FileLocation.resolve(URL) uris
// return URI.create(file.getProject().getLocationURI().toString() + "/"); //$NON-NLS-1$
// Instead, return a platform URL:
- return URI.create("platform:/resource/" + file.getProject().getName() + "/"); //$NON-NLS-1$ //$NON-NLS-2$
+ try
+ {
+ return new URI("platform", "/resource/" + file.getProject().getName() + "/", null);
+ }
+ catch (URISyntaxException e)
+ {
+ // This convoluted code is Java 1.4 friendly. new IllegalArgumentException(e) is only in 1.5 and later
+ IllegalArgumentException wrapper = new IllegalArgumentException();
+ wrapper.initCause(e);
+ throw wrapper;
+ }
}
/* (non-Javadoc)
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverBuilder.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverBuilder.java
index e828cfd..872fb23 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverBuilder.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverBuilder.java
@@ -12,11 +12,12 @@
*
* </copyright>
*
- * $Id: WorkspaceTemplateResolverBuilder.java,v 1.1 2007/04/04 14:59:33 pelder Exp $
+ * $Id: WorkspaceTemplateResolverBuilder.java,v 1.2 2007/05/01 19:49:11 pelder Exp $
*/
package org.eclipse.jet.internal.parser.resources;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -40,7 +41,18 @@ public final class WorkspaceTemplateResolverBuilder {
{
// don't use project.getLocationURI() - this is a file:/ URI
// Instead, create a platform:/resource/ URI
- baseLocations.add(URI.create("platform:/resource/" + project.getName() + "/")); //$NON-NLS-1$//$NON-NLS-2$
+ try
+ {
+ URI uri = new URI("platform", "/resource/" + project.getName() + "/", null);
+ baseLocations.add(uri);
+ }
+ catch (URISyntaxException e)
+ {
+ // This convoluted code is Java 1.4 friendly. new IllegalArgumentException(e) is only in 1.5 and later
+ IllegalArgumentException wrapper = new IllegalArgumentException();
+ wrapper.initCause(e);
+ throw wrapper;
+ }
}
/**
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverHelperFactory.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverHelperFactory.java
index a8e7c49..df0343a 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverHelperFactory.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/parser/resources/WorkspaceTemplateResolverHelperFactory.java
@@ -12,17 +12,17 @@
*
* </copyright>
*
- * $Id: WorkspaceTemplateResolverHelperFactory.java,v 1.1 2007/04/04 14:59:33 pelder Exp $
+ * $Id: WorkspaceTemplateResolverHelperFactory.java,v 1.2 2007/05/01 19:49:11 pelder Exp $
*/
package org.eclipse.jet.internal.parser.resources;
-import java.io.IOException;
-import java.net.MalformedURLException;
import java.net.URI;
import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
-import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
import org.eclipse.jet.core.parser.ITemplateResolverHelper;
import org.eclipse.jet.core.parser.ITemplateResolverHelperFactory;
@@ -60,25 +60,22 @@ public class WorkspaceTemplateResolverHelperFactory implements ITemplateResolver
private IContainer getWorkspaceContainer(URI baseLocation)
{
- try
+ if ("platform".equals(baseLocation.getScheme()))
{
- URI location = URI.create(FileLocator.resolve(baseLocation.toURL()).toExternalForm());
- // fix FileLocator.resolve
- if("file".equals(location.getScheme()) && location.getPath() == null) { //$NON-NLS-1$
- location = URI.create("file:/" + location.getSchemeSpecificPart()); //$NON-NLS-1$
+ IPath p = new Path(baseLocation.getSchemeSpecificPart());
+ if("resource".equals(p.segment(0))) {
+ p = p.removeFirstSegments(1);
+ IResource member = ResourcesPlugin.getWorkspace().getRoot().findMember(p);
+ return member instanceof IContainer ? (IContainer)member : null;
+ } else {
+ return null;
}
- final IContainer[] containers = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(location);
- return containers.length > 0 ? containers[0] : null;
- }
- catch (MalformedURLException e)
- {
- // ignore
}
- catch (IOException e)
+ else
{
- // ignore
+ final IContainer[] containers = ResourcesPlugin.getWorkspace().getRoot().findContainersForLocationURI(baseLocation);
+ return containers.length > 0 ? containers[0] : null;
}
- return null;
}
}
diff --git a/tests/org.eclipse.jet.tests.core/data/templates/base1/Copy of test1.txt b/tests/org.eclipse.jet.tests.core/data/templates/base1/Copy of test1.txt
new file mode 100644
index 0000000..7c6f088
--- /dev/null
+++ b/tests/org.eclipse.jet.tests.core/data/templates/base1/Copy of test1.txt
@@ -0,0 +1 @@
+This is Copy of test1.txt in data/templates/base1. \ No newline at end of file
diff --git a/tests/org.eclipse.jet.tests.core/src/org/eclipse/jet/tests/tools/compiler/TestTemplateInput.java b/tests/org.eclipse.jet.tests.core/src/org/eclipse/jet/tests/tools/compiler/TestTemplateInput.java
index 0ad8c5c..1232dc6 100644
--- a/tests/org.eclipse.jet.tests.core/src/org/eclipse/jet/tests/tools/compiler/TestTemplateInput.java
+++ b/tests/org.eclipse.jet.tests.core/src/org/eclipse/jet/tests/tools/compiler/TestTemplateInput.java
@@ -10,10 +10,14 @@
*/
package org.eclipse.jet.tests.tools.compiler;
+import java.io.IOException;
+import java.io.Reader;
import java.net.URI;
+import java.net.URISyntaxException;
import junit.framework.TestCase;
+import org.eclipse.jet.core.parser.TemplateInputException;
import org.eclipse.jet.internal.core.parser.DefaultTemplateInput;
/**
@@ -48,4 +52,52 @@ public class TestTemplateInput extends TestCase {
assertEquals(TEMPLATE_PATH, templateInput.getTemplatePath());
}
+ /**
+ * Test method for {@link org.eclipse.jet.internal.core.parser.DefaultTemplateInput#getTemplatePath()}.
+ * @throws IOException
+ * @throws TemplateInputException
+ * @throws URISyntaxException
+ */
+ public void testGetTemplatePath_pathWithSpaces() throws IOException, TemplateInputException, URISyntaxException {
+ final String templatePath = "Copy of test1.txt";
+ URI location2 = new URI("file", "/C:/usr/workspaces/ws-m2t-jet-head/org.eclipse.jet.tests.core/data/templates/base1/", null);
+ DefaultTemplateInput defaultTemplateInput = new DefaultTemplateInput(location2, templatePath, "UTF-8");
+
+ assertEquals(templatePath, defaultTemplateInput.getTemplatePath());
+
+ Reader reader = null;
+ try {
+ reader = defaultTemplateInput.getReader();
+ assertNotNull("reader", reader);
+ } finally {
+ if(reader != null) {
+ reader.close();
+ }
+ }
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.core.parser.DefaultTemplateInput#getTemplatePath()}.
+ * @throws IOException
+ * @throws TemplateInputException
+ * @throws URISyntaxException
+ */
+ public void testGetTemplatePath_pathWithSpaces1() throws IOException, TemplateInputException, URISyntaxException {
+ final String templatePath = "Copy of test1.txt";
+ URI location2 = new URI("file", "/C:/usr/workspaces/ws-m2t-jet-head/org.eclipse.jet.tests.core/data/templates/base1/", null);
+ DefaultTemplateInput defaultTemplateInput = new DefaultTemplateInput(location1, templatePath, "UTF-8");
+
+ assertEquals(templatePath, defaultTemplateInput.getTemplatePath());
+
+ Reader reader = null;
+ try {
+ reader = defaultTemplateInput.getReader();
+ assertNotNull("reader", reader);
+ } finally {
+ if(reader != null) {
+ reader.close();
+ }
+ }
+ }
+
}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/AllTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/AllTests.java
index fd2efbb..6445e93 100644
--- a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/AllTests.java
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/AllTests.java
@@ -18,6 +18,7 @@ public class AllTests {
suite.addTest(org.eclipse.jet.tests.taglib.java.AllTests.suite());
suite.addTest(org.eclipse.jet.tests.taglib.format.AllTests.suite());
suite.addTest(org.eclipse.jet.tests.runtime.AllTests.suite());
+ suite.addTest(org.eclipse.jet.tests.parser.resources.AllTests.suite());
//$JUnit-BEGIN$
//$JUnit-END$
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/AllTests.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/AllTests.java
new file mode 100644
index 0000000..515e2c8
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/AllTests.java
@@ -0,0 +1,19 @@
+package org.eclipse.jet.tests.parser.resources;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+public class AllTests {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite(
+ "Test for org.eclipse.jet.tests.parser.resources");
+ //$JUnit-BEGIN$
+ suite.addTest(ResourceTemplateInputTest.suite());
+ suite.addTest(WorkspaceTemplateResolverBuilderTest.suite());
+ suite.addTest(WorkspaceTemplateResolverHelperFactoryTest.suite());
+ //$JUnit-END$
+ return suite;
+ }
+
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/ResourceTemplateInputTest.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/ResourceTemplateInputTest.java
new file mode 100644
index 0000000..6b84bd2
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/ResourceTemplateInputTest.java
@@ -0,0 +1,136 @@
+/**
+ *
+ */
+package org.eclipse.jet.tests.parser.resources;
+
+import java.io.Reader;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jet.JET2Platform;
+import org.eclipse.jet.core.parser.ITemplateInput;
+import org.eclipse.jet.core.parser.TemplateInputException;
+import org.eclipse.jet.internal.parser.resources.ResourceTemplateInput;
+
+/**
+ * @author pelder
+ *
+ */
+public class ResourceTemplateInputTest extends TestCase {
+
+ protected static IProject project;
+
+ public static Test suite() {
+ TestSetup setup = new TestSetup(new TestSuite(ResourceTemplateInputTest.class)) {
+ protected void setUp() throws Exception {
+ setUpAllTests();
+ }
+
+ protected void tearDown() throws Exception {
+ tearDownAllTests();
+ }
+ };
+
+ return setup;
+ }
+
+ protected static void setUpAllTests() {
+ String newProjectModel = "<newProjectModel>" +
+ "<project" +
+ " name=\"" + TEST_PROJECT_NAME + "\"" +
+ "/>" +
+ "</newProjectModel>";
+
+ IStatus status = JET2Platform.runTransformOnString("org.eclipse.jet.transforms.newproject", newProjectModel, new NullProgressMonitor());
+ assertTrue("setUpAllTests", status.isOK());
+ try {
+ ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
+
+ public void run(IProgressMonitor monitor) throws CoreException {
+ // TODO Auto-generated method stub
+
+ }}, new NullProgressMonitor());
+ } catch (CoreException e) {
+ fail(e.getMessage());
+ }
+
+ project = ResourcesPlugin.getWorkspace().getRoot().getProject(TEST_PROJECT_NAME);
+ assertTrue("Test project exists", project.exists());
+
+ }
+
+ protected static void tearDownAllTests() {
+ // TODO Auto-generated method stub
+
+ }
+
+ protected ITemplateInput templateInput;
+ private static final String TEST_PROJECT_NAME = "org.eclipse.jet.tests.resources.ResourceTemplateInput Tests";
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ IFile templateFile = project.getFile("templates/main.jet");
+ templateInput = new ResourceTemplateInput(templateFile);
+ }
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.ResourceTemplateInput#ResourceTemplateInput(org.eclipse.core.resources.IFile)}.
+ */
+ public void testResourceTemplateInput() {
+
+ assertNotNull(templateInput);
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.ResourceTemplateInput#getBaseLocation()}.
+ * @throws URISyntaxException
+ */
+ public void testGetBaseLocation() throws URISyntaxException {
+ URI baseLocation = templateInput.getBaseLocation();
+
+ assertEquals(new URI("platform", "/resource/" + TEST_PROJECT_NAME + "/", null), baseLocation);
+
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.ResourceTemplateInput#getReader()}.
+ * @throws TemplateInputException
+ */
+ public void testGetReader() throws TemplateInputException {
+ Reader reader = templateInput.getReader();
+ assertNotNull(reader);
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.ResourceTemplateInput#getTemplatePath()}.
+ */
+ public void testGetTemplatePath() {
+ String templatePath = templateInput.getTemplatePath();
+
+ assertEquals("templates/main.jet", templatePath);
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.ResourceTemplateInput#getEncoding()}.
+ * @throws TemplateInputException
+ * @throws CoreException
+ */
+ public void testGetEncoding() throws TemplateInputException, CoreException {
+ String encoding = templateInput.getEncoding();
+ assertEquals(project.getFile(templateInput.getTemplatePath()).getCharset(), encoding);
+ }
+
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/WorkspaceTemplateResolverBuilderTest.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/WorkspaceTemplateResolverBuilderTest.java
new file mode 100644
index 0000000..ac609f1
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/WorkspaceTemplateResolverBuilderTest.java
@@ -0,0 +1,114 @@
+/**
+ *
+ */
+package org.eclipse.jet.tests.parser.resources;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jet.JET2Platform;
+import org.eclipse.jet.core.parser.ITemplateResolver;
+import org.eclipse.jet.internal.parser.resources.WorkspaceTemplateResolverBuilder;
+
+/**
+ * @author pelder
+ *
+ */
+public class WorkspaceTemplateResolverBuilderTest extends TestCase {
+
+ private static final String TEST_PROJECT_NAME = "org.eclipse.jet.tests.parser.Workspace Template Resover Builder Test";
+ protected static IProject project;
+
+ public static Test suite() {
+ TestSetup setup = new TestSetup(new TestSuite(WorkspaceTemplateResolverBuilderTest.class)) {
+ protected void setUp() throws Exception {
+ setUpAllTests();
+ }
+
+ protected void tearDown() throws Exception {
+ tearDownAllTests();
+ }
+ };
+
+ return setup;
+ }
+
+ protected static void tearDownAllTests() {
+ // TODO Auto-generated method stub
+
+ }
+
+ protected static void setUpAllTests() {
+ String newProjectModel = "<newProjectModel>" +
+ "<project" +
+ " name=\"" + TEST_PROJECT_NAME + "\"" +
+ "/>" +
+ "</newProjectModel>";
+
+ IStatus status = JET2Platform.runTransformOnString("org.eclipse.jet.transforms.newproject", newProjectModel, new NullProgressMonitor());
+ assertTrue("setUpAllTests", status.isOK());
+ try {
+ ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
+
+ public void run(IProgressMonitor monitor) throws CoreException {
+ // TODO Auto-generated method stub
+
+ }}, new NullProgressMonitor());
+ } catch (CoreException e) {
+ fail(e.getMessage());
+ }
+
+ project = ResourcesPlugin.getWorkspace().getRoot().getProject(TEST_PROJECT_NAME);
+ assertTrue("Test project exists", project.exists());
+
+ }
+
+ private WorkspaceTemplateResolverBuilder builder;
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ builder = new WorkspaceTemplateResolverBuilder(project);
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.WorkspaceTemplateResolverBuilder#WorkspaceTemplateResolverBuilder(org.eclipse.core.resources.IProject)}.
+ * @throws URISyntaxException
+ */
+ public void testWorkspaceTemplateResolverBuilder() throws URISyntaxException {
+ assertNotNull(builder);
+ ITemplateResolver templateResolver = builder.build();
+
+ URI[] baseLocations = templateResolver.getBaseLocations();
+ assertEquals(1, baseLocations.length);
+ assertEquals(new URI("platform", "/resource/" + TEST_PROJECT_NAME + "/", null), baseLocations[0]);
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.WorkspaceTemplateResolverBuilder#addAltBaseLocations(java.net.URI[])}.
+ * @throws URISyntaxException
+ */
+ public void testAddAltBaseLocations() throws URISyntaxException {
+ builder.addAltBaseLocations(new URI[] {
+ new URI("file", "/c:/usr/altLocation/", null), });
+
+ ITemplateResolver templateResolver = builder.build();
+
+ URI[] baseLocations = templateResolver.getBaseLocations();
+ assertEquals(2, baseLocations.length);
+ assertEquals(new URI("file", "/c:/usr/altLocation/", null), baseLocations[1]);
+
+ }
+
+}
diff --git a/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/WorkspaceTemplateResolverHelperFactoryTest.java b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/WorkspaceTemplateResolverHelperFactoryTest.java
new file mode 100644
index 0000000..8ac4153
--- /dev/null
+++ b/tests/org.eclipse.jet.tests/src/org/eclipse/jet/tests/parser/resources/WorkspaceTemplateResolverHelperFactoryTest.java
@@ -0,0 +1,126 @@
+/**
+ *
+ */
+package org.eclipse.jet.tests.parser.resources;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspaceRunnable;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jet.JET2Platform;
+import org.eclipse.jet.core.parser.ITemplateResolverHelper;
+import org.eclipse.jet.internal.parser.resources.WorkspaceTemplateResolverHelperFactory;
+
+/**
+ * @author pelder
+ *
+ */
+public class WorkspaceTemplateResolverHelperFactoryTest extends TestCase {
+
+ private static final String TEST_PROJECT_NAME = "org.eclipse.jet.tests.parser.Workspace Template Resover Helper Factory Test";
+ protected static IProject project;
+
+ public static Test suite() {
+ TestSetup setup = new TestSetup(new TestSuite(WorkspaceTemplateResolverHelperFactoryTest.class)) {
+ protected void setUp() throws Exception {
+ setUpAllTests();
+ }
+
+ protected void tearDown() throws Exception {
+ tearDownAllTests();
+ }
+ };
+
+ return setup;
+ }
+
+ protected static void tearDownAllTests() {
+ // TODO Auto-generated method stub
+
+ }
+
+ protected static void setUpAllTests() {
+ String newProjectModel = "<newProjectModel>" +
+ "<project" +
+ " name=\"" + TEST_PROJECT_NAME + "\"" +
+ "/>" +
+ "</newProjectModel>";
+
+ IStatus status = JET2Platform.runTransformOnString("org.eclipse.jet.transforms.newproject", newProjectModel, new NullProgressMonitor());
+ assertTrue("setUpAllTests", status.isOK());
+ try {
+ ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
+
+ public void run(IProgressMonitor monitor) throws CoreException {
+ // TODO Auto-generated method stub
+
+ }}, new NullProgressMonitor());
+ } catch (CoreException e) {
+ fail(e.getMessage());
+ }
+
+ project = ResourcesPlugin.getWorkspace().getRoot().getProject(TEST_PROJECT_NAME);
+ assertTrue("Test project exists", project.exists());
+
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.WorkspaceTemplateResolverHelperFactory#getTemplateResolverHelper(java.net.URI)}.
+ * @throws URISyntaxException
+ */
+ public void testGetTemplateResolverHelper_project_platformURI() throws URISyntaxException {
+
+ URI baseLocation = new URI("platform", "/resource/" + project.getName() + "/", null);
+ ITemplateResolverHelper templateResolverHelper = WorkspaceTemplateResolverHelperFactory.getInstance().getTemplateResolverHelper(baseLocation);
+ String templatePath = "templates/main.jet";
+ assertTrue("template Exists", templateResolverHelper.inputExists(templatePath));
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.WorkspaceTemplateResolverHelperFactory#getTemplateResolverHelper(java.net.URI)}.
+ * @throws URISyntaxException
+ */
+ public void testGetTemplateResolverHelper_folder_platformURI() throws URISyntaxException {
+
+ URI baseLocation = new URI("platform", "/resource/" + project.getName() + "/templates/", null);
+ ITemplateResolverHelper templateResolverHelper = WorkspaceTemplateResolverHelperFactory.getInstance().getTemplateResolverHelper(baseLocation);
+ String templatePath = "main.jet";
+ assertTrue("template Exists", templateResolverHelper.inputExists(templatePath));
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.WorkspaceTemplateResolverHelperFactory#getTemplateResolverHelper(java.net.URI)}.
+ * @throws URISyntaxException
+ */
+ public void testGetTemplateResolverHelper_project_fileURI() throws URISyntaxException {
+
+ URI baseLocation = project.getLocationURI();
+ ITemplateResolverHelper templateResolverHelper = WorkspaceTemplateResolverHelperFactory.getInstance().getTemplateResolverHelper(baseLocation);
+ String templatePath = "templates/main.jet";
+ assertTrue("template Exists", templateResolverHelper.inputExists(templatePath));
+ }
+
+ /**
+ * Test method for {@link org.eclipse.jet.internal.parser.resources.WorkspaceTemplateResolverHelperFactory#getTemplateResolverHelper(java.net.URI)}.
+ * @throws URISyntaxException
+ */
+ public void testGetTemplateResolverHelper_folder_fileURI() throws URISyntaxException {
+
+ URI baseLocation = project.getFolder("templates").getLocationURI();
+ ITemplateResolverHelper templateResolverHelper = WorkspaceTemplateResolverHelperFactory.getInstance().getTemplateResolverHelper(baseLocation);
+ String templatePath = "main.jet";
+ assertTrue("template Exists", templateResolverHelper.inputExists(templatePath));
+ }
+
+}