1 package org.appfuse.tool;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.hibernate.mapping.Column;
6
7 import java.sql.Timestamp;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10 import java.util.Locale;
11 import java.util.MissingResourceException;
12 import java.util.ResourceBundle;
13
14
15
16
17
18
19
20
21 public class DataHelper {
22 private static final Log log = LogFactory.getLog(DataHelper.class);
23 private static String datePattern = "yyyy-MM-dd";
24 private static String uiDatePattern = getDatePattern();
25
26
27
28
29
30
31 public String getTestValueForDbUnit(Column column) {
32 StringBuffer result = new StringBuffer();
33 String type = column.getValue().getType().getReturnedClass().getName();
34
35 if ("java.lang.Integer".equals(type) || "int".equals(type)) {
36 result.append((int) ((Math.random() * Integer.MAX_VALUE)) );
37 } else if ("java.lang.Float".equals(type) || "float".equals(type)) {
38 result.append((float) ((Math.random() * Float.MAX_VALUE)));
39 } else if ("java.lang.Long".equals(type) || "long".equals(type)) {
40 result.append((long) ((Math.random() * Long.MAX_VALUE)));
41 } else if ("java.lang.Double".equals(type) || "double".equals(type)) {
42 result.append((double) ((Math.random() * Double.MAX_VALUE)));
43 } else if ("java.lang.Short".equals(type) || "short".equals(type)) {
44 result.append((short) ((Math.random() * Short.MAX_VALUE)));
45 } else if ("java.lang.Byte".equals(type) || "byte".equals(type)) {
46 result.append((byte) ((Math.random() * Byte.MAX_VALUE)));
47 } else if ("java.lang.Boolean".equals(type) || "boolean".equals(type)) {
48 result.append("0");
49 } else if ("java.util.Date".equals(type) || "java.sql.Date".equals(type)) {
50 result.append(getDate(new Date()));
51 } else if ("java.sql.Timestamp".equals(type)) {
52 result.append(new Timestamp(new Date().getTime()).toString());
53 } else {
54 String stringWithQuotes = generateStringValue(column);
55 result.append(stringWithQuotes.substring(1, stringWithQuotes.length()-1));
56 }
57
58 return result.toString();
59 }
60
61
62
63
64
65
66 public String getValueForJavaTest(Column column) {
67 StringBuffer result = new StringBuffer();
68 String type = column.getValue().getType().getReturnedClass().getName();
69
70 if ("java.lang.Integer".equals(type)) {
71 result.append((int) ((Math.random() * Integer.MAX_VALUE)));
72 } else if ("int".equals(type)) {
73 result.append("(int) ").append((int) ((Math.random() * Integer.MAX_VALUE)));
74 } else if ("java.lang.Float".equals(type) ) {
75 result.append("new Float(").append((float) ((Math.random() * Float.MAX_VALUE))).append(")");
76 } else if ("float".equals(type)) {
77 result.append("(float) ").append((float) ((Math.random() * Float.MAX_VALUE)));
78 } else if ("java.lang.Long".equals(type)) {
79
80 result.append(Math.random() * Integer.MAX_VALUE).append("L");
81 } else if ("long".equals(type)) {
82
83 result.append((long) ((Math.random() * Integer.MAX_VALUE)));
84 } else if ("java.lang.Double".equals(type)) {
85 result.append("new Double(").append((Math.random() * Double.MAX_VALUE)).append(")");
86 } else if ("double".equals(type)) {
87 result.append((Math.random() * Double.MAX_VALUE));
88 } else if ("java.lang.Short".equals(type)) {
89 result.append("new Short(\"").append((short) ((Math.random() * Short.MAX_VALUE))).append("\")");
90 } else if ("short".equals(type)) {
91 result.append("(short)").append((short) ((Math.random() * Short.MAX_VALUE)));
92 } else if ("java.lang.Byte".equals(type)) {
93 result.append("new Byte(\"").append((byte) ((Math.random() * Byte.MAX_VALUE))).append("\")");
94 } else if ("byte".equals(type)) {
95 result.append("(byte) ").append((byte) ((Math.random() * Byte.MAX_VALUE)));
96 } else if ("java.lang.Boolean".equals(type)) {
97 result.append("Boolean.FALSE");
98 } else if ("boolean".equals(type)) {
99 result.append("false");
100 } else if ("java.util.Date".equals(type)) {
101 result.append("new java.util.Date()");
102 } else if ("java.sql.Date".equals(type)) {
103 result.append("new java.sql.Date()");
104 } else if ("java.sql.Timestamp".equals(type)) {
105 result.append("java.sql.Timestamp.valueOf(\"")
106 .append(new Timestamp(new Date().getTime()).toString()).append("\")");
107 } else {
108 result.append(generateStringValue(column));
109 }
110
111 return result.toString();
112 }
113
114
115
116
117
118
119 public String getValueForWebTest(Column column) {
120 String type = column.getValue().getType().getReturnedClass().getName();
121 String value = getTestValueForDbUnit(column);
122 if (type.equalsIgnoreCase(Date.class.getName())) {
123 value = getDate(new Date(), uiDatePattern);
124 } else if ("boolean".equals(type) || "java.lang.Boolean".equals(type)) {
125 value = "true";
126 }
127
128 return value;
129 }
130
131 private String generateStringValue(Column column) {
132 int maxLen = column.getLength();
133 if (maxLen > 5000) {
134 log.warn("Column length greater than 5000 characters for '" + column.getName() +
135 "', setting maxlength to 5000.");
136 maxLen = 5000;
137 }
138
139 StringBuffer result = new StringBuffer("\"");
140
141 for (int i = 0; (i < maxLen); i++) {
142 int j = 0;
143 if (i % 2 == 0) {
144 j = (int) ((Math.random() * 26) + 65);
145 } else {
146 j = (int) ((Math.random() * 26) + 97);
147 }
148 result.append(Character.toString((char) j));
149 }
150
151
152 result.append("\"");
153
154 return result.toString();
155 }
156
157 public String generateRandomStringValue(Column column) {
158 return "\"\" + Math.random()";
159 }
160
161 private static String getDate(Date aDate) {
162 return getDate(aDate, datePattern);
163 }
164
165 private static String getDate(Date aDate, String pattern) {
166 SimpleDateFormat df;
167 String returnValue = "";
168
169 if (aDate != null) {
170 df = new SimpleDateFormat(pattern);
171 returnValue = df.format(aDate);
172 }
173
174 return returnValue;
175 }
176
177
178
179
180
181 public static synchronized String getDatePattern() {
182 String result;
183 try {
184 result = ResourceBundle.getBundle("ApplicationResources", Locale.getDefault())
185 .getString("date.format");
186 } catch (MissingResourceException mse) {
187 result = "MM/dd/yyyy";
188 }
189 return result;
190 }
191
192
193
194
195
196
197 public String getFieldDescription(String fieldName) {
198 StringBuffer buffer = new StringBuffer();
199 boolean nextUpper = false;
200 for (int i = 0; i < fieldName.length(); i++) {
201 char c = fieldName.charAt(i);
202
203 if (i == 0) {
204 buffer.append(Character.toUpperCase(c));
205 continue;
206 }
207
208 if (Character.isUpperCase(c)) {
209 buffer.append(' ');
210 buffer.append(c);
211 continue;
212 }
213
214 if (c == '.') {
215 buffer.delete(0, buffer.length());
216 nextUpper = true;
217 continue;
218 }
219
220 char x = nextUpper ? Character.toUpperCase(c) : c;
221 buffer.append(x);
222 nextUpper = false;
223 }
224
225 return buffer.toString();
226 }
227
228
229
230
231
232
233
234 public String getJdbcType(String javaType) {
235 String jdbcType = "VARCHAR";
236
237 javaType = javaType.toLowerCase();
238
239 if (javaType.indexOf("date") > 0) {
240 jdbcType = "TIMESTAMP";
241 } else if (javaType.indexOf("timestamp") > 0) {
242 jdbcType = "TIMESTAMP";
243 } else if ((javaType.indexOf("int") > 0) || (javaType.indexOf("long") > 0) || (javaType.indexOf("short") > 0)) {
244 jdbcType = "INTEGER";
245 } else if (javaType.indexOf("double") > 0) {
246 jdbcType = "DOUBLE";
247 } else if (javaType.indexOf("float") > 0) {
248 jdbcType = "FLOAT";
249 }
250
251 return jdbcType;
252 }
253 }