Created
September 12, 2012 01:08
-
-
Save bijukunjummen/3703430 to your computer and use it in GitHub Desktop.
AsOf.java
This file contains hidden or 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
package org.bk.webtest2; | |
import java.text.SimpleDateFormat; | |
import org.springframework.core.MethodParameter; | |
import org.springframework.web.bind.support.WebDataBinderFactory; | |
import org.springframework.web.context.request.NativeWebRequest; | |
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | |
import org.springframework.web.method.support.ModelAndViewContainer; | |
public class AsOfWebArgumentResolver implements HandlerMethodArgumentResolver { | |
@Override | |
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | |
SimpleDateFormat dateFormat = new SimpleDateFormat("mm-dd-yyyy"); | |
try{ | |
return dateFormat.parse(webRequest.getParameter("asOf")); | |
}catch(Exception e){ | |
throw new RuntimeException(e); | |
} | |
} | |
@Override | |
public boolean supportsParameter(MethodParameter parameter) { | |
return (parameter.getParameterAnnotation(AsOf.class)!=null); | |
} | |
} |
This file contains hidden or 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
package org.bk.webtest2; | |
import static org.springframework.test.web.server.request.MockMvcRequestBuilders.* ; | |
import static org.springframework.test.web.server.result.MockMvcResultMatchers.*; | |
import static org.springframework.test.web.server.setup.MockMvcBuilders.*; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.List; | |
import org.junit.Test; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.test.web.server.MockMvc; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | |
import org.springframework.web.servlet.config.annotation.EnableWebMvc; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
public class CustomWebArugmentTest { | |
@Test | |
public void testAWebFlow() throws Exception { | |
MockMvc mockMvc = annotationConfigSetup(CustomWebArugmentTest.TestConfiguration.class) | |
.build(); | |
mockMvc.perform(get("/members/1?asOf=01-01-2012")) | |
.andExpect(status().isOk()) | |
.andExpect(view().name("get")) | |
.andExpect(model().attribute("aDate", "2012-01-01")); | |
} | |
@Configuration | |
@EnableWebMvc | |
@ComponentScan(basePackages="org.bk.webtest2") | |
public static class TestConfiguration extends WebMvcConfigurerAdapter{ | |
@Override | |
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { | |
argumentResolvers.add(new AsOfWebArgumentResolver()); | |
} | |
} | |
} | |
@Controller | |
class CustomController{ | |
@RequestMapping(value="/members/{id}", method=RequestMethod.GET) | |
public String get(@PathVariable("id") Integer id, @AsOf Date aDate, Model model){ | |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd"); | |
model.addAttribute("aDate", formatter.format(aDate)); | |
return "get"; | |
} | |
} |
This file contains hidden or 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
package org.bk.webtest2; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface AsOf { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks! This is very useful!