01: package junitx.ddtunit.data.processing;
02:
03: import java.text.DateFormat;
04: import java.text.SimpleDateFormat;
05: import java.util.HashMap;
06: import java.util.Locale;
07: import java.util.Map;
08:
09: import junit.framework.TestCase;
10: import junitx.ddtunit.data.TypedObject;
11: import junitx.ddtunit.util.DDTConfiguration;
12:
13: import org.apache.log4j.BasicConfigurator;
14:
15: public class DateCreatorActionTest extends TestCase {
16: private static final String DDTCONFIG_RESOURCE = "/ddtunitConfig.properties";
17:
18: private IAction contentAction;
19:
20: private TypedObject dateString;
21:
22: @Override
23: protected void setUp() throws Exception {
24: // prepare logging
25: BasicConfigurator.configure();
26: // prepare DDTUnit configuration details
27: DDTConfiguration.getInstance().load(DDTCONFIG_RESOURCE);
28: Map<String, String> attrMap = new HashMap<String, String>();
29: attrMap.put("hint", HintTypes.CONTENT.toString());
30: contentAction = new ContentCreatorAction(attrMap);
31: dateString = new TypedObject("content",
32: "java.lang.StringBuffer");
33: contentAction.setObject(dateString);
34: }
35:
36: public void testShortFormat() {
37: String testFormat = "dd.MM.yyyy";
38: Map<String, String> attrMap = new HashMap<String, String>();
39: attrMap.put("type", "java.util.Date");
40: attrMap.put("hint", "date");
41: // basically identify format and content to parse
42: attrMap.put("dateformat", "generic");
43: String testDate = "10.01.2007";
44: dateString.setValue(testDate);
45: // prepare supported structures
46: IAction dateAction = new DateCreatorAction(attrMap);
47: dateAction.setNext(contentAction);
48: contentAction.setPrevious(dateAction);
49: dateAction
50: .setObject(new TypedObject("myObj", "java.util.Date"));
51:
52: dateAction.processSuccessor(contentAction);
53: TypedObject obj = dateAction.getObject();
54: assertNotNull(obj);
55: DateFormat formater = new SimpleDateFormat(testFormat);
56: assertEquals("Wrong date created", testDate, formater
57: .format(obj.getValue()));
58: }
59:
60: public void testspecialFormat() {
61: String testFormat = "EEE MMM dd HH:mm:ss zzz yyyy";
62: Locale.setDefault(new Locale("en", "US"));
63: SimpleDateFormat formater = new SimpleDateFormat(testFormat);
64: String testDate = "Wed Jan 10 00:00:00 CET 2007";
65: Map<String, String> attrMap = new HashMap<String, String>();
66: attrMap.put("type", "java.util.Date");
67: attrMap.put("hint", "date");
68: // basically identify format and content to parse
69: attrMap.put("dateformat", testFormat);
70: dateString.setValue(testDate);
71: // prepare supported structures
72: IAction dateAction = new DateCreatorAction(attrMap);
73: dateAction.setNext(contentAction);
74: contentAction.setPrevious(dateAction);
75: dateAction
76: .setObject(new TypedObject("myObj", "java.util.Date"));
77: dateAction.processSuccessor(contentAction);
78: TypedObject obj = dateAction.getObject();
79: assertNotNull(obj);
80: String dateString = formater.format(obj.getValue());
81: System.err.println(testDate + " - " + dateString);
82: assertEquals("Wrong date generated", testDate, dateString);
83: }
84:
85: }
|