From 6ecb6da5a762eb4fb1d48125f8b66da908b8d145 Mon Sep 17 00:00:00 2001 From: Michal Drabik Date: Sat, 14 Jan 2017 17:02:28 +0100 Subject: [PATCH] Preconditions for middleware module (#56) * Add preconditions for parsers * Remove redundant checks * Refactored factory * Add factory tests * Change codestyle * Fix PMD checks --- .../store/middleware/GsonParserFactory.java | 69 ++++++------------ .../store/middleware/GsonReaderParser.java | 4 ++ .../store/middleware/GsonSourceParser.java | 4 ++ .../store/middleware/GsonStringParser.java | 6 +- .../external/store/GsonParserFactoryTest.java | 71 +++++++++++++++++++ 5 files changed, 104 insertions(+), 50 deletions(-) create mode 100644 middleware/src/test/java/com/nytimes/android/external/store/GsonParserFactoryTest.java diff --git a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonParserFactory.java b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonParserFactory.java index 1b9f3b3..6d52189 100644 --- a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonParserFactory.java +++ b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonParserFactory.java @@ -17,18 +17,21 @@ public final class GsonParserFactory { private GsonParserFactory() { } + /** + * Returns a new Parser which parses from {@link Reader} to the specified type, using + * a new default configured {@link Gson} instance. + */ + @NonNull + public static Parser createReaderParser(@NonNull Type type) { + return createReaderParser(new Gson(), type); + } + /** * Returns a new Parser which parses from {@link Reader} to the specified type, using * the provided {@link Gson} instance. */ @NonNull public static Parser createReaderParser(@NonNull Gson gson, @NonNull Type type) { - if (gson == null) { - throw new IllegalArgumentException("gson cannot be null."); - } - if (type == null) { - throw new IllegalArgumentException("type cannot be null."); - } return new GsonReaderParser<>(gson, type); } @@ -37,11 +40,8 @@ public final class GsonParserFactory { * a new default configured {@link Gson} instance. */ @NonNull - public static Parser createReaderParser(@NonNull Type type) { - if (type == null) { - throw new IllegalArgumentException("type cannot be null."); - } - return new GsonReaderParser<>(new Gson(), type); + public static Parser createSourceParser(@NonNull Type type) { + return createSourceParser(new Gson(), type); } /** @@ -50,52 +50,25 @@ public final class GsonParserFactory { */ @NonNull public static Parser createSourceParser(@NonNull Gson gson, @NonNull Type type) { - if (gson == null) { - throw new IllegalArgumentException("gson cannot be null."); - } - if (type == null) { - throw new IllegalArgumentException("type cannot be null."); - } return new GsonSourceParser<>(gson, type); } - /** - * Returns a new Parser which parses from {@link Reader} to the specified type, using - * a new default configured {@link Gson} instance. - */ - @NonNull - public static Parser createSourceParser(@NonNull Type type) { - if (type == null) { - throw new IllegalArgumentException("type cannot be null."); - } - return new GsonSourceParser<>(new Gson(), type); - } - - /** - * Returns a new Parser which parses from a String to the specified type, using - * the provided {@link Gson} instance. - */ - @NonNull - public static Parser createStringParser(@NonNull Gson gson, @NonNull Type type) { - if (gson == null) { - throw new IllegalArgumentException("gson cannot be null."); - } - if (type == null) { - throw new IllegalArgumentException("type cannot be null."); - } - return new GsonStringParser<>(gson, type); - } - /** * Returns a new Parser which parses from a String to the specified type, using * a new default {@link Gson} instance. */ @NonNull public static Parser createStringParser(@NonNull Class type) { - if (type == null) { - throw new IllegalArgumentException("type cannot be null."); - } - return new GsonStringParser<>(new Gson(), type); + return createStringParser(new Gson(), type); + } + + /** + * Returns a new Parser which parses from a String to the specified type, using + * the provided {@link Gson} instance. + */ + @NonNull + public static Parser createStringParser(@NonNull Gson gson, @NonNull Type type) { + return new GsonStringParser<>(gson, type); } } diff --git a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonReaderParser.java b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonReaderParser.java index fcfbe47..5497489 100644 --- a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonReaderParser.java +++ b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonReaderParser.java @@ -10,6 +10,8 @@ import java.lang.reflect.Type; import javax.inject.Inject; +import static com.nytimes.android.external.cache.Preconditions.checkNotNull; + public class GsonReaderParser implements Parser { private final Gson gson; @@ -17,6 +19,8 @@ public class GsonReaderParser implements Parser { @Inject public GsonReaderParser(Gson gson, Type type) { + checkNotNull(gson, "Gson can't be null"); + checkNotNull(type, "Type can't be null"); this.gson = gson; this.type = type; } diff --git a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonSourceParser.java b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonSourceParser.java index de5d184..36c213d 100644 --- a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonSourceParser.java +++ b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonSourceParser.java @@ -13,6 +13,8 @@ import javax.inject.Inject; import okio.BufferedSource; +import static com.nytimes.android.external.cache.Preconditions.checkNotNull; + /** * Parser to be used when going from a BufferedSource to any Parsed Type * example usage: @@ -31,6 +33,8 @@ public class GsonSourceParser implements Parser @Inject public GsonSourceParser(Gson gson, Type type) { + checkNotNull(gson, "Gson can't be null"); + checkNotNull(type, "Type can't be null"); this.gson = gson; this.type = type; } diff --git a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonStringParser.java b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonStringParser.java index 806079a..85f1e65 100644 --- a/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonStringParser.java +++ b/middleware/src/main/java/com/nytimes/android/external/store/middleware/GsonStringParser.java @@ -2,11 +2,11 @@ package com.nytimes.android.external.store.middleware; import com.google.gson.Gson; import com.nytimes.android.external.store.base.Parser; - import java.lang.reflect.Type; - import javax.inject.Inject; +import static com.nytimes.android.external.cache.Preconditions.checkNotNull; + public class GsonStringParser implements Parser { private final Gson gson; @@ -14,6 +14,8 @@ public class GsonStringParser implements Parser { @Inject public GsonStringParser(Gson gson, Type parsedClass) { + checkNotNull(gson, "Gson can't be null"); + checkNotNull(parsedClass, "Type can't be null"); this.gson = gson; this.type = parsedClass; } diff --git a/middleware/src/test/java/com/nytimes/android/external/store/GsonParserFactoryTest.java b/middleware/src/test/java/com/nytimes/android/external/store/GsonParserFactoryTest.java new file mode 100644 index 0000000..fedd0ce --- /dev/null +++ b/middleware/src/test/java/com/nytimes/android/external/store/GsonParserFactoryTest.java @@ -0,0 +1,71 @@ +package com.nytimes.android.external.store; + +import com.google.gson.Gson; +import com.nytimes.android.external.store.middleware.GsonParserFactory; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.lang.reflect.Type; + +public class GsonParserFactoryTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Mock + Type type; + private final Gson gson = new Gson(); + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void shouldCreateParsersProperly() { + GsonParserFactory.createReaderParser(gson, type); + GsonParserFactory.createSourceParser(gson, type); + GsonParserFactory.createStringParser(gson, type); + } + + @Test + public void shouldThrowExceptionWhenCreatingReaderWithNullType() { + expectedException.expect(NullPointerException.class); + GsonParserFactory.createReaderParser(gson, null); + } + + @Test + public void shouldThrowExceptionWhenCreatingReaderWithNullGson() { + expectedException.expect(NullPointerException.class); + GsonParserFactory.createReaderParser(null, type); + } + + @Test + public void shouldThrowExceptionWhenCreatingSourceWithNullType() { + expectedException.expect(NullPointerException.class); + GsonParserFactory.createSourceParser(gson, null); + } + + @Test + public void shouldThrowExceptionWhenCreatingSourceWithNullGson() { + expectedException.expect(NullPointerException.class); + GsonParserFactory.createSourceParser(null, type); + } + + @Test + public void shouldThrowExceptionWhenCreatingStringWithNullType() { + expectedException.expect(NullPointerException.class); + GsonParserFactory.createStringParser(gson, null); + } + + @Test + public void shouldThrowExceptionWhenCreatingStringWithNullGson() { + expectedException.expect(NullPointerException.class); + GsonParserFactory.createStringParser(null, type); + } +}