|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| TestSet.java | 88,9% | 95,6% | 100% | 94,7% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* Copyright (C) 2003, Marco Jansen / Contrado Technologies (http://www.contrado.nl)
|
|
| 3 |
*
|
|
| 4 |
* This program is free software; you can redistribute it and/or
|
|
| 5 |
* modify it under the terms of the GNU General Public License as
|
|
| 6 |
* published by the Free Software Foundation; either version 2 of the
|
|
| 7 |
* License, or (at your option) any later version.
|
|
| 8 |
*
|
|
| 9 |
* This program is distributed in the hope that it will be useful, but
|
|
| 10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
| 12 |
* General Public License for more details.
|
|
| 13 |
*/
|
|
| 14 |
|
|
| 15 |
package net.sourceforge.anttestsetgen;
|
|
| 16 |
|
|
| 17 |
import org.apache.tools.ant.BuildException;
|
|
| 18 |
import org.apache.tools.ant.Project;
|
|
| 19 |
import org.apache.tools.ant.Task;
|
|
| 20 |
|
|
| 21 |
import java.sql.Connection;
|
|
| 22 |
import java.sql.ResultSet;
|
|
| 23 |
import java.sql.SQLException;
|
|
| 24 |
import java.sql.Statement;
|
|
| 25 |
import java.util.ArrayList;
|
|
| 26 |
import java.util.List;
|
|
| 27 |
|
|
| 28 |
/**
|
|
| 29 |
* A TestSet contains the description of a test set.
|
|
| 30 |
* <p/>
|
|
| 31 |
* A TestSet has the following attributes:
|
|
| 32 |
* - name
|
|
| 33 |
* - rowtoselect
|
|
| 34 |
* <p/>
|
|
| 35 |
* A TestSet has the following subtasks:
|
|
| 36 |
* - Query
|
|
| 37 |
* - Entries
|
|
| 38 |
*
|
|
| 39 |
* @author <a href="mailto:m.c.jansen.spam at planet.nl">Marco Jansen (Contrado Technologies)</a>
|
|
| 40 |
* @version $Id: TestSet.java,v 1.3 2004/01/04 15:00:18 mcjansen Exp $
|
|
| 41 |
*/
|
|
| 42 |
public class TestSet extends Task { |
|
| 43 |
|
|
| 44 |
/** Select the first valid row from the result set. */
|
|
| 45 |
public static final String SELECT_FIRST_ROW = "First"; |
|
| 46 |
|
|
| 47 |
/** Select all valid rows from the result set. */
|
|
| 48 |
public static final String SELECT_ALL = "All"; |
|
| 49 |
|
|
| 50 |
/** The name of the test set. */
|
|
| 51 |
private String name;
|
|
| 52 |
|
|
| 53 |
/**
|
|
| 54 |
* The row to select.
|
|
| 55 |
*
|
|
| 56 |
* @see #SELECT_ALL
|
|
| 57 |
* @see #SELECT_FIRST_ROW
|
|
| 58 |
*/
|
|
| 59 |
private String rowtoselect = SELECT_FIRST_ROW;
|
|
| 60 |
|
|
| 61 |
/** The sql query that should be used to retrieve the property values from the database. */
|
|
| 62 |
private Query query = null; |
|
| 63 |
|
|
| 64 |
/** A list with entries that define the mapping from the database results to the propert file. */
|
|
| 65 |
private List entries = new ArrayList(); |
|
| 66 |
|
|
| 67 |
/** An optional validation class, used perform an additional validation on the results from the database. */
|
|
| 68 |
private Validation validation = null; |
|
| 69 |
|
|
| 70 |
/** A list that contains the unique entries. Used to determine if a property file entry already is used. */
|
|
| 71 |
private List uniqueEntriesInTestSet = new ArrayList(); |
|
| 72 |
|
|
| 73 |
/**
|
|
| 74 |
* Set the name attribute.
|
|
| 75 |
*
|
|
| 76 |
* @param name The name of the TestSet
|
|
| 77 |
* @see #getName
|
|
| 78 |
*/
|
|
| 79 | 78 |
public void setName(String name) { |
| 80 | 78 |
this.name = name;
|
| 81 |
} |
|
| 82 |
|
|
| 83 |
/**
|
|
| 84 |
* Retreive the name attribute.
|
|
| 85 |
*
|
|
| 86 |
* @return A string with the name of the TestSet
|
|
| 87 |
* @see #setName
|
|
| 88 |
*/
|
|
| 89 | 162 |
public String getName() {
|
| 90 | 162 |
return AntTestSetGenUtil.requiredCheck("name", name); |
| 91 |
} |
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* Set the rowtoselect attribute. This attribute defines which row should be select from the resultset. The attribute
|
|
| 95 |
* can contain the following values:
|
|
| 96 |
* - first
|
|
| 97 |
* - last
|
|
| 98 |
* - random
|
|
| 99 |
* <p/>
|
|
| 100 |
* The default value for this attribute is the first row.
|
|
| 101 |
*
|
|
| 102 |
* @param rowtoselect Defines which row should be selected from the resultset: the first, the last or a random row.
|
|
| 103 |
* @throws org.apache.tools.ant.BuildException
|
|
| 104 |
* A BuildException is thrown if rowtoselect contains an invalid value.
|
|
| 105 |
* @see #getRowtoselect
|
|
| 106 |
*/
|
|
| 107 | 78 |
public void setRowtoselect(String rowtoselect) throws BuildException { |
| 108 |
|
|
| 109 | 78 |
if (rowtoselect == null || SELECT_FIRST_ROW.toUpperCase().equals(rowtoselect.toUpperCase())) { |
| 110 | 66 |
this.rowtoselect = SELECT_FIRST_ROW;
|
| 111 | 12 |
} else if (SELECT_ALL.toUpperCase().equals(rowtoselect.toUpperCase())) { |
| 112 | 10 |
this.rowtoselect = SELECT_ALL;
|
| 113 |
} else {
|
|
| 114 | 2 |
throw new BuildException("Rowtoselect should contain either " + SELECT_FIRST_ROW + " or " + SELECT_ALL); |
| 115 |
} |
|
| 116 |
|
|
| 117 |
} |
|
| 118 |
|
|
| 119 |
/**
|
|
| 120 |
* Retrieve the rowtoselect attribute.
|
|
| 121 |
*
|
|
| 122 |
* @return The row that is used from the resultset (The first, the last or a random rowtoselect)
|
|
| 123 |
* @throws org.apache.tools.ant.BuildException
|
|
| 124 |
* A BuildException is thrown in rowtoselect contains an invalid value.
|
|
| 125 |
* @see #setRowtoselect
|
|
| 126 |
*/
|
|
| 127 | 526 |
public String getRowtoselect() {
|
| 128 | 526 |
return rowtoselect;
|
| 129 |
} |
|
| 130 |
|
|
| 131 |
/**
|
|
| 132 |
* Retrieve a list with all the unique entries in the test set.
|
|
| 133 |
*
|
|
| 134 |
* @return a list with the names of the unique entries. The elements are of type String
|
|
| 135 |
*/
|
|
| 136 | 72 |
public List getUniqueEntriesInTestSet() {
|
| 137 | 72 |
return uniqueEntriesInTestSet;
|
| 138 |
} |
|
| 139 |
|
|
| 140 |
/**
|
|
| 141 |
* Add a Query to the TestSet. A TestSet should contain only one query. If more queries are added
|
|
| 142 |
* a BuildException is thrown.
|
|
| 143 |
*
|
|
| 144 |
* @param query The query to added to the TestSet
|
|
| 145 |
* @throws org.apache.tools.ant.BuildException
|
|
| 146 |
* A BuildException is thrown in more then one queries are added
|
|
| 147 |
* @see #getQuery
|
|
| 148 |
*/
|
|
| 149 | 76 |
public void addConfiguredQuery(Query query) throws BuildException { |
| 150 | 76 |
if (this.query == null) { |
| 151 | 74 |
this.query = query;
|
| 152 |
} else {
|
|
| 153 | 2 |
throw new BuildException("A TestSet should contain only one query."); |
| 154 |
} |
|
| 155 |
} |
|
| 156 |
|
|
| 157 |
/**
|
|
| 158 |
* Retrieve the query from the TestSet. If there are any ant properties, replaced them with their values.
|
|
| 159 |
*
|
|
| 160 |
* @return The query from the TestSet. If there is no query defined, null is returned.
|
|
| 161 |
* @throws org.apache.tools.ant.BuildException
|
|
| 162 |
* In case the test set doesn't contain a query.
|
|
| 163 |
* @see #addConfiguredQuery
|
|
| 164 |
*/
|
|
| 165 | 78 |
public String getQuery() throws BuildException { |
| 166 | 78 |
if (query != null) { |
| 167 | 76 |
return query.toString();
|
| 168 |
} else {
|
|
| 169 | 2 |
throw new BuildException("A TestSet should contain a query!"); |
| 170 |
} |
|
| 171 |
} |
|
| 172 |
|
|
| 173 |
/**
|
|
| 174 |
* Add an Configured Validation to this TestSet.
|
|
| 175 |
*
|
|
| 176 |
* @param validation The validation class (required)
|
|
| 177 |
* @throws org.apache.tools.ant.BuildException
|
|
| 178 |
* In case there already is a validation defined in this testset.
|
|
| 179 |
* @see #getValidation
|
|
| 180 |
*/
|
|
| 181 | 18 |
public void addConfiguredValidation(Validation validation) throws BuildException { |
| 182 | 18 |
if (this.validation == null) { |
| 183 | 18 |
this.validation = validation;
|
| 184 |
} else {
|
|
| 185 | 0 |
throw new BuildException("A TestSet should contain only one validation."); |
| 186 |
} |
|
| 187 |
} |
|
| 188 |
|
|
| 189 |
/**
|
|
| 190 |
* Retrieve the validation class.
|
|
| 191 |
*
|
|
| 192 |
* @return The validation class
|
|
| 193 |
* @see #addConfiguredValidation
|
|
| 194 |
*/
|
|
| 195 | 288 |
public Validation getValidation() {
|
| 196 | 288 |
return validation;
|
| 197 |
} |
|
| 198 |
|
|
| 199 |
/**
|
|
| 200 |
* Add a entry to the testset.
|
|
| 201 |
*
|
|
| 202 |
* @param entry A entry that describes how to map a database column on a key in the property file.
|
|
| 203 |
*/
|
|
| 204 | 86 |
public void addConfiguredEntry(Entry entry) { |
| 205 | 86 |
if (!uniqueEntriesInTestSet.contains(entry.getKey())) {
|
| 206 | 82 |
uniqueEntriesInTestSet.add(entry.getKey()); |
| 207 |
} else {
|
|
| 208 | 2 |
log(entry.getKey() + " already defined as entry ", Project.MSG_WARN);
|
| 209 |
} |
|
| 210 | 84 |
entries.add(entry); |
| 211 |
} |
|
| 212 |
|
|
| 213 |
/**
|
|
| 214 |
* Retrieve of the entries of the TestSet. A warning is given if there are no entries.
|
|
| 215 |
*
|
|
| 216 |
* @return A list with entries of the TestSet. The elements of the list are of type Entry
|
|
| 217 |
* @see Entry
|
|
| 218 |
*/
|
|
| 219 | 550 |
public List getEntries() {
|
| 220 | 550 |
if (entries.size() == 0) {
|
| 221 | 4 |
log("The TestSet " + getName() + " doesn't contain any entries that describes the mapping to the property file", Project.MSG_WARN); |
| 222 |
} |
|
| 223 | 550 |
return entries;
|
| 224 |
} |
|
| 225 |
|
|
| 226 |
/**
|
|
| 227 |
* Generate a testset.
|
|
| 228 |
*
|
|
| 229 |
* @param connection An open connection to the database
|
|
| 230 |
*/
|
|
| 231 | 80 |
public void generateTestSet(Connection connection) { |
| 232 |
|
|
| 233 |
// Check required fields
|
|
| 234 | 80 |
getName(); |
| 235 | 78 |
getRowtoselect(); |
| 236 | 78 |
getQuery(); |
| 237 |
|
|
| 238 | 76 |
log("rowtoselect set to " + rowtoselect, Project.MSG_VERBOSE);
|
| 239 |
|
|
| 240 | 76 |
Statement statement = null;
|
| 241 | 76 |
ResultSet resultSet = null;
|
| 242 |
|
|
| 243 |
// Execute the query
|
|
| 244 | 76 |
log("Generating TestSet " + name, Project.MSG_INFO);
|
| 245 |
|
|
| 246 | 76 |
try {
|
| 247 | 76 |
log("Executing query: " + query, Project.MSG_VERBOSE);
|
| 248 |
|
|
| 249 | 76 |
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
| 250 | 76 |
resultSet = statement.executeQuery(query.toString()); |
| 251 |
|
|
| 252 | 74 |
for (int i = 0; i < entries.size(); i++) { |
| 253 | 102 |
Entry entry = (Entry) entries.get(i); |
| 254 | 102 |
entry.processResult(resultSet, this);
|
| 255 |
} |
|
| 256 |
|
|
| 257 |
} catch (SQLException e) {
|
|
| 258 | 2 |
throw new BuildException("Error while executing query for '" + name + "': " + e.getMessage()); |
| 259 |
|
|
| 260 |
} finally {
|
|
| 261 | 76 |
if (statement != null) { |
| 262 | 76 |
try {
|
| 263 | 76 |
statement.close(); |
| 264 |
} catch (SQLException e) {
|
|
| 265 | 0 |
throw new BuildException("Error while closing database connection for test set '" + name + "': " + e.getMessage()); |
| 266 |
} |
|
| 267 |
} |
|
| 268 |
} |
|
| 269 |
} |
|
| 270 |
|
|
| 271 |
} |
|
| 272 |
|
|
||||||||||