1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.tags;
17  
18  import static org.easymock.EasyMock.createMock;
19  import static org.easymock.EasyMock.expect;
20  import static org.easymock.EasyMock.replay;
21  import static org.easymock.EasyMock.verify;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.util.ArrayList;
27  import java.util.Calendar;
28  import java.util.Date;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.junit.Test;
37  import org.seasar.cubby.internal.controller.ThreadContext;
38  import org.seasar.cubby.internal.controller.ThreadContext.Command;
39  
40  public class CubbyFunctionsTest {
41  
42  	@Test
43  	public void containsInCollection() {
44  		List<String> action = new ArrayList<String>();
45  		action.add("unvalidate");
46  		action.add("validateRecord");
47  		action.add("branch");
48  		assertFalse(CubbyFunctions.contains(action, "validate"));
49  		action.add("validate");
50  		assertTrue(CubbyFunctions.contains(action, "validate"));
51  	}
52  
53  	@Test
54  	public void containsInArray() {
55  		String[] array1 = { "unvalidate", "validateRecord", "branch" };
56  		String[] array2 = { "unvalidate", "validateRecord", "branch",
57  				"validate" };
58  		assertFalse(CubbyFunctions.contains(array1, "validate"));
59  		assertTrue(CubbyFunctions.contains(array2, "validate"));
60  	}
61  
62  	@Test
63  	public void containsInNull() {
64  		assertFalse(CubbyFunctions.contains(null, null));
65  	}
66  
67  	@Test
68  	public void containesKey() {
69  		Map<String, Object> map = new HashMap<String, Object>();
70  		map.put("name", "value");
71  		assertTrue(CubbyFunctions.containsKey(map, "name"));
72  		assertFalse(CubbyFunctions.containsKey(map, "value"));
73  	}
74  
75  	@Test
76  	public void containesValue() {
77  		Map<String, Object> map = new HashMap<String, Object>();
78  		map.put("name", "value");
79  		assertTrue(CubbyFunctions.containsValue(map, "value"));
80  		assertFalse(CubbyFunctions.containsValue(map, "name"));
81  	}
82  
83  	@Test
84  	public void odd() {
85  		assertEquals("a", CubbyFunctions.odd(0, "a, b, c"));
86  		assertEquals("b", CubbyFunctions.odd(1, "a, b, c"));
87  		assertEquals("c", CubbyFunctions.odd(2, "a, b, c"));
88  		assertEquals("a", CubbyFunctions.odd(3, "a, b, c"));
89  		assertEquals("b", CubbyFunctions.odd(4, "a, b, c"));
90  		assertEquals("c", CubbyFunctions.odd(5, "a, b, c"));
91  	}
92  
93  	@Test
94  	public void out() {
95  		assertEquals("abc&amp;&lt;&gt;&quot;&#39;def", CubbyFunctions
96  				.out("abc&<>\"'def"));
97  		assertEquals("", CubbyFunctions.out(null));
98  	}
99  
100 	@Test
101 	public void dateFormat() {
102 		Calendar calendar = Calendar.getInstance();
103 		calendar.set(2008, Calendar.DECEMBER, 24);
104 		Date date = calendar.getTime();
105 		assertEquals("20081224", CubbyFunctions.dateFormat(date, "yyyyMMdd"));
106 		assertEquals("", CubbyFunctions.dateFormat(new Object(), "yyyyMMdd"));
107 	}
108 
109 	@Test
110 	public void ifrender() {
111 		assertEquals("abc", CubbyFunctions.ifrender(true, "abc"));
112 		assertEquals(TagUtils.REMOVE_ATTRIBUTE, CubbyFunctions.ifrender(false,
113 				"abc"));
114 	}
115 
116 	@Test
117 	public void urlWithUTF8() throws Exception {
118 		HttpServletRequest request = createMock(HttpServletRequest.class);
119 		expect(request.getCharacterEncoding()).andStubReturn("UTF-8");
120 		HttpServletResponse response = createMock(HttpServletResponse.class);
121 		replay(request, response);
122 
123 		ThreadContext.runInContext(request, response, new Command() {
124 
125 			public void execute(final HttpServletRequest request,
126 					final HttpServletResponse response) throws Exception {
127 				assertEquals("abc%20%E3%81%82%E3%81%84%E3%81%86",
128 						CubbyFunctions.url("abc あいう"));
129 				assertEquals("", CubbyFunctions.url(null));
130 			}
131 
132 		});
133 
134 		verify(request, response);
135 	}
136 
137 	@Test
138 	public void urlWithWindows31J() throws Exception {
139 		HttpServletRequest request = createMock(HttpServletRequest.class);
140 		expect(request.getCharacterEncoding()).andStubReturn("Windows-31J");
141 		HttpServletResponse response = createMock(HttpServletResponse.class);
142 		replay(request, response);
143 
144 		ThreadContext.runInContext(request, response, new Command() {
145 
146 			public void execute(final HttpServletRequest request,
147 					final HttpServletResponse response) throws Exception {
148 				assertEquals("abc%20%82%A0%82%A2%82%A4", CubbyFunctions
149 						.url("abc あいう"));
150 				assertEquals("", CubbyFunctions.url(null));
151 			}
152 
153 		});
154 
155 		verify(request, response);
156 	}
157 
158 }