KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > monitor > test > MonitorTestCase


1 /*
2  * Copyright (c) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8
9 package org.apache.avalon.excalibur.monitor.test;
10
11 import org.apache.avalon.framework.component.Component;
12 import org.apache.avalon.framework.component.ComponentException;
13 import org.apache.avalon.framework.component.ComponentSelector;
14 import org.apache.avalon.framework.logger.AbstractLoggable;
15
16 import org.apache.avalon.excalibur.testcase.ExcaliburTestCase;
17 import org.apache.avalon.excalibur.testcase.CascadingAssertionFailedError;
18 import org.apache.avalon.excalibur.monitor.Monitor;
19 import org.apache.avalon.excalibur.monitor.ActiveMonitor;
20 import org.apache.avalon.excalibur.monitor.PassiveMonitor;
21 import org.apache.avalon.excalibur.monitor.FileResource;
22
23 import org.apache.log.Priority;
24
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27
28 import java.io.File JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.Writer JavaDoc;
32
33 /**
34  * Junit TestCase for all the monitors in Excalibur.
35  *
36  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
37  * @version $Id: MonitorTestCase.java,v 1.6 2001/12/11 09:53:39 jefft Exp $
38  */

39 public class MonitorTestCase extends ExcaliburTestCase
40 {
41     /**
42      * The constructor for the MonitorTest
43      */

44     public MonitorTestCase( String JavaDoc name )
45     {
46         super( name );
47     }
48
49     public void testActiveMonitor()
50         throws CascadingAssertionFailedError
51     {
52         ComponentSelector selector = null;
53         Monitor activeMonitor = null;
54
55         try
56         {
57             selector = (ComponentSelector) manager.lookup( Monitor.ROLE + "Selector" );
58             activeMonitor = (Monitor) selector.select( "active" );
59
60             internalTestProcedure( activeMonitor, true );
61         }
62         catch ( ComponentException ce )
63         {
64             if ( getLogger().isDebugEnabled() )
65             {
66                 getLogger().debug( "There was an error in the ActiveMonitor test", ce );
67             }
68
69             throw new CascadingAssertionFailedError( "There was an error in the ActiveMonitor test", ce );
70         }
71         finally
72         {
73             assertTrue( "The monitor selector could not be retrieved.", null != selector );
74
75             selector.release( (Component) activeMonitor );
76             manager.release( selector );
77         }
78     }
79
80     public void testPassiveMonitor()
81         throws CascadingAssertionFailedError
82     {
83         ComponentSelector selector = null;
84         Monitor passiveMonitor = null;
85
86         try
87         {
88             selector = (ComponentSelector) manager.lookup( Monitor.ROLE + "Selector" );
89             passiveMonitor = (Monitor) selector.select( "passive" );
90
91             internalTestProcedure( passiveMonitor, false );
92         }
93         catch ( ComponentException ce )
94         {
95             if ( getLogger().isDebugEnabled() )
96             {
97                 getLogger().debug( "There was an error in the PassiveMonitor test", ce );
98             }
99
100             throw new CascadingAssertionFailedError( "There was an error in the PassiveMonitor test", ce );
101         }
102         finally
103         {
104             assertTrue( "The monitor selector could not be retrieved.", null != selector );
105
106             selector.release( (Component) passiveMonitor );
107             manager.release( selector );
108         }
109     }
110
111     private void internalTestProcedure( Monitor testMonitor, boolean active )
112     {
113         try
114         {
115             long sleepTo;
116             File JavaDoc thirdWheel = new File JavaDoc( "test.txt" );
117
118             thirdWheel.createNewFile();
119
120             MonitorTestCaseListener listener = new MonitorTestCaseListener();
121             listener.setLogger( getLogger() );
122
123             FileResource resource = new FileResource( "test.txt" );
124             resource.addPropertyChangeListener( listener );
125
126             testMonitor.addResource( resource );
127
128             thirdWheel.setLastModified( System.currentTimeMillis() );
129
130             if( active )
131             {
132                 FileWriter JavaDoc externalWriter = new FileWriter JavaDoc( thirdWheel );
133                 externalWriter.write( "External Writer modification" );
134                 externalWriter.flush();
135                 externalWriter.close();
136
137                 sleepTo = System.currentTimeMillis() + 1000L;
138
139                 while( System.currentTimeMillis() < sleepTo && ( ! listener.hasBeenModified() ) )
140                 {
141                     try
142                     {
143                         Thread.sleep( 10 ); // sleep 10 millis per iteration
144
}
145                     catch( final InterruptedException JavaDoc ie )
146                     {
147                         // ignore and keep waiting
148
}
149                 }
150
151                 assertTrue( "File not changed", listener.hasBeenModified() );
152             }
153
154             listener.reset();
155
156             OutputStream JavaDoc out = resource.setResourceAsStream();
157             out.write( "Test line 1\n".getBytes() );
158
159             try
160             {
161                 Thread.sleep( 10 ); // sleep 10 millis at a time
162
}
163             catch( final InterruptedException JavaDoc ie )
164             {
165                 // ignore and keep waiting
166
}
167
168             out.flush();
169             out.close();
170
171             sleepTo = System.currentTimeMillis() + 1000L;
172
173             while( System.currentTimeMillis() < sleepTo && ( ! listener.hasBeenModified() ) )
174             {
175                 try
176                 {
177                     Thread.sleep( 10 ); // sleep 10 millis at a time
178
}
179                 catch( final InterruptedException JavaDoc ie )
180                 {
181                     // ignore and keep waiting
182
}
183             }
184
185             assertTrue( "File not changed", listener.hasBeenModified() );
186             listener.reset();
187
188             Writer JavaDoc write = resource.setResourceAsWriter();
189             write.write( "Test line 2\n" );
190
191             try
192             {
193                 Thread.sleep( 10 ); // sleep 10 millis at a time
194
}
195             catch( final InterruptedException JavaDoc ie )
196             {
197                 // ignore and keep waiting
198
}
199
200             write.flush();
201             write.close();
202
203             sleepTo = System.currentTimeMillis() + 1000L;
204
205             while( System.currentTimeMillis() < sleepTo && ( ! listener.hasBeenModified() ) )
206             {
207                 try
208                 {
209                     Thread.sleep( 10 ); // sleep 10 millis at a time
210
}
211                 catch( final InterruptedException JavaDoc ie )
212                 {
213                     // ignore and keep waiting
214
}
215             }
216
217             assertTrue( "File not changed", listener.hasBeenModified() );
218             listener.reset();
219
220             resource.removePropertyChangeListener( listener );
221             testMonitor.removeResource( resource );
222             thirdWheel.delete();
223         }
224         catch( final Exception JavaDoc e )
225         {
226             if( getLogger().isDebugEnabled() )
227             {
228                 getLogger().debug( "Error running the test", e );
229             }
230
231             throw new CascadingAssertionFailedError( "Error running the test", e );
232         }
233     }
234
235     public static class MonitorTestCaseListener
236         extends AbstractLoggable
237         implements PropertyChangeListener JavaDoc
238     {
239         private volatile boolean m_hasChanged = false;
240
241         public boolean hasBeenModified()
242         {
243             return m_hasChanged;
244         }
245
246         public void reset()
247         {
248             m_hasChanged = false;
249         }
250
251         public void propertyChange( final PropertyChangeEvent JavaDoc propertyChangeEvent )
252         {
253             m_hasChanged = true;
254
255             if( getLogger().isInfoEnabled() )
256             {
257                 getLogger().info( "NOTIFICATION LATENCY: " + (System.currentTimeMillis() -
258                                    ((Long JavaDoc)propertyChangeEvent.getNewValue()).longValue()) +
259                                    "ms");
260                 getLogger().info( "Received notification for " +
261                                   ((FileResource) propertyChangeEvent.getSource()).getResourceKey());
262                 getLogger().info( propertyChangeEvent.getPropertyName() +
263                                   "\n IS::" + (Long JavaDoc)propertyChangeEvent.getNewValue() +
264                                   "\n WAS::" + (Long JavaDoc)propertyChangeEvent.getOldValue() +
265                                   "\n TIME SINCE LAST MOD::" +
266                                   (((Long JavaDoc)propertyChangeEvent.getNewValue()).longValue() -
267                                   ((Long JavaDoc)propertyChangeEvent.getOldValue()).longValue()) );
268             }
269         }
270     }
271 }
272
Popular Tags