Author: henning
Date: Fri Nov 24 16:26:32 2006
New Revision: 479058
URL: http://svn.apache.org/viewvc?view=rev&rev=479058
Log:
Add the suggested StringResourceLoader from VELOCITY-183.
Added:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java
(with props)
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResource.java
(with props)
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepository.java
(with props)
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepositoryImpl.java
(with props)
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/StringResourceLoaderTestCase.java
(with props)
jakarta/velocity/engine/trunk/test/stringloader/
jakarta/velocity/engine/trunk/test/stringloader/compare/
jakarta/velocity/engine/trunk/test/stringloader/compare/change1.cmp
jakarta/velocity/engine/trunk/test/stringloader/compare/change2.cmp
jakarta/velocity/engine/trunk/test/stringloader/compare/multi1.cmp
jakarta/velocity/engine/trunk/test/stringloader/compare/multi2.cmp
jakarta/velocity/engine/trunk/test/stringloader/compare/simpletemplate.cmp
Added:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java?view=auto&rev=479058
==============================================================================
---
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java
(added)
+++
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java
Fri Nov 24 16:26:32 2006
@@ -0,0 +1,281 @@
+package org.apache.velocity.runtime.resource.loader;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+
+import org.apache.commons.collections.ExtendedProperties;
+import org.apache.commons.lang.StringUtils;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.velocity.exception.VelocityException;
+import org.apache.velocity.runtime.log.Log;
+import org.apache.velocity.runtime.resource.Resource;
+import org.apache.velocity.runtime.resource.util.StringResource;
+import org.apache.velocity.runtime.resource.util.StringResourceRepository;
+import org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl;
+import org.apache.velocity.util.ClassUtils;
+
+/**
+ * Resource loader that works with Strings. Users should manually add
+ * resources to the repository that is know by the factory of this package.
+ *
+ * Below is an example configuration for this loader.
+ * Note that 'repositoryimpl' is not mandatory;
+ * if not provided, the factory will fall back on using the default
+ * implementation of this package.
+ *
+ * string.resource.loader.description = Velocity StringResource loader
+ * string.resource.loader.class =
org.apache.velocity.runtime.resource.loader..StringResourceLoader
+ * string.resource.loader.repository.class =
org.apache.velocity.runtime.resource.loader.StringResourceRepositoryImpl
+ *
+ * Resources can be added to the repository like this:
+ * <code>
+ * StringResourceRepository = StringResourceLoader.getRepository();
+ *
+ * String myTemplateName = "/somewhere/intherepo/name";
+ * String myTemplateBody = "Hi, ${username}... this is a some template!";
+ * vsRepository.putStringResource(myTemplateName, myTemplateBody);
+ * </code>
+ *
+ * After this, the templates can be retrieved as usual.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Eelco Hillenius</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
+ * @version $Id$
+ */
+public class StringResourceLoader extends ResourceLoader
+{
+ /** Key to look up the repository implementation class. */
+ public static final String REPOSITORY_CLASS = "repository.class";
+
+ /** The default implementation class. */
+ public static final String REPOSITORY_CLASS_DEFAULT =
StringResourceRepositoryImpl.class.getName();
+
+ /** Key to look up the repository char encoding. */
+ public static final String REPOSITORY_ENCODING = "repository.encoding";
+
+ /** The default repository encoding. */
+ public static final String REPOSITORY_ENCODING_DEFAULT = "UTF-8";
+
+ /**
+ * Returns a reference to the Repository.
+ *
+ * @return A StringResourceRepository Reference.
+ */
+ public static StringResourceRepository getRepository()
+ {
+ return RepositoryFactory.getRepository();
+ }
+
+ /**
+ * @see
org.apache.velocity.runtime.resource.loader.ResourceLoader#init(org.apache.commons.collections.ExtendedProperties)
+ */
+ public void init(final ExtendedProperties configuration)
+ {
+ log.info("StringResourceLoader : initialization starting.");
+
+ String repositoryClass = configuration.getString(REPOSITORY_CLASS,
REPOSITORY_CLASS_DEFAULT);
+ String encoding = configuration.getString(REPOSITORY_ENCODING,
REPOSITORY_ENCODING_DEFAULT);
+
+ RepositoryFactory.setRepositoryClass(repositoryClass);
+ RepositoryFactory.setEncoding(encoding);
+ RepositoryFactory.init(log);
+
+ log.info("StringResourceLoader : initialization complete.");
+ }
+
+ /**
+ * Get an InputStream so that the Runtime can build a
+ * template with it.
+ *
+ * @param name name of template to get.
+ * @return InputStream containing the template.
+ * @throws ResourceNotFoundException Ff template not found
+ * in the RepositoryFactory.
+ */
+ public InputStream getResourceStream(final String name)
+ throws ResourceNotFoundException
+ {
+ if (StringUtils.isEmpty(name))
+ {
+ throw new ResourceNotFoundException("No template name provided");
+ }
+
+ StringResource resource = getRepository().getStringResource(name);
+
+ if(resource == null)
+ {
+ throw new ResourceNotFoundException("Could not locate resource '"
+ name + "'");
+ }
+
+ byte [] byteArray = null;
+
+ try
+ {
+ byteArray = resource.getBody().getBytes(resource.getEncoding());
+ return new ByteArrayInputStream(byteArray);
+ }
+ catch(UnsupportedEncodingException ue)
+ {
+ throw new VelocityException("Could not convert String using
encoding " + resource.getEncoding(), ue);
+ }
+ }
+
+ /**
+ * @see
org.apache.velocity.runtime.resource.loader.ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource)
+ */
+ public boolean isSourceModified(final Resource resource)
+ {
+ StringResource original = null;
+ boolean result = true;
+
+ original = getRepository().getStringResource(resource.getName());
+
+ if (original != null)
+ {
+ result = original.getLastModified() != resource.getLastModified();
+ }
+
+ return result;
+ }
+
+ /**
+ * @see
org.apache.velocity.runtime.resource.loader.ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
+ */
+ public long getLastModified(final Resource resource)
+ {
+ StringResource original = null;
+
+ original = getRepository().getStringResource(resource.getName());
+
+ return (original != null)
+ ? original.getLastModified()
+ : 0;
+ }
+
+
+ /**
+ * Factory for constructing and obtaining the instance of
+ * StringResourceRepository implementation.
+ *
+ * Users can provide their own implementation by setting the property
'repository.class'
+ * for the resource loader. Note that at this time only one instance of a
+ * string resource repository can be used in a single VM.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Eelco Hillenius</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
+ * @version $Id$
+ */
+ private static final class RepositoryFactory
+ {
+ /**
+ * is the factory initialised properly?
+ */
+ private static boolean isInitialized = false;
+
+ /**
+ * repository instance
+ */
+ private static StringResourceRepository repository = null;
+
+ /**
+ * Sets the repository class.
+ *
+ * @param className class that implements StringResourceRepository.
+ */
+ public static void setRepositoryClass(final String className)
+ {
+ if (isInitialized)
+ {
+ throw new IllegalStateException("The RepositoryFactory has
already been initialized!");
+ }
+
+ try
+ {
+ repository = (StringResourceRepository)
ClassUtils.getNewInstance(className);
+ }
+ catch (ClassNotFoundException cnfe)
+ {
+ throw new VelocityException("Could not find '" + className +
"'", cnfe);
+ }
+ catch (IllegalAccessException iae)
+ {
+ throw new VelocityException("Could not access '" + className +
"'", iae);
+ }
+ catch (InstantiationException ie)
+ {
+ throw new VelocityException("Could not instantiante '" +
className + "'", ie);
+ }
+ }
+
+ /**
+ * Sets the current repository encoding.
+ *
+ * @param encoding The current repository encoding.
+ */
+ public static void setEncoding(final String encoding)
+ {
+ if (repository == null)
+ {
+ throw new IllegalStateException("The Repository class has not
yet been set!");
+ }
+
+ repository.setEncoding(encoding);
+ }
+
+ /**
+ * Init the factory with the user given class name.
+ *
+ * @throws VelocityException If something goes wrong.
+ */
+ public static synchronized void init(final Log log)
+ throws VelocityException
+ {
+ if (isInitialized)
+ {
+ throw new IllegalStateException("Attempted to re-initialize
Factory!");
+ }
+
+ if (log.isInfoEnabled())
+ {
+ log.info("Using " + repository.getClass().getName() + " as
repository implementation");
+ log.info("Current repository encoding is " +
repository.getEncoding());
+ }
+ isInitialized = true;
+ }
+
+ /**
+ * Get a reference to the repository.
+ * @return A StringResourceRepository implementation object.
+ */
+ public static StringResourceRepository getRepository()
+ {
+ if(!isInitialized)
+ {
+ throw new IllegalStateException(
+ "RepositoryFactory was not properly set up");
+ }
+ return repository;
+ }
+ }
+}
+
Propchange:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/StringResourceLoader.java
------------------------------------------------------------------------------
svn:keywords = Id Author Date Revision
Added:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResource.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResource.java?view=auto&rev=479058
==============================================================================
---
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResource.java
(added)
+++
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResource.java
Fri Nov 24 16:26:32 2006
@@ -0,0 +1,106 @@
+package org.apache.velocity.runtime.resource.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Wrapper for Strings containing templates, allowing to add additional meta
+ * data like timestamps.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Eelco Hillenius</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
+ * @version $Id$
+ */
+public final class StringResource
+{
+ /** template body */
+ private String body;
+
+ /** encoding */
+ private String encoding;
+
+ /** last modified ts */
+ private long lastModified;
+
+ /**
+ * convenience constructor; sets body to 'body' and sets lastModified to
now
+ * @param body
+ */
+ public StringResource(final String body, final String encoding)
+ {
+ setBody(body);
+ setEncoding(encoding);
+ }
+
+ /**
+ * Sets the template body.
+ * @return String containing the template body.
+ */
+ public String getBody()
+ {
+ return body;
+ }
+
+ /**
+ * Returns the modification date of the template.
+ * @return Modification date in milliseconds.
+ */
+ public long getLastModified()
+ {
+ return lastModified;
+ }
+
+ /**
+ * Sets a new value for the template body.
+ * @param body New body value
+ */
+ public void setBody(final String body)
+ {
+ this.body = body;
+ this.lastModified = System.currentTimeMillis();
+ }
+
+ /**
+ * Changes the last modified parameter.
+ * @param lastModified The modification time in millis.
+ */
+ public void setLastModified(final long lastModified)
+ {
+ this.lastModified = lastModified;
+ }
+
+ /**
+ * Returns the encoding of this String resource.
+ *
+ * @return The encoding of this String resource.
+ */
+ public String getEncoding() {
+ return this.encoding;
+ }
+
+ /**
+ * Sets the encoding of this string resource.
+ *
+ * @param encoding The new encoding of this resource.
+ */
+ public void setEncoding(final String encoding)
+ {
+ this.encoding = encoding;
+ }
+}
Propchange:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResource.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResource.java
------------------------------------------------------------------------------
svn:keywords = Id Author Date Revision
Added:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepository.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepository.java?view=auto&rev=479058
==============================================================================
---
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepository.java
(added)
+++
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepository.java
Fri Nov 24 16:26:32 2006
@@ -0,0 +1,67 @@
+package org.apache.velocity.runtime.resource.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * A StringResourceRepository functions as a central repository for Velocity
templates
+ * stored in Strings.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Eelco Hillenius</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
+ * @version $Id$
+ */
+public interface StringResourceRepository
+{
+ /**
+ * get the string resource that is stored with given key
+ * @param name String name to retrieve from the repository.
+ * @return A StringResource containing the template.
+ */
+ StringResource getStringResource(String name);
+
+ /**
+ * add a string resource with given key.
+ * @param name The String name to store the template under.
+ * @param body A String containing a template.
+ */
+ void putStringResource(String name, String body);
+
+ /**
+ * delete a string resource with given key.
+ * @param name The string name to remove from the repository.
+ */
+ void removeStringResource(String name);
+
+ /**
+ * Sets the encoding of the repository. The encoding should be stored per
+ * template string so that multiple encodings can be stored in a single
repository.
+ * The default implementation does this correctly.
+ *
+ * @param encoding The encoding to use.
+ */
+ void setEncoding(String encoding);
+
+ /**
+ * Returns the current encoding of this repository.
+ *
+ * @return The current encoding of this repository.
+ */
+ String getEncoding();
+}
Propchange:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepository.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepository.java
------------------------------------------------------------------------------
svn:keywords = Id Author Date Revision
Added:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepositoryImpl.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepositoryImpl.java?view=auto&rev=479058
==============================================================================
---
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepositoryImpl.java
(added)
+++
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepositoryImpl.java
Fri Nov 24 16:26:32 2006
@@ -0,0 +1,88 @@
+package org.apache.velocity.runtime.resource.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
+
+/**
+ * Default implementation of StringResourceRepository.
+ * Uses a HashMap for storage
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Eelco Hillenius</a>
+ * @author <a href="mailto:[EMAIL PROTECTED]">Henning P. Schmiedehausen</a>
+ * @version $Id$
+ */
+public class StringResourceRepositoryImpl
+ implements StringResourceRepository
+{
+ /**
+ * mem store
+ */
+ protected Map resources = Collections.synchronizedMap(new HashMap());
+
+ /**
+ * Current Repository encoding.
+ */
+ private String encoding = StringResourceLoader.REPOSITORY_ENCODING_DEFAULT;
+
+ /**
+ * @see
org.apache.velocity.tools.stringresources.StringResourceRepository#getStringResource(java.lang.String)
+ */
+ public StringResource getStringResource(final String name)
+ {
+ return (StringResource)resources.get(name);
+ }
+
+ /**
+ * @see
org.apache.velocity.tools.stringresources.StringResourceRepository#putStringResource(java.lang.String,
java.lang.String)
+ */
+ public void putStringResource(final String name, final String body)
+ {
+ resources.put(name, new StringResource(body, getEncoding()));
+ }
+
+ /**
+ * @see
org.apache.velocity.tools.stringresources.StringResourceRepository#removeStringResource(java.lang.String)
+ */
+ public void removeStringResource(final String name)
+ {
+ resources.remove(name);
+ }
+
+ /**
+ * @see
org.apache.velocity.runtime.resource.util.StringResourceRepository#getEncoding()
+ */
+ public String getEncoding()
+ {
+ return encoding;
+ }
+
+ /**
+ * @see
org.apache.velocity.runtime.resource.util.StringResourceRepository#setEncoding(java.lang.String)
+ */
+ public void setEncoding(final String encoding)
+ {
+ this.encoding = encoding;
+ }
+}
Propchange:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepositoryImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/util/StringResourceRepositoryImpl.java
------------------------------------------------------------------------------
svn:keywords = Id Author Date Revision
Added:
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/StringResourceLoaderTestCase.java
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/StringResourceLoaderTestCase.java?view=auto&rev=479058
==============================================================================
---
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/StringResourceLoaderTestCase.java
(added)
+++
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/StringResourceLoaderTestCase.java
Fri Nov 24 16:26:32 2006
@@ -0,0 +1,207 @@
+package org.apache.velocity.test;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.BufferedWriter;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.runtime.RuntimeSingleton;
+import org.apache.velocity.runtime.log.NullLogChute;
+import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
+
+/**
+ * Multiple paths in the file resource loader.
+ *
+ * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
+ * @version $Id$
+ */
+public class StringResourceLoaderTestCase extends BaseTestCase
+{
+ /**
+ * Results relative to the build directory.
+ */
+ private static final String RESULTS_DIR = TEST_RESULT_DIR +
"/stringloader";
+
+ /**
+ * Results relative to the build directory.
+ */
+ private static final String COMPARE_DIR = TEST_COMPARE_DIR +
"/stringloader/compare";
+
+ /**
+ * Default constructor.
+ */
+ public StringResourceLoaderTestCase(String name)
+ {
+ super(name);
+ }
+
+ public static Test suite()
+ {
+ return new TestSuite(StringResourceLoaderTestCase.class);
+ }
+
+ public void setUp()
+ throws Exception
+ {
+ assureResultsDirectoryExists(RESULTS_DIR);
+
+ Velocity.setProperty(Velocity.RESOURCE_LOADER, "string");
+ Velocity.addProperty("string.resource.loader.class",
StringResourceLoader.class.getName());
+
Velocity.addProperty("string.resource.loader.modificationCheckInterval", "1");
+
+ // Silence the logger.
+ Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
NullLogChute.class.getName());
+
+ Velocity.init();
+ }
+
+ public void testSimpleTemplate()
+ throws Exception
+ {
+
StringResourceLoader.getRepository().putStringResource("simpletemplate.vm",
"This is a test for ${foo}");
+
+ Template template = RuntimeSingleton.getTemplate(getFileName(null,
"simpletemplate", TMPL_FILE_EXT));
+
+ FileOutputStream fos =
+ new FileOutputStream (
+ getFileName(RESULTS_DIR, "simpletemplate", RESULT_FILE_EXT));
+
+ Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
+
+ VelocityContext context = new VelocityContext();
+ context.put("foo", "a foo object");
+
+ template.merge(context, writer);
+ writer.flush();
+ writer.close();
+
+ if (!isMatch(RESULTS_DIR, COMPARE_DIR, "simpletemplate",
+ RESULT_FILE_EXT, CMP_FILE_EXT))
+ {
+ fail("Output incorrect.");
+ }
+ }
+
+ public void testMultipleTemplates()
+ throws Exception
+ {
+ StringResourceLoader.getRepository().putStringResource("multi1.vm", "I
am the $first template.");
+ StringResourceLoader.getRepository().putStringResource("multi2.vm", "I
am the $second template.");
+
+ Template template1 = RuntimeSingleton.getTemplate(getFileName(null,
"multi1", TMPL_FILE_EXT));
+
+ FileOutputStream fos =
+ new FileOutputStream (
+ getFileName(RESULTS_DIR, "multi1", RESULT_FILE_EXT));
+
+ Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
+
+ VelocityContext context = new VelocityContext();
+ context.put("first", new Integer(1));
+ context.put("second", "two");
+
+ template1.merge(context, writer);
+ writer.flush();
+ writer.close();
+
+ Template template2 = RuntimeSingleton.getTemplate(getFileName(null,
"multi2", TMPL_FILE_EXT));
+
+ fos = new FileOutputStream (
+ getFileName(RESULTS_DIR, "multi2", RESULT_FILE_EXT));
+
+ writer = new BufferedWriter(new OutputStreamWriter(fos));
+
+ template2.merge(context, writer);
+ writer.flush();
+ writer.close();
+
+
+
+ if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi1",
+ RESULT_FILE_EXT, CMP_FILE_EXT))
+ {
+ fail("Template 1 incorrect.");
+ }
+
+ if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi2",
+ RESULT_FILE_EXT, CMP_FILE_EXT))
+ {
+ fail("Template 2 incorrect.");
+ }
+ }
+
+ public void testContentChange()
+ throws Exception
+ {
+ StringResourceLoader.getRepository().putStringResource("change.vm", "I
am the $first template.");
+
+ Template template = RuntimeSingleton.getTemplate(getFileName(null,
"change", TMPL_FILE_EXT));
+
+ FileOutputStream fos =
+ new FileOutputStream (
+ getFileName(RESULTS_DIR, "change1", RESULT_FILE_EXT));
+
+ Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
+
+ VelocityContext context = new VelocityContext();
+ context.put("first", new Integer(1));
+ context.put("second", "two");
+
+ template.merge(context, writer);
+ writer.flush();
+ writer.close();
+
+ StringResourceLoader.getRepository().putStringResource("change.vm", "I
am the $second template.");
+ Thread.sleep(2000L);
+ template = RuntimeSingleton.getTemplate(getFileName(null, "change",
TMPL_FILE_EXT));
+
+ fos = new FileOutputStream (
+ getFileName(RESULTS_DIR, "change2", RESULT_FILE_EXT));
+
+ writer = new BufferedWriter(new OutputStreamWriter(fos));
+
+ template.merge(context, writer);
+ writer.flush();
+ writer.close();
+
+
+
+ if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change1",
+ RESULT_FILE_EXT, CMP_FILE_EXT))
+ {
+ fail("Template 1 incorrect.");
+ }
+
+ if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change2",
+ RESULT_FILE_EXT, CMP_FILE_EXT))
+ {
+ fail("Template 2 incorrect.");
+ }
+ }
+
+}
Propchange:
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/StringResourceLoaderTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/StringResourceLoaderTestCase.java
------------------------------------------------------------------------------
svn:keywords = Id Author Date Revision
Added: jakarta/velocity/engine/trunk/test/stringloader/compare/change1.cmp
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/stringloader/compare/change1.cmp?view=auto&rev=479058
==============================================================================
--- jakarta/velocity/engine/trunk/test/stringloader/compare/change1.cmp (added)
+++ jakarta/velocity/engine/trunk/test/stringloader/compare/change1.cmp Fri Nov
24 16:26:32 2006
@@ -0,0 +1 @@
+I am the 1 template.
\ No newline at end of file
Added: jakarta/velocity/engine/trunk/test/stringloader/compare/change2.cmp
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/stringloader/compare/change2.cmp?view=auto&rev=479058
==============================================================================
--- jakarta/velocity/engine/trunk/test/stringloader/compare/change2.cmp (added)
+++ jakarta/velocity/engine/trunk/test/stringloader/compare/change2.cmp Fri Nov
24 16:26:32 2006
@@ -0,0 +1 @@
+I am the two template.
\ No newline at end of file
Added: jakarta/velocity/engine/trunk/test/stringloader/compare/multi1.cmp
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/stringloader/compare/multi1.cmp?view=auto&rev=479058
==============================================================================
--- jakarta/velocity/engine/trunk/test/stringloader/compare/multi1.cmp (added)
+++ jakarta/velocity/engine/trunk/test/stringloader/compare/multi1.cmp Fri Nov
24 16:26:32 2006
@@ -0,0 +1 @@
+I am the 1 template.
\ No newline at end of file
Added: jakarta/velocity/engine/trunk/test/stringloader/compare/multi2.cmp
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/stringloader/compare/multi2.cmp?view=auto&rev=479058
==============================================================================
--- jakarta/velocity/engine/trunk/test/stringloader/compare/multi2.cmp (added)
+++ jakarta/velocity/engine/trunk/test/stringloader/compare/multi2.cmp Fri Nov
24 16:26:32 2006
@@ -0,0 +1 @@
+I am the two template.
\ No newline at end of file
Added:
jakarta/velocity/engine/trunk/test/stringloader/compare/simpletemplate.cmp
URL:
http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/stringloader/compare/simpletemplate.cmp?view=auto&rev=479058
==============================================================================
--- jakarta/velocity/engine/trunk/test/stringloader/compare/simpletemplate.cmp
(added)
+++ jakarta/velocity/engine/trunk/test/stringloader/compare/simpletemplate.cmp
Fri Nov 24 16:26:32 2006
@@ -0,0 +1 @@
+This is a test for a foo object
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]