ReceiptNumberGeneratorFactory { public static IReceiptNumberGenerator getGenerator() { if (generator == null) { generator = createGeneratorInstance(); if (generator == null) { return null; } } if (!generator.isLoaded()) { generator.load(); } return generator; } protected  ReceiptNumberGeneratorFactory(); static IReceiptNumberGenerator getGenerator(); static void setGenerator(IReceiptNumberGenerator generator); static IReceiptNumberGenerator[] locateGenerators();  }
@Test public void getGenerator_shouldReturnTheCurrentlyDefinedReceiptNumberGenerator() throws Exception { when(administrationService.getGlobalProperty(ModuleSettings.SYSTEM_RECEIPT_NUMBER_GENERATOR)) .thenReturn(TestReceiptNumberGenerator.class.getName()); IReceiptNumberGenerator generator = ReceiptNumberGeneratorFactory.getGenerator(); Assert.assertNotNull(generator); Assert.assertEquals(TestReceiptNumberGenerator.class, generator.getClass()); generator = ReceiptNumberGeneratorFactory.getGenerator(); Assert.assertNotNull(generator); Assert.assertEquals(TestReceiptNumberGenerator.class, generator.getClass()); verify(administrationService, times(1)) .getGlobalProperty(ModuleSettings.SYSTEM_RECEIPT_NUMBER_GENERATOR); }
@Test public void getGenerator_shouldLoadTheGeneratorIfItHasNotBeenLoaded() throws Exception { when(administrationService.getGlobalProperty(ModuleSettings.SYSTEM_RECEIPT_NUMBER_GENERATOR)) .thenReturn(TestReceiptNumberGenerator.class.getName()); TestReceiptNumberGenerator generator = (TestReceiptNumberGenerator)ReceiptNumberGeneratorFactory.getGenerator(); Assert.assertNotNull(generator); Assert.assertEquals(true, generator.isLoaded()); Assert.assertEquals(1, generator.getLoadedCount()); }
@Test public void getGenerator_shouldReturnNullIfNoGeneratorHasBeenDefined() throws Exception { when(administrationService.getGlobalProperty(ModuleSettings.SYSTEM_RECEIPT_NUMBER_GENERATOR)) .thenReturn(null); IReceiptNumberGenerator generator = ReceiptNumberGeneratorFactory.getGenerator(); Assert.assertNull(generator); }
@Test(expected = APIException.class) public void getGenerator_shouldThrowAPIExceptionIfGeneratorClassCannotBeFound() throws Exception { when(administrationService.getGlobalProperty(ModuleSettings.SYSTEM_RECEIPT_NUMBER_GENERATOR)) .thenReturn("org.openmrs.module.openhmis.cashier.NotAValidClass"); ReceiptNumberGeneratorFactory.getGenerator(); }
@Test(expected = APIException.class) public void getGenerator_shouldThrowAPIExceptionIfGeneratorClassCannotBeInstantiated() throws Exception { when(administrationService.getGlobalProperty(ModuleSettings.SYSTEM_RECEIPT_NUMBER_GENERATOR)) .thenReturn(InvalidReceiptNumberGenerator.class.getName()); ReceiptNumberGeneratorFactory.getGenerator(); }