1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon;
20
21 import java.io.File;
22
23 import net.sf.jameleon.data.AbstractFileDrivableTag;
24 import net.sf.jameleon.data.CsvDataDriver;
25 import net.sf.jameleon.data.DataDriver;
26 import net.sf.jameleon.util.Configurator;
27
28 /***
29 * This DataDrivable tag is an implementation of a CSV data source.
30 */
31 public abstract class AbstractCsvTag extends AbstractFileDrivableTag {
32
33 protected CsvDataDriver csv;
34 protected String name;
35 protected String csvFileName;
36
37 /***
38 * Calculates the location of the state to be stored for any tags under this tag.
39 * The result should be a relative path that simply has the row number in it along
40 * with some unique indentifier for this tag like the handle name or something.
41 * @return the location of the state to be stored for any tags under this tag minus the baseDir calculation stuff.
42 */
43 protected String getNewStateStoreLocation(int rowNum){
44 String fName = getCsvFile().getName();
45 int index = fName.lastIndexOf(".");
46 if (index == -1) {
47 index = fName.length();
48 }
49 fName = fName.substring(0, index);
50 if (rowNum > 0) {
51 fName = fName + File.separator + rowNum;
52 }
53 return fName;
54 }
55
56 /***
57 * Gets an error message to be displayed when a error occurs due to the DataDriver.
58 * @return an error message to be displayed when a error occurs due to the DataDriver.
59 */
60 protected String getDataExceptionMessage(){
61 return "Trouble reading file "+getCsvFile();
62 }
63
64 /***
65 * Sets up the DataDriver by calling any implementation-dependent
66 * methods.
67 */
68 protected void setupDataDriver(){
69 csv.setFile(getCsvFile());
70 if (getCsvCharset() == null) {
71 Configurator config = Configurator.getInstance();
72 config.setConfigName(tct.getJameleonConfigName());
73 setCsvCharset(config.getValue("csvCharset"));
74 }
75 if (getCsvCharset() != null && getCsvCharset().trim().length() > 0) {
76 csv.setEncoding(getCsvCharset());
77 }
78 }
79
80 /***
81 * Gets the file that will be used as a data source.
82 * @return The csv file to run the test against
83 */
84 public File getCsvFile(){
85 File csvFile = null;
86 if (csvFileName != null && csvFileName.length() > 0) {
87 csvFile = new File(tct.getCsvDir(false), csvFileName);
88 }else{
89 csvFile = new File(tct.getCsvDir(true), name+".csv");
90 }
91 return csvFile;
92 }
93
94
95 protected DataDriver getDataDriver(){
96 csv = new CsvDataDriver();
97 return csv;
98 }
99
100 /***
101 * Gets the relative path to the name of the csv file. This does
102 * not use any logic in considering testEnvironment nor organization.
103 * @return the relative path and name of the csv file to read in.
104 */
105 public String getCsvFileName() {
106 return this.csvFileName;
107 }
108
109 /***
110 * Sets the relative path to the name of the csv file. This does
111 * not use any logic in considering testEnvironment nor organization.
112 * @param csvFileName - the relative path and name of the csv file to read in.
113 * @jameleon.attribute
114 */
115 public void setCsvFileName(String csvFileName) {
116 this.csvFileName = csvFileName;
117 }
118
119 /***
120 * @return the name of the csv file to read in.
121 */
122 public String getName() {
123 return this.name;
124 }
125
126 /***
127 * Sets the name of the csv file to read in.
128 * @param name - The name of the csv file.
129 * @jameleon.attribute
130 */
131 public void setName(String name) {
132 this.name = name;
133 }
134
135 /***
136 * @return The directory of the csv file (relative to baseDir).
137 */
138 public File getCsvDir() {
139 return getDataDir();
140 }
141
142 /***
143 * Sets the directory of the csv file to read in (relative to baseDir).
144 * @param csvDir - The directory of the csv file.
145 * @jameleon.attribute
146 */
147 public void setCsvDir(File csvDir) {
148 setDataDir(csvDir);
149 }
150
151 /***
152 * @return the separator used to separate CSV files.
153 */
154 public char getCsvValueSeparator(){
155 return csv.getDelimiter();
156 }
157
158 /***
159 * Sets the separator used to separate CSV files.
160 * @param valueSeparator - The separator used to separate CSV files.
161 * @jameleon.attribute
162 */
163 public void setCsvValueSeparator(char valueSeparator){
164 csv.setDelimiter(valueSeparator);
165 }
166
167 /***
168 * Sets the character set to use when reading in the CSV file
169 * @param charset - the character set to use when reading in the CSV file (Defaults to UTF-8)
170 * @jameleon.attribute
171 */
172 public void setCsvCharset(String charset){
173 setCharset(charset);
174 }
175
176 public String getCsvCharset() {
177 return charset;
178 }
179
180 }