KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > server > state > PlaybackSessionImpl


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18
19 package org.apache.beehive.netui.tools.testrecorder.server.state;
20
21 import java.io.File JavaDoc;
22 import java.rmi.server.UID JavaDoc;
23 import java.lang.StringBuffer JavaDoc;
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 JavaDoc uid;
41     private String JavaDoc stringUid;
42     private File JavaDoc recordFile;
43     private File JavaDoc playbackFile;
44     private File JavaDoc diffFile;
45
46     // record bean is read only
47
private RecordSessionBean recordSessionBean;
48     private PlaybackSessionBean playbackSessionBean;
49
50     PlaybackSessionImpl( String JavaDoc sessionName, File JavaDoc playbackFile, File JavaDoc recordFile,
51             File JavaDoc 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 JavaDoc();
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 a version of this file exists, kill it
88
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         // file doesn't exist
114
try {
115             if ( !playbackFile.createNewFile() ) {
116                 error( "unable to create new playback file( " + playbackFile.getAbsolutePath() + " )" );
117             }
118         }
119         catch ( Exception JavaDoc ex ) {
120             error( "unable to create new playback file( " + playbackFile.getAbsolutePath() + " )", ex );
121         }
122         setSessionState( BEGIN );
123     }
124
125     public String JavaDoc 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 JavaDoc msg = "Unable to start test, session must be started before starting a test";
137             error( msg );
138         }
139         else if ( getSessionState() == END ) {
140             String JavaDoc 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 JavaDoc msg = "Unable to start test, Session has encountered a previous unrecoverable error";
145             error( msg );
146         }
147         else {
148             // getSessionState() == STOP ... return false, no new tests
149
}
150         return rtnVal;
151     }
152
153     // returns the current test count or -1 if endTest() fails
154
public synchronized int endTest( FilterData filterData ) throws SessionFailedException {
155         if ( getSessionState() >= ERROR ) {
156             String JavaDoc msg = "Unable to end test, Session has encountered a previous unrecoverable error";
157             error( msg );
158         }
159         if ( getSessionState() == BEGIN ) {
160             String JavaDoc msg = "Session must be started before using test operations";
161             error( msg );
162         }
163         else if ( getSessionState() == END ) {
164             String JavaDoc msg = "Session is finished, no test operations may be performed";
165             error( msg );
166         }
167         if ( inProgressCnt() == 0 ) {
168             // no tests in progress to end.
169
String JavaDoc msg = "No session in progress to end";
170             error( msg );
171         }
172         else if ( inProgressCnt() != 1 ) {
173             // programming error, unrecoverable
174
String JavaDoc msg = "inProgress( " + inProgressCnt() + " ) is invalid";
175             error( msg, null, true );
176         }
177         // write to record file.
178
if ( filterData.getReqData() == null ) {
179             // unrecoverable, test data cannot be persisted
180
String JavaDoc msg = "request data may not be null, test count(" + ( testCount() + 1 ) + ")";
181             error( msg, null, true );
182         }
183         else if ( filterData.getRespData() == null ) {
184             // unrecoverable, test data cannot be persisted
185
String JavaDoc 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         // do diff work ... get TestResults
191
TestResults results = new TestResults( testCount() + 1, filterData.getReqData().getPath() );
192         // add test and session exceptions to diff results.
193
Throwable JavaDoc 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 JavaDoc resp = filterData.getRespData().getBody();
208             String JavaDoc body = null;
209             // replace the CDATA in the body. We escape the CDATA so we can nest this.
210
int pos = resp.indexOf("<![CDATA[");
211             if (pos != -1) {
212                 body = resp;
213                 resp = resp.replaceAll("\\Q<![CDATA[\\E","&lt;![CDATA[");
214                 resp = resp.replaceAll("\\Q]]>","]]&gt;");
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 JavaDoc e ) {
225             String JavaDoc 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             // cleans up the session if getSessionState() == STOP and inProgressCnt() == 0
234
checkSessionComplete();
235         }
236         if ( log.isDebugEnabled() ) {
237             log.debug( "testCount( " + testCount() + " )" );
238         }
239         return testCount();
240     }
241
242     // returns true if the session is successfully started, false otherwise.
243
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 JavaDoc e ) {
268             setSessionState( ERROR_END );
269             String JavaDoc 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             // test failed, write diff data
276
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 JavaDoc e ) {
283                 setSessionState( ERROR_END );
284                 String JavaDoc msg = "ERROR: failed to create diff XML file( " + getDiffFile().getAbsolutePath() +
285                         " ), exception( " + e.getMessage() + " )";
286                 log.error( msg, e );
287                 // don't hide previous error
288
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 JavaDoc getStringUID() {
299         return stringUid;
300     }
301
302     public File JavaDoc getPlaybackFile() {
303         return playbackFile;
304     }
305
306     public File JavaDoc getDiffFile() {
307         return diffFile;
308     }
309
310     public File JavaDoc 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 JavaDoc recordFile ) throws SessionFailedException {
327         RecordSessionBean sessionBean = null;
328         try {
329             sessionBean = XMLHelper.getRecordSessionBean( recordFile );
330         }
331         catch ( Exception JavaDoc e ) {
332             String JavaDoc msg = "ERROR: failed parsing record file( " + recordFile + " ), exception( " + e.getMessage() +
333                     " )";
334             error( msg, e );
335         }
336         return sessionBean;
337     }
338
339     public String JavaDoc toString() {
340         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 128 );
341         sb.append( "[ " );
342         sb.append( "sessionName( " + getSessionName() + " )" );
343 // sb.append( ", status( " + getStatus() + " )" );
344
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