Added tests & renamed classes

This commit is contained in:
Erhan KARAKAYA 2019-06-16 09:31:29 +03:00
parent de469b4940
commit 1d52e33e2d
4 changed files with 39 additions and 5 deletions

View File

@ -4,8 +4,8 @@ import com.google.auto.service.AutoService;
import java.util.Locale;
@AutoService(TranslateService.class)
public class BingTranslateServiceProvider implements TranslateService {
@AutoService(TranslationService.class)
public class BingTranslationServiceProvider implements TranslationService {
public String translate(String message, Locale from, Locale to) {
return "translated by Bing";

View File

@ -4,8 +4,8 @@ import com.google.auto.service.AutoService;
import java.util.Locale;
@AutoService(TranslateService.class)
public class GoogleTranslateServiceProvider implements TranslateService {
@AutoService(TranslationService.class)
public class GoogleTranslationServiceProvider implements TranslationService {
public String translate(String message, Locale from, Locale to) {
return "translated by Google";

View File

@ -2,7 +2,7 @@ package com.baeldung.autoservice;
import java.util.Locale;
public interface TranslateService {
public interface TranslationService {
String translate(String message, Locale from, Locale to);
}

View File

@ -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));
}
}