View Javadoc

1   /* Version 1.0 based on Apache Software License 1.1
2    *
3    * Copyright (c) 2003 Piotr Maj and DBMonster developers. All rights
4    * reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are
8    * met:
9    *
10   * 1. Redistributions of source code must retain the above copyright
11   *    notice, this list of conditions and the following disclaimer.
12   *
13   * 2. Redistributions in binary form must reproduce the above copyright
14   *    notice, this list of conditions and the following disclaimer in the
15   *    documentation and/or other materials provided with the distribution.
16   *
17   * 3. The end-user documentation included with the redistribution, if any,
18   *    must include the following acknowledgment:
19   *
20   *    "This product includes software developed by DBMonster developers
21   *    (http://dbmonster.kernelpanic.pl/)."
22   *
23   *  Alternately, this acknowledgment may appear in the software itself,
24   *  if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The name "DBMonster" must not be used to endorse or promote products
27   *    derived from this software without prior written permission. For
28   *    written permission, please contact piotr.maj@kernelpanic.pl.
29   *
30   * 5. Products derived from this software may not be called "DBMonster",
31   *    nor may "DBMonster" appear in their name, without prior written
32   *    permission of Piotr Maj.
33   *
34   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
35   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37   * IN NO EVENT SHALL THE DBMONSTER DEVELOPERS BE LIABLE FOR ANY DIRECT,
38   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
40   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43   * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44   * POSSIBILITY OF SUCH DAMAGE.
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 &lt;piotr.maj@kernelpanic.pl&gt;
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 }