Components { public static Object invoke(final RequestMapper mapper) { return invoke(mapper, Maps.newHashMap()); } private  Components(); static final void load(); static final void reload(); static final void destroy(); @SuppressWarnings("deprecation") static final Object[] bindParam(final Method method, final Map<String, Object> params, final Object... objs); static Object invoke(final RequestMapper mapper); static final Object invoke(final RequestMapper mapper, final Map<String, Object> parameter, final Object... objs);  }
@Test public void componentTest() throws LoaderException, IOException { final RequestMapper mapper = Routes.route().lookup("/v1/test", RequestMethod.GET); Assert.assertNotNull(mapper); final RequestMethod[] requestMethods = mapper.getRequestMethods(); final List<RequestMethod> methods = Lists.asList(requestMethods[0], requestMethods); Assert.assertEquals(methods.contains(RequestMethod.GET), true); Assert.assertEquals(methods.contains(RequestMethod.POST), true); Assert.assertEquals(mapper.getInstance() instanceof TestComponent, true); final Object ret = Components.invoke(mapper, Maps.newHashMap()); Assert.assertNotNull(ret); Assert.assertEquals(ret, "OK"); }
@Test public void hasParamTest() { final RequestMapper mapper = Routes.route().lookup("/v1/param/hello", RequestMethod.GET); Assert.assertNotNull(mapper); final Object hasParam = Components.invoke(mapper, MapBuilder.<String, Object> builder().put("param1", "world").build()); Assert.assertEquals(hasParam, "hello=world"); }
@Test public void emptyParamTest() { final RequestMapper mapper = Routes.route().lookup("/v1/param/hello", RequestMethod.GET); Assert.assertNotNull(mapper); try { Components.invoke(mapper, null); } catch (final Throwable e) { Assert.assertEquals(e instanceof ComponentInvokeException, true); } }
@Test public void beforeAopTest() { final RequestMapper mapper = Routes.route().lookup("/v1/aop/before", RequestMethod.PUT); Assert.assertNotNull(mapper); final Object ret = Components.invoke(mapper, MapBuilder.<String, Object> builder().put("param", "before").build()); Assert.assertEquals(ret, "before"); Assert.assertEquals(BeforeAOP.RESULT, "before"); BeforeAOP.RESULT = null; }
@Test public void afterAopTest() { final RequestMapper mapper = Routes.route().lookup("/v1/aop/after", RequestMethod.PUT); Assert.assertNotNull(mapper); Components.invoke(mapper, null); Assert.assertEquals(AfterAOP.RESULT, "OK"); AfterAOP.RESULT = null; }
@Test public void afterAopErrorTest() { final RequestMapper mapper = Routes.route().lookup("/v1/aop/after/error", RequestMethod.PUT); Assert.assertNotNull(mapper); try { Components.invoke(mapper, null); } catch (final Throwable e) { Assert.assertEquals(e instanceof ComponentInvokeException, true); Assert.assertEquals(e.getCause() instanceof UnsupportedAccessException, true); } Assert.assertEquals(AfterAOP.RESULT instanceof UnsupportedAccessException, true); AfterAOP.RESULT = null; }
@Test public void arrayTest() { final RequestMapper mapper = Routes.route().lookup("/v1/array", RequestMethod.PUT); final String[] array = { "1", "2", "3" }; Object value = Components.invoke(mapper, MapBuilder.<String, Object> builder().put("ARRAY[]", array).build()); Assert.assertNotNull(value); Assert.assertEquals(JSON.toJSONString(array), value); }