Changed sample impls & refactored test
This commit is contained in:
parent
1d52e33e2d
commit
2826baee3b
@ -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)";
|
||||
}
|
||||
}
|
||||
|
@ -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)";
|
||||
}
|
||||
}
|
||||
|
@ -9,26 +9,31 @@ import java.util.stream.StreamSupport;
|
||||
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);
|
||||
long count = StreamSupport.stream(loader.spliterator(), false).count();
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
@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));
|
||||
|
||||
TranslationService googleService = StreamSupport.stream(loader.spliterator(), false)
|
||||
.filter(p -> p.getClass().getSimpleName().equals("GoogleTranslationServiceProvider"))
|
||||
.findFirst()
|
||||
.get();
|
||||
|
||||
String message = "message";
|
||||
assertEquals(message + " (translated by Google)", googleService.translate(message, null, null));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user