@PostConstruct public void configureLinkToHandler() { new LinkToHandler(context, router).start(); }
String path = context.getContextPath().replace('/', '$'); String interfaceName = controller.getName() + "$linkTo" + path; linkToInterface = createLinkToInterface(controller, interfaceName); interfaces.put(controller, linkToInterface);
@Override public Object intercept(Object proxy, Method method, Object[] args, SuperMethod superMethod) { String methodName = StringUtils.decapitalize(method.getName().replaceFirst("^get", "")); List<Object> params = args.length == 0 ? Collections.emptyList() : Arrays.asList(args); return linker(controller, methodName, params).getLink(); }
String uri = invoke(handler.get(new DefaultBeanClass(TestController.class)), "getAnotherMethod"); assertThat(uri, is("/path/expectedURL")); Proxifier proxifier = new JavassistProxifier(); LinkToHandler handler0 = new LinkToHandler(context0, router, proxifier, reflectionProvider); LinkToHandler handler1 = new LinkToHandler(context1, router, proxifier, reflectionProvider);
CtClass objectType = pool.get(Object.class.getName()); for (Method m : getMethods(controller)) { String name = m.getName(); CtClass[] params = createParameters(objectType, m.getParameterTypes().length); CtClass[] empty = new CtClass[0];
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); this.handler = new LinkToHandler(context, router, new JavassistProxifier(), new DefaultReflectionProvider()); when(context.getContextPath()).thenReturn("/path"); this.method2params = new Mirror().on(TestController.class).reflect().method("method").withArgs(String.class, int.class); this.method1param = new Mirror().on(TestController.class).reflect().method("method").withArgs(String.class); this.anotherMethod = new Mirror().on(TestController.class).reflect().method("anotherMethod").withArgs(String.class, int.class); }
@Test public void shouldReturnWantedUrlWithParamArgs() throws Throwable { String a = "test"; int b = 3; when(router.urlFor(TestController.class, method2params, new Object[]{a, b})).thenReturn("/expectedURL"); //${linkTo[TestController].method('test', 3)} String uri = invoke(handler.get(new DefaultBeanClass(TestController.class)), "method", a, b); assertThat(uri, is("/path/expectedURL")); }
@Test public void shouldReturnWantedUrlWithPartialParamArgs() throws Throwable { String a = "test"; when(router.urlFor(TestController.class, anotherMethod, new Object[]{a, null})).thenReturn("/expectedUrl"); //${linkTo[TestController].anotherMethod('test')} String uri = invoke(handler.get(new DefaultBeanClass(TestController.class)), "anotherMethod", a); assertThat(uri, is("/path/expectedUrl")); }
@Test public void shouldReturnWantedUrlWithoutArgs() throws Throwable { when(router.urlFor(TestController.class, anotherMethod, new Object[2])).thenReturn("/expectedURL"); //${linkTo[TestController].anotherMethod()} String uri = invoke(handler.get(new DefaultBeanClass(TestController.class)), "anotherMethod"); assertThat(uri, is("/path/expectedURL")); }
@Test public void shouldThrowExceptionWhenMethodIsAmbiguous() throws Throwable { try { //${linkTo[TestController].method()} invoke(handler.get(new DefaultBeanClass(TestController.class)), "method"); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage().toLowerCase(), startsWith("ambiguous method")); } }
@Test public void shouldThrowExceptionWhenUsingParametersOfWrongTypes() throws Throwable { //${linkTo[TestController].method(123)} try { invoke(handler.get(new DefaultBeanClass(TestController.class)), "method", 123); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage().toLowerCase(), startsWith("there are no methods")); } }