1 15 package org.apache.tapestry.script; 16 17 import java.util.Arrays ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.hivemind.test.HiveMindTestCase; 23 import org.easymock.MockControl; 24 25 31 public class TestForeachToken extends HiveMindTestCase 32 { 33 private static class EchoToken extends AbstractToken 34 { 35 private String _key; 36 37 public EchoToken(String key) 38 { 39 super(null); 40 41 _key = key; 42 } 43 44 public void write(StringBuffer buffer, ScriptSession session) 45 { 46 Object value = session.getSymbols().get(_key); 47 48 buffer.append(value); 49 buffer.append("\n"); 50 } 51 } 52 53 public void testFull() 54 { 55 Map symbols = new HashMap (); 56 57 MockControl sc = newControl(ScriptSession.class); 58 ScriptSession s = (ScriptSession) sc.getMock(); 59 60 StringBuffer buffer = new StringBuffer (); 61 62 Iterator i = Arrays.asList(new String [] 63 { "buffy", "angel" }).iterator(); 64 65 s.evaluate("EXPRESSION", Iterator .class); 66 sc.setReturnValue(i); 67 68 s.getSymbols(); 69 sc.setReturnValue(symbols, 5); 70 71 replayControls(); 72 73 ForeachToken t = new ForeachToken("value", "index", "EXPRESSION", null); 74 t.addToken(new EchoToken("value")); 75 t.addToken(new EchoToken("index")); 76 77 t.write(buffer, s); 78 79 verifyControls(); 80 81 assertEquals("buffy\n0\nangel\n1\n", buffer.toString()); 82 83 assertEquals("angel", symbols.get("value")); 84 assertEquals("1", symbols.get("index")); 85 } 86 87 public void testNoIndex() 88 { 89 Map symbols = new HashMap (); 90 91 symbols.put("index", "none"); 92 93 MockControl sc = newControl(ScriptSession.class); 94 ScriptSession s = (ScriptSession) sc.getMock(); 95 96 StringBuffer buffer = new StringBuffer (); 97 98 Iterator i = Arrays.asList(new String [] 99 { "buffy", "angel" }).iterator(); 100 101 s.evaluate("EXPRESSION", Iterator .class); 102 sc.setReturnValue(i); 103 104 s.getSymbols(); 105 sc.setReturnValue(symbols, 5); 106 107 replayControls(); 108 109 ForeachToken t = new ForeachToken("value", null, "EXPRESSION", null); 110 t.addToken(new EchoToken("value")); 111 t.addToken(new EchoToken("index")); 112 113 t.write(buffer, s); 114 115 verifyControls(); 116 117 assertEquals("buffy\nnone\nangel\nnone\n", buffer.toString()); 118 119 assertEquals("angel", symbols.get("value")); 120 assertEquals("none", symbols.get("index")); 121 } 122 123 public void testNullIterator() 124 { 125 MockControl sc = newControl(ScriptSession.class); 126 ScriptSession s = (ScriptSession) sc.getMock(); 127 128 s.evaluate("EXPRESSION", Iterator .class); 129 sc.setReturnValue(null); 130 131 IScriptToken inner = (IScriptToken) newMock(IScriptToken.class); 132 133 replayControls(); 134 ForeachToken t = new ForeachToken("value", "index", "EXPRESSION", null); 135 t.addToken(inner); 136 137 t.write(null, s); 138 139 verifyControls(); 140 } 141 } | Popular Tags |