KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > ImmutablePicoContainerProxyFactoryTest


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by Paul Hammant *
9  *****************************************************************************/

10
11 package org.picocontainer.defaults;
12
13 import org.picocontainer.ComponentAdapter;
14 import org.picocontainer.PicoContainer;
15 import org.picocontainer.PicoVisitor;
16
17 import org.jmock.Mock;
18 import org.jmock.MockObjectTestCase;
19
20
21 /**
22  * @author Paul Hammant
23  * @author Jörg Schaible
24  */

25 public class ImmutablePicoContainerProxyFactoryTest extends MockObjectTestCase {
26
27     public void testImmutingOfNullBarfs() {
28         try {
29             ImmutablePicoContainerProxyFactory.newProxyInstance(null);
30             fail("Should have barfed");
31         } catch (NullPointerException JavaDoc e) {
32             // expected
33
}
34     }
35
36     public void testLifecylceBarfs() {
37         Mock mockPicoContainer = mock(PicoContainer.class);
38         PicoContainer ipc = ImmutablePicoContainerProxyFactory.newProxyInstance((PicoContainer)mockPicoContainer
39                 .proxy());
40         try {
41             ipc.start();
42             fail("should have barfed");
43         } catch (UnsupportedOperationException JavaDoc e) {
44             // expected
45
}
46         try {
47             ipc.stop();
48             fail("should have barfed");
49         } catch (UnsupportedOperationException JavaDoc e) {
50             // expected
51
}
52         try {
53             ipc.dispose();
54             fail("should have barfed");
55         } catch (UnsupportedOperationException JavaDoc e) {
56             // expected
57
}
58     }
59
60     public void testVisitingOfImmutableContainerWorks() {
61         DefaultPicoContainer pico = new DefaultPicoContainer();
62         Object JavaDoc foo = new Object JavaDoc();
63         ComponentAdapter componentAdapter = pico.registerComponentInstance(foo);
64
65         Mock fooVisitor = new Mock(PicoVisitor.class);
66         fooVisitor.expects(once()).method("visitContainer").with(same(pico));
67         fooVisitor.expects(once()).method("visitComponentAdapter").with(same(componentAdapter));
68
69         PicoContainer ipc = ImmutablePicoContainerProxyFactory.newProxyInstance(pico);
70         ipc.accept((PicoVisitor)fooVisitor.proxy());
71     }
72
73     public void testProxyEquals() throws Exception JavaDoc {
74         DefaultPicoContainer pico = new DefaultPicoContainer();
75         PicoContainer ipc = ImmutablePicoContainerProxyFactory.newProxyInstance(pico);
76         assertEquals(ipc, ipc);
77         assertEquals(ipc, ImmutablePicoContainerProxyFactory.newProxyInstance(pico));
78     }
79
80     public void testHashCodeIsSame() throws Exception JavaDoc {
81         DefaultPicoContainer pico = new DefaultPicoContainer();
82         PicoContainer ipc = ImmutablePicoContainerProxyFactory.newProxyInstance(pico);
83         assertEquals(ipc.hashCode(), ImmutablePicoContainerProxyFactory.newProxyInstance(pico).hashCode());
84     }
85     
86     public void testDoesNotEqualsToNull() {
87         DefaultPicoContainer pico = new DefaultPicoContainer();
88         PicoContainer ipc = ImmutablePicoContainerProxyFactory.newProxyInstance(pico);
89         assertFalse(ipc.equals(null));
90     }
91 }
92
Popular Tags