AnnotationPartitionContextSupplier implements PartitionContextSupplier { @Override public PartitionContext forCall(Method method, Object... args) { String[] mappings = _keyMappings.get(method); if (mappings == null) { return PartitionContextBuilder.empty(); } PartitionContextBuilder builder = new PartitionContextBuilder(); for (int i = 0; i < mappings.length; i++) { if (mappings[i] != null && args[i] != null) { builder.put(mappings[i], args[i]); } } return builder.build(); } <S> AnnotationPartitionContextSupplier(Class<S> ifc, Class<? extends S> impl); @Override PartitionContext forCall(Method method, Object... args);  }
@Test public void testNoArgs() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyService.class, MyServiceImpl.class); PartitionContext partitionContext = contextSupplier.forCall(MyService.class.getMethod("noArgs")); assertTrue(partitionContext.asMap().isEmpty()); }
@Test public void testUnnamedKey() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyService.class, MyServiceImpl.class); assertEquals(ImmutableMap.<String, Object>of("", "a"), contextSupplier.forCall(MyService.class.getMethod("unnamed", String.class), "a").asMap()); }
@Test public void testNamedKey() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyService.class, MyServiceImpl.class); assertEquals(ImmutableMap.<String, Object>of("n", "named"), contextSupplier.forCall(MyService.class.getMethod("named", String.class), "named").asMap()); }
@Test public void testOverloadedMethod() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyService.class, MyServiceImpl.class); assertEquals(ImmutableMap.<String, Object>of("string", "data"), contextSupplier.forCall(MyService.class.getMethod("overloaded", String.class), "data").asMap()); assertEquals(ImmutableMap.<String, Object>of("int", 5), contextSupplier.forCall(MyService.class.getMethod("overloaded", int.class), 5).asMap()); }
@Test public void testNoKeys() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyService.class, MyServiceImpl.class); assertTrue(contextSupplier.forCall(MyService.class.getMethod("noKey", boolean.class), false).asMap().isEmpty()); }
@Test public void testTwoArg() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyService.class, MyServiceImpl.class); assertEquals(ImmutableMap.<String, Object>of("", "a"), contextSupplier.forCall(MyService.class.getMethod("twoArgsOneKey", int.class, String.class), 1, "a") .asMap()); }
@Test public void testMultiplePartitionKeys() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyService.class, MyServiceImpl.class); assertEquals(ImmutableMap.<String, Object>of("", "one", "b", "two", "c", "three"), contextSupplier.forCall( MyService.class.getMethod("threeKey", String.class, String.class, String.class), "one", "two", "three").asMap()); }
@Test public void testCovariant() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyService.class, MyServiceImpl.class); PartitionContext partitionContext = contextSupplier.forCall( MyService.class.getMethod("covariant", String.class), "value"); assertEquals(ImmutableMap.<String, Object>of("", "value"), partitionContext.asMap()); }
@Test public void testSubclassInheritedMethod() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyExtendedService.class, MyExtendedServiceImpl.class); PartitionContext partitionContext = contextSupplier.forCall( MyExtendedService.class.getMethod("unnamed", String.class), "value"); assertEquals(ImmutableMap.<String, Object>of("", "value"), partitionContext.asMap()); }
@Test public void testSubclassNewMethod() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyExtendedService.class, MyExtendedServiceImpl.class); PartitionContext partitionContext = contextSupplier.forCall( MyExtendedService.class.getMethod("extension", String.class), "value"); assertEquals(ImmutableMap.<String, Object>of("", "value"), partitionContext.asMap()); }
@Test public void testStaticMethod() throws Exception { PartitionContextSupplier contextSupplier = new AnnotationPartitionContextSupplier(MyServiceImpl.class, MyExtendedServiceImpl.class); assertTrue(contextSupplier.forCall(MyServiceImpl.class.getMethod("staticMethod")).asMap().isEmpty()); }