001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.script; 016 017 import static org.easymock.EasyMock.expect; 018 019 import java.util.Arrays; 020 import java.util.HashMap; 021 import java.util.Iterator; 022 import java.util.Map; 023 024 import org.apache.tapestry.BaseComponentTestCase; 025 import org.testng.annotations.Test; 026 027 /** 028 * Tests {@link org.apache.tapestry.script.ForeachToken}. 029 * 030 * @author Howard M. Lewis Ship 031 * @since 4.0 032 */ 033 @Test(sequential=true) 034 public class TestForeachToken extends BaseComponentTestCase 035 { 036 private static class EchoToken extends AbstractToken 037 { 038 private String _key; 039 040 public EchoToken(String key) 041 { 042 super(null); 043 044 _key = key; 045 } 046 047 public void write(StringBuffer buffer, ScriptSession session) 048 { 049 Object value = session.getSymbols().get(_key); 050 051 buffer.append(value); 052 buffer.append("\n"); 053 } 054 } 055 056 public void testFull() 057 { 058 Map symbols = new HashMap(); 059 060 ScriptSession s = newMock(ScriptSession.class); 061 062 StringBuffer buffer = new StringBuffer(); 063 064 Iterator i = Arrays.asList(new String[] 065 { "buffy", "angel" }).iterator(); 066 067 expect(s.evaluate("EXPRESSION", Iterator.class)).andReturn(i); 068 069 expect(s.getSymbols()).andReturn(symbols).times(5); 070 071 replay(); 072 073 ForeachToken t = new ForeachToken("value", "index", "EXPRESSION", null); 074 t.addToken(new EchoToken("value")); 075 t.addToken(new EchoToken("index")); 076 077 t.write(buffer, s); 078 079 verify(); 080 081 assertEquals("buffy\n0\nangel\n1\n", buffer.toString()); 082 083 assertEquals("angel", symbols.get("value")); 084 assertEquals("1", symbols.get("index")); 085 } 086 087 public void testNoIndex() 088 { 089 Map symbols = new HashMap(); 090 091 symbols.put("index", "none"); 092 093 ScriptSession s = newMock(ScriptSession.class); 094 095 StringBuffer buffer = new StringBuffer(); 096 097 Iterator i = Arrays.asList(new String[] 098 { "buffy", "angel" }).iterator(); 099 100 expect(s.evaluate("EXPRESSION", Iterator.class)).andReturn(i); 101 102 expect(s.getSymbols()).andReturn(symbols).times(5); 103 104 replay(); 105 106 ForeachToken t = new ForeachToken("value", null, "EXPRESSION", null); 107 t.addToken(new EchoToken("value")); 108 t.addToken(new EchoToken("index")); 109 110 t.write(buffer, s); 111 112 verify(); 113 114 assertEquals("buffy\nnone\nangel\nnone\n", buffer.toString()); 115 116 assertEquals("angel", symbols.get("value")); 117 assertEquals("none", symbols.get("index")); 118 } 119 120 public void testNullIterator() 121 { 122 ScriptSession s = newMock(ScriptSession.class); 123 124 expect(s.evaluate("EXPRESSION", Iterator.class)).andReturn(null); 125 126 IScriptToken inner = newMock(IScriptToken.class); 127 128 replay(); 129 ForeachToken t = new ForeachToken("value", "index", "EXPRESSION", null); 130 t.addToken(inner); 131 132 t.write(null, s); 133 134 verify(); 135 } 136 }