Merge pull request #7087 from er-han/BAEL-2955
Added code sample for autoservice
This commit is contained in:
commit
efe62f6dc4
|
@ -29,6 +29,12 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.auto.service</groupId>
|
||||
<artifactId>auto-service</artifactId>
|
||||
<version>${auto-service.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
|
@ -40,6 +46,7 @@
|
|||
<properties>
|
||||
<auto-value.version>1.3</auto-value.version>
|
||||
<auto-factory.version>1.0-beta5</auto-factory.version>
|
||||
<auto-service.version>1.0-rc5</auto-service.version>
|
||||
<guice.version>4.2.0</guice.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.autoservice;
|
||||
|
||||
import com.google.auto.service.AutoService;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@AutoService(TranslationService.class)
|
||||
public class BingTranslationServiceProvider implements TranslationService {
|
||||
@Override
|
||||
public String translate(String message, Locale from, Locale to) {
|
||||
// implementation details
|
||||
return message + " (translated by Bing)";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.autoservice;
|
||||
|
||||
import com.google.auto.service.AutoService;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@AutoService(TranslationService.class)
|
||||
public class GoogleTranslationServiceProvider implements TranslationService {
|
||||
@Override
|
||||
public String translate(String message, Locale from, Locale to) {
|
||||
// implementation details
|
||||
return message + " (translated by Google)";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.autoservice;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public interface TranslationService {
|
||||
String translate(String message, Locale from, Locale to);
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.autoservice;
|
||||
|
||||
import com.baeldung.autoservice.TranslationService;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ServiceLoader;
|
||||
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() {
|
||||
long count = StreamSupport.stream(loader.spliterator(), false).count();
|
||||
assertEquals(2, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenServiceLoaderLoadsGoogleService_thenGoogleIsLoaded() {
|
||||
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…
Reference in New Issue