Added tests & renamed classes
This commit is contained in:
parent
de469b4940
commit
1d52e33e2d
@ -4,8 +4,8 @@ import com.google.auto.service.AutoService;
|
|||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
@AutoService(TranslateService.class)
|
@AutoService(TranslationService.class)
|
||||||
public class BingTranslateServiceProvider implements TranslateService {
|
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";
|
return "translated by Bing";
|
@ -4,8 +4,8 @@ import com.google.auto.service.AutoService;
|
|||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
@AutoService(TranslateService.class)
|
@AutoService(TranslationService.class)
|
||||||
public class GoogleTranslateServiceProvider implements TranslateService {
|
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";
|
return "translated by Google";
|
@ -2,7 +2,7 @@ package com.baeldung.autoservice;
|
|||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public interface TranslateService {
|
public interface TranslationService {
|
||||||
|
|
||||||
String translate(String message, Locale from, Locale to);
|
String translate(String message, Locale from, Locale to);
|
||||||
}
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.autoservice;
|
||||||
|
|
||||||
|
import com.baeldung.autoservice.TranslationService;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ServiceLoader;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class TranslationServiceUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenServiceLoaderLoads_thenLoadsAllProviders() {
|
||||||
|
|
||||||
|
ServiceLoader<TranslationService> loader = ServiceLoader.load(TranslationService.class);
|
||||||
|
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));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user