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 class BingTranslationServiceProvider implements TranslationService {
|
||||||
|
|
||||||
public String translate(String message, Locale from, Locale to) {
|
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 class GoogleTranslationServiceProvider implements TranslationService {
|
||||||
|
|
||||||
public String translate(String message, Locale from, Locale to) {
|
public String translate(String message, Locale from, Locale to) {
|
||||||
return "translated by Google";
|
|
||||||
|
// implementation details
|
||||||
|
return message + " (translated by Google)";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,10 +10,17 @@ import static org.junit.Assert.assertEquals;
|
|||||||
|
|
||||||
public class TranslationServiceUnitTest {
|
public class TranslationServiceUnitTest {
|
||||||
|
|
||||||
|
private ServiceLoader<TranslationService> loader;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
|
||||||
|
loader = ServiceLoader.load(TranslationService.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenServiceLoaderLoads_thenLoadsAllProviders() {
|
public void whenServiceLoaderLoads_thenLoadsAllProviders() {
|
||||||
|
|
||||||
ServiceLoader<TranslationService> loader = ServiceLoader.load(TranslationService.class);
|
|
||||||
long count = StreamSupport.stream(loader.spliterator(), false).count();
|
long count = StreamSupport.stream(loader.spliterator(), false).count();
|
||||||
assertEquals(2, count);
|
assertEquals(2, count);
|
||||||
}
|
}
|
||||||
@ -21,14 +28,12 @@ public class TranslationServiceUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void whenServiceLoaderLoadsGoogleService_thenGoogleIsLoaded() {
|
public void whenServiceLoaderLoadsGoogleService_thenGoogleIsLoaded() {
|
||||||
|
|
||||||
ServiceLoader<TranslationService> loader = ServiceLoader.load(TranslationService.class);
|
|
||||||
|
|
||||||
TranslationService googleService = StreamSupport.stream(loader.spliterator(), false)
|
TranslationService googleService = StreamSupport.stream(loader.spliterator(), false)
|
||||||
.filter(p -> p.getClass().getSimpleName().equals("GoogleTranslationServiceProvider"))
|
.filter(p -> p.getClass().getSimpleName().equals("GoogleTranslationServiceProvider"))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
assertEquals("translated by Google", googleService.translate("message", null, null));
|
String message = "message";
|
||||||
|
assertEquals(message + " (translated by Google)", googleService.translate(message, null, null));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user