Author: wglass Date: Sun Sep 25 00:03:09 2005 New Revision: 291378 URL: http://svn.apache.org/viewcvs?rev=291378&view=rev Log: pass template info to uberspector for methods and properties
Added: jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/InfoTestCase.java jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestException.java jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestImpl.java jakarta/velocity/core/trunk/test/info/ jakarta/velocity/core/trunk/test/info/info1.vm jakarta/velocity/core/trunk/test/info/info2.vm Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java?rev=291378&r1=291377&r2=291378&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/runtime/parser/node/ASTMethod.java Sun Sep 25 00:03:09 2005 @@ -126,15 +126,14 @@ MethodCacheKey mck = new MethodCacheKey(paramClasses); IntrospectionCacheData icd = context.icacheGet( mck ); - Class c = o.getClass(); - + /* * like ASTIdentifier, if we have cache information, and the * Class of Object o is the same as that in the cache, we are * safe. */ - if ( icd != null && icd.contextData == c ) + if ( icd != null && (o != null && icd.contextData == o.getClass()) ) { /* @@ -153,12 +152,12 @@ for (int j = 0; j < paramCount; j++) params[j] = jjtGetChild(j + 1).value(context); - method = rsvc.getUberspect().getMethod(o, methodName, params, new Info("",1,1)); + method = rsvc.getUberspect().getMethod(o, methodName, params, new Info(context.getCurrentTemplateName(), getLine(), getColumn())); if (method != null) { icd = new IntrospectionCacheData(); - icd.contextData = c; + icd.contextData = o.getClass(); icd.thingy = method; context.icachePut( mck, icd ); Modified: jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java?rev=291378&r1=291377&r2=291378&view=diff ============================================================================== --- jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java (original) +++ jakarta/velocity/core/trunk/src/java/org/apache/velocity/util/introspection/UberspectImpl.java Sun Sep 25 00:03:09 2005 @@ -49,7 +49,7 @@ /** * the default Velocity introspector */ - private static Introspector introspector; + protected static Introspector introspector; /** * init - does nothing - we need to have setRuntimeLogger @@ -170,7 +170,7 @@ identifier); } - return (executor != null) ? new VelGetterImpl(executor) : null; + return (executor.isAlive()) ? new VelGetterImpl(executor) : null; } /** Added: jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/InfoTestCase.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/InfoTestCase.java?rev=291378&view=auto ============================================================================== --- jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/InfoTestCase.java (added) +++ jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/InfoTestCase.java Sun Sep 25 00:03:09 2005 @@ -0,0 +1,127 @@ +package org.apache.velocity.test; + +import java.io.IOException; +import java.io.StringWriter; + +import org.apache.velocity.Template; +import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; +import org.apache.velocity.app.VelocityEngine; +import org.apache.velocity.context.Context; +import org.apache.velocity.test.misc.UberspectTestException; +import org.apache.velocity.test.misc.UberspectTestImpl; +import org.apache.velocity.util.introspection.Info; + +import junit.framework.Test; +import junit.framework.TestSuite; + + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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. + */ + + +/** + * Test that the Info class in the Introspector holds the correct information. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Will Glass-Husain</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Llewellyn Falco</a> + * @version $Id: IntrospectorTestCase.java 290983 2005-09-22 17:05:48Z henning $ + */ +public class InfoTestCase extends BaseTestCase implements TemplateTestBase +{ + VelocityEngine ve; + + /** + * Default constructor. + */ + public InfoTestCase(String name) + { + super(name); + } + + public static Test suite () + { + return new TestSuite(InfoTestCase.class); + } + + public void setUp() throws Exception + { + ve = new VelocityEngine(); + ve.setProperty( + "runtime.introspector.uberspect", "org.apache.velocity.test.misc.UberspectTestImpl"); + + ve.setProperty( + Velocity.FILE_RESOURCE_LOADER_PATH, "test/info"); + + ve.init(); + } + + + + public void testInfoProperty() throws Exception + { + // check property + checkInfo("info1.vm", 1, 7); + } + + public void testInfoMethod() throws Exception + { + // check method + checkInfo("info2.vm", 1, 7); + } + + public void checkInfo(String templateName, + int expectedLine, int expectedCol) throws Exception + { + Context context = new VelocityContext(); + StringWriter writer = new StringWriter(); + Template template = ve.getTemplate(templateName, "UTF-8"); + Info info = null; + + context.put("main", this); + + try + { + template.merge(context, writer); + writer.flush(); + fail("Uberspect should have thrown an exception"); + } + catch (UberspectTestException E) + { + info = E.getInfo(); + } + finally + { + try + { + writer.close(); + } + catch (IOException E) + { + } + } + assertInfoEqual(info, templateName, expectedLine, expectedCol); + + } + + private void assertInfoEqual(Info i, String name, int line, int column) + { + assertEquals("Template Name", name, i.getTemplateName()); + assertEquals("Template Line", line, i.getLine()); + assertEquals("Template Column", column, i.getColumn()); + } + +} Added: jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestException.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestException.java?rev=291378&view=auto ============================================================================== --- jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestException.java (added) +++ jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestException.java Sun Sep 25 00:03:09 2005 @@ -0,0 +1,48 @@ +package org.apache.velocity.test.misc; + +import org.apache.velocity.util.introspection.Info; + +/* + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed 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. + */ + + +/** + * Exception that returns an Info object for testing after a introspection problem. + * This extends Error so that it will stop parsing and allow + * internal info to be examined. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Will Glass-Husain</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Llewellyn Falco</a> + * @version $Id: IntrospectorTestCase.java 290983 2005-09-22 17:05:48Z henning $ + */ +public class UberspectTestException extends Error +{ + + Info info; + + public UberspectTestException(String string, Info i) + { + super(string); + info = i; + } + + public Info getInfo() + { + return info; + } + + +} Added: jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestImpl.java URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestImpl.java?rev=291378&view=auto ============================================================================== --- jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestImpl.java (added) +++ jakarta/velocity/core/trunk/src/test/org/apache/velocity/test/misc/UberspectTestImpl.java Sun Sep 25 00:03:09 2005 @@ -0,0 +1,57 @@ +package org.apache.velocity.test.misc; + +import org.apache.velocity.util.introspection.Info; +import org.apache.velocity.util.introspection.UberspectImpl; +import org.apache.velocity.util.introspection.VelMethod; +import org.apache.velocity.util.introspection.VelPropertyGet; + +/* + * Copyright 2000-2004 The Apache Software Foundation. + * + * Licensed 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 introspector that allows testing when methods are not found. + */ +public class UberspectTestImpl extends UberspectImpl +{ + + public VelMethod getMethod(Object obj, String methodName, Object[] args, Info i) throws Exception + { + VelMethod method = super.getMethod(obj, methodName, args, i); + + if (method == null) + { + throw new UberspectTestException("Method " + + obj.getClass().getName() + "." + methodName + + " does not exist.", i); + } + + return method; + } + + public VelPropertyGet getPropertyGet(Object obj, String identifier, Info i) throws Exception + { + VelPropertyGet propertyGet = super.getPropertyGet(obj, identifier, i); + + if (propertyGet == null) + { + throw new UberspectTestException("Did not find "+ + obj.getClass().getName()+"."+identifier, i); + } + + return propertyGet; + } + +} \ No newline at end of file Added: jakarta/velocity/core/trunk/test/info/info1.vm URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/test/info/info1.vm?rev=291378&view=auto ============================================================================== --- jakarta/velocity/core/trunk/test/info/info1.vm (added) +++ jakarta/velocity/core/trunk/test/info/info1.vm Sun Sep 25 00:03:09 2005 @@ -0,0 +1 @@ +$main.unknownField \ No newline at end of file Added: jakarta/velocity/core/trunk/test/info/info2.vm URL: http://svn.apache.org/viewcvs/jakarta/velocity/core/trunk/test/info/info2.vm?rev=291378&view=auto ============================================================================== --- jakarta/velocity/core/trunk/test/info/info2.vm (added) +++ jakarta/velocity/core/trunk/test/info/info2.vm Sun Sep 25 00:03:09 2005 @@ -0,0 +1 @@ +$main.unknownMethod() \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]