This repository has been archived by the owner on Feb 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade to Spring 3.2 and Servlet API 3 and add tests
Tests take advantage of the new Spring MVC Test framework in 3.2.
- Loading branch information
1 parent
3d0e9f9
commit b03226a
Showing
22 changed files
with
1,183 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/test/java/org/springframework/samples/mvc/AbstractContextControllerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2002-2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.samples.mvc; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.web.WebAppConfiguration; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
@WebAppConfiguration | ||
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml") | ||
public class AbstractContextControllerTests { | ||
|
||
@Autowired | ||
protected WebApplicationContext wac; | ||
|
||
} |
132 changes: 132 additions & 0 deletions
132
src/test/java/org/springframework/samples/mvc/convert/ConvertControllerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright 2002-2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.samples.mvc.convert; | ||
|
||
import static org.hamcrest.Matchers.startsWith; | ||
import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.status; | ||
import static org.springframework.test.web.mock.servlet.setup.MockMvcBuilders.standaloneSetup; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.format.support.DefaultFormattingConversionService; | ||
import org.springframework.format.support.FormattingConversionService; | ||
import org.springframework.test.web.mock.servlet.MockMvc; | ||
|
||
public class ConvertControllerTests { | ||
|
||
private MockMvc mockMvc; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
FormattingConversionService cs = new DefaultFormattingConversionService(); | ||
cs.addFormatterForFieldAnnotation(new MaskFormatAnnotationFormatterFactory()); | ||
|
||
this.mockMvc = standaloneSetup(new ConvertController()) | ||
.setConversionService(cs) | ||
.alwaysExpect(status().isOk()) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void primitive() throws Exception { | ||
this.mockMvc.perform(get("/convert/primitive").param("value", "3")) | ||
.andExpect(content().string("Converted primitive 3")); | ||
} | ||
|
||
@Test | ||
public void date() throws Exception { | ||
this.mockMvc.perform(get("/convert/date/2010-07-04")) | ||
.andExpect(content().string("Converted date Sun Jul 04 00:00:00 EDT 2010")); | ||
} | ||
|
||
@Test | ||
public void collection() throws Exception { | ||
this.mockMvc.perform(get("/convert/collection?values=1&values=2&values=3&values=4&values=5")) | ||
.andExpect(content().string("Converted collection [1, 2, 3, 4, 5]")); | ||
} | ||
|
||
@Test | ||
public void collection2() throws Exception { | ||
this.mockMvc.perform(get("/convert/collection?values=1,2,3,4,5")) | ||
.andExpect(content().string("Converted collection [1, 2, 3, 4, 5]")); | ||
} | ||
|
||
@Test | ||
public void formattedCollection() throws Exception { | ||
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 EDT 2010, Mon Jul 04 00:00:00 EDT 2011]")); | ||
} | ||
|
||
@Test | ||
public void valueOf() throws Exception { | ||
this.mockMvc.perform(get("/convert/value?value=123456789")) | ||
.andExpect(content().string(startsWith( | ||
"Converted value object org.springframework.samples.mvc.convert.SocialSecurityNumber"))); | ||
} | ||
|
||
@Test | ||
public void custom() throws Exception { | ||
this.mockMvc.perform(get("/convert/custom?value=123-45-6789")) | ||
.andExpect(content().string("Converted '123456789' with a custom converter")); | ||
} | ||
|
||
@Test | ||
public void beanPrimitive() throws Exception { | ||
this.mockMvc.perform(get("/convert/bean?primitive=3")) | ||
.andExpect(content().string("Converted JavaBean primitive=3")); | ||
} | ||
|
||
@Test | ||
public void beanDate() throws Exception { | ||
this.mockMvc.perform(get("/convert/bean?date=2010-07-04")) | ||
.andExpect(content().string("Converted JavaBean date=Sun Jul 04 00:00:00 EDT 2010")); | ||
} | ||
|
||
@Test | ||
public void beanMasked() throws Exception { | ||
this.mockMvc.perform(get("/convert/bean?masked=(205) 333-3333")) | ||
.andExpect(content().string("Converted JavaBean masked=2053333333")); | ||
} | ||
|
||
@Test | ||
public void beanCollection() throws Exception { | ||
this.mockMvc.perform(get("/convert/bean?list[0]=1&list[1]=2&list[2]=3")) | ||
.andExpect(content().string("Converted JavaBean list=[1, 2, 3]")); | ||
} | ||
|
||
@Test | ||
public void beanFormattedCollection() throws Exception { | ||
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 EDT 2010, Mon Jul 04 00:00:00 EDT 2011]")); | ||
} | ||
|
||
@Test | ||
public void beanMap() throws Exception { | ||
this.mockMvc.perform(get("/convert/bean?map[0]=apple&map[1]=pear")) | ||
.andExpect(content().string("Converted JavaBean map={0=apple, 1=pear}")); | ||
} | ||
|
||
@Test | ||
public void beanNested() throws Exception { | ||
this.mockMvc.perform(get("/convert/bean?nested.foo=bar&nested.list[0].foo=baz&nested.map[key].list[0].foo=bip")) | ||
.andExpect(content().string( | ||
"Converted JavaBean nested=NestedBean foo=bar list=[NestedBean foo=baz] map={key=NestedBean list=[NestedBean foo=bip]}")); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/test/java/org/springframework/samples/mvc/data/CustomArgumentControllerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2002-2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.samples.mvc.data; | ||
|
||
import static org.springframework.test.web.mock.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.mock.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.mock.servlet.setup.MockMvcBuilders.standaloneSetup; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.samples.mvc.data.custom.CustomArgumentController; | ||
import org.springframework.samples.mvc.data.custom.CustomArgumentResolver; | ||
import org.springframework.test.web.mock.servlet.MockMvc; | ||
|
||
public class CustomArgumentControllerTests { | ||
private MockMvc mockMvc; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
this.mockMvc = standaloneSetup(new CustomArgumentController()) | ||
.setCustomArgumentResolvers(new CustomArgumentResolver()).build(); | ||
} | ||
|
||
@Test | ||
public void param() throws Exception { | ||
this.mockMvc.perform(get("/data/custom")) | ||
.andExpect(content().string("Got 'foo' request attribute value 'bar'")); | ||
} | ||
|
||
} |
Oops, something went wrong.