1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package pl.kernelpanic.dbmonster.generator;
48
49 import pl.kernelpanic.dbmonster.DBMonster;
50 import pl.kernelpanic.dbmonster.DBMonsterContext;
51 import pl.kernelpanic.dbmonster.Dictionary;
52 import pl.kernelpanic.dbmonster.DictionaryManager;
53
54 /***
55 * The dictionary generator uses external dictionary as the source
56 * of test data.
57 *
58 * <p>
59 * Dictionary is a flat text file. Each line in this file is a dictionary item.
60 * <u>Items must be unique!</u>
61 * A file may be compressed using ZIP or GZIP algorithm. The extension of the
62 * file (.txt, .zip, .gz) decides which compression method is used.
63 * </p>
64 *
65 * <p>
66 * Dictionary files must be encoded in UTF-8.
67 * </p>
68 *
69 * <h2>Properties</h2>
70 * <table>
71 * <tr>
72 * <td>dictFile</td>
73 * <td>Absolute or relative to the schema file path to the dictionary.</td>
74 * </tr>
75 * <tr>
76 * <td>unique</td>
77 * <td>boolean value which indicates whether generated value should
78 * be unique or random (default: false - random item is used).</td>
79 * </tr>
80 * </table>
81 *
82 * @author Piotr Maj <piotr.maj@kernelpanic.pl>
83 *
84 * @version $Id: DictionaryGenerator.html,v 1.1 2007/06/21 08:38:13 sbahloul Exp $
85 */
86 public class DictionaryGenerator extends BasicDataGenerator implements Initializable {
87
88 /***
89 * Context.
90 */
91 private DBMonsterContext context = null;
92
93 /***
94 * Dictionary file name.
95 */
96 private String dictFile = null;
97
98 /***
99 * Should this generator produce unique values?
100 */
101 private boolean unique = false;
102
103 /***
104 * The dictionary.
105 */
106 private Dictionary dictionary = null;
107
108 /***
109 * Is this generator used for the first time?
110 */
111 private boolean firstUse = true;
112
113 /***
114 * Initializes the generator.
115 *
116 * @param ctx context
117 *
118 * @throws Exception thrown when this generator cannot be initialized.
119 */
120 public final void initialize(final DBMonsterContext ctx) throws Exception {
121 context = ctx;
122 DictionaryManager dm =
123 (DictionaryManager) context.getProperty(
124 DBMonster.DICTIONARY_MANAGER_KEY);
125 String schemaPath = column.getTable().getSchema().getHome();
126 dictionary = dm.getDictionary(schemaPath, dictFile);
127 }
128
129 /***
130 * Generates the value using a dictionary.
131 *
132 * @return value
133 *
134 * @throws Exception if generattion fails
135 */
136 public final Object generate() throws Exception {
137 if (firstUse) {
138 dictionary.reset();
139 firstUse = false;
140 }
141 if (unique) {
142 return dictionary.getNextUniqueItem();
143 } else {
144 return dictionary.getNextRandomItem();
145 }
146 }
147
148 /***
149 * Sets the dictionary name.
150 *
151 * @param name the name of the file.
152 */
153 public final void setDictFile(final String name) {
154 dictFile = name;
155 }
156
157 /***
158 * Returns the dictionary name.
159 *
160 * @return a dictionary file name
161 */
162 public final String getDictFile() {
163 return dictFile;
164 }
165
166 /***
167 * Sets the unique parameter.
168 *
169 * @param b unique
170 */
171 public final void setUnique(final boolean b) {
172 unique = b;
173 }
174
175 /***
176 * Returns the unique parameter.
177 *
178 * @return <code>true</code> if generated value are unique,
179 * <code>false</code> if generated value are random.
180 */
181 public final boolean getUnique() {
182 return unique;
183 }
184
185 /***
186 * Resets the generator.
187 */
188 public final void reset() {
189 firstUse = true;
190 }
191 }