View Javadoc

1   /*
2    * Copyright 2012 The Apache Software Foundation.
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package javax.faces.component;
17  
18  import java.io.Serializable;
19  import javax.faces.context.FacesContext;
20  import javax.faces.convert.Converter;
21  import javax.faces.convert.LongConverter;
22  import junit.framework.Assert;
23  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
24  import org.junit.Test;
25  
26  /**
27   *
28   * @author lu4242
29   */
30  public class UIOutputPSSTest extends AbstractJsfTestCase
31  {
32      
33      /**
34       * Check null delta when converter only implements Converter interface.
35       * 
36       * @throws Exception 
37       */
38      @Test
39      public void testConverterDeltaState1() throws Exception
40      {
41          UIOutput output = new UIOutput();
42          output.setId("output1");
43          output.setConverter(new LongConverter());
44          output.markInitialState();
45          
46          Object state1 = output.saveState(facesContext);
47          
48          // null delta expected
49          Assert.assertNull(state1);
50          
51          UIOutput output2 = new UIOutput();
52          output2.setId("output1");
53          Converter converter2 = new LongConverter();
54          output2.setConverter(converter2);
55          output2.markInitialState();
56          
57          output2.restoreState(facesContext, state1);
58          
59          // Check converter is not replaced, because it is not necessary
60          Assert.assertEquals(converter2, output2.getConverter());
61      }
62      
63      @Test
64      public void testConverterDeltaState2() throws Exception
65      {
66          UIOutput output = new UIOutput();
67          output.setId("output1");
68          output.setConverter(new LongSerializableConverter());
69          output.markInitialState();
70          
71          Object state1 = output.saveState(facesContext);
72          
73          // null delta expected
74          Assert.assertNull(state1);
75          
76          UIOutput output2 = new UIOutput();
77          output2.setId("output1");
78          Converter converter2 = new LongSerializableConverter();
79          output2.setConverter(converter2);
80          output2.markInitialState();
81          
82          output2.restoreState(facesContext, state1);
83          
84          // Check converter is not replaced, because it is not necessary
85          Assert.assertEquals(converter2, output2.getConverter());
86      }
87      
88      @Test
89      public void testConverterDeltaState3() throws Exception
90      {
91          UIOutput output = new UIOutput();
92          output.setId("output1");
93          output.setConverter(new LongStateHolderConverter());
94          output.markInitialState();
95          
96          Object state1 = output.saveState(facesContext);
97          
98          // StateHolder only force always to save the state
99          Assert.assertNotNull(state1);
100         
101         UIOutput output2 = new UIOutput();
102         output2.setId("output1");
103         Converter converter2 = new LongStateHolderConverter();
104         output2.setConverter(converter2);
105         output2.markInitialState();
106         
107         output2.restoreState(facesContext, state1);
108         
109         // Check converter IS replaced, because it should be always stored into the state
110         Assert.assertNotSame(converter2, output2.getConverter());
111     }
112     
113     @Test
114     public void testConverterDeltaState4() throws Exception
115     {
116         UIOutput output = new UIOutput();
117         output.setId("output1");
118         output.setConverter(new LongPartialStateHolderConverter());
119         output.markInitialState();
120         
121         Object state1 = output.saveState(facesContext);
122         
123         // PartialStateHolder only save state if delta is null
124         Assert.assertNull(state1);
125         
126         UIOutput output2 = new UIOutput();
127         output2.setId("output1");
128         Converter converter2 = new LongPartialStateHolderConverter();
129         output2.setConverter(converter2);
130         output2.markInitialState();
131         
132         output2.restoreState(facesContext, state1);
133         
134         // Check converter is not replaced, because it is not necessary
135         Assert.assertEquals(converter2, output2.getConverter());
136     }
137     
138     @Test
139     public void testConverterDeltaState5() throws Exception
140     {
141         UIOutput output = new UIOutput();
142         output.setId("output1");
143         output.setConverter(new LongPartialStateHolderConverter2());
144         output.markInitialState();
145         
146         Object state1 = output.saveState(facesContext);
147         
148         // PartialStateHolder only save state if delta is null
149         Assert.assertNotNull(state1);
150         
151         UIOutput output2 = new UIOutput();
152         output2.setId("output1");
153         Converter converter2 = new LongPartialStateHolderConverter2();
154         output2.setConverter(converter2);
155         output2.markInitialState();
156         
157         output2.restoreState(facesContext, state1);
158         
159         // Check converter is not replaced, because it is not necessary (delta change)
160         Assert.assertEquals(converter2, output2.getConverter());
161     }
162     
163     @Test
164     public void testConverterDeltaState6() throws Exception
165     {
166         UIOutput output = new UIOutput();
167         output.setId("output1");
168         output.setConverter(new LongConverter());
169         output.markInitialState();
170         
171         //Force parentDelta to be not null
172         output.getAttributes().put("style", "style1");
173         
174         Object[] state1 = (Object[])output.saveState(facesContext);
175         
176         // null delta expected
177         Assert.assertNotNull(state1);
178         Assert.assertEquals(state1.length, 1);
179         
180         UIOutput output2 = new UIOutput();
181         output2.setId("output1");
182         Converter converter2 = new LongConverter();
183         output2.setConverter(converter2);
184         output2.markInitialState();
185         
186         output2.restoreState(facesContext, state1);
187         
188         // Check converter is not replaced, because it is not necessary
189         Assert.assertEquals(converter2, output2.getConverter());
190         
191         // Check parentDelta works
192         Assert.assertEquals("style1", output2.getAttributes().get("style"));
193     }
194 
195     @Test
196     public void testConverterDeltaState7() throws Exception
197     {
198         UIOutput output = new UIOutput();
199         output.setId("output1");
200         output.setConverter(new LongSerializableConverter());
201         output.markInitialState();
202         
203         //Force parentDelta to be not null
204         output.getAttributes().put("style", "style1");
205         
206         Object[] state1 = (Object[])output.saveState(facesContext);
207         
208         // null delta expected
209         Assert.assertNotNull(state1);
210         Assert.assertEquals(state1.length, 1);
211         
212         UIOutput output2 = new UIOutput();
213         output2.setId("output1");
214         Converter converter2 = new LongSerializableConverter();
215         output2.setConverter(converter2);
216         output2.markInitialState();
217         
218         output2.restoreState(facesContext, state1);
219         
220         // Check converter is not replaced, because it is not necessary
221         Assert.assertEquals(converter2, output2.getConverter());
222         
223         // Check parentDelta works
224         Assert.assertEquals("style1", output2.getAttributes().get("style"));
225     }
226     
227     @Test
228     public void testConverterDeltaState8() throws Exception
229     {
230         UIOutput output = new UIOutput();
231         output.setId("output1");
232         output.setConverter(new LongStateHolderConverter());
233         output.markInitialState();
234         
235         //Force parentDelta to be not null
236         output.getAttributes().put("style", "style1");
237         
238         Object[] state1 = (Object[])output.saveState(facesContext);
239         
240         // StateHolder force state
241         Assert.assertNotNull(state1);
242         Assert.assertEquals(state1.length, 2);
243         Assert.assertNotNull(state1[1]);
244         Assert.assertNotNull(state1[0]);
245         
246         UIOutput output2 = new UIOutput();
247         output2.setId("output1");
248         Converter converter2 = new LongStateHolderConverter();
249         output2.setConverter(converter2);
250         output2.markInitialState();
251         
252         output2.restoreState(facesContext, state1);
253         
254         // Check converter IS replaced, because it should be always stored into the state
255         Assert.assertNotSame(converter2, output2.getConverter());
256         
257         // Check parentDelta works
258         Assert.assertEquals("style1", output2.getAttributes().get("style"));
259     }
260 
261     @Test
262     public void testConverterDeltaState9() throws Exception
263     {
264         UIOutput output = new UIOutput();
265         output.setId("output1");
266         output.setConverter(new LongPartialStateHolderConverter());
267         output.markInitialState();
268         
269         //Force parentDelta to be not null
270         output.getAttributes().put("style", "style1");
271         
272         Object[] state1 = (Object[])output.saveState(facesContext);
273         
274         // null delta expected
275         Assert.assertNotNull(state1);
276         Assert.assertEquals(state1.length, 1);
277         
278         UIOutput output2 = new UIOutput();
279         output2.setId("output1");
280         Converter converter2 = new LongPartialStateHolderConverter();
281         output2.setConverter(converter2);
282         output2.markInitialState();
283         
284         output2.restoreState(facesContext, state1);
285         
286         // Check converter is not replaced, because it is not necessary
287         Assert.assertEquals(converter2, output2.getConverter());
288         
289         // Check parentDelta works
290         Assert.assertEquals("style1", output2.getAttributes().get("style"));
291     }
292     
293     @Test
294     public void testConverterDeltaState10() throws Exception
295     {
296         UIOutput output = new UIOutput();
297         output.setId("output1");
298         output.setConverter(new LongPartialStateHolderConverter2());
299         output.markInitialState();
300         
301         //Force parentDelta to be not null
302         output.getAttributes().put("style", "style1");
303         
304         Object[] state1 = (Object[])output.saveState(facesContext);
305         
306         // PartialStateHolder force state only when there is delta
307         Assert.assertNotNull(state1);
308         Assert.assertEquals(state1.length, 2);
309         Assert.assertNotNull(state1[1]);
310         Assert.assertNotNull(state1[0]);
311         
312         UIOutput output2 = new UIOutput();
313         output2.setId("output1");
314         Converter converter2 = new LongPartialStateHolderConverter2();
315         output2.setConverter(converter2);
316         output2.markInitialState();
317         
318         output2.restoreState(facesContext, state1);
319         
320         // Check converter is not replaced, because it is not necessary (delta change)
321         Assert.assertEquals(converter2, output2.getConverter());
322         
323         // Check parentDelta works
324         Assert.assertEquals("style1", output2.getAttributes().get("style"));
325     }
326     
327     /**
328      * Check set null converter after markInitialState case
329      * 
330      * @throws Exception 
331      */
332     @Test
333     public void testConverterDeltaState11() throws Exception
334     {
335         Converter[] converters = new Converter[]{
336             new LongConverter(),
337             new LongSerializableConverter(),
338             new LongStateHolderConverter(),
339             new LongPartialStateHolderConverter(),
340             new LongPartialStateHolderConverter2()
341         };
342         Converter[] converters2 = new Converter[]{
343             new LongConverter(),
344             new LongSerializableConverter(),
345             new LongStateHolderConverter(),
346             new LongPartialStateHolderConverter(),
347             new LongPartialStateHolderConverter2()
348         };
349         for (int i = 0; i < converters.length; i++)
350         {
351             UIOutput output = new UIOutput();
352             output.setId("output1");
353             output.setConverter(converters[i]);
354             output.markInitialState();
355 
356             output.setConverter(null);
357 
358             Object[] state1 = (Object[])output.saveState(facesContext);
359 
360             // The null spot force state to be set.
361             Assert.assertNotNull(state1);
362             Assert.assertEquals(state1.length, 2);
363             Assert.assertNull(state1[1]); // null spot in state means null Converter
364             Assert.assertNotNull(state1[0]); //because _isConverterSet()
365 
366             UIOutput output2 = new UIOutput();
367             output2.setId("output1");
368             Converter converter2 = converters2[i];
369             output2.setConverter(converter2);
370             output2.markInitialState();
371 
372             output2.restoreState(facesContext, state1);
373 
374             // Check converter is set to null
375             Assert.assertNull(output2.getConverter());
376         }
377     }
378 
379     @Test
380     public void testConverterDeltaState12() throws Exception
381     {
382         UIOutput output = new UIOutput();
383         output.setId("output1");
384         output.setConverter(new LongTransientStateHolderConverter());
385         output.markInitialState();
386         
387         Object state1 = output.saveState(facesContext);
388         
389         // Transient means no state, but the effect is the converter dissapear
390         // (because is transient, and in JSF 1.2/1.1 that was the effect).
391         Assert.assertNotNull(state1);
392         
393         UIOutput output2 = new UIOutput();
394         output2.setId("output1");
395         Converter converter2 = new LongTransientStateHolderConverter();
396         output2.setConverter(converter2);
397         output2.markInitialState();
398         
399         output2.restoreState(facesContext, state1);
400         
401         // Check no converter should be found
402         Assert.assertNull(output2.getConverter());
403     }
404     
405     @Test
406     public void testConverterDeltaState13() throws Exception
407     {
408         UIOutput output = new UIOutput();
409         output.setId("output1");
410         output.setConverter(new LongTransientPartialStateHolderConverter());
411         output.markInitialState();
412         
413         Object state1 = output.saveState(facesContext);
414         
415         // Transient means no state, but the effect is the converter dissapear
416         // (because is transient, and in JSF 1.2/1.1 that was the effect).
417         Assert.assertNotNull(state1);
418         
419         UIOutput output2 = new UIOutput();
420         output2.setId("output1");
421         Converter converter2 = new LongTransientPartialStateHolderConverter();
422         output2.setConverter(converter2);
423         output2.markInitialState();
424         
425         output2.restoreState(facesContext, state1);
426         
427         // Check no converter should be found
428         Assert.assertNull(output2.getConverter());
429     }
430 
431     public static class LongSerializableConverter extends LongConverter implements Serializable
432     {
433         
434     }
435     
436     public static class LongStateHolderConverter extends LongConverter implements StateHolder
437     {
438         public Object saveState(FacesContext context)
439         {
440             return null;
441         }
442 
443         public void restoreState(FacesContext context, Object state)
444         {
445         }
446 
447         public boolean isTransient()
448         {
449             return false;
450         }
451 
452         public void setTransient(boolean newTransientValue)
453         {
454         }
455 
456     }
457     
458     public static class LongTransientStateHolderConverter extends LongConverter implements StateHolder
459     {
460         public Object saveState(FacesContext context)
461         {
462             return null;
463         }
464 
465         public void restoreState(FacesContext context, Object state)
466         {
467         }
468 
469         public boolean isTransient()
470         {
471             return true;
472         }
473 
474         public void setTransient(boolean newTransientValue)
475         {
476         }
477 
478     }
479     
480     public static class LongPartialStateHolderConverter extends LongConverter implements PartialStateHolder
481     {
482         private boolean _markInitialState;
483 
484         public void clearInitialState()
485         {
486             _markInitialState = false;
487         }
488 
489         public boolean initialStateMarked()
490         {
491             return _markInitialState;
492         }
493 
494         public void markInitialState()
495         {
496             _markInitialState = true;
497         }
498         
499         public Object saveState(FacesContext context)
500         {
501             return null;
502         }
503 
504         public void restoreState(FacesContext context, Object state)
505         {
506         }
507 
508         public boolean isTransient()
509         {
510             return false;
511         }
512 
513         public void setTransient(boolean newTransientValue)
514         {
515         }
516     }
517     
518     public static class LongPartialStateHolderConverter2 
519         extends LongPartialStateHolderConverter implements PartialStateHolder
520     {
521         
522         public Object saveState(FacesContext context)
523         {
524             return 1;
525         }
526 
527         public void restoreState(FacesContext context, Object state)
528         {
529             //ignore
530         }
531 
532         public boolean isTransient()
533         {
534             return false;
535         }
536 
537         public void setTransient(boolean newTransientValue)
538         {
539         }
540     }
541     
542     public static class LongTransientPartialStateHolderConverter
543         extends LongPartialStateHolderConverter implements PartialStateHolder
544     {
545         
546         public Object saveState(FacesContext context)
547         {
548             return 1;
549         }
550 
551         public void restoreState(FacesContext context, Object state)
552         {
553             //ignore
554         }
555 
556         public boolean isTransient()
557         {
558             return true;
559         }
560 
561         public void setTransient(boolean newTransientValue)
562         {
563         }
564     }
565     
566 }