### Question:
SingletonsFactory { public T getInstance(String type) { if (!singletons.containsKey(type)) { synchronized (SingletonsFactory.class) { if (!singletons.containsKey(type)) { T singleton = newInstance(type); singletons.put(type, singleton); } } } return singletons.get(type); } SingletonsFactory(Configuration classMap); T getInstance(String type);  }### Answer:
@Test public void shouldInstantiateAnObjectFromAGivenClass() { final String type = "STRING"; Configuration conf = new Configuration("No file"); conf.set(type, "java.lang.String"); SingletonsFactory<String> factory = new SingletonsFactory<String>(conf); String singleton = factory.getInstance(type); assertEquals(String.class, singleton.getClass()); }
@Test public void shouldAlwaysReturnTheSameObjectToTheSameType() { final String type = "STRING"; Configuration conf = new Configuration("No file"); conf.set(type, "java.lang.String"); SingletonsFactory<String> factory = new SingletonsFactory<String>(conf); String singleton1 = factory.getInstance(type); String singleton2 = factory.getInstance(type); assertTrue(singleton1 == singleton2); }
@Test public void shouldAlwaysReturnDifferentObjectsToDifferentTypes() { final String type1 = "STRING1"; final String type2 = "STRING2"; Configuration conf = new Configuration("No file"); conf.set(type1, "java.lang.String"); conf.set(type2, "java.lang.String"); SingletonsFactory<String> factory = new SingletonsFactory<String>(conf); String singleton1 = factory.getInstance(type1); String singleton2 = factory.getInstance(type2); assertTrue(singleton1 != singleton2); }