1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.tags;
18  
19  import static org.easymock.EasyMock.createMock;
20  import static org.easymock.EasyMock.expect;
21  import static org.easymock.EasyMock.replay;
22  import static org.easymock.EasyMock.verify;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertFalse;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.util.ArrayList;
28  import java.util.Calendar;
29  import java.util.Date;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  import org.junit.Test;
38  import org.seasar.cubby.internal.controller.ThreadContext;
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", "validate"};
57  		assertFalse(CubbyFunctions.contains(array1, "validate"));
58  		assertTrue(CubbyFunctions.contains(array2, "validate"));
59  	}
60  
61  	@Test
62  	public void containsInNull() {
63  		assertFalse(CubbyFunctions.contains(null, null));
64  	}
65  
66  	@Test
67  	public void containesKey() {
68  		Map<String, Object> map = new HashMap<String, Object>();
69  		map.put("name", "value");
70  		assertTrue(CubbyFunctions.containsKey(map, "name"));
71  		assertFalse(CubbyFunctions.containsKey(map, "value"));
72  	}
73  
74  	@Test
75  	public void containesValue() {
76  		Map<String, Object> map = new HashMap<String, Object>();
77  		map.put("name", "value");
78  		assertTrue(CubbyFunctions.containsValue(map, "value"));
79  		assertFalse(CubbyFunctions.containsValue(map, "name"));
80  	}
81  
82  	@Test
83  	public void odd() {
84  		assertEquals("a", CubbyFunctions.odd(0, "a, b, c"));
85  		assertEquals("b", CubbyFunctions.odd(1, "a, b, c"));
86  		assertEquals("c", CubbyFunctions.odd(2, "a, b, c"));
87  		assertEquals("a", CubbyFunctions.odd(3, "a, b, c"));
88  		assertEquals("b", CubbyFunctions.odd(4, "a, b, c"));
89  		assertEquals("c", CubbyFunctions.odd(5, "a, b, c"));
90  	}
91  
92  	@Test
93  	public void out() {
94  		assertEquals("abc&amp;&lt;&gt;&quot;&#39;def", CubbyFunctions
95  				.out("abc&<>\"'def"));
96  		assertEquals("", CubbyFunctions.out(null));
97  	}
98  
99  	@Test
100 	public void dateFormat() {
101 		Calendar calendar = Calendar.getInstance();
102 		calendar.set(2008, Calendar.DECEMBER, 24);
103 		Date date = calendar.getTime();
104 		assertEquals("20081224", CubbyFunctions.dateFormat(date, "yyyyMMdd"));
105 		assertEquals("", CubbyFunctions.dateFormat(new Object(), "yyyyMMdd"));
106 	}
107 
108 	@Test
109 	public void ifrender() {
110 		assertEquals("abc", CubbyFunctions.ifrender(true, "abc"));
111 		assertEquals(TagUtils.REMOVE_ATTRIBUTE, CubbyFunctions.ifrender(false,
112 				"abc"));
113 	}
114 
115 	@Test
116 	public void urlWithUTF8() throws Exception {
117 		HttpServletRequest request = createMock(HttpServletRequest.class);
118 		expect(request.getCharacterEncoding()).andStubReturn("UTF-8");
119 		HttpServletResponse response = createMock(HttpServletResponse.class);
120 		replay(request, response);
121 
122 		ThreadContext.enter(request, response);
123 		try {
124 			assertEquals("abc%20%E3%81%82%E3%81%84%E3%81%86", CubbyFunctions
125 					.url("abc あいう"));
126 			assertEquals("", CubbyFunctions.url(null));
127 		} finally {
128 			ThreadContext.exit();
129 		}
130 		ThreadContext.remove();
131 
132 		verify(request, response);
133 	}
134 
135 	@Test
136 	public void urlWithWindows31J() throws Exception {
137 		HttpServletRequest request = createMock(HttpServletRequest.class);
138 		expect(request.getCharacterEncoding()).andStubReturn("Windows-31J");
139 		HttpServletResponse response = createMock(HttpServletResponse.class);
140 		replay(request, response);
141 
142 		ThreadContext.enter(request, response);
143 		try {
144 			assertEquals("abc%20%82%A0%82%A2%82%A4", CubbyFunctions
145 					.url("abc あいう"));
146 			assertEquals("", CubbyFunctions.url(null));
147 		} finally {
148 			ThreadContext.exit();
149 		}
150 		ThreadContext.remove();
151 
152 		verify(request, response);
153 	}
154 
155 }