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