1 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 ; 26 import java.beans.PropertyChangeEvent ; 27 28 import java.io.File ; 29 import java.io.FileWriter ; 30 import java.io.OutputStream ; 31 import java.io.Writer ; 32 33 39 public class MonitorTestCase extends ExcaliburTestCase 40 { 41 44 public MonitorTestCase( String 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 thirdWheel = new File ( "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 externalWriter = new FileWriter ( 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 ); } 145 catch( final InterruptedException ie ) 146 { 147 } 149 } 150 151 assertTrue( "File not changed", listener.hasBeenModified() ); 152 } 153 154 listener.reset(); 155 156 OutputStream out = resource.setResourceAsStream(); 157 out.write( "Test line 1\n".getBytes() ); 158 159 try 160 { 161 Thread.sleep( 10 ); } 163 catch( final InterruptedException ie ) 164 { 165 } 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 ); } 179 catch( final InterruptedException ie ) 180 { 181 } 183 } 184 185 assertTrue( "File not changed", listener.hasBeenModified() ); 186 listener.reset(); 187 188 Writer write = resource.setResourceAsWriter(); 189 write.write( "Test line 2\n" ); 190 191 try 192 { 193 Thread.sleep( 10 ); } 195 catch( final InterruptedException ie ) 196 { 197 } 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 ); } 211 catch( final InterruptedException ie ) 212 { 213 } 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 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 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 propertyChangeEvent ) 252 { 253 m_hasChanged = true; 254 255 if( getLogger().isInfoEnabled() ) 256 { 257 getLogger().info( "NOTIFICATION LATENCY: " + (System.currentTimeMillis() - 258 ((Long )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 )propertyChangeEvent.getNewValue() + 264 "\n WAS::" + (Long )propertyChangeEvent.getOldValue() + 265 "\n TIME SINCE LAST MOD::" + 266 (((Long )propertyChangeEvent.getNewValue()).longValue() - 267 ((Long )propertyChangeEvent.getOldValue()).longValue()) ); 268 } 269 } 270 } 271 } 272 | Popular Tags |