001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import com.tc.object.config.ConfigVisitor;
007: import com.tc.object.config.DSOClientConfigHelper;
008: import com.tc.object.util.ReadOnlyException;
009: import com.tc.simulator.app.ApplicationConfig;
010: import com.tc.simulator.listener.ListenerProvider;
011: import com.tc.util.Assert;
012:
013: import java.sql.Time;
014: import java.sql.Timestamp;
015: import java.text.DateFormat;
016: import java.text.ParseException;
017: import java.text.SimpleDateFormat;
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: public class GenericDateTestApp extends GenericTestApp {
022: private static java.util.Date referenceDate = createDate("Feb 20, 2006");
023: private static java.util.Date alternateDate = createDate("Feb 22, 2006");
024: private static Timestamp referenceTimestamp = createTimestamp("Feb 20, 2006 03:20:20");
025:
026: public GenericDateTestApp(String appId, ApplicationConfig cfg,
027: ListenerProvider listenerProvider) {
028: super (appId, cfg, listenerProvider, java.util.Date.class);
029: }
030:
031: protected Object getTestObject(String testName) {
032: List list = (List) sharedMap.get("list");
033: return list.iterator();
034: }
035:
036: protected void setupTestObject(String testName) {
037: List list = new ArrayList();
038: list.add(new java.util.Date(referenceDate.getTime()));
039: list.add(new java.sql.Date(referenceDate.getTime()));
040: list.add(new Time(referenceDate.getTime()));
041: list.add(new Timestamp(referenceDate.getTime()));
042:
043: sharedMap.put("list", list);
044: }
045:
046: void testModifyDate(java.util.Date date, boolean validate) {
047: if (validate) {
048: assertEqualDate(alternateDate, date);
049: } else {
050: synchronized (date) {
051: date.setTime(createDate("Feb 22, 2006").getTime());
052: }
053: }
054: }
055:
056: void testDateToString(java.util.Date date, boolean validate) {
057: if (validate) {
058: assertEqualDate(referenceDate, date);
059: } else {
060: synchronized (date) {
061: System.out.println(date.toString());
062: }
063: }
064: }
065:
066: // date.setYear() is a deprecated method.
067: void testDateSetYear(java.util.Date date, boolean validate) {
068: if (date instanceof java.sql.Time) {
069: return;
070: } // java.sql.Time throws IllegalArgumentException in setYear().
071:
072: if (validate) {
073: assertEqualDate(createDate("Feb 20, 1999"), date);
074: } else {
075: synchronized (date) {
076: date.setYear(99);
077: }
078: }
079: }
080:
081: // date.setMonth() is a deprecated method.
082: void testDateSetMonth(java.util.Date date, boolean validate) {
083: if (date instanceof java.sql.Time) {
084: return;
085: } // java.sql.Time throws IllegalArgumentException in setMonth().
086:
087: if (validate) {
088: assertEqualDate(createDate("Apr 20, 2006"), date);
089: } else {
090: synchronized (date) {
091: date.setMonth(3);
092: }
093: }
094: }
095:
096: // date.setDate() is a deprecated method.
097: void testDateSetDate(java.util.Date date, boolean validate) {
098: if (date instanceof java.sql.Time) {
099: return;
100: } // java.sql.Time throws IllegalArgumentException in setDate().
101:
102: if (validate) {
103: assertEqualDate(createDate("Feb 03, 2006"), date);
104: } else {
105: synchronized (date) {
106: date.setDate(3);
107: }
108: }
109: }
110:
111: // date.setHour(), date.setMinutes(), and date.setSeconds() are deprecated methods.
112: void testDateSetHourMinutesSeconds(java.util.Date date,
113: boolean validate) {
114: if (date instanceof java.sql.Date) {
115: return;
116: } // java.sql.Date throws IllegalArgumentException in setHours(),
117: // setMinutes(), and setSeconds().
118:
119: if (validate) {
120: assertEqualDate(createDateTime("Feb 20, 2006 03:20:20"),
121: date);
122: } else {
123: synchronized (date) {
124: date.setHours(3);
125: date.setMinutes(20);
126: date.setSeconds(20);
127: }
128: }
129: }
130:
131: void testModifyTimestamp(java.util.Date date, boolean validate) {
132: if (!(date instanceof java.sql.Timestamp)) {
133: return;
134: }
135:
136: Timestamp timestamp = (Timestamp) date;
137: if (validate) {
138: assertEqualTimestamp(referenceTimestamp, timestamp);
139: } else {
140: synchronized (timestamp) {
141: timestamp.setTime(referenceTimestamp.getTime());
142: }
143: }
144: }
145:
146: void testTimestampSetNanos(java.util.Date date, boolean validate) {
147: if (!(date instanceof java.sql.Timestamp)) {
148: return;
149: }
150:
151: Timestamp timestamp = (Timestamp) date;
152: if (validate) {
153: Timestamp t = new Timestamp(referenceDate.getTime());
154: t.setNanos(1000000);
155: assertEqualTimestamp(t, timestamp);
156: } else {
157: synchronized (timestamp) {
158: timestamp.setNanos(1000000);
159: }
160: }
161: }
162:
163: // Read only tests.
164: void testDateReadOnlySetTime(java.util.Date date, boolean validate) {
165: if (validate) {
166: assertEqualDate(referenceDate, date);
167: } else {
168: synchronized (date) {
169: try {
170: date.setTime(createDate("Feb 22, 2006").getTime());
171: throw new AssertionError(
172: "Should have thrown a ReadOnlyException");
173: } catch (ReadOnlyException t) {
174: // Expected
175: }
176: }
177: }
178: }
179:
180: void testDateReadOnlySetYear(java.util.Date date, boolean validate) {
181: if (date instanceof java.sql.Time) {
182: return;
183: } // java.sql.Time throws IllegalArgumentException in setYear().
184:
185: if (validate) {
186: assertEqualDate(referenceDate, date);
187: } else {
188: synchronized (date) {
189: try {
190: date.setYear(99);
191: throw new AssertionError(
192: "Should have thrown a ReadOnlyException");
193: } catch (ReadOnlyException t) {
194: // Expected
195: }
196: }
197: }
198: }
199:
200: void testDateReadOnlySetMonth(java.util.Date date, boolean validate) {
201: if (date instanceof java.sql.Time) {
202: return;
203: } // java.sql.Time throws IllegalArgumentException in setMonth().
204:
205: if (validate) {
206: assertEqualDate(referenceDate, date);
207: } else {
208: synchronized (date) {
209: try {
210: date.setMonth(3);
211: throw new AssertionError(
212: "Should have thrown a ReadOnlyException");
213: } catch (ReadOnlyException t) {
214: // Expected
215: }
216: }
217: }
218: }
219:
220: void testDateReadOnlySetDate(java.util.Date date, boolean validate) {
221: if (date instanceof java.sql.Time) {
222: return;
223: } // java.sql.Time throws IllegalArgumentException in setDate().
224:
225: if (validate) {
226: assertEqualDate(referenceDate, date);
227: } else {
228: synchronized (date) {
229: try {
230: date.setDate(3);
231: throw new AssertionError(
232: "Should have thrown a ReadOnlyException");
233: } catch (ReadOnlyException t) {
234: // Expected
235: }
236: }
237: }
238: }
239:
240: void testDateReadOnlySetHoursMinutesSeconds(java.util.Date date,
241: boolean validate) {
242: if (date instanceof java.sql.Date) {
243: return;
244: } // java.sql.Date throws IllegalArgumentException in setHours(),
245: // setMinutes(), and setSeconds().
246:
247: if (validate) {
248: assertEqualDate(referenceDate, date);
249: } else {
250: synchronized (date) {
251: try {
252: date.setHours(3);
253: throw new AssertionError(
254: "Should have thrown a ReadOnlyException");
255: } catch (ReadOnlyException t) {
256: // Expected
257: }
258: try {
259: date.setMinutes(20);
260: throw new AssertionError(
261: "Should have thrown a ReadOnlyException");
262: } catch (ReadOnlyException t) {
263: // Expected
264: }
265: try {
266: date.setSeconds(20);
267: throw new AssertionError(
268: "Should have thrown a ReadOnlyException");
269: } catch (ReadOnlyException t) {
270: // Expected
271: }
272: }
273: }
274: }
275:
276: private static java.util.Date createDate(String dateStr) {
277: try {
278: return DateFormat.getDateInstance(DateFormat.MEDIUM).parse(
279: dateStr);
280: } catch (ParseException e) {
281: return null;
282: }
283: }
284:
285: private static java.util.Date createDateTime(String dateTimeStr) {
286: try {
287: SimpleDateFormat df = new SimpleDateFormat(
288: "MMM dd, yyyy HH:mm:ss");
289: return df.parse(dateTimeStr);
290: } catch (ParseException e) {
291: return null;
292: }
293: }
294:
295: private static java.sql.Timestamp createTimestamp(String dateTimeStr) {
296: java.util.Date date = createDateTime(dateTimeStr);
297: Timestamp timestamp = new Timestamp(date.getTime());
298: timestamp.setNanos(1000000);
299: return timestamp;
300: }
301:
302: private void assertEqualTimestamp(Timestamp expect, Timestamp actual) {
303: Assert.assertNotNull(expect);
304: Assert.assertNotNull(actual);
305: Assert.assertTrue(expect.equals(actual));
306: }
307:
308: private void assertEqualDate(java.util.Date expect,
309: java.util.Date actual) {
310: Assert.assertNotNull(expect);
311: Assert.assertNotNull(actual);
312: Assert.assertTrue(expect.equals(actual));
313: }
314:
315: public static void visitL1DSOConfig(ConfigVisitor visitor,
316: DSOClientConfigHelper config) {
317: String testClass = GenericDateTestApp.class.getName();
318: config.getOrCreateSpec(testClass);
319: String writeAllowedMethodExpression = "* " + testClass
320: + "*.*(..)";
321: config.addWriteAutolock(writeAllowedMethodExpression);
322: String readOnlyMethodExpression = "* " + testClass
323: + "*.*ReadOnly*(..)";
324: config.addReadAutolock(readOnlyMethodExpression);
325: }
326:
327: }
|