1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.compiler;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import javax.faces.context.ResponseWriter;
24 import org.apache.myfaces.view.facelets.FaceletTestCase;
25 import org.apache.myfaces.view.facelets.util.FastWriter;
26 import org.junit.Test;
27 import org.testng.Assert;
28
29
30
31
32
33 public class CompressSpacesTextUnitTestCase extends FaceletTestCase
34 {
35
36 @Test
37 public void testSimpleCompress1() throws Exception {
38
39 List<Instruction> instructions = new ArrayList<Instruction>();
40
41 instructions.add(new LiteralTextInstruction(" "));
42 instructions.add(new StartElementInstruction("p"));
43 instructions.add(new LiteralTextInstruction(" hello "));
44 instructions.add(new EndElementInstruction("p"));
45 instructions.add(new StartElementInstruction("tr"));
46 instructions.add(new StartElementInstruction("td"));
47 instructions.add(new LiteralTextInstruction(" "));
48 instructions.add(new EndElementInstruction("td"));
49 instructions.add(new EndElementInstruction("tr"));
50 instructions.add(new LiteralTextInstruction(" "));
51
52 int size = instructions.size();
53 size = TextUnit.compressSpaces(instructions, size);
54
55 FastWriter fw = new FastWriter();
56 ResponseWriter rw = facesContext.getResponseWriter();
57 rw = rw.cloneWithWriter(fw);
58 facesContext.setResponseWriter(rw);
59
60 for (Instruction i : instructions)
61 {
62 i.write(facesContext);
63 }
64
65 Assert.assertEquals(fw.toString(), " <p> hello </p><tr><td/></tr> ");
66 }
67
68 @Test
69 public void testSimpleCompress2() throws Exception {
70
71 List<Instruction> instructions = new ArrayList<Instruction>();
72
73 instructions.add(new StartElementInstruction("p"));
74 instructions.add(new LiteralTextInstruction(" hello "));
75 instructions.add(new EndElementInstruction("p"));
76
77 int size = instructions.size();
78 size = TextUnit.compressSpaces(instructions, size);
79
80 FastWriter fw = new FastWriter();
81 ResponseWriter rw = facesContext.getResponseWriter();
82 rw = rw.cloneWithWriter(fw);
83 facesContext.setResponseWriter(rw);
84
85 for (Instruction i : instructions)
86 {
87 i.write(facesContext);
88 }
89
90 Assert.assertEquals(fw.toString(), "<p> hello </p>");
91
92 }
93
94 @Test
95 public void testSimpleCompress3() throws Exception {
96
97 List<Instruction> instructions = new ArrayList<Instruction>();
98
99 instructions.add(new LiteralTextInstruction(" "));
100 instructions.add(new StartElementInstruction("p"));
101 instructions.add(new LiteralTextInstruction(" hello "));
102 instructions.add(new EndElementInstruction("p"));
103 instructions.add(new StartElementInstruction("tr"));
104 instructions.add(new StartElementInstruction("td"));
105 instructions.add(new LiteralTextInstruction("\n "));
106 instructions.add(new EndElementInstruction("td"));
107 instructions.add(new EndElementInstruction("tr"));
108 instructions.add(new LiteralTextInstruction("\n "));
109
110 int size = instructions.size();
111 size = TextUnit.compressSpaces(instructions, size);
112
113 FastWriter fw = new FastWriter();
114 ResponseWriter rw = facesContext.getResponseWriter();
115 rw = rw.cloneWithWriter(fw);
116 facesContext.setResponseWriter(rw);
117
118 for (Instruction i : instructions)
119 {
120 i.write(facesContext);
121 }
122
123 Assert.assertEquals(fw.toString(), " <p> hello </p><tr><td/></tr>\n");
124 }
125
126 @Test
127 public void testSimpleCompress4() throws Exception {
128
129 List<Instruction> instructions = new ArrayList<Instruction>();
130
131 instructions.add(new LiteralTextInstruction(" \n "));
132
133 int size = instructions.size();
134 size = TextUnit.compressSpaces(instructions, size);
135
136 FastWriter fw = new FastWriter();
137 ResponseWriter rw = facesContext.getResponseWriter();
138 rw = rw.cloneWithWriter(fw);
139 facesContext.setResponseWriter(rw);
140
141 for (Instruction i : instructions)
142 {
143 i.write(facesContext);
144 }
145
146 Assert.assertEquals(fw.toString(), "\n");
147
148 }
149
150 @Test
151 public void testSimpleCompress4_1() throws Exception {
152
153 List<Instruction> instructions = new ArrayList<Instruction>();
154
155 instructions.add(new LiteralTextInstruction(" \r\n "));
156
157 int size = instructions.size();
158 size = TextUnit.compressSpaces(instructions, size);
159
160 FastWriter fw = new FastWriter();
161 ResponseWriter rw = facesContext.getResponseWriter();
162 rw = rw.cloneWithWriter(fw);
163 facesContext.setResponseWriter(rw);
164
165 for (Instruction i : instructions)
166 {
167 i.write(facesContext);
168 }
169
170 Assert.assertEquals(fw.toString(), "\r\n");
171
172 }
173
174 @Test
175 public void testSimpleCompress5() throws Exception {
176
177 List<Instruction> instructions = new ArrayList<Instruction>();
178
179 instructions.add(new LiteralTextInstruction(" \n "));
180
181 int size = instructions.size();
182 size = TextUnit.compressSpaces(instructions, size);
183
184 FastWriter fw = new FastWriter();
185 ResponseWriter rw = facesContext.getResponseWriter();
186 rw = rw.cloneWithWriter(fw);
187 facesContext.setResponseWriter(rw);
188
189 for (Instruction i : instructions)
190 {
191 i.write(facesContext);
192 }
193
194 Assert.assertEquals(fw.toString(), "&#160;\n");
195
196 }
197
198 @Test
199 public void testSimpleCompress6() throws Exception {
200
201 List<Instruction> instructions = new ArrayList<Instruction>();
202
203 instructions.add(new LiteralTextInstruction(" \r\n"));
204 instructions.add(new StartElementInstruction("script"));
205 instructions.add(new LiteralAttributeInstruction("type", "text/javascript"));
206 instructions.add(new LiteralTextInstruction("\r\n //"));
207 instructions.add(new LiteralXMLInstruction("<![CDATA[ "));
208 instructions.add(new LiteralTextInstruction(" \r\n someJavascript();\r\n //"));
209 instructions.add(new LiteralXMLInstruction("]]>"));
210 instructions.add(new LiteralTextInstruction("\r\n"));
211 instructions.add(new EndElementInstruction("script"));
212 instructions.add(new LiteralTextInstruction(" "));
213
214 int size = instructions.size();
215 size = TextUnit.compressSpaces(instructions, size);
216
217 FastWriter fw = new FastWriter();
218 ResponseWriter rw = facesContext.getResponseWriter();
219 rw = rw.cloneWithWriter(fw);
220 facesContext.setResponseWriter(rw);
221
222 for (Instruction i : instructions)
223 {
224 i.write(facesContext);
225 }
226
227 Assert.assertEquals(fw.toString(), "\r\n<script type=\"text/javascript\">\r\n//<![CDATA[ "+
228 "\r\n someJavascript();\r\n //]]>\r\n</script> ");
229 }
230
231 @Test
232 public void testCompressOnELExpressions() throws Exception
233 {
234 Assert.assertEquals(tryCompress("#{bean.name}"), "#{bean.name}");
235 Assert.assertEquals(tryCompress(" #{bean.name}"), " #{bean.name}");
236 Assert.assertEquals(tryCompress("#{bean.name} "), "#{bean.name} ");
237 String text = tryCompress(" #{bean.name} ");
238 Assert.assertEquals(tryCompress(" #{bean.name} "), " #{bean.name} ");
239 Assert.assertEquals(tryCompress("\n #{bean.name}\n "), "\n#{bean.name}\n");
240 Assert.assertEquals(tryCompress(" \r\n #{bean.name} \r\n "), "\r\n#{bean.name} ");
241 }
242
243 public String tryCompress(String value)
244 {
245
246 return TextUnit.compressELText(value);
247 }
248 }