Advertisement
Guest User

Untitled

a guest
Jul 30th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. public interface Service {
  2. }
  3.  
  4. @FactoryEntry(key = "one")
  5. public class ServiceImplOne implements Service {
  6. }
  7.  
  8. @FactoryEntry(key = "two")
  9. public class ServiceImplTwo implements Service {
  10. }
  11.  
  12. @FactoryEntry(key = "one", factoryName = "customFactory")
  13. public class CustomServiceImplOne implements Service {
  14. }
  15.  
  16. @FactoryEntry(key = "two", factoryName = "customFactory")
  17. public class CustomServiceImplTwo implements Service {
  18. }
  19.  
  20. public class GenericFactory<T> {
  21.     private Map<String, T> map;
  22.  
  23.     public GenericFactory(Map<String, T> map) {
  24.         this.map = map;
  25.     }
  26.  
  27.     public T get(String key){
  28.         return map.get(key);
  29.     }
  30. }
  31.  
  32. @RunWith(WeldJUnit4Runner.class)
  33. public class BeanSampleFactoryTest {
  34.  
  35.     @Inject
  36.     @Factory
  37.     private GenericFactory<Service> factory;
  38.  
  39.     @Inject
  40.     @Factory(factoryName = "customFactory")
  41.     private GenericFactory<Service> customFactory;
  42.  
  43.     @Test
  44.     public void testCreate() throws Exception {
  45.         assertNotNull(factory);
  46.         assertNotNull(factory.get("one"));
  47.         assertNotNull(factory.get("two"));
  48.  
  49.         assertNotNull(customFactory);
  50.         assertNotNull(customFactory.get("one"));
  51.         assertNotNull(customFactory.get("two"));
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement