View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   */
19  package org.apache.myfaces.trinidad.bean;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.ObjectInputStream;
25  import java.io.ObjectOutputStream;
26  
27  import java.util.Iterator;
28  
29  import javax.faces.el.ValueBinding;
30  import javax.faces.context.FacesContext;
31  
32  import org.apache.myfaces.trinidad.bean.PropertyKey;
33  
34  import junit.framework.Test;
35  import junit.framework.TestCase;
36  import junit.framework.TestSuite;
37  
38  import org.apache.myfaces.trinidad.context.MockRequestContext;
39  import org.apache.myfaces.trinidadbuild.test.FacesTestCase;
40  
41  public class FacesBeanImplTest extends FacesTestCase
42  {
43    public static final Test suite()
44    {
45      return new TestSuite(FacesBeanImplTest.class);
46    }
47  
48    public static void main(String[] args) throws Throwable
49    {
50      junit.textui.TestRunner.run(suite());
51    }
52  
53    public FacesBeanImplTest(
54      String testName)
55    {
56      super(testName);
57    }
58  
59  
60    private MockRequestContext _mafct;
61  
62    @Override
63    protected void setUp() throws Exception
64    {
65      super.setUp();
66      _mafct = new MockRequestContext();
67    }
68  
69    @Override
70    protected void tearDown() throws Exception
71    {
72      _mafct.release();
73      _mafct = null;
74      super.tearDown();
75    }
76  
77    public void testInitialValues()
78    {
79      TestBean bean = new TestBean();
80      assertNull(bean.getFirst());
81      assertNull(bean.getSecond());
82    }
83  
84    public void testSubclass()
85    {
86      SubTypeBean bean = new SubTypeBean();
87      assertNull(bean.getFirst());
88      assertNull(bean.getSecond());
89      assertNull(bean.getSub());
90      bean.setSub("sub");
91      assertEquals("sub", bean.getSub());
92    }
93  
94    public void testSetValues()
95    {
96      TestBean bean = new TestBean();
97      bean.setFirst("first");
98      bean.setSecond("second");
99      assertEquals(bean.getFirst(), "first");
100     assertEquals(bean.getSecond(), "second");
101 
102     bean.setFirst(null);
103     bean.setSecond(null);
104 
105     assertNull(bean.getFirst());
106     assertNull(bean.getSecond());
107 
108     bean.setSecond("newSecond");
109     assertEquals(bean.getSecond(), "newSecond");
110     assertEquals(bean.getProperty(TestBean.SECOND_KEY), "newSecond");
111     assertEquals(bean.getLocalProperty(TestBean.SECOND_KEY), "newSecond");
112 
113     bean.setProperty(TestBean.FIRST_KEY, "newFirst");
114     assertEquals(bean.getFirst(), "newFirst");
115   }
116 
117   public void testAliases()
118   {
119     TestBean bean = new TestBean();
120     bean.setFirstAlias("alias");
121     assertEquals("alias", bean.getFirst());
122     assertEquals("alias", bean.getFirstAlias());
123 
124     bean = new TestBean();
125     bean.setFirst("alias2");
126     assertEquals("alias2", bean.getFirst());
127     assertEquals("alias2", bean.getFirstAlias());
128   }
129 
130   public void testAnonymousKeys()
131   {
132     // Create an anonymous key
133     PropertyKey thirdKey = new PropertyKey("third");
134     TestBean bean = new TestBean();
135     bean.setFirst("first");
136     bean.setSecond("second");
137     bean.setProperty(thirdKey, "third");
138     assertEquals(bean.getFirst(), "first");
139     assertEquals(bean.getSecond(), "second");
140     assertEquals(bean.getProperty(thirdKey), "third");
141 
142     PropertyKey extraInstance = new PropertyKey("third");
143     assertEquals(bean.getProperty(thirdKey),
144                  bean.getProperty(extraInstance));
145   }
146 
147 
148   public void testBindingNotAllowed()
149   {
150     TestBean bean = new TestBean();
151     try
152     {
153       bean.setValueBinding(TestBean.CANT_BE_BOUND_KEY,
154                            new TestValueBinding());
155       fail();
156     }
157     catch (IllegalArgumentException e)
158     {
159     }
160   }
161 
162 
163 
164   public void testBindings()
165   {
166     TestBean bean = new TestBean();
167     TestValueBinding vb1 = new TestValueBinding();
168     vb1.setValue(null, "vbFirst");
169     bean.setValueBinding(TestBean.FIRST_KEY, vb1);
170 
171     assertSame(bean.getValueBinding(TestBean.FIRST_KEY), vb1);
172 
173     assertEquals("vbFirst", bean.getFirst());
174 
175     bean.setFirst("first");
176     assertEquals("first", bean.getFirst());
177 
178     bean.setFirst(null);
179     assertEquals("vbFirst", bean.getFirst());
180   }
181 
182   public void testSets()
183   {
184     TestBean bean = new TestBean();
185     assertTrue(bean.keySet().isEmpty());
186     assertTrue(bean.bindingKeySet().isEmpty());
187 
188     bean.setFirst("first");
189     bean.setSecond("second");
190 
191     assertEquals(2, bean.keySet().size());
192 
193     bean.setSecond("newSecond");
194 
195     assertEquals(2, bean.keySet().size());
196 
197     bean.setSecond(null);
198 
199     // This test is somewhat dubious...
200     assertEquals(1, bean.keySet().size());
201 
202     // Create an anonymous key
203     PropertyKey thirdKey = new PropertyKey("third");
204 
205     bean.setValueBinding(TestBean.FIRST_KEY, new TestValueBinding());
206     assertEquals(1, bean.bindingKeySet().size());
207 
208     bean.setValueBinding(TestBean.FIRST_KEY, new TestValueBinding());
209     assertEquals(1, bean.bindingKeySet().size());
210 
211     bean.setValueBinding(thirdKey, new TestValueBinding());
212     assertEquals(2, bean.bindingKeySet().size());
213 
214     assertTrue(bean.bindingKeySet().contains(thirdKey));
215     assertTrue(bean.bindingKeySet().contains(TestBean.FIRST_KEY));
216     assertTrue(!bean.bindingKeySet().contains(TestBean.SECOND_KEY));
217   }
218 
219   public void testLists()
220   {
221     TestBean bean = new TestBean();
222     Iterator<Object> iterator = bean.items();
223     assertTrue(!iterator.hasNext());
224     Integer[] array = bean.getItems();
225     assertNotNull(array);
226     assertEquals(0, array.length);
227 
228     bean.addItem(new Integer(1));
229     assertEquals(1, bean.getItems().length);
230 
231     bean.addItem(new Integer(2));
232     assertEquals(2, bean.getItems().length);
233 
234     array = bean.getItems();
235     assertEquals(array[0], new Integer(1));
236     assertEquals(array[1], new Integer(2));
237 
238     // Verify that this is a *list*, not a Set, so adding the
239     // same value twice works as expected
240     bean.addItem(new Integer(2));
241     assertEquals(3, bean.getItems().length);
242     bean.removeItem(new Integer(2));
243 
244     iterator = bean.items();
245     assertEquals(new Integer(1), iterator.next());
246     assertEquals(new Integer(2), iterator.next());
247     assertTrue(!iterator.hasNext());
248 
249     assertTrue(bean.containsEntry(TestBean.ITEMS_KEY, Number.class));
250     assertTrue(bean.containsEntry(TestBean.ITEMS_KEY, Integer.class));
251     assertTrue(!bean.containsEntry(TestBean.ITEMS_KEY, Long.class));
252 
253     bean.removeItem(new Integer(1));
254     bean.removeItem(new Integer(2));
255 
256     iterator = bean.items();
257     assertTrue(!iterator.hasNext());
258     array = bean.getItems();
259     assertNotNull(array);
260     assertEquals(0, array.length);
261 
262     // List items cannot be set, bound, or retrieved
263     try
264     {
265       bean.setProperty(TestBean.ITEMS_KEY, "Shouldn't work");
266       fail();
267     }
268     catch (IllegalArgumentException iae)
269     {
270       // expected
271     }
272 
273 
274     try
275     {
276       bean.getProperty(TestBean.ITEMS_KEY);
277       fail();
278     }
279     catch (IllegalArgumentException iae)
280     {
281       // expected
282     }
283 
284     try
285     {
286       bean.getLocalProperty(TestBean.ITEMS_KEY);
287       fail();
288     }
289     catch (IllegalArgumentException iae)
290     {
291       // expected
292     }
293 
294     try
295     {
296       bean.setValueBinding(TestBean.ITEMS_KEY, new TestValueBinding());
297       fail();
298     }
299     catch (IllegalArgumentException iae)
300     {
301       // expected
302     }
303 
304     // Meanwhile, you can't use the list APIs for non-list keys
305     try
306     {
307       bean.addEntry(TestBean.FIRST_KEY, null);
308       fail();
309     }
310     catch (IllegalArgumentException iae)
311     {
312       // expected
313     }
314 
315     try
316     {
317       bean.addEntry(TestBean.FIRST_KEY, null);
318       fail();
319     }
320     catch (IllegalArgumentException iae)
321     {
322       // expected
323     }
324 
325     try
326     {
327       bean.removeEntry(TestBean.FIRST_KEY, null);
328       fail();
329     }
330     catch (IllegalArgumentException iae)
331     {
332       // expected
333     }
334 
335     try
336     {
337       bean.getEntries(TestBean.FIRST_KEY, Object.class);
338       fail();
339     }
340     catch (IllegalArgumentException iae)
341     {
342       // expected
343     }
344 
345     try
346     {
347       bean.entries(TestBean.FIRST_KEY);
348       fail();
349     }
350     catch (IllegalArgumentException iae)
351     {
352       // expected
353     }
354   }
355 
356   public void testTypeLocked()
357   {
358     try
359     {
360       TestBean.TYPE.registerKey("ShouldntWork");
361       fail();
362     }
363     catch (IllegalStateException ise)
364     {
365     }
366   }
367 
368   public void testAddAll()
369   {
370     TestBean bean = new TestBean();
371     bean.setFirst("first");
372     TestValueBinding binding = new TestValueBinding();
373     binding.setValue(null, "FirstBinding");
374     bean.setValueBinding(TestBean.FIRST_KEY, binding);
375     bean.setSecond("second");
376     bean.setProperty(new PropertyKey("sub"), "subValue");
377     bean.addItem(new Integer(1));
378     bean.addItem(new Integer(2));
379 
380     SubTypeBean sub = new SubTypeBean();
381     sub.setSecond("third");
382     sub.addAll(bean);
383 
384     assertEquals("first", sub.getFirst());
385     assertEquals("second", sub.getSecond());
386     assertEquals("subValue", sub.getSub());
387     assertNotNull(sub.getValueBinding(TestBean.FIRST_KEY));
388     assertEquals("FirstBinding",
389       sub.getValueBinding(TestBean.FIRST_KEY).getValue(
390         FacesContext.getCurrentInstance()));
391     Integer[] items = sub.getItems();
392     assertNotNull(items);
393     assertEquals(2, items.length);
394     assertEquals(new Integer(1), items[0]);
395     assertEquals(new Integer(2), items[1]);
396 
397     TestBean andBackAgain = new TestBean();
398     andBackAgain.addAll(sub);
399     assertEquals("subValue",
400                  andBackAgain.getLocalProperty(new PropertyKey("sub")));
401   }
402 
403   public void testStateSaveAndRestore()
404   {
405     // Build a bean
406     SubTypeBean bean = new SubTypeBean();
407     TestValueBinding vb1 = new TestValueBinding();
408     vb1.setValue(null, "vbFirst");
409     bean.setValueBinding(SubTypeBean.FIRST_KEY, vb1);
410     bean.setSecond("second");
411     bean.setTransient("Won't be there");
412     bean.setSub("sub");
413     bean.addItem(new Integer(1));
414     bean.addItem(new Integer(2));
415 
416     SillyStateHolder silly = new SillyStateHolder();
417     bean.setProperty(SubTypeBean.SILLY_KEY, silly);
418 
419     assertEquals("0", silly.toString());
420 
421     // Save its state
422     Object savedState = bean.saveState(null);
423     assertNotNull(savedState);
424 
425     try
426     {
427       savedState = _copyObjectThroughSerialization(savedState);
428     }
429     catch (Exception e)
430     {
431       e.printStackTrace();
432       fail();
433     }
434 
435     // Verify that our "silly" object has had its state saved
436     assertEquals("1", silly.toString());
437 
438     // Build a new bean, and restore its state
439     SubTypeBean newBean = new SubTypeBean();
440     newBean.restoreState(null, savedState);
441 
442     // Verify it looks like the old bean
443     assertEquals("vbFirst", newBean.getFirst());
444     assertNull(newBean.getLocalProperty(SubTypeBean.FIRST_KEY));
445     assertEquals("second", newBean.getSecond());
446     assertNull(newBean.getValueBinding(SubTypeBean.SECOND_KEY));
447     assertEquals("sub", newBean.getSub());
448 
449     // Verify that our "silly" object has had its state restored
450     assertEquals("2", newBean.getProperty(SubTypeBean.SILLY_KEY).toString());
451 
452     Integer[] array = newBean.getItems();
453     assertEquals(2, array.length);
454     assertEquals(new Integer(1), array[0]);
455     assertEquals(new Integer(2), array[1]);
456 
457 
458     // Make sure the transient value is now null
459     assertNull(newBean.getTransient());
460 
461     // Make sure the value binding looks the same, but is
462     // not actually the same instance
463     ValueBinding vb = newBean.getValueBinding(SubTypeBean.FIRST_KEY);
464     assertTrue(vb instanceof TestValueBinding);
465     assertTrue(vb != vb1);
466     assertEquals(vb.getValue(FacesContext.getCurrentInstance()), "vbFirst");
467 
468     // Now change the value binding, and verify the original
469     // bean is unchanged
470     vb.setValue(null, "changedVB");
471     assertEquals("changedVB", newBean.getFirst());
472     assertEquals("vbFirst", bean.getFirst());
473 
474     // Now, verify that if we mark the initial state and save, that we get
475     // a non-null value
476     newBean.markInitialState();
477     assertNull(newBean.saveState(null));
478 
479     // Now, we'll set a value, so we should get a non-null state
480 
481     // Our current delta support *does not* keep track of the original value.
482     // If it does, add this test
483     // String oldFirst = newBean.getFirst();
484     newBean.setFirst("foo");
485     assertNotNull(newBean.saveState(null));
486 
487     // Our current delta support *does not* keep track of the original value.
488     // If it does, add this test
489 //    newBean.setFirst(oldFirst);
490 //    assertNull(newBean.saveState(null));
491   }
492 
493   static private Object _copyObjectThroughSerialization(Object o)
494     throws IOException, ClassNotFoundException
495   {
496     ByteArrayOutputStream bos = new ByteArrayOutputStream();
497     ObjectOutputStream oos = new ObjectOutputStream(bos);
498     oos.writeObject(o);
499     oos.close();
500 
501     byte[] byteArray = bos.toByteArray();
502     ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
503     ObjectInputStream ois = new ObjectInputStream(bis);
504 
505     return ois.readObject();
506   }
507 
508   // -= Simon Lessard =-
509   // Never read locally as of 2006-08-09
510   //private PropertyKey _thirdKey;
511 }