1   // 
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   //All rights reserved. 
4   //   
5   //Redistribution and use in source and binary forms, with or without modification,  
6   //are permitted provided that the following conditions are met: 
7   //   
8   //* Redistributions of source code must retain the above copyright notice,  
9   //this list of conditions and the following disclaimer. 
10  //* Redistributions in binary form must reproduce the above copyright notice,  
11  //this list of conditions and the following disclaimer in the documentation  
12  //and/or other materials provided with the distribution. 
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //nor the names of its contributors may be used to endorse or promote products  
15  //derived from this software without specific prior written permission. 
16  // 
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  //POSSIBILITY OF SUCH DAMAGE. 
27  //
28  
29  package org.objectledge.event;
30  
31  import java.lang.reflect.Method;
32  
33  import junit.framework.TestCase;
34  
35  import org.apache.log4j.BasicConfigurator;
36  import org.jcontainer.dna.Configuration;
37  import org.jcontainer.dna.Logger;
38  import org.jcontainer.dna.impl.DefaultConfiguration;
39  import org.jcontainer.dna.impl.Log4JLogger;
40  import org.objectledge.context.Context;
41  import org.objectledge.pipeline.Valve;
42  import org.objectledge.threads.ThreadPool;
43  
44  /**
45   * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a>
46   *
47   * To change the template for this generated type comment go to
48   * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
49   */
50  public class EventSystemTest extends TestCase
51      implements FooInterface
52  {
53      private EventWhiteboardFactory event;
54      
55      private EventWhiteboard whiteboard;
56      
57      private String testString;
58      
59      /***
60       * Constructor for EventSystemTest.
61       * @param arg0
62       */
63      public EventSystemTest(String arg0)
64      {
65          super(arg0);
66          BasicConfigurator.resetConfiguration();
67          BasicConfigurator.configure();
68          Context context = new Context();
69          Configuration config = new DefaultConfiguration("config", "", "/config");
70          Logger logger = new Log4JLogger(org.apache.log4j.Logger.getLogger(getClass()));
71          Valve cleanup = null;
72          ThreadPool pool = new ThreadPool(cleanup, context, config, logger);
73          event = new EventWhiteboardFactory(config,logger,pool);
74          whiteboard = event.newInstance();
75      }
76  
77      public void testGetForwarder()
78      {
79          EventWhiteboard ef = event.newInstance();
80          assertNotNull(ef);
81      }
82  
83      public void testAddListener()
84      {
85          whiteboard.addListener(FooInterface.class,this,this);
86      }
87  
88      public void testRemoveListener()
89      {
90          whiteboard.removeListener(FooInterface.class,this,this);
91      }
92  
93      public void testAddRemoteListener()
94      {
95          // TODO implement or remove
96      }
97  
98      public void testRemoveRemoteListener()
99      {
100         // TODO implement or remove
101     }
102 
103     public void testFireEvent()
104         throws Exception
105     {
106         testString = "";
107         assertEquals(testString,"");
108         
109         Method method = FooInterface.class.getMethod("callBar",new Class[]{String.class});
110         whiteboard.fireEvent(method, new String[]{"foo"},this);
111         assertEquals(testString,"");
112         whiteboard.addListener(FooInterface.class,this,this);
113         whiteboard.fireEvent(method, new String[]{"foo"},this);
114         assertEquals(testString,"foo");
115     }
116 
117     public void testInboundOutboundForwarder()
118         throws Exception
119     {
120         EventWhiteboard ef = event.newInstance();
121         EventWhiteboard outF = new OutboundEventWhiteboard(ef);
122         EventWhiteboard inF = new InboundEventWhiteboard(ef);
123         try
124         {
125             outF.addListener(null,null,null);
126             fail("should throw the exception");
127         }
128         catch(IllegalStateException e)
129         {
130             //ok!
131         }
132         try
133         {
134             outF.removeListener(null,null,null);
135             fail("should throw the exception");
136         }
137         catch(IllegalStateException e)
138         {
139             //ok!
140         }
141         try
142         {
143             outF.addRemoteListener(null,null,null);
144             fail("should throw the exception");
145         }
146         catch(IllegalStateException e)
147         {
148             //ok!
149         }
150         try
151         {
152             outF.removeRemoteListener(null,null,null);
153             fail("should throw the exception");
154         }
155         catch(IllegalStateException e)
156         {
157             //ok!
158         }
159         try
160         {
161             inF.fireEvent(null,null,null);
162             fail("should throw the exception");
163         }
164         catch(IllegalStateException e)
165         {
166             //ok!
167         }
168         testString = "";
169         inF.removeListener(FooInterface.class,this,this);
170         Method method = FooInterface.class.getMethod("callBar",new Class[]{String.class});
171         outF.fireEvent(method, new String[]{"foo"},this);
172         assertEquals(testString,"");
173         inF.addListener(FooInterface.class,this,this);
174         outF.fireEvent(method, new String[]{"foo"},this);
175         assertEquals(testString,"foo");
176     }
177 
178     // foo interface implementation 
179     public void callBar(String arg0)
180     {
181         testString = arg0;
182     }
183     
184     
185 }