Преглед изворни кода

Eric - Fixed bug in CargoTrackingViewAdapterTest.java, which worked only for Central Europe Time.

Changed TrackingViewAdapter to show time in the time zone of the location of the event.
To do this, we added a TimeZone to the Location, which is passed into the constructor.
ericevans пре 17 година
родитељ
комит
99b9d556d5

+ 14
- 4
dddsample/src/main/java/se/citerus/dddsample/domain/model/location/Location.java Прегледај датотеку

@@ -1,5 +1,7 @@
1 1
 package se.citerus.dddsample.domain.model.location;
2 2
 
3
+import java.util.TimeZone;
4
+
3 5
 import org.apache.commons.lang.Validate;
4 6
 import se.citerus.dddsample.domain.shared.Entity;
5 7
 
@@ -14,27 +16,31 @@ public final class Location implements Entity<Location> {
14 16
 
15 17
   private UnLocode unLocode;
16 18
   private String name;
19
+  private TimeZone timeZone;
17 20
 
18 21
   /**
19 22
    * Special Location object that marks an unknown location.
20 23
    */
21 24
   public static final Location UNKNOWN = new Location(
22
-    new UnLocode("XXXXX"), "Unknown location"
25
+    new UnLocode("XXXXX"), "Unknown location", TimeZone.getTimeZone("Zulu")
23 26
   );
24 27
 
25 28
   /**
26 29
    * Package-level constructor, visible for test only.
27 30
    *
28 31
    * @param unLocode UN Locode
29
-   * @param name     location name
32
+ * @param name     location name
33
+ * @param timeZone TODO
30 34
    * @throws IllegalArgumentException if the UN Locode or name is null
31 35
    */
32
-  Location(final UnLocode unLocode, final String name) {
33
-    Validate.notNull(unLocode);
36
+  Location(final UnLocode unLocode, final String name, TimeZone timeZone) {
37
+	Validate.notNull(unLocode);
34 38
     Validate.notNull(name);
39
+    Validate.notNull(timeZone);
35 40
     
36 41
     this.unLocode = unLocode;
37 42
     this.name = name;
43
+    this.timeZone = timeZone;
38 44
   }
39 45
 
40 46
   /**
@@ -94,4 +100,8 @@ public final class Location implements Entity<Location> {
94 100
 
95 101
   private Long id;
96 102
 
103
+public TimeZone timeZone() {
104
+	return timeZone;
105
+}
106
+
97 107
 }

+ 22
- 14
dddsample/src/main/java/se/citerus/dddsample/domain/model/location/SampleLocations.java Прегледај датотеку

@@ -5,26 +5,34 @@ import java.util.ArrayList;
5 5
 import java.util.HashMap;
6 6
 import java.util.List;
7 7
 import java.util.Map;
8
+import java.util.TimeZone;
8 9
 
9 10
 /**
10 11
  * Sample locations, for test purposes.
11 12
  * 
12 13
  */
13 14
 public class SampleLocations {
14
-
15
-  public static final Location HONGKONG = new Location(new UnLocode("CNHKG"), "Hongkong");
16
-  public static final Location MELBOURNE = new Location(new UnLocode("AUMEL"), "Melbourne");
17
-  public static final Location STOCKHOLM = new Location(new UnLocode("SESTO"), "Stockholm");
18
-  public static final Location HELSINKI = new Location(new UnLocode("FIHEL"), "Helsinki");
19
-  public static final Location CHICAGO = new Location(new UnLocode("USCHI"), "Chicago");
20
-  public static final Location TOKYO = new Location(new UnLocode("JNTKO"), "Tokyo");
21
-  public static final Location HAMBURG = new Location(new UnLocode("DEHAM"), "Hamburg");
22
-  public static final Location SHANGHAI = new Location(new UnLocode("CNSHA"), "Shanghai");
23
-  public static final Location ROTTERDAM = new Location(new UnLocode("NLRTM"), "Rotterdam");
24
-  public static final Location GOTHENBURG = new Location(new UnLocode("SEGOT"), "Göteborg");
25
-  public static final Location HANGZOU = new Location(new UnLocode("CNHGH"), "Hangzhou");
26
-  public static final Location NEWYORK = new Location(new UnLocode("USNYC"), "New York");
27
-  public static final Location DALLAS = new Location(new UnLocode("USDAL"), "Dallas");
15
+  private static final TimeZone CHINA =  TimeZone.getTimeZone("Asia/Shanghai");
16
+  private static final TimeZone CENTRAL_EUROPE =  TimeZone.getTimeZone("Europe/Stockholm");
17
+  private static final TimeZone JAPAN =  TimeZone.getTimeZone("Asia/Tokyo");
18
+  private static final TimeZone EASTERN =  TimeZone.getTimeZone("America/New_York");
19
+  private static final TimeZone CENTRAL =  TimeZone.getTimeZone("America/Chicago");
20
+  private static final TimeZone EASTERN_AUSTRALIA =  TimeZone.getTimeZone("Australia/Melbourne");
21
+  
22
+
23
+  public static final Location HONGKONG = new Location(new UnLocode("CNHKG"), "Hongkong",  CHINA);
24
+  public static final Location MELBOURNE = new Location(new UnLocode("AUMEL"), "Melbourne", EASTERN_AUSTRALIA);
25
+  public static final Location STOCKHOLM = new Location(new UnLocode("SESTO"), "Stockholm",  CENTRAL_EUROPE);
26
+  public static final Location HELSINKI = new Location(new UnLocode("FIHEL"), "Helsinki",  CENTRAL_EUROPE);
27
+  public static final Location CHICAGO = new Location(new UnLocode("USCHI"), "Chicago", CENTRAL);
28
+  public static final Location TOKYO = new Location(new UnLocode("JNTKO"), "Tokyo", JAPAN);
29
+  public static final Location HAMBURG = new Location(new UnLocode("DEHAM"), "Hamburg", CENTRAL_EUROPE);
30
+  public static final Location SHANGHAI = new Location(new UnLocode("CNSHA"), "Shanghai", CHINA);
31
+  public static final Location ROTTERDAM = new Location(new UnLocode("NLRTM"), "Rotterdam", CENTRAL_EUROPE);
32
+  public static final Location GOTHENBURG = new Location(new UnLocode("SEGOT"), "Göteborg", CENTRAL_EUROPE);
33
+  public static final Location HANGZOU = new Location(new UnLocode("CNHGH"), "Hangzhou", CHINA);
34
+  public static final Location NEWYORK = new Location(new UnLocode("USNYC"), "New York", EASTERN);
35
+  public static final Location DALLAS = new Location(new UnLocode("USDAL"), "Dallas", CENTRAL);
28 36
 
29 37
   public static final Map<UnLocode, Location> ALL = new HashMap<UnLocode, Location>();
30 38
 

+ 11
- 3
dddsample/src/main/java/se/citerus/dddsample/interfaces/tracking/CargoTrackingViewAdapter.java Прегледај датотеку

@@ -20,7 +20,7 @@ public final class CargoTrackingViewAdapter {
20 20
   private final MessageSource messageSource;
21 21
   private final Locale locale;
22 22
   private final List<HandlingEventViewAdapter> events;
23
-  private final String FORMAT = "yyyy-MM-dd hh:mm";
23
+  private final String FORMAT = "yyyy-MM-dd hh:mm z";
24 24
 
25 25
   /**
26 26
    * Constructor.
@@ -107,7 +107,12 @@ public final class CargoTrackingViewAdapter {
107 107
     Date eta = cargo.delivery().estimatedTimeOfArrival();
108 108
 
109 109
     if (eta == null) return "?";
110
-    else return new SimpleDateFormat(FORMAT).format(eta);
110
+	else {
111
+		Location destination = cargo.routeSpecification().destination();
112
+		SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT);
113
+		dateFormat.setTimeZone(destination.timeZone());
114
+		return dateFormat.format(eta);
115
+	}
111 116
   }
112 117
 
113 118
   public String getNextExpectedActivity() {
@@ -165,7 +170,10 @@ public final class CargoTrackingViewAdapter {
165 170
      * @return Time when the event was completed.
166 171
      */
167 172
     public String getTime() {
168
-      return new SimpleDateFormat(FORMAT).format(handlingEvent.completionTime());
173
+		SimpleDateFormat dateFormat = new SimpleDateFormat(FORMAT);
174
+		dateFormat.setTimeZone(handlingEvent.location().timeZone());
175
+
176
+      return dateFormat.format(handlingEvent.completionTime());
169 177
     }
170 178
 
171 179
     /**

+ 11
- 7
dddsample/src/test/java/se/citerus/dddsample/domain/model/location/LocationTest.java Прегледај датотеку

@@ -1,20 +1,24 @@
1 1
 package se.citerus.dddsample.domain.model.location;
2 2
 
3
+import java.util.TimeZone;
4
+
3 5
 import junit.framework.TestCase;
4 6
 
5 7
 public class LocationTest extends TestCase {
6
-
8
+	private static final TimeZone CET = TimeZone.getTimeZone("Europe/Amsterdam"); 
9
+	
7 10
   public void testEquals() {
11
+	  
8 12
     // Same UN locode - equal
9
-    assertTrue(new Location(new UnLocode("ATEST"),"test-name").
10
-        equals(new Location(new UnLocode("ATEST"),"test-name")));
13
+    assertTrue(new Location(new UnLocode("ATEST"),"test-name", CET).
14
+        equals(new Location(new UnLocode("ATEST"),"test-name", CET)));
11 15
 
12 16
     // Different UN locodes - not equal
13
-    assertFalse(new Location(new UnLocode("ATEST"),"test-name").
14
-         equals(new Location(new UnLocode("TESTB"), "test-name")));
17
+    assertFalse(new Location(new UnLocode("ATEST"),"test-name", CET).
18
+         equals(new Location(new UnLocode("TESTB"), "test-name", CET)));
15 19
 
16 20
     // Always equal to itself
17
-    Location location = new Location(new UnLocode("ATEST"),"test-name");
21
+    Location location = new Location(new UnLocode("ATEST"),"test-name", CET);
18 22
     assertTrue(location.equals(location));
19 23
 
20 24
     // Never equal to null
@@ -24,7 +28,7 @@ public class LocationTest extends TestCase {
24 28
     assertTrue(Location.UNKNOWN.equals(Location.UNKNOWN));
25 29
 
26 30
     try {
27
-      new Location(null, null);
31
+      new Location(null, null, null);
28 32
       fail("Should not allow any null constructor arguments");
29 33
     } catch (IllegalArgumentException expected) {}
30 34
   }

+ 3
- 3
dddsample/src/test/java/se/citerus/dddsample/interfaces/tracking/CargoTrackingViewAdapterTest.java Прегледај датотеку

@@ -42,21 +42,21 @@ public class CargoTrackingViewAdapterTest extends TestCase {
42 42
     CargoTrackingViewAdapter.HandlingEventViewAdapter event = it.next();
43 43
     assertEquals("RECEIVE", event.getType());
44 44
     assertEquals("Hangzhou", event.getLocation());
45
-    assertEquals("1970-01-01 01:00", event.getTime());
45
+    assertEquals("1970-01-01 08:00 CST", event.getTime());
46 46
     assertEquals("", event.getVoyageNumber());
47 47
     assertTrue(event.isExpected());
48 48
 
49 49
     event = it.next();
50 50
     assertEquals("LOAD", event.getType());
51 51
     assertEquals("Hangzhou", event.getLocation());
52
-    assertEquals("1970-01-01 01:00", event.getTime());
52
+    assertEquals("1970-01-01 08:00 CST", event.getTime());
53 53
     assertEquals("CM001", event.getVoyageNumber());
54 54
     assertTrue(event.isExpected());
55 55
 
56 56
     event = it.next();
57 57
     assertEquals("UNLOAD", event.getType());
58 58
     assertEquals("Helsinki", event.getLocation());
59
-    assertEquals("1970-01-01 01:00", event.getTime());
59
+    assertEquals("1970-01-01 01:00 CET", event.getTime());
60 60
     assertEquals("CM001", event.getVoyageNumber());
61 61
     assertTrue(event.isExpected());
62 62
   }