1 18 19 package org.apache.beehive.netui.tools.testrecorder.server.state; 20 21 import java.io.File ; 22 import java.rmi.server.UID ; 23 import java.lang.StringBuffer ; 24 25 import org.apache.beehive.netui.tools.testrecorder.shared.Logger; 26 import org.apache.beehive.netui.tools.testrecorder.shared.RecordSessionBean; 27 import org.apache.beehive.netui.tools.testrecorder.shared.PlaybackSessionBean; 28 import org.apache.beehive.netui.tools.testrecorder.shared.xmlbeans.XMLHelper; 29 import org.apache.beehive.netui.tools.testrecorder.shared.TestResults; 30 import org.apache.beehive.netui.tools.testrecorder.shared.util.DateHelper; 31 import org.apache.beehive.netui.tools.testrecorder.server.DiffEngineFactory; 32 import org.apache.beehive.netui.tools.testrecorder.server.FilterData; 33 import org.apache.beehive.netui.tools.testrecorder.server.DiffEngine; 34 35 36 class PlaybackSessionImpl extends SessionImpl implements PlaybackSession { 37 38 private static final Logger log = Logger.getInstance( PlaybackSessionImpl.class ); 39 40 private UID uid; 41 private String stringUid; 42 private File recordFile; 43 private File playbackFile; 44 private File diffFile; 45 46 private RecordSessionBean recordSessionBean; 48 private PlaybackSessionBean playbackSessionBean; 49 50 PlaybackSessionImpl( String sessionName, File playbackFile, File recordFile, 51 File diffFile ) throws SessionFailedException { 52 super( new PlaybackSessionBean( sessionName ) ); 53 playbackSessionBean = (PlaybackSessionBean) getSessionBean(); 54 if ( sessionName == null || sessionName.trim().length() <= 0 ) { 55 error( "the session name supplied( " + sessionName + 56 " ) is invalid, it must be a non-null string of non-zero length" ); 57 } 58 if ( playbackFile == null || diffFile == null ) { 59 error( "the playback and diff files may not be null" ); 60 } 61 uid = new UID (); 62 stringUid = uid.toString(); 63 this.playbackFile = playbackFile; 64 this.diffFile = diffFile; 65 this.recordFile = recordFile; 66 if ( !recordFile.exists() ) { 67 error( "record file( " + recordFile.getAbsolutePath() + " ) does NOT exist" ); 68 } 69 if ( !recordFile.canRead() ) { 70 error( "record file( " + recordFile.getAbsolutePath() + " ) is not readable" ); 71 } 72 recordSessionBean = parseRecordFile( recordFile ); 73 playbackSessionBean.setRecordedTestCount( recordSessionBean.getTestCount() ); 74 if ( playbackSessionBean.getTester() == null || playbackSessionBean.getTester().length() == 0 ) { 75 if ( recordSessionBean.getTester() == null || recordSessionBean.getTester().length() == 0 ) { 76 playbackSessionBean.setTester( "unknown" ); 77 } 78 playbackSessionBean.setTester( recordSessionBean.getTester() ); 79 } 80 if ( playbackSessionBean.getDescription() == null || playbackSessionBean.getDescription().length() == 0 ) { 81 if ( recordSessionBean.getDescription() == null || 82 recordSessionBean.getDescription().length() == 0 ) { 83 playbackSessionBean.setDescription( "unknown" ); 84 } 85 playbackSessionBean.setDescription( recordSessionBean.getTester() ); 86 } 87 if ( log.isDebugEnabled() ) { 89 log.debug( "playbackFile( " + playbackFile.getAbsolutePath() + " )" ); 90 } 91 if ( playbackFile.exists() ) { 92 boolean rtnVal = playbackFile.delete(); 93 if ( rtnVal ) { 94 if ( log.isDebugEnabled() ) { 95 log.debug( "playback file exists, deleted(" + rtnVal + ")" ); 96 } 97 } 98 else { 99 error( "unable to delete playback file( " + playbackFile.getAbsolutePath() + " )" ); 100 } 101 } 102 if ( diffFile.exists() ) { 103 boolean rtnVal = diffFile.delete(); 104 if ( rtnVal ) { 105 if ( log.isDebugEnabled() ) { 106 log.debug( "diff file exists, deleted(" + rtnVal + ")" ); 107 } 108 } 109 else { 110 error( "unable to delete diff file( " + diffFile.getAbsolutePath() + " )" ); 111 } 112 } 113 try { 115 if ( !playbackFile.createNewFile() ) { 116 error( "unable to create new playback file( " + playbackFile.getAbsolutePath() + " )" ); 117 } 118 } 119 catch ( Exception ex ) { 120 error( "unable to create new playback file( " + playbackFile.getAbsolutePath() + " )", ex ); 121 } 122 setSessionState( BEGIN ); 123 } 124 125 public String getStatus() { 126 return playbackSessionBean.getStatus(); 127 } 128 129 public synchronized boolean startTest() throws SessionFailedException { 130 boolean rtnVal = false; 131 if ( getSessionState() == PLAYBACK ) { 132 incrementInProgress(); 133 rtnVal = true; 134 } 135 else if ( getSessionState() == BEGIN ) { 136 String msg = "Unable to start test, session must be started before starting a test"; 137 error( msg ); 138 } 139 else if ( getSessionState() == END ) { 140 String msg = "Unable to start test, session is finished, no new tests may be started"; 141 error( msg ); 142 } 143 else if ( getSessionState() >= ERROR ) { 144 String msg = "Unable to start test, Session has encountered a previous unrecoverable error"; 145 error( msg ); 146 } 147 else { 148 } 150 return rtnVal; 151 } 152 153 public synchronized int endTest( FilterData filterData ) throws SessionFailedException { 155 if ( getSessionState() >= ERROR ) { 156 String msg = "Unable to end test, Session has encountered a previous unrecoverable error"; 157 error( msg ); 158 } 159 if ( getSessionState() == BEGIN ) { 160 String msg = "Session must be started before using test operations"; 161 error( msg ); 162 } 163 else if ( getSessionState() == END ) { 164 String msg = "Session is finished, no test operations may be performed"; 165 error( msg ); 166 } 167 if ( inProgressCnt() == 0 ) { 168 String msg = "No session in progress to end"; 170 error( msg ); 171 } 172 else if ( inProgressCnt() != 1 ) { 173 String msg = "inProgress( " + inProgressCnt() + " ) is invalid"; 175 error( msg, null, true ); 176 } 177 if ( filterData.getReqData() == null ) { 179 String msg = "request data may not be null, test count(" + ( testCount() + 1 ) + ")"; 181 error( msg, null, true ); 182 } 183 else if ( filterData.getRespData() == null ) { 184 String msg = "response data may not be null, test count(" + ( testCount() + 1 ) + ")"; 186 error( msg, null, true ); 187 } 188 decrementInProgress(); 189 playbackSessionBean.addRequestResponseData( filterData.getReqData(), filterData.getRespData() ); 190 TestResults results = new TestResults( testCount() + 1, filterData.getReqData().getPath() ); 192 Throwable throwable = null; 194 for ( int i = 0; i < filterData.getTestExceptionCount(); i++ ) { 195 throwable = filterData.getTestException( i ); 196 results.addDiffResult( "encountered test exception(" + i + ")( " + 197 Logger.format( throwable, throwable ) + " )" ); 198 } 199 for ( int i = 0; i < filterData.getSessionExceptionCount(); i++ ) { 200 throwable = filterData.getSessionException( i ); 201 results.addDiffResult( "encountered test recorder session exception(" + i + ")( " + 202 Logger.format( throwable, throwable ) + " )" ); 203 } 204 try { 205 DiffEngine engine = DiffEngineFactory.getInstance( filterData ); 206 207 String resp = filterData.getRespData().getBody(); 208 String body = null; 209 int pos = resp.indexOf("<![CDATA["); 211 if (pos != -1) { 212 body = resp; 213 resp = resp.replaceAll("\\Q<![CDATA[\\E","<![CDATA["); 214 resp = resp.replaceAll("\\Q]]>","]]>"); 215 filterData.getRespData().setBody(resp); 216 } 217 results = engine.diff( recordSessionBean.getRequestData( testCount() ), 218 recordSessionBean.getResponseData( testCount() ), 219 filterData.getReqData(), filterData.getRespData(), results ); 220 if (body != null) { 221 filterData.getRespData().setBody(body); 222 } 223 } 224 catch ( Exception e ) { 225 String msg = "Failed diffing results for session( " + getSessionName() + 226 " ), test count(" + testCount() + "), exception( " + e.getMessage() + " )"; 227 results.addDiffResult( "Test Recorder ERROR: " + msg, true ); 228 results.addDiffResult( Logger.format( "EXCEPTION: ", e ) ); 229 error( msg, e, true ); 230 } 231 finally { 232 playbackSessionBean.addTestResults( results ); 233 checkSessionComplete(); 235 } 236 if ( log.isDebugEnabled() ) { 237 log.debug( "testCount( " + testCount() + " )" ); 238 } 239 return testCount(); 240 } 241 242 public synchronized boolean sessionStart() throws SessionFailedException { 244 boolean rtnVal = false; 245 if ( getSessionState() != BEGIN ) { 246 rtnVal = false; 247 } 248 else { 249 setStartDate( DateHelper.getCalendarInstance() ); 250 setSessionState( PLAYBACK ); 251 rtnVal = true; 252 } 253 return rtnVal; 254 } 255 256 protected synchronized void closeSessionInternal() throws SessionFailedException { 257 if ( log.isDebugEnabled() ) { 258 log.debug( "closing session file ( " + getPlaybackFile() + " ), session status( " + 259 playbackSessionBean.getStatus() + 260 " )" ); 261 } 262 SessionFailedException caught = null; 263 try { 264 setEndDate( DateHelper.getCalendarInstance() ); 265 XMLHelper.createPlaybackFile( getPlaybackFile(), playbackSessionBean ); 266 } 267 catch ( Exception e ) { 268 setSessionState( ERROR_END ); 269 String msg = "ERROR: failed to create playback XML file( " + getPlaybackFile().getAbsolutePath() + 270 " ), exception( " + e.getMessage() + " )"; 271 log.error( msg, e ); 272 caught = new SessionFailedException( msg, e ); 273 } 274 if ( playbackSessionBean.getFailedCount() > 0 || playbackSessionBean.isError() ) { 275 try { 277 if ( !diffFile.createNewFile() ) { 278 error( "unable to create new diff file( " + diffFile.getAbsolutePath() + " )" ); 279 } 280 XMLHelper.createDiffFile( getDiffFile(), playbackSessionBean ); 281 } 282 catch ( Exception e ) { 283 setSessionState( ERROR_END ); 284 String msg = "ERROR: failed to create diff XML file( " + getDiffFile().getAbsolutePath() + 285 " ), exception( " + e.getMessage() + " )"; 286 log.error( msg, e ); 287 if ( caught == null ) { 289 caught = new SessionFailedException( msg, e ); 290 } 291 } 292 } 293 if ( caught != null ) { 294 throw caught; 295 } 296 } 297 298 public String getStringUID() { 299 return stringUid; 300 } 301 302 public File getPlaybackFile() { 303 return playbackFile; 304 } 305 306 public File getDiffFile() { 307 return diffFile; 308 } 309 310 public File getRecordFile() { 311 return recordFile; 312 } 313 314 public int passCount() { 315 return playbackSessionBean.getPassedCount(); 316 } 317 318 public int failCount() { 319 return playbackSessionBean.getFailedCount(); 320 } 321 322 public int testCount() { 323 return playbackSessionBean.getTestCount(); 324 } 325 326 private RecordSessionBean parseRecordFile( File recordFile ) throws SessionFailedException { 327 RecordSessionBean sessionBean = null; 328 try { 329 sessionBean = XMLHelper.getRecordSessionBean( recordFile ); 330 } 331 catch ( Exception e ) { 332 String msg = "ERROR: failed parsing record file( " + recordFile + " ), exception( " + e.getMessage() + 333 " )"; 334 error( msg, e ); 335 } 336 return sessionBean; 337 } 338 339 public String toString() { 340 StringBuffer sb = new StringBuffer ( 128 ); 341 sb.append( "[ " ); 342 sb.append( "sessionName( " + getSessionName() + " )" ); 343 sb.append( ", uid( " + uid + " )" ); 345 sb.append( ", startDate( " + getStartDateString() + " )" ); 346 sb.append( ", endDate( " + getEndDateString() + " )" ); 347 sb.append( ", playbackFile( " + playbackFile + " )" ); 348 sb.append( ", recordFile( " + recordFile + " )" ); 349 sb.append( ", testUser( " + getTestUser() + " )" ); 350 sb.append( ", recordDesc( " + recordSessionBean.getDescription() + " )" ); 351 sb.append( ", state( " + getSessionState() + " )" ); 352 sb.append( ", testCount( " + testCount() + " )" ); 353 sb.append( ", recorded test count( " + playbackSessionBean.getRecordedTestCount() + " )" ); 354 sb.append( ", passed( " + playbackSessionBean.getPassedCount() + " )" ); 355 sb.append( ", failed( " + playbackSessionBean.getFailedCount() + " )" ); 356 sb.append( ", inProgress( " + inProgressCnt() + " )" ); 357 sb.append( " ]" ); 358 return sb.toString(); 359 } 360 361 } 362 | Popular Tags |