[Jsptest-svn-commits] SF.net SVN: jsptest:[246] trunk
Status: Alpha
Brought to you by:
lkoskela
From: <lko...@us...> - 2008-11-08 07:11:57
|
Revision: 246 http://jsptest.svn.sourceforge.net/jsptest/?rev=246&view=rev Author: lkoskela Date: 2008-11-08 07:11:53 +0000 (Sat, 08 Nov 2008) Log Message: ----------- Refactored mocking of taglibs to use a single HashMap of TagKey->Class instead of two. Modified Paths: -------------- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/JspCompilationInfo.java trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspCompiler.java trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/JspTestCase.java trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspCompiler.java trunk/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspCompilerImpl.java trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperExecution.java trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JspCompilerImpl.java trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/MockOptions.java trunk/jsptest-jsp20/src/main/java/org/apache/jasper/compiler/MockTagPluginManager.java trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspCompilerImpl.java Modified: trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/JspCompilationInfo.java =================================================================== --- trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/JspCompilationInfo.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-generic/jsptest-common/src/main/java/net/sf/jsptest/compiler/JspCompilationInfo.java 2008-11-08 07:11:53 UTC (rev 246) @@ -37,12 +37,10 @@ private String classOutputDir; private String webRoot; private Map taglibs; - private Map tags; private static Map compilationHistory = new HashMap(); public JspCompilationInfo() { this.taglibs = new HashMap(); - this.tags = new HashMap(); } private String absolute(String path) { @@ -127,16 +125,8 @@ this.taglibs = new HashMap(taglibs); } - public Map getTags() { - return tags; - } - - public void setTags(Map tags) { - this.tags = new HashMap(tags); - } - public synchronized void compilationWasSuccessful() { - compilationHistory.put(jspSourceLocation, new LastCompile(taglibs, tags)); + compilationHistory.put(jspSourceLocation, new LastCompile(taglibs)); } public synchronized boolean jspCompilationRequired() { @@ -146,7 +136,7 @@ File java = new File(javaSourceLocation); File clazz = new File(classFileLocation); return (doesNotExistOrIsTooOld(clazz) || doesNotExistOrIsTooOld(java) - || jsp.lastModified() > java.lastModified() || taglibsHaveChangedSinceLastCompile() || tagsHaveChangedSinceLastCompile()); + || jsp.lastModified() > java.lastModified() || taglibsHaveChangedSinceLastCompile()); } private boolean taglibsHaveChangedSinceLastCompile() { @@ -157,14 +147,6 @@ return !lastCompile.getTaglibs().equals(getTaglibs()); } - private boolean tagsHaveChangedSinceLastCompile() { - LastCompile lastCompile = (LastCompile) compilationHistory.get(getJspSource()); - if (lastCompile == null) { - return true; - } - return !lastCompile.getTags().equals(getTags()); - } - private boolean doesNotExistOrIsTooOld(File file) { long expirationAge = 5 * 60 * 1000L; long expirationThreshold = System.currentTimeMillis() - expirationAge; @@ -179,17 +161,11 @@ private static final class LastCompile { private final Map taglibs; - private final Map tags; - public LastCompile(Map taglibs, Map tags) { + public LastCompile(Map taglibs) { this.taglibs = taglibs; - this.tags = tags; } - public Map getTags() { - return tags; - } - public Map getTaglibs() { return taglibs; } Modified: trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspCompiler.java =================================================================== --- trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspCompiler.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-generic/jsptest-compiler-api/src/main/java/net/sf/jsptest/compiler/api/JspCompiler.java 2008-11-08 07:11:53 UTC (rev 246) @@ -8,7 +8,7 @@ */ public interface JspCompiler { - Jsp compile(String path, Map taglibs, Map tags); + Jsp compile(String path, Map taglibs); void setWebRoot(String directory); Modified: trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/JspTestCase.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/JspTestCase.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-generic/jsptest-framework/src/main/java/net/sf/jsptest/JspTestCase.java 2008-11-08 07:11:53 UTC (rev 246) @@ -40,7 +40,6 @@ private Map requestAttributes; private Map sessionAttributes; private Map substituteTaglibs; - private Map substituteTags; private JspExecution execution; public JspTestCase() { @@ -55,7 +54,6 @@ requestAttributes = new HashMap(); sessionAttributes = new HashMap(); substituteTaglibs = new HashMap(); - substituteTags = new HashMap(); } /** @@ -127,7 +125,7 @@ + new File(getWebRoot()).getAbsolutePath()); compiler.setWebRoot(getWebRoot()); compiler.setOutputDirectory(getOutputDirectory()); - Jsp jsp = compiler.compile(path, substituteTaglibs, substituteTags); + Jsp jsp = compiler.compile(path, substituteTaglibs); log.debug("Simulating a request to " + path); execution = jsp.request(httpMethod, requestAttributes, sessionAttributes); } @@ -177,6 +175,6 @@ * The new (substitute) implementation to use. */ protected void substituteTag(String prefix, String name, Class newImplementation) { - substituteTags.put(new TagKey(prefix, name), newImplementation); + substituteTaglibs.put(new TagKey(prefix, name), newImplementation); } } Modified: trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspCompiler.java =================================================================== --- trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspCompiler.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-generic/jsptest-framework/src/test/java/net/sf/jsptest/compiler/dummy/FakeJspCompiler.java 2008-11-08 07:11:53 UTC (rev 246) @@ -32,7 +32,7 @@ fakedOutput.append(content); } - public Jsp compile(String path, Map taglibs, Map tags) { + public Jsp compile(String path, Map taglibs) { lastCompiledWebRoot = getWebRoot(); lastCompiledPath = path; return new Jsp() { Modified: trunk/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspCompilerImpl.java =================================================================== --- trunk/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspCompilerImpl.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-jsp12/src/main/java/net/sf/jsptest/compiler/jsp12/JspCompilerImpl.java 2008-11-08 07:11:53 UTC (rev 246) @@ -10,7 +10,7 @@ */ public class JspCompilerImpl implements JspCompiler { - public Jsp compile(String path, Map taglibs, Map tags) { + public Jsp compile(String path, Map taglibs) { throw new RuntimeException("Not implemented"); } Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperCompiler.java 2008-11-08 07:11:53 UTC (rev 246) @@ -107,11 +107,9 @@ * The path to the JSP source file to compile, given relative to the web root. * @param mockTaglibs * Mapping of tag names to tag handler classes - * @param mockTags - * Mapping of <tt>TagKey</tt>s to tag handler classes */ - public JspCompilationInfo compile(String path, Map mockTaglibs, Map mockTags) throws Exception { - JspCompilationInfo info = createJspCompilationInfo(path, mockTaglibs, mockTags); + public JspCompilationInfo compile(String path, Map mockTaglibs) throws Exception { + JspCompilationInfo info = createJspCompilationInfo(path, mockTaglibs); if (info.jspCompilationRequired()) { compileJsp(info); compileJavaToBytecode(info); @@ -139,14 +137,13 @@ } } - private JspCompilationInfo createJspCompilationInfo(String jsp, Map mockTaglibs, Map mockTags) { + private JspCompilationInfo createJspCompilationInfo(String jsp, Map mockTaglibs) { JspCompilationInfo info = new JspCompilationInfo(); info.setJspPath(jsp); info.setClassOutputDir(classOutputBaseDir); info.setJspSource(resolveJspSourceFile(jsp)); info.setWebRoot(getWebRoot()); info.setTaglibs(mockTaglibs); - info.setTags(mockTags); resolveJavaSourceFile(info); resolveClassFileLocation(info); resolveClassName(info); Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperExecution.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperExecution.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JasperExecution.java 2008-11-08 07:11:53 UTC (rev 246) @@ -185,15 +185,10 @@ } private Class compileToClass(String path) throws Exception, ClassNotFoundException { - // TODO: why are the mocks for taglibs and tags handled differently here? - JspCompilationInfo compilation = getCompiler().compile(path, mockTaglibs, getMockTags()); + JspCompilationInfo compilation = getCompiler().compile(path, mockTaglibs); return loadJspClass(compilation.getClassName()); } - protected Map getMockTags() { - return new HashMap(); - } - private JasperCompiler getCompiler() { compiler.setWebRoot(getWebRoot()); compiler.setClassOutputBaseDir(getClassOutputBaseDir()); Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JspCompilerImpl.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JspCompilerImpl.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/JspCompilerImpl.java 2008-11-08 07:11:53 UTC (rev 246) @@ -33,12 +33,12 @@ return webRoot; } - public Jsp compile(final String jspPath, Map taglibs, Map tags) { + public Jsp compile(final String jspPath, Map taglibs) { try { JasperCompiler compiler = new JasperCompiler(); compiler.setWebRoot(getWebRoot()); compiler.setClassOutputBaseDir(getOutputDirectory()); - JspCompilationInfo info = compiler.compile(jspPath, taglibs, tags); + JspCompilationInfo info = compiler.compile(jspPath, taglibs); final Class servletClass = compileToClass(info); return new JspImpl(servletClass); } catch (Exception e) { Modified: trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/MockOptions.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/MockOptions.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-jsp20/src/main/java/net/sf/jsptest/compiler/jsp20/mock/MockOptions.java 2008-11-08 07:11:53 UTC (rev 246) @@ -133,7 +133,7 @@ public TagPluginManager getTagPluginManager() { return new MockTagPluginManager(servletContext, options.getTagPluginManager(), - compilationInfo.getTaglibs(), compilationInfo.getTags()); + compilationInfo.getTaglibs()); } public boolean genStringAsCharArray() { Modified: trunk/jsptest-jsp20/src/main/java/org/apache/jasper/compiler/MockTagPluginManager.java =================================================================== --- trunk/jsptest-jsp20/src/main/java/org/apache/jasper/compiler/MockTagPluginManager.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-jsp20/src/main/java/org/apache/jasper/compiler/MockTagPluginManager.java 2008-11-08 07:11:53 UTC (rev 246) @@ -17,49 +17,35 @@ public class MockTagPluginManager extends TagPluginManager { protected Map mockTaglibs; - protected Map mockTags; - public MockTagPluginManager(ServletContext ctx, TagPluginManager manager, Map taglibs, Map tags) { + public MockTagPluginManager(ServletContext ctx, TagPluginManager manager, Map taglibs) { super(ctx); mockTaglibs = new HashMap(taglibs); - mockTags = new HashMap(tags); } public void apply(Nodes nodes, ErrorDispatcher errorDispatcher, PageInfo pageInfo) throws JasperException { invokePrivateInitWith(errorDispatcher); assignToPrivateField("pageInfo", pageInfo); - visitPageNodes(nodes); + substituteMocksFor(nodes); } - private void visitPageNodes(Nodes nodes) throws JasperException { - replaceTaglibs(nodes); - replaceTags(nodes); + private void substituteMocksFor(Nodes nodes) throws JasperException { + nodes.visit(new TagSubstitutor(mockTaglibs)); } - private void replaceTaglibs(Nodes nodes) throws JasperException { - nodes.visit(new TagSubstitutor() { + /** + * Walks through the node tree representing the JSP and replaces the <i>handler</i> of certain + * tags with a mock implementation if one has been configured. + */ + private static class TagSubstitutor extends Node.Visitor { - protected Class substitute(TagInfo tagInfo) { - String tagName = tagInfo.getTagLibrary().getPrefixString(); - return (Class) mockTaglibs.get(new TagKey(tagName)); - } - }); - } + private final Map mocks; - private void replaceTags(Nodes nodes) throws JasperException { - nodes.visit(new TagSubstitutor() { + public TagSubstitutor(Map mockTaglibs) { + this.mocks = mockTaglibs; + } - protected Class substitute(TagInfo tagInfo) { - String prefix = tagInfo.getTagLibrary().getPrefixString(); - String name = tagInfo.getTagName(); - return (Class) mockTags.get(new TagKey(prefix, name)); - } - }); - } - - private static abstract class TagSubstitutor extends Node.Visitor { - public void visit(Node.CustomTag n) throws JasperException { Class mockClass = substitute(n.getTagInfo()); if (mockClass != null) { @@ -68,7 +54,21 @@ super.visit(n); } - protected abstract Class substitute(TagInfo tagInfo); + private Class substitute(TagInfo tagInfo) { + String prefix = tagInfo.getTagLibrary().getPrefixString(); + String name = tagInfo.getTagName(); + TagKey[] matchingOrder = new TagKey[] { new TagKey(prefix, name), new TagKey(prefix) }; + return firstMatch(mocks, matchingOrder); + } + + private Class firstMatch(Map map, TagKey[] keys) { + for (int i = 0; i < keys.length; i++) { + if (map.containsKey(keys[i])) { + return (Class) mocks.get(keys[i]); + } + } + return null; + } } private void assignToPrivateField(String fieldName, Object value) { Modified: trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspCompilerImpl.java =================================================================== --- trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspCompilerImpl.java 2008-11-08 06:42:59 UTC (rev 245) +++ trunk/jsptest-jsp21/src/main/java/net/sf/jsptest/compiler/jsp21/JspCompilerImpl.java 2008-11-08 07:11:53 UTC (rev 246) @@ -10,7 +10,7 @@ */ public class JspCompilerImpl implements JspCompiler { - public Jsp compile(String path, Map taglibs, Map tags) { + public Jsp compile(String path, Map taglibs) { throw new RuntimeException("Not implemented"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |