From 95e4a4592542f22caac714808d8f2fb0a73673a6 Mon Sep 17 00:00:00 2001 From: Adrian Pillinger Date: Mon, 29 Oct 2012 22:40:31 +0000 Subject: [PATCH] #9 Fixing timezone bugs in unit tests --- .../mvc/convert/ConvertControllerTests.java | 40 ++++++++++++------- .../samples/mvc/form/FormControllerTests.java | 22 +++++++++- 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/test/java/org/springframework/samples/mvc/convert/ConvertControllerTests.java b/src/test/java/org/springframework/samples/mvc/convert/ConvertControllerTests.java index 9a23977d9..6053f876e 100644 --- a/src/test/java/org/springframework/samples/mvc/convert/ConvertControllerTests.java +++ b/src/test/java/org/springframework/samples/mvc/convert/ConvertControllerTests.java @@ -6,6 +6,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; +import java.util.Calendar; import java.util.Date; import java.util.TimeZone; @@ -16,15 +17,7 @@ import org.springframework.test.web.servlet.MockMvc; public class ConvertControllerTests { - - private static String TIMEZONE; - - static { - TimeZone timezone = TimeZone.getDefault(); - boolean inDaylight = timezone.inDaylightTime(new Date()); - TIMEZONE = TimeZone.getDefault().getDisplayName(inDaylight, TimeZone.SHORT); - } - + private MockMvc mockMvc; @Before @@ -46,8 +39,9 @@ public void primitive() throws Exception { @Test public void date() throws Exception { + String timezone = getTimezone(2010, 7, 4); this.mockMvc.perform(get("/convert/date/2010-07-04")) - .andExpect(content().string("Converted date Sun Jul 04 00:00:00 " + TIMEZONE + " 2010")); + .andExpect(content().string("Converted date Sun Jul 04 00:00:00 " + timezone + " 2010")); } @Test @@ -64,10 +58,12 @@ public void collection2() throws Exception { @Test public void formattedCollection() throws Exception { + String timezone2010 = getTimezone(2010, 7, 4); + String timezone2011 = getTimezone(2011, 7, 4); this.mockMvc.perform(get("/convert/formattedCollection?values=2010-07-04,2011-07-04")) .andExpect(content().string( "Converted formatted collection [Sun Jul 04 00:00:00 " - + TIMEZONE + " 2010, Mon Jul 04 00:00:00 " + TIMEZONE + " 2011]")); + + timezone2010 + " 2010, Mon Jul 04 00:00:00 " + timezone2011 + " 2011]")); } @Test @@ -91,8 +87,9 @@ public void beanPrimitive() throws Exception { @Test public void beanDate() throws Exception { + String timezone = getTimezone(2010, 7, 4); this.mockMvc.perform(get("/convert/bean?date=2010-07-04")) - .andExpect(content().string("Converted JavaBean date=Sun Jul 04 00:00:00 " + TIMEZONE + " 2010")); + .andExpect(content().string("Converted JavaBean date=Sun Jul 04 00:00:00 " + timezone + " 2010")); } @Test @@ -109,10 +106,12 @@ public void beanCollection() throws Exception { @Test public void beanFormattedCollection() throws Exception { + String timezone2010 = getTimezone(2010, 7, 4); + String timezone2011 = getTimezone(2011, 7, 4); this.mockMvc.perform(get("/convert/bean?formattedList[0]=2010-07-04&formattedList[1]=2011-07-04")) .andExpect(content().string( - "Converted JavaBean formattedList=[Sun Jul 04 00:00:00 " + TIMEZONE - + " 2010, Mon Jul 04 00:00:00 " + TIMEZONE + " 2011]")); + "Converted JavaBean formattedList=[Sun Jul 04 00:00:00 " + timezone2010 + + " 2010, Mon Jul 04 00:00:00 " + timezone2011 + " 2011]")); } @Test @@ -128,4 +127,17 @@ public void beanNested() throws Exception { "Converted JavaBean nested=NestedBean foo=bar list=[NestedBean foo=baz] map={key=NestedBean list=[NestedBean foo=bip]}")); } + private String getTimezone(int year, int month, int day) + { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.YEAR, year); + calendar.set(Calendar.MONTH, month); + calendar.set(Calendar.DAY_OF_MONTH, day); + Date date = calendar.getTime(); + TimeZone timezone = TimeZone.getDefault(); + boolean inDaylight = timezone.inDaylightTime(date); + return timezone.getDisplayName(inDaylight, TimeZone.SHORT); + } + + } diff --git a/src/test/java/org/springframework/samples/mvc/form/FormControllerTests.java b/src/test/java/org/springframework/samples/mvc/form/FormControllerTests.java index 8c05fdfea..c919e7159 100644 --- a/src/test/java/org/springframework/samples/mvc/form/FormControllerTests.java +++ b/src/test/java/org/springframework/samples/mvc/form/FormControllerTests.java @@ -9,6 +9,10 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + import org.junit.Before; import org.junit.Test; import org.springframework.http.MediaType; @@ -31,6 +35,7 @@ public void setup() throws Exception { @Test public void submitSuccess() throws Exception { + String timezone = getTimezone(1941, 12, 16); this.mockMvc.perform( post("/form") .param("name", "Joe") @@ -51,13 +56,14 @@ public void submitSuccess() throws Exception { .andExpect(redirectedUrl("/form")) .andExpect(flash().attribute("message", "Form submitted successfully. Bound properties name='Joe', age=56, " + - "birthDate=Tue Dec 16 00:00:00 EST 1941, phone='(347) 888-1234', " + + "birthDate=Tue Dec 16 00:00:00 " + timezone + " 1941, phone='(347) 888-1234', " + "currency=123.33, percent=0.89, inquiry=comment, inquiryDetails='what is?'," + " subscribeNewsletter=false, additionalInfo={java=true, mvc=true}")); } @Test public void submitSuccessAjax() throws Exception { + String timezone = getTimezone(1941, 12, 16); this.mockMvc.perform( post("/form") .header("X-Requested-With", "XMLHttpRequest") @@ -79,7 +85,7 @@ public void submitSuccessAjax() throws Exception { .andExpect(model().hasNoErrors()) .andExpect(model().attribute("message", "Form submitted successfully. Bound properties name='Joe', age=56, " + - "birthDate=Tue Dec 16 00:00:00 EST 1941, phone='(347) 888-1234', " + + "birthDate=Tue Dec 16 00:00:00 " + timezone + " 1941, phone='(347) 888-1234', " + "currency=123.33, percent=0.89, inquiry=comment, inquiryDetails='what is?'," + " subscribeNewsletter=false, additionalInfo={java=true, mvc=true}")); } @@ -93,5 +99,17 @@ public void submitError() throws Exception { .andExpect(model().errorCount(2)) .andExpect(model().attributeHasFieldErrors("formBean", "name", "age")); } + + private String getTimezone(int year, int month, int day) + { + Calendar calendar = Calendar.getInstance(); + calendar.set(Calendar.YEAR, year); + calendar.set(Calendar.MONTH, month); + calendar.set(Calendar.DAY_OF_MONTH, day); + Date date = calendar.getTime(); + TimeZone timezone = TimeZone.getDefault(); + boolean inDaylight = timezone.inDaylightTime(date); + return timezone.getDisplayName(inDaylight, TimeZone.SHORT); + } }