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 package org.apache.tapestry.services.impl; 015 016 import static org.easymock.EasyMock.*; 017 018 import java.util.HashMap; 019 import java.util.Map; 020 021 import org.apache.hivemind.Location; 022 import org.apache.hivemind.Messages; 023 import org.apache.tapestry.BaseComponentTestCase; 024 import org.apache.tapestry.IComponent; 025 import org.apache.tapestry.IMarkupWriter; 026 import org.apache.tapestry.IRequestCycle; 027 import org.apache.tapestry.parse.LocalizationToken; 028 import org.testng.annotations.Test; 029 030 031 /** 032 * Tests functionality of {@link LocalizedStringRender} . 033 * 034 * @author jkuhnert 035 */ 036 @Test 037 public class TestLocalizedStringRender extends BaseComponentTestCase 038 { 039 protected LocalizationToken newToken(String tag, String key, boolean raw, Map attributes) 040 { 041 Location l = newLocation(); 042 return new LocalizationToken(tag, key, raw, attributes, l); 043 } 044 045 public void test_No_Attributes() 046 { 047 IRequestCycle cycle = newCycle(false); 048 IMarkupWriter writer = newWriter(); 049 IComponent c = newComponent(); 050 LocalizationToken tok = newToken(null, "hello", false, null); 051 Messages m = newMock(Messages.class); 052 053 LocalizedStringRender render = new LocalizedStringRender(c, tok); 054 055 expect(c.getMessages()).andReturn(m); 056 expect(m.getMessage("hello")).andReturn("World"); 057 058 writer.print("World", false); 059 060 replay(); 061 062 render.render(writer, cycle); 063 064 verify(); 065 } 066 067 public void test_Attributes() 068 { 069 IRequestCycle cycle = newCycle(false); 070 IMarkupWriter writer = newWriter(); 071 IComponent c = newComponent(); 072 073 Map attr = new HashMap(); 074 attr.put("class", "feely mcfeels"); 075 attr.put("joo", "talkin to me?"); 076 077 LocalizationToken tok = newToken(null, "hello", false, attr); 078 Messages m = newMock(Messages.class); 079 080 LocalizedStringRender render = new LocalizedStringRender(c, tok); 081 082 expect(c.getMessages()).andReturn(m); 083 expect(m.getMessage("hello")).andReturn("World"); 084 085 writer.begin("span"); 086 writer.attribute("joo", (String)attr.get("joo")); 087 writer.attribute("class", (String)attr.get("class")); 088 writer.print("World", false); 089 writer.end(); 090 091 replay(); 092 093 render.render(writer, cycle); 094 095 verify(); 096 } 097 098 public void test_Attributes_Tag() 099 { 100 IRequestCycle cycle = newCycle(false); 101 IMarkupWriter writer = newWriter(); 102 IComponent c = newComponent(); 103 104 Map attr = new HashMap(); 105 attr.put("class", "feely mcfeels"); 106 107 LocalizationToken tok = newToken("div", "hello", false, attr); 108 Messages m = newMock(Messages.class); 109 110 LocalizedStringRender render = new LocalizedStringRender(c, tok); 111 112 expect(c.getMessages()).andReturn(m); 113 expect(m.getMessage("hello")).andReturn("World"); 114 115 writer.begin("div"); 116 writer.attribute("class", (String)attr.get("class")); 117 writer.print("World", false); 118 writer.end(); 119 120 replay(); 121 122 render.render(writer, cycle); 123 124 verify(); 125 } 126 }