DiskValidatorFactory { public static DiskValidator getInstance(Class<? extends DiskValidator> clazz) { DiskValidator diskValidator; if (INSTANCES.containsKey(clazz)) { diskValidator = INSTANCES.get(clazz); } else { diskValidator = ReflectionUtils.newInstance(clazz, null); DiskValidator diskValidatorRet = INSTANCES.putIfAbsent(clazz, diskValidator); if (diskValidatorRet != null) { diskValidator = diskValidatorRet; } } return diskValidator; } private  DiskValidatorFactory(); static DiskValidator getInstance(Class<? extends DiskValidator> clazz); @SuppressWarnings("unchecked") static DiskValidator getInstance(String diskValidator);  }
@Test public void testGetInstance() throws DiskErrorException { DiskValidator diskValidator = DiskValidatorFactory.getInstance("basic"); assertNotNull("Fail to get the instance.", diskValidator); assertEquals("Fail to create the correct instance.", diskValidator.getClass(), BasicDiskValidator.class); assertNotNull("Fail to cache the object", DiskValidatorFactory.INSTANCES. get(BasicDiskValidator.class)); }
@Test(expected = DiskErrorException.class) public void testGetInstanceOfNonExistClass() throws DiskErrorException { DiskValidatorFactory.getInstance("non-exist"); }