Changed sample impls & refactored test

This commit is contained in:
Erhan Karakaya 2019-06-17 14:52:29 +03:00
parent 1d52e33e2d
commit 2826baee3b
3 changed files with 23 additions and 14 deletions

View File

@ -8,6 +8,8 @@ import java.util.Locale;
public class BingTranslationServiceProvider implements TranslationService {
public String translate(String message, Locale from, Locale to) {
return "translated by Bing";
// implementation details
return message + " (translated by Bing)";
}
}

View File

@ -8,6 +8,8 @@ import java.util.Locale;
public class GoogleTranslationServiceProvider implements TranslationService {
public String translate(String message, Locale from, Locale to) {
return "translated by Google";
// implementation details
return message + " (translated by Google)";
}
}

View File

@ -10,10 +10,17 @@ import static org.junit.Assert.assertEquals;
public class TranslationServiceUnitTest {
private ServiceLoader<TranslationService> loader;
@Before
public void setUp() {
loader = ServiceLoader.load(TranslationService.class);
}
@Test
public void whenServiceLoaderLoads_thenLoadsAllProviders() {
ServiceLoader<TranslationService> loader = ServiceLoader.load(TranslationService.class);
long count = StreamSupport.stream(loader.spliterator(), false).count();
assertEquals(2, count);
}
@ -21,14 +28,12 @@ public class TranslationServiceUnitTest {
@Test
public void whenServiceLoaderLoadsGoogleService_thenGoogleIsLoaded() {
ServiceLoader<TranslationService> loader = ServiceLoader.load(TranslationService.class);
TranslationService googleService = StreamSupport.stream(loader.spliterator(), false)
.filter(p -> p.getClass().getSimpleName().equals("GoogleTranslationServiceProvider"))
.findFirst()
.get();
assertEquals("translated by Google", googleService.translate("message", null, null));
String message = "message";
assertEquals(message + " (translated by Google)", googleService.translate(message, null, null));
}
}