KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > shared > config > TestDefinition


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.shared.config;
20
21 import org.apache.beehive.netui.tools.testrecorder.shared.util.StringHelper;
22 import org.apache.beehive.netui.tools.testrecorder.shared.Constants;
23 import org.apache.beehive.netui.tools.testrecorder.shared.Logger;
24
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 /**
32  * User: ozzy
33  * Date: Apr 9, 2004
34  * Time: 2:36:17 PM
35  */

36 public class TestDefinition implements Comparable JavaDoc {
37
38     private static final Logger log = Logger.getInstance( TestDefinition.class );
39
40     private String JavaDoc name;
41     private String JavaDoc description;
42     private WebappConfig webapp;
43     private List JavaDoc categories;
44
45     public TestDefinition( String JavaDoc name, String JavaDoc description, WebappConfig webapp, List JavaDoc categories ) {
46         this.name = name;
47         assert name != null : "ERROR: test name may not be null";
48         if ( description == null ) {
49             this.description = "";
50         }
51         else {
52             this.description = description;
53         }
54         assert webapp != null : "ERROR: webapp may not be null, test name( " + name + " )";
55         this.webapp = webapp;
56         this.categories = categories;
57         if ( this.categories == null ) {
58             this.categories = new ArrayList JavaDoc();
59         }
60     }
61
62     public String JavaDoc getName() {
63         return name;
64     }
65
66     public String JavaDoc getDescription() {
67         return description;
68     }
69
70     public void setDescription( String JavaDoc description ) {
71         this.description = description;
72     }
73
74     public WebappConfig getWebapp() {
75         return webapp;
76     }
77
78     private List JavaDoc getCategoriesInternal() {
79         return categories;
80     }
81
82     public List JavaDoc getCategories() {
83         return Collections.unmodifiableList( categories );
84     }
85
86     public void addCategory( Category category ) {
87         getCategoriesInternal().add( category );
88     }
89
90     public String JavaDoc getTestFilePath() {
91         return getWebapp().getTestDirectory() + "/" + getName() + Constants.XML;
92     }
93
94     public String JavaDoc getResultFilePath() {
95         return getWebapp().getResultsDirectory() + "/" + getName() + Constants.XML;
96     }
97
98     public String JavaDoc getResultDiffFilePath() {
99         return getWebapp().getResultsDirectory() + "/" + getName() + Constants.DIFF + Constants.XML;
100     }
101
102     public void createRecordFile() throws IOException JavaDoc {
103         createRecordFile( false );
104     }
105
106     public void createRecordFile( boolean overwrite ) throws IOException JavaDoc {
107         if ( ! getWebapp().createTestDirectory()) {
108             String JavaDoc msg = "ERROR: unable to create test directory( " + getWebapp().getTestDirectory() + " )";
109             log.error( msg );
110             throw new IOException JavaDoc( msg );
111         }
112         File JavaDoc file = new File JavaDoc( getTestFilePath() );
113         if ( overwrite ) {
114             if ( file.exists() && ! file.delete() ) {
115                 String JavaDoc msg = "ERROR: unable to delete existing test file( " + file + " )";
116                 log.error( msg );
117                 throw new IOException JavaDoc( msg );
118             }
119         }
120         else {
121             if ( file.exists() ) {
122                 String JavaDoc msg = "ERROR: test file exists( " + file + " )";
123                 log.error( msg );
124                 throw new IOException JavaDoc( msg );
125             }
126         }
127         if ( ! file.createNewFile() ) {
128             String JavaDoc msg = "ERROR: unable to create new test file( " + file + " )";
129             log.error( msg );
130             throw new IOException JavaDoc( msg );
131         }
132     }
133
134     public void createPlaybackFile( ) throws IOException JavaDoc {
135         if ( !getWebapp().createResultsDirectory() ) {
136             String JavaDoc msg = "ERROR: unable to create results directory( " + getWebapp().getResultsDirectory() + " )";
137             log.error( msg );
138             throw new IOException JavaDoc( msg );
139         }
140         File JavaDoc file = new File JavaDoc( getResultFilePath() );
141         if ( file.exists() && !file.delete() ) {
142             String JavaDoc msg = "ERROR: unable to delete existing playback file( " + file + " )";
143             log.error( msg );
144             throw new IOException JavaDoc( msg );
145         }
146         if ( !file.createNewFile() ) {
147             String JavaDoc msg = "ERROR: unable to create new test playback file( " + file + " )";
148             log.error( msg );
149             throw new IOException JavaDoc( msg );
150         }
151     }
152
153     public void createPlaybackDiffFile() throws IOException JavaDoc {
154         if ( !getWebapp().createResultsDirectory() ) {
155             String JavaDoc msg = "ERROR: unable to create results directory( " + getWebapp().getResultsDirectory() + " )";
156             log.error( msg );
157             throw new IOException JavaDoc( msg );
158         }
159         File JavaDoc file = new File JavaDoc( getResultDiffFilePath() );
160         if ( file.exists() && !file.delete() ) {
161             String JavaDoc msg = "ERROR: unable to delete existing playback diff file( " + file + " )";
162             log.error( msg );
163             throw new IOException JavaDoc( msg );
164         }
165         if ( !file.createNewFile() ) {
166             String JavaDoc msg = "ERROR: unable to create new playback diff file( " + file + " )";
167             log.error( msg );
168             throw new IOException JavaDoc( msg );
169         }
170     }
171
172     public int compareTo( Object JavaDoc o ) {
173         if ( o instanceof TestDefinition ) {
174             TestDefinition other = (TestDefinition)o;
175             return getName().compareToIgnoreCase( other.getName() );
176         }
177         return 1;
178     }
179
180     public String JavaDoc toString() {
181         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 64 );
182         sb.append( "[ " );
183         sb.append( "name( " + name + " )" );
184         sb.append( ", description( " + description + " )" );
185         sb.append( ", webapp( " + getWebapp().getName() + " )" );
186         sb.append( ",\n\t categories( " + StringHelper.toString( categories.toArray(), "\n", "\n\t" ) + " )" );
187         sb.append( " ]" );
188         return sb.toString();
189     }
190
191     public static String JavaDoc testListToString( List JavaDoc list ) {
192         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 16 * list.size() );
193         TestDefinition test = null;
194         for ( int i = 0; i < list.size(); i++ ) {
195             test = (TestDefinition) list.get( i );
196             if ( i != 0 ) {
197                 sb.append( ", " );
198             }
199             sb.append( test.getName() );
200         }
201         return sb.toString();
202     }
203
204 }
205
Popular Tags