Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from adrianpillinger/master
Browse files Browse the repository at this point in the history
Pull request to fix test bugs
  • Loading branch information
rstoyanchev committed Oct 30, 2012
2 parents 528182f + 95e4a45 commit 3411dc3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
}


}
Expand Up @@ -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;
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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}"));
}
Expand All @@ -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);
}

}

0 comments on commit 3411dc3

Please sign in to comment.