1 4 package com.tctest; 5 6 import com.tc.object.config.ConfigVisitor; 7 import com.tc.object.config.DSOClientConfigHelper; 8 import com.tc.object.tx.ReadOnlyException; 9 import com.tc.simulator.app.ApplicationConfig; 10 import com.tc.simulator.listener.ListenerProvider; 11 import com.tc.util.Assert; 12 13 import java.sql.Time ; 14 import java.sql.Timestamp ; 15 import java.text.DateFormat ; 16 import java.text.ParseException ; 17 import java.text.SimpleDateFormat ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 public class GenericDateTestApp extends GenericTestApp { 22 private static java.util.Date referenceDate = createDate("Feb 20, 2006"); 23 private static java.util.Date alternateDate = createDate("Feb 22, 2006"); 24 private static Timestamp referenceTimestamp = createTimestamp("Feb 20, 2006 03:20:20"); 25 26 public GenericDateTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 27 super(appId, cfg, listenerProvider, java.util.Date .class); 28 } 29 30 protected Object getTestObject(String testName) { 31 List list = (List ) sharedMap.get("list"); 32 return list.iterator(); 33 } 34 35 protected void setupTestObject(String testName) { 36 List list = new ArrayList (); 37 list.add(new java.util.Date (referenceDate.getTime())); 38 list.add(new java.sql.Date (referenceDate.getTime())); 39 list.add(new Time (referenceDate.getTime())); 40 list.add(new Timestamp (referenceDate.getTime())); 41 42 sharedMap.put("list", list); 43 } 44 45 void testModifyDate(java.util.Date date, boolean validate) { 46 if (validate) { 47 assertEqualDate(alternateDate, date); 48 } else { 49 synchronized (date) { 50 date.setTime(createDate("Feb 22, 2006").getTime()); 51 } 52 } 53 } 54 55 void testDateToString(java.util.Date date, boolean validate) { 56 if (validate) { 57 assertEqualDate(referenceDate, date); 58 } else { 59 synchronized (date) { 60 System.out.println(date.toString()); 61 } 62 } 63 } 64 65 void testDateSetYear(java.util.Date date, boolean validate) { 67 if (date instanceof java.sql.Time ) { return; } 69 if (validate) { 70 assertEqualDate(createDate("Feb 20, 1999"), date); 71 } else { 72 synchronized (date) { 73 date.setYear(99); 74 } 75 } 76 } 77 78 void testDateSetMonth(java.util.Date date, boolean validate) { 80 if (date instanceof java.sql.Time ) { return; } 82 if (validate) { 83 assertEqualDate(createDate("Apr 20, 2006"), date); 84 } else { 85 synchronized (date) { 86 date.setMonth(3); 87 } 88 } 89 } 90 91 void testDateSetDate(java.util.Date date, boolean validate) { 93 if (date instanceof java.sql.Time ) { return; } 95 if (validate) { 96 assertEqualDate(createDate("Feb 03, 2006"), date); 97 } else { 98 synchronized (date) { 99 date.setDate(3); 100 } 101 } 102 } 103 104 void testDateSetHourMinutesSeconds(java.util.Date date, boolean validate) { 106 if (date instanceof java.sql.Date ) { return; } 109 if (validate) { 110 assertEqualDate(createDateTime("Feb 20, 2006 03:20:20"), date); 111 } else { 112 synchronized (date) { 113 date.setHours(3); 114 date.setMinutes(20); 115 date.setSeconds(20); 116 } 117 } 118 } 119 120 void testModifyTimestamp(java.util.Date date, boolean validate) { 121 if (! (date instanceof java.sql.Timestamp )) { return; } 122 123 Timestamp timestamp = (Timestamp ) date; 124 if (validate) { 125 assertEqualTimestamp(referenceTimestamp, timestamp); 126 } else { 127 synchronized(timestamp) { 128 timestamp.setTime(referenceTimestamp.getTime()); 129 } 130 } 131 } 132 133 void testTimestampSetNanos(java.util.Date date, boolean validate) { 134 if (! (date instanceof java.sql.Timestamp )) { return; } 135 136 Timestamp timestamp = (Timestamp ) date; 137 if (validate) { 138 Timestamp t = new Timestamp (referenceDate.getTime()); 139 t.setNanos(1000000); 140 assertEqualTimestamp(t, timestamp); 141 } else { 142 synchronized(timestamp) { 143 timestamp.setNanos(1000000); 144 } 145 } 146 } 147 148 void testDateReadOnlySetTime(java.util.Date date, boolean validate) { 150 if (validate) { 151 assertEqualDate(referenceDate, date); 152 } else { 153 synchronized (date) { 154 try { 155 date.setTime(createDate("Feb 22, 2006").getTime()); 156 throw new AssertionError ("Should have thrown a ReadOnlyException"); 157 } catch ( ReadOnlyException t) { 158 } 160 } 161 } 162 } 163 164 void testDateReadOnlySetYear(java.util.Date date, boolean validate) { 165 if (date instanceof java.sql.Time ) { return; } 167 if (validate) { 168 assertEqualDate(referenceDate, date); 169 } else { 170 synchronized (date) { 171 try { 172 date.setYear(99); 173 throw new AssertionError ("Should have thrown a ReadOnlyException"); 174 } catch ( ReadOnlyException t) { 175 } 177 } 178 } 179 } 180 181 void testDateReadOnlySetMonth(java.util.Date date, boolean validate) { 182 if (date instanceof java.sql.Time ) { return; } 184 if (validate) { 185 assertEqualDate(referenceDate, date); 186 } else { 187 synchronized (date) { 188 try { 189 date.setMonth(3); 190 throw new AssertionError ("Should have thrown a ReadOnlyException"); 191 } catch ( ReadOnlyException t) { 192 } 194 } 195 } 196 } 197 198 void testDateReadOnlySetDate(java.util.Date date, boolean validate) { 199 if (date instanceof java.sql.Time ) { return; } 201 if (validate) { 202 assertEqualDate(referenceDate, date); 203 } else { 204 synchronized (date) { 205 try { 206 date.setDate(3); 207 throw new AssertionError ("Should have thrown a ReadOnlyException"); 208 } catch ( ReadOnlyException t) { 209 } 211 } 212 } 213 } 214 215 void testDateReadOnlySetHoursMinutesSeconds(java.util.Date date, boolean validate) { 216 if (date instanceof java.sql.Date ) { return; } 219 if (validate) { 220 assertEqualDate(referenceDate, date); 221 } else { 222 synchronized (date) { 223 try { 224 date.setHours(3); 225 throw new AssertionError ("Should have thrown a ReadOnlyException"); 226 } catch ( ReadOnlyException t) { 227 } 229 try { 230 date.setMinutes(20); 231 throw new AssertionError ("Should have thrown a ReadOnlyException"); 232 } catch ( ReadOnlyException t) { 233 } 235 try { 236 date.setSeconds(20); 237 throw new AssertionError ("Should have thrown a ReadOnlyException"); 238 } catch ( ReadOnlyException t) { 239 } 241 } 242 } 243 } 244 245 private static java.util.Date createDate(String dateStr) { 246 try { 247 return DateFormat.getDateInstance(DateFormat.MEDIUM).parse(dateStr); 248 } catch (ParseException e) { 249 return null; 250 } 251 } 252 253 private static java.util.Date createDateTime(String dateTimeStr) { 254 try { 255 SimpleDateFormat df = new SimpleDateFormat ("MMM dd, yyyy HH:mm:ss"); 256 return df.parse(dateTimeStr); 257 } catch (ParseException e) { 258 return null; 259 } 260 } 261 262 private static java.sql.Timestamp createTimestamp(String dateTimeStr) { 263 java.util.Date date = createDateTime(dateTimeStr); 264 Timestamp timestamp = new Timestamp (date.getTime()); 265 timestamp.setNanos(1000000); 266 return timestamp; 267 } 268 269 private void assertEqualTimestamp(Timestamp expect, Timestamp actual) { 270 Assert.assertNotNull(expect); 271 Assert.assertNotNull(actual); 272 Assert.assertTrue(expect.equals(actual)); 273 } 274 275 private void assertEqualDate(java.util.Date expect, java.util.Date actual) { 276 Assert.assertNotNull(expect); 277 Assert.assertNotNull(actual); 278 Assert.assertTrue(expect.equals(actual)); 279 } 280 281 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 282 String testClass = GenericDateTestApp.class.getName(); 283 config.getOrCreateSpec(testClass); 284 String writeAllowedMethodExpression = "* " + testClass + "*.*(..)"; 285 config.addWriteAutolock(writeAllowedMethodExpression); 286 String readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)"; 287 config.addReadAutolock(readOnlyMethodExpression); 288 } 289 290 } 291 | Popular Tags |