1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.core;
17
18 import java.io.StringReader;
19 import java.io.StringWriter;
20 import java.util.List;
21 import java.util.ArrayList;
22
23 import org.apache.commons.jelly.JellyContext;
24 import org.apache.commons.jelly.JellyTagException;
25 import org.apache.commons.jelly.MissingAttributeException;
26 import org.apache.commons.jelly.Script;
27 import org.apache.commons.jelly.TagLibrary;
28 import org.apache.commons.jelly.TagSupport;
29 import org.apache.commons.jelly.XMLOutput;
30
31 import junit.framework.TestCase;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
34
35 /***
36 * This test illustrates pre-1.0 Jelly behavior that did not cache tags by default. Many user tag implementations
37 * will assume that they are at an initalized state when doTag() is called, rather than still being "dirty" from a
38 * prior run.
39 *
40 * @author <a href="mailto:proyal@apache.org">peter royal</a>
41 */
42 public class TestUnexpectedTagCaching extends TestCase
43 {
44 public void testExpectFreshTagOnEachRun() throws Exception
45 {
46 final JellyContext scriptContext = new JellyContext();
47
48 scriptContext.registerTagLibrary( "a", new TestCachingTagLibrary() );
49
50 final String scriptText = "<a:write xmlns:a=\"a\"><a:set>${message}</a:set></a:write>";
51 final Script script = scriptContext.compileScript( new InputSource( new StringReader( scriptText ) ) );
52
53 assertScriptResult( "one", script, scriptContext );
54 assertScriptResult( "two", script, scriptContext );
55 assertScriptResult( "three", script, scriptContext );
56 }
57
58 private static void assertScriptResult( final String message,
59 final Script script,
60 final JellyContext scriptContext ) throws JellyTagException
61 {
62 final JellyContext context = new JellyContext( scriptContext );
63
64 context.setVariable( "message", message );
65
66 final StringWriter writer = new StringWriter();
67
68 script.run( context, XMLOutput.createXMLOutput( writer ) );
69
70 assertEquals( "["+ message + "]", writer.toString() );
71 }
72
73 public static class TestCachingTagLibrary extends TagLibrary
74 {
75 public TestCachingTagLibrary()
76 {
77 registerTag( "set", SetTag.class );
78 registerTag( "write", WriteTag.class );
79 }
80 }
81
82 public static class WriteTag extends TagSupport
83 {
84 private List m_strings = new ArrayList();
85
86 public List getStrings()
87 {
88 return m_strings;
89 }
90
91 public void addString( final String string )
92 {
93 m_strings.add( string );
94 }
95
96 public void doTag( final XMLOutput output ) throws MissingAttributeException, JellyTagException
97 {
98 invokeBody( output );
99
100 try
101 {
102 output.write( getStrings().toString() );
103 }
104 catch( SAXException e )
105 {
106 throw new JellyTagException( "Unable to write message", e );
107 }
108 }
109 }
110
111 public static class SetTag extends TagSupport
112 {
113 public void doTag( final XMLOutput output ) throws MissingAttributeException, JellyTagException
114 {
115 ( (WriteTag)getParent() ).addString( getBodyText() );
116 }
117 }
118 }