From 4633c51b175b8b16cc51522c1f59abe152965b6e Mon Sep 17 00:00:00 2001 From: Erhan KARAKAYA Date: Wed, 5 Jun 2019 01:44:57 +0300 Subject: [PATCH 1/6] Added code sample for autoservice --- autovalue/pom.xml | 7 +++++++ .../autoservice/BingTranslateServiceProvider.java | 11 +++++++++++ .../autoservice/GoogleTranslateServiceProvider.java | 11 +++++++++++ .../com/baeldung/autoservice/TranslateService.java | 8 ++++++++ 4 files changed, 37 insertions(+) create mode 100644 autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java create mode 100644 autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java create mode 100644 autovalue/src/main/java/com/baeldung/autoservice/TranslateService.java diff --git a/autovalue/pom.xml b/autovalue/pom.xml index 3ec2d26b35..a10e8ef055 100644 --- a/autovalue/pom.xml +++ b/autovalue/pom.xml @@ -29,6 +29,12 @@ + + com.google.auto.service + auto-service + ${auto-service.version} + true + com.google.inject @@ -40,6 +46,7 @@ 1.3 1.0-beta5 + 1.0-rc5 4.2.0 diff --git a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java new file mode 100644 index 0000000000..93ab8f4fe4 --- /dev/null +++ b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java @@ -0,0 +1,11 @@ +package com.baeldung.autoservice; + +import java.util.Locale; + +@AutoService(TranslateService.class) +public class BingTranslateServiceProvider implements TranslateService { + + public String translate(String message, Locale from, Locale to) { + return "translated by Bing"; + } +} diff --git a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java new file mode 100644 index 0000000000..07772e2afe --- /dev/null +++ b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java @@ -0,0 +1,11 @@ +package com.baeldung.autoservice; + +import java.util.Locale; + +@AutoService(TranslateService.class) +public class GoogleTranslateServiceProvider implements TranslateService { + + public String translate(String message, Locale from, Locale to) { + return "translated by Google"; + } +} diff --git a/autovalue/src/main/java/com/baeldung/autoservice/TranslateService.java b/autovalue/src/main/java/com/baeldung/autoservice/TranslateService.java new file mode 100644 index 0000000000..6d68f6aca3 --- /dev/null +++ b/autovalue/src/main/java/com/baeldung/autoservice/TranslateService.java @@ -0,0 +1,8 @@ +package com.baeldung.autoservice; + +import java.util.Locale; + +public interface TranslateService { + + String translate(String message, Locale from, Locale to); +} From de469b49409ac2e615b00762d5d9c551fa3e813f Mon Sep 17 00:00:00 2001 From: Erhan KARAKAYA Date: Wed, 5 Jun 2019 02:00:53 +0300 Subject: [PATCH 2/6] Added missing imports --- .../com/baeldung/autoservice/BingTranslateServiceProvider.java | 2 ++ .../baeldung/autoservice/GoogleTranslateServiceProvider.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java index 93ab8f4fe4..00f21a943e 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java @@ -1,5 +1,7 @@ package com.baeldung.autoservice; +import com.google.auto.service.AutoService; + import java.util.Locale; @AutoService(TranslateService.class) diff --git a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java index 07772e2afe..786d9bd443 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java @@ -1,5 +1,7 @@ package com.baeldung.autoservice; +import com.google.auto.service.AutoService; + import java.util.Locale; @AutoService(TranslateService.class) From 1d52e33e2db33e9a1450f3dbd246cfa315810cab Mon Sep 17 00:00:00 2001 From: Erhan KARAKAYA Date: Sun, 16 Jun 2019 09:31:29 +0300 Subject: [PATCH 3/6] Added tests & renamed classes --- ...va => BingTranslationServiceProvider.java} | 4 +-- ... => GoogleTranslationServiceProvider.java} | 4 +-- ...teService.java => TranslationService.java} | 2 +- .../TranslationServiceUnitTest.java | 34 +++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) rename autovalue/src/main/java/com/baeldung/autoservice/{BingTranslateServiceProvider.java => BingTranslationServiceProvider.java} (65%) rename autovalue/src/main/java/com/baeldung/autoservice/{GoogleTranslateServiceProvider.java => GoogleTranslationServiceProvider.java} (65%) rename autovalue/src/main/java/com/baeldung/autoservice/{TranslateService.java => TranslationService.java} (76%) create mode 100644 autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java diff --git a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java similarity index 65% rename from autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java rename to autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java index 00f21a943e..74666b2f7d 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslateServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java @@ -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"; diff --git a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java similarity index 65% rename from autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java rename to autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java index 786d9bd443..68889472bf 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslateServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java @@ -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"; diff --git a/autovalue/src/main/java/com/baeldung/autoservice/TranslateService.java b/autovalue/src/main/java/com/baeldung/autoservice/TranslationService.java similarity index 76% rename from autovalue/src/main/java/com/baeldung/autoservice/TranslateService.java rename to autovalue/src/main/java/com/baeldung/autoservice/TranslationService.java index 6d68f6aca3..cfcff4015c 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/TranslateService.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/TranslationService.java @@ -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); } diff --git a/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java b/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java new file mode 100644 index 0000000000..f167dad6bd --- /dev/null +++ b/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java @@ -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 loader = ServiceLoader.load(TranslationService.class); + long count = StreamSupport.stream(loader.spliterator(), false).count(); + assertEquals(2, count); + } + + @Test + public void whenServiceLoaderLoadsGoogleService_thenGoogleIsLoaded() { + + ServiceLoader 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)); + + } +} \ No newline at end of file From 2826baee3b6395a1c84729c470da5f18de37ba7b Mon Sep 17 00:00:00 2001 From: Erhan Karakaya Date: Mon, 17 Jun 2019 14:52:29 +0300 Subject: [PATCH 4/6] Changed sample impls & refactored test --- .../BingTranslationServiceProvider.java | 4 ++- .../GoogleTranslationServiceProvider.java | 4 ++- .../TranslationServiceUnitTest.java | 29 +++++++++++-------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java index 74666b2f7d..0aed76b834 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java @@ -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)"; } } diff --git a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java index 68889472bf..eb5d191a26 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java @@ -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)"; } } diff --git a/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java b/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java index f167dad6bd..2f39a306c2 100644 --- a/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java +++ b/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java @@ -9,26 +9,31 @@ import java.util.stream.StreamSupport; import static org.junit.Assert.assertEquals; public class TranslationServiceUnitTest { + + private ServiceLoader loader; + + @Before + public void setUp() { + + loader = ServiceLoader.load(TranslationService.class); + } @Test public void whenServiceLoaderLoads_thenLoadsAllProviders() { - ServiceLoader 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 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)); } } \ No newline at end of file From ac850bbc0b39ee45b3b93010e02c3d7ba0b9aa0b Mon Sep 17 00:00:00 2001 From: Erhan Karakaya Date: Mon, 17 Jun 2019 14:57:45 +0300 Subject: [PATCH 5/6] Fixed typos --- .../baeldung/autoservice/BingTranslationServiceProvider.java | 2 +- .../baeldung/autoservice/GoogleTranslationServiceProvider.java | 2 +- .../com/baeldung/autoservice/TranslationServiceUnitTest.java | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java index 0aed76b834..8ef4d6a45e 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java @@ -9,7 +9,7 @@ public class BingTranslationServiceProvider implements TranslationService { public String translate(String message, Locale from, Locale to) { - // implementation details + // implementation details return message + " (translated by Bing)"; } } diff --git a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java index eb5d191a26..3d6d1edc9f 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java @@ -9,7 +9,7 @@ public class GoogleTranslationServiceProvider implements TranslationService { public String translate(String message, Locale from, Locale to) { - // implementation details + // implementation details return message + " (translated by Google)"; } } diff --git a/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java b/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java index 2f39a306c2..2d7c8d7b6d 100644 --- a/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java +++ b/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java @@ -1,6 +1,7 @@ package com.baeldung.autoservice; import com.baeldung.autoservice.TranslationService; +import org.junit.Before; import org.junit.Test; import java.util.ServiceLoader; From d0b2aa86351f7fe9ce39329a45f28aad986051b1 Mon Sep 17 00:00:00 2001 From: Erhan Karakaya Date: Thu, 20 Jun 2019 11:00:12 +0300 Subject: [PATCH 6/6] removed empty beggining lines & added override annotation --- .../baeldung/autoservice/BingTranslationServiceProvider.java | 3 +-- .../autoservice/GoogleTranslationServiceProvider.java | 3 +-- .../java/com/baeldung/autoservice/TranslationService.java | 3 +-- .../com/baeldung/autoservice/TranslationServiceUnitTest.java | 5 +---- 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java index 8ef4d6a45e..86d42e80fa 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/BingTranslationServiceProvider.java @@ -6,9 +6,8 @@ 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)"; } diff --git a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java index 3d6d1edc9f..0bf91ee5ec 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/GoogleTranslationServiceProvider.java @@ -6,9 +6,8 @@ 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)"; } diff --git a/autovalue/src/main/java/com/baeldung/autoservice/TranslationService.java b/autovalue/src/main/java/com/baeldung/autoservice/TranslationService.java index cfcff4015c..580db46cd1 100644 --- a/autovalue/src/main/java/com/baeldung/autoservice/TranslationService.java +++ b/autovalue/src/main/java/com/baeldung/autoservice/TranslationService.java @@ -3,6 +3,5 @@ package com.baeldung.autoservice; import java.util.Locale; public interface TranslationService { - String translate(String message, Locale from, Locale to); -} +} \ No newline at end of file diff --git a/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java b/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java index 2d7c8d7b6d..9e1bd6d291 100644 --- a/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java +++ b/autovalue/src/test/java/com/baeldung/autoservice/TranslationServiceUnitTest.java @@ -10,25 +10,22 @@ import java.util.stream.StreamSupport; import static org.junit.Assert.assertEquals; public class TranslationServiceUnitTest { - + private ServiceLoader 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()