1 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 ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.io.File ; 29 import java.io.IOException ; 30 31 36 public class TestDefinition implements Comparable { 37 38 private static final Logger log = Logger.getInstance( TestDefinition.class ); 39 40 private String name; 41 private String description; 42 private WebappConfig webapp; 43 private List categories; 44 45 public TestDefinition( String name, String description, WebappConfig webapp, List 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 (); 59 } 60 } 61 62 public String getName() { 63 return name; 64 } 65 66 public String getDescription() { 67 return description; 68 } 69 70 public void setDescription( String description ) { 71 this.description = description; 72 } 73 74 public WebappConfig getWebapp() { 75 return webapp; 76 } 77 78 private List getCategoriesInternal() { 79 return categories; 80 } 81 82 public List getCategories() { 83 return Collections.unmodifiableList( categories ); 84 } 85 86 public void addCategory( Category category ) { 87 getCategoriesInternal().add( category ); 88 } 89 90 public String getTestFilePath() { 91 return getWebapp().getTestDirectory() + "/" + getName() + Constants.XML; 92 } 93 94 public String getResultFilePath() { 95 return getWebapp().getResultsDirectory() + "/" + getName() + Constants.XML; 96 } 97 98 public String getResultDiffFilePath() { 99 return getWebapp().getResultsDirectory() + "/" + getName() + Constants.DIFF + Constants.XML; 100 } 101 102 public void createRecordFile() throws IOException { 103 createRecordFile( false ); 104 } 105 106 public void createRecordFile( boolean overwrite ) throws IOException { 107 if ( ! getWebapp().createTestDirectory()) { 108 String msg = "ERROR: unable to create test directory( " + getWebapp().getTestDirectory() + " )"; 109 log.error( msg ); 110 throw new IOException ( msg ); 111 } 112 File file = new File ( getTestFilePath() ); 113 if ( overwrite ) { 114 if ( file.exists() && ! file.delete() ) { 115 String msg = "ERROR: unable to delete existing test file( " + file + " )"; 116 log.error( msg ); 117 throw new IOException ( msg ); 118 } 119 } 120 else { 121 if ( file.exists() ) { 122 String msg = "ERROR: test file exists( " + file + " )"; 123 log.error( msg ); 124 throw new IOException ( msg ); 125 } 126 } 127 if ( ! file.createNewFile() ) { 128 String msg = "ERROR: unable to create new test file( " + file + " )"; 129 log.error( msg ); 130 throw new IOException ( msg ); 131 } 132 } 133 134 public void createPlaybackFile( ) throws IOException { 135 if ( !getWebapp().createResultsDirectory() ) { 136 String msg = "ERROR: unable to create results directory( " + getWebapp().getResultsDirectory() + " )"; 137 log.error( msg ); 138 throw new IOException ( msg ); 139 } 140 File file = new File ( getResultFilePath() ); 141 if ( file.exists() && !file.delete() ) { 142 String msg = "ERROR: unable to delete existing playback file( " + file + " )"; 143 log.error( msg ); 144 throw new IOException ( msg ); 145 } 146 if ( !file.createNewFile() ) { 147 String msg = "ERROR: unable to create new test playback file( " + file + " )"; 148 log.error( msg ); 149 throw new IOException ( msg ); 150 } 151 } 152 153 public void createPlaybackDiffFile() throws IOException { 154 if ( !getWebapp().createResultsDirectory() ) { 155 String msg = "ERROR: unable to create results directory( " + getWebapp().getResultsDirectory() + " )"; 156 log.error( msg ); 157 throw new IOException ( msg ); 158 } 159 File file = new File ( getResultDiffFilePath() ); 160 if ( file.exists() && !file.delete() ) { 161 String msg = "ERROR: unable to delete existing playback diff file( " + file + " )"; 162 log.error( msg ); 163 throw new IOException ( msg ); 164 } 165 if ( !file.createNewFile() ) { 166 String msg = "ERROR: unable to create new playback diff file( " + file + " )"; 167 log.error( msg ); 168 throw new IOException ( msg ); 169 } 170 } 171 172 public int compareTo( Object 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 toString() { 181 StringBuffer sb = new StringBuffer ( 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 testListToString( List list ) { 192 StringBuffer sb = new StringBuffer ( 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 |