diff --git a/build.gradle b/build.gradle index 8820e72..3a20404 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:3.5.3' + classpath 'com.android.tools.build:gradle:3.6.1' classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4' } } diff --git a/gradle.properties b/gradle.properties index aac7c9b..c7fb972 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,3 +15,5 @@ org.gradle.jvmargs=-Xmx1536m # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true + +android.useAndroidX=true \ No newline at end of file diff --git a/lib/build.gradle b/lib/build.gradle index 2052d25..5eccd55 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -39,7 +39,10 @@ dependencies { testImplementation 'junit:junit:4.13' testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0' - testImplementation 'org.robolectric:robolectric:4.3.1' + testImplementation('org.robolectric:robolectric:4.3.1') { + // https://github.com/robolectric/robolectric/issues/5245 + exclude group: 'com.google.auto.service', module: 'auto-service' + } testImplementation 'org.mockito:mockito-core:3.2.4' } diff --git a/lib/src/main/java/com/auth0/android/jwt/ClaimImpl.java b/lib/src/main/java/com/auth0/android/jwt/ClaimImpl.java index db088fb..c0dc64d 100644 --- a/lib/src/main/java/com/auth0/android/jwt/ClaimImpl.java +++ b/lib/src/main/java/com/auth0/android/jwt/ClaimImpl.java @@ -14,7 +14,6 @@ /** * The ClaimImpl class implements the Claim interface. */ -@SuppressWarnings("WeakerAccess") class ClaimImpl extends BaseClaim { private final JsonElement value; diff --git a/lib/src/main/java/com/auth0/android/jwt/DecodeException.java b/lib/src/main/java/com/auth0/android/jwt/DecodeException.java index 7b055cf..a1182b8 100644 --- a/lib/src/main/java/com/auth0/android/jwt/DecodeException.java +++ b/lib/src/main/java/com/auth0/android/jwt/DecodeException.java @@ -1,5 +1,6 @@ package com.auth0.android.jwt; +@SuppressWarnings("WeakerAccess") public class DecodeException extends RuntimeException { DecodeException(String message) { diff --git a/lib/src/main/java/com/auth0/android/jwt/JWT.java b/lib/src/main/java/com/auth0/android/jwt/JWT.java index 39f1685..7c52d8b 100644 --- a/lib/src/main/java/com/auth0/android/jwt/JWT.java +++ b/lib/src/main/java/com/auth0/android/jwt/JWT.java @@ -3,13 +3,16 @@ import android.os.Parcel; import android.os.Parcelable; import android.util.Base64; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; -import java.io.UnsupportedEncodingException; + import java.lang.reflect.Type; +import java.nio.charset.Charset; import java.util.Date; import java.util.List; import java.util.Map; @@ -21,7 +24,6 @@ public class JWT implements Parcelable { private static final String TAG = JWT.class.getSimpleName(); - private static final String ENCODING_UTF_8 = "UTF-8"; private final String token; private Map header; @@ -161,8 +163,8 @@ public boolean isExpired(long leeway) { throw new IllegalArgumentException("The leeway must be a positive value."); } long todayTime = (long) (Math.floor(new Date().getTime() / 1000) * 1000); //truncate millis - Date futureToday = new Date((todayTime + leeway * 1000)); - Date pastToday = new Date((todayTime - leeway * 1000)); + Date futureToday = new Date(todayTime + leeway * 1000); + Date pastToday = new Date(todayTime - leeway * 1000); boolean expValid = payload.exp == null || !pastToday.after(payload.exp); boolean iatValid = payload.iat == null || !futureToday.before(payload.iat); return !expValid || !iatValid; @@ -230,11 +232,9 @@ private String base64Decode(String string) { String decoded; try { byte[] bytes = Base64.decode(string, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING); - decoded = new String(bytes, ENCODING_UTF_8); + decoded = new String(bytes, Charset.defaultCharset()); } catch (IllegalArgumentException e) { throw new DecodeException("Received bytes didn't correspond to a valid Base64 encoded string.", e); - } catch (UnsupportedEncodingException e) { - throw new DecodeException("Device doesn't support UTF-8 charset encoding.", e); } return decoded; } diff --git a/lib/src/main/java/com/auth0/android/jwt/JWTDeserializer.java b/lib/src/main/java/com/auth0/android/jwt/JWTDeserializer.java index 3a379fa..2b3d339 100644 --- a/lib/src/main/java/com/auth0/android/jwt/JWTDeserializer.java +++ b/lib/src/main/java/com/auth0/android/jwt/JWTDeserializer.java @@ -42,6 +42,7 @@ public JWTPayload deserialize(JsonElement json, Type typeOfT, JsonDeserializatio return new JWTPayload(iss, sub, exp, nbf, iat, jti, aud, extra); } + @SuppressWarnings("SameParameterValue") private List getStringOrArray(JsonObject obj, String claimName) { List list = Collections.emptyList(); if (obj.has(claimName)) { diff --git a/lib/src/test/java/com/auth0/android/jwt/BaseClaimTest.java b/lib/src/test/java/com/auth0/android/jwt/BaseClaimTest.java index 90b9465..41eaf67 100644 --- a/lib/src/test/java/com/auth0/android/jwt/BaseClaimTest.java +++ b/lib/src/test/java/com/auth0/android/jwt/BaseClaimTest.java @@ -5,64 +5,64 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.collection.IsArrayWithSize.emptyArray; import static org.hamcrest.collection.IsEmptyCollection.empty; import static org.hamcrest.core.IsNull.nullValue; -import static org.junit.Assert.assertThat; public class BaseClaimTest { private BaseClaim claim; @Before - public void setUp() throws Exception { + public void setUp() { claim = new BaseClaim(); } @Test - public void shouldGetAsBoolean() throws Exception { + public void shouldGetAsBoolean() { assertThat(claim.asBoolean(), is(nullValue())); } @Test - public void shouldGetAsInt() throws Exception { + public void shouldGetAsInt() { assertThat(claim.asInt(), is(nullValue())); } @Test - public void shouldGetAsLong() throws Exception { + public void shouldGetAsLong() { assertThat(claim.asLong(), is(nullValue())); } @Test - public void shouldGetAsDouble() throws Exception { + public void shouldGetAsDouble() { assertThat(claim.asDouble(), is(nullValue())); } @Test - public void shouldGetAsString() throws Exception { + public void shouldGetAsString() { assertThat(claim.asString(), is(nullValue())); } @Test - public void shouldGetAsDate() throws Exception { + public void shouldGetAsDate() { assertThat(claim.asDate(), is(nullValue())); } @Test - public void shouldGetAsArray() throws Exception { + public void shouldGetAsArray() { assertThat(claim.asArray(Object.class), is(notNullValue())); assertThat(claim.asArray(Object.class), is(emptyArray())); } @Test - public void shouldGetAsList() throws Exception { + public void shouldGetAsList() { assertThat(claim.asList(Object.class), is(notNullValue())); assertThat(claim.asList(Object.class), is(empty())); } @Test - public void shouldGetAsObject() throws Exception { + public void shouldGetAsObject() { assertThat(claim.asObject(Object.class), is(nullValue())); } } \ No newline at end of file diff --git a/lib/src/test/java/com/auth0/android/jwt/ClaimImplTest.java b/lib/src/test/java/com/auth0/android/jwt/ClaimImplTest.java index d4730cc..4f1d61d 100644 --- a/lib/src/test/java/com/auth0/android/jwt/ClaimImplTest.java +++ b/lib/src/test/java/com/auth0/android/jwt/ClaimImplTest.java @@ -1,17 +1,8 @@ package com.auth0.android.jwt; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; -import static org.hamcrest.core.IsCollectionContaining.hasItems; -import static org.junit.Assert.assertThat; -import static org.mockito.MockitoAnnotations.initMocks; - import com.google.gson.Gson; import com.google.gson.JsonElement; -import java.util.Arrays; -import java.util.Date; + import org.hamcrest.collection.IsArrayWithSize; import org.hamcrest.collection.IsEmptyCollection; import org.junit.Before; @@ -22,22 +13,33 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; +import java.util.Arrays; +import java.util.Date; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; +import static org.hamcrest.core.IsCollectionContaining.hasItems; +import static org.mockito.MockitoAnnotations.initMocks; + @RunWith(RobolectricTestRunner.class) @Config(sdk = 23) public class ClaimImplTest { - Gson gson; + private Gson gson; @Rule public ExpectedException exception = ExpectedException.none(); @Before - public void setUp() throws Exception { + public void setUp() { initMocks(this); gson = new Gson(); } @Test - public void shouldGetBooleanValue() throws Exception { + public void shouldGetBooleanValue() { JsonElement value = gson.toJsonTree(true); ClaimImpl claim = new ClaimImpl(value); @@ -46,7 +48,7 @@ public void shouldGetBooleanValue() throws Exception { } @Test - public void shouldGetNullBooleanIfNotPrimitiveValue() throws Exception { + public void shouldGetNullBooleanIfNotPrimitiveValue() { JsonElement value = gson.toJsonTree(new Object()); ClaimImpl claim = new ClaimImpl(value); @@ -54,7 +56,7 @@ public void shouldGetNullBooleanIfNotPrimitiveValue() throws Exception { } @Test - public void shouldGetIntValue() throws Exception { + public void shouldGetIntValue() { JsonElement value = gson.toJsonTree(123); ClaimImpl claim = new ClaimImpl(value); @@ -63,7 +65,7 @@ public void shouldGetIntValue() throws Exception { } @Test - public void shouldGetLongValue() throws Exception { + public void shouldGetLongValue() { JsonElement value = gson.toJsonTree(123L); ClaimImpl claim = new ClaimImpl(value); @@ -72,7 +74,7 @@ public void shouldGetLongValue() throws Exception { } @Test - public void shouldGetNullIntIfNotPrimitiveValue() throws Exception { + public void shouldGetNullIntIfNotPrimitiveValue() { JsonElement value = gson.toJsonTree(new Object()); ClaimImpl claim = new ClaimImpl(value); @@ -80,7 +82,7 @@ public void shouldGetNullIntIfNotPrimitiveValue() throws Exception { } @Test - public void shouldGetNullLongIfNotPrimitiveValue() throws Exception { + public void shouldGetNullLongIfNotPrimitiveValue() { JsonElement value = gson.toJsonTree(new Object()); ClaimImpl claim = new ClaimImpl(value); @@ -88,7 +90,7 @@ public void shouldGetNullLongIfNotPrimitiveValue() throws Exception { } @Test - public void shouldGetDoubleValue() throws Exception { + public void shouldGetDoubleValue() { JsonElement value = gson.toJsonTree(1.5); ClaimImpl claim = new ClaimImpl(value); @@ -97,7 +99,7 @@ public void shouldGetDoubleValue() throws Exception { } @Test - public void shouldGetNullDoubleIfNotPrimitiveValue() throws Exception { + public void shouldGetNullDoubleIfNotPrimitiveValue() { JsonElement value = gson.toJsonTree(new Object()); ClaimImpl claim = new ClaimImpl(value); @@ -105,7 +107,7 @@ public void shouldGetNullDoubleIfNotPrimitiveValue() throws Exception { } @Test - public void shouldGetLargeDateValue() throws Exception { + public void shouldGetLargeDateValue() { long seconds = Integer.MAX_VALUE + 10000L; JsonElement value = gson.toJsonTree(seconds); ClaimImpl claim = new ClaimImpl(value); @@ -117,7 +119,7 @@ public void shouldGetLargeDateValue() throws Exception { } @Test - public void shouldGetDateValue() throws Exception { + public void shouldGetDateValue() { JsonElement value = gson.toJsonTree("1476824844"); ClaimImpl claim = new ClaimImpl(value); @@ -126,7 +128,7 @@ public void shouldGetDateValue() throws Exception { } @Test - public void shouldGetNullDateIfNotPrimitiveValue() throws Exception { + public void shouldGetNullDateIfNotPrimitiveValue() { JsonElement value = gson.toJsonTree(new Object()); ClaimImpl claim = new ClaimImpl(value); @@ -134,7 +136,7 @@ public void shouldGetNullDateIfNotPrimitiveValue() throws Exception { } @Test - public void shouldGetStringValue() throws Exception { + public void shouldGetStringValue() { JsonElement value = gson.toJsonTree("string"); ClaimImpl claim = new ClaimImpl(value); @@ -143,7 +145,7 @@ public void shouldGetStringValue() throws Exception { } @Test - public void shouldGetNullStringIfNotPrimitiveValue() throws Exception { + public void shouldGetNullStringIfNotPrimitiveValue() { JsonElement value = gson.toJsonTree(new Object()); ClaimImpl claim = new ClaimImpl(value); @@ -151,7 +153,7 @@ public void shouldGetNullStringIfNotPrimitiveValue() throws Exception { } @Test - public void shouldGetArrayValueOfCustomClass() throws Exception { + public void shouldGetArrayValueOfCustomClass() { JsonElement value = gson.toJsonTree(new UserPojo[]{new UserPojo("George", 1), new UserPojo("Mark", 2)}); ClaimImpl claim = new ClaimImpl(value); @@ -160,7 +162,7 @@ public void shouldGetArrayValueOfCustomClass() throws Exception { } @Test - public void shouldGetArrayValue() throws Exception { + public void shouldGetArrayValue() { JsonElement value = gson.toJsonTree(new String[]{"string1", "string2"}); ClaimImpl claim = new ClaimImpl(value); @@ -169,7 +171,7 @@ public void shouldGetArrayValue() throws Exception { } @Test - public void shouldGetEmptyArrayIfNullValue() throws Exception { + public void shouldGetEmptyArrayIfNullValue() { JsonElement value = gson.toJsonTree(null); ClaimImpl claim = new ClaimImpl(value); @@ -178,7 +180,7 @@ public void shouldGetEmptyArrayIfNullValue() throws Exception { } @Test - public void shouldGetEmptyArrayIfNonArrayValue() throws Exception { + public void shouldGetEmptyArrayIfNonArrayValue() { JsonElement value = gson.toJsonTree(1); ClaimImpl claim = new ClaimImpl(value); @@ -187,7 +189,7 @@ public void shouldGetEmptyArrayIfNonArrayValue() throws Exception { } @Test - public void shouldThrowIfArrayClassMismatch() throws Exception { + public void shouldThrowIfArrayClassMismatch() { JsonElement value = gson.toJsonTree(new String[]{"keys", "values"}); ClaimImpl claim = new ClaimImpl(value); @@ -196,7 +198,7 @@ public void shouldThrowIfArrayClassMismatch() throws Exception { } @Test - public void shouldGetListValueOfCustomClass() throws Exception { + public void shouldGetListValueOfCustomClass() { JsonElement value = gson.toJsonTree(Arrays.asList(new UserPojo("George", 1), new UserPojo("Mark", 2))); ClaimImpl claim = new ClaimImpl(value); @@ -205,7 +207,7 @@ public void shouldGetListValueOfCustomClass() throws Exception { } @Test - public void shouldGetListValue() throws Exception { + public void shouldGetListValue() { JsonElement value = gson.toJsonTree(Arrays.asList("string1", "string2")); ClaimImpl claim = new ClaimImpl(value); @@ -214,7 +216,7 @@ public void shouldGetListValue() throws Exception { } @Test - public void shouldGetEmptyListIfNullValue() throws Exception { + public void shouldGetEmptyListIfNullValue() { JsonElement value = gson.toJsonTree(null); ClaimImpl claim = new ClaimImpl(value); @@ -223,7 +225,7 @@ public void shouldGetEmptyListIfNullValue() throws Exception { } @Test - public void shouldGetEmptyListIfNonArrayValue() throws Exception { + public void shouldGetEmptyListIfNonArrayValue() { JsonElement value = gson.toJsonTree(1); ClaimImpl claim = new ClaimImpl(value); @@ -232,7 +234,7 @@ public void shouldGetEmptyListIfNonArrayValue() throws Exception { } @Test - public void shouldThrowIfListClassMismatch() throws Exception { + public void shouldThrowIfListClassMismatch() { JsonElement value = gson.toJsonTree(new String[]{"keys", "values"}); ClaimImpl claim = new ClaimImpl(value); @@ -241,7 +243,7 @@ public void shouldThrowIfListClassMismatch() throws Exception { } @Test - public void shouldGetAsObject() throws Exception { + public void shouldGetAsObject() { UserPojo data = new UserPojo("George", 1); JsonElement userValue = gson.toJsonTree(data); ClaimImpl userClaim = new ClaimImpl(userValue); @@ -263,7 +265,7 @@ public void shouldGetAsObject() throws Exception { } @Test - public void shouldGetNullObjectIfNullValue() throws Exception { + public void shouldGetNullObjectIfNullValue() { JsonElement value = gson.toJsonTree(null); ClaimImpl claim = new ClaimImpl(value); @@ -271,7 +273,7 @@ public void shouldGetNullObjectIfNullValue() throws Exception { } @Test - public void shouldThrowIfObjectClassMismatch() throws Exception { + public void shouldThrowIfObjectClassMismatch() { JsonElement value = gson.toJsonTree(1); ClaimImpl claim = new ClaimImpl(value); diff --git a/lib/src/test/java/com/auth0/android/jwt/JWTTest.java b/lib/src/test/java/com/auth0/android/jwt/JWTTest.java index 69aec78..14318c7 100644 --- a/lib/src/test/java/com/auth0/android/jwt/JWTTest.java +++ b/lib/src/test/java/com/auth0/android/jwt/JWTTest.java @@ -1,26 +1,12 @@ package com.auth0.android.jwt; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.not; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.collection.IsCollectionWithSize.hasSize; -import static org.hamcrest.collection.IsMapContaining.hasEntry; -import static org.hamcrest.core.IsCollectionContaining.hasItems; -import static org.hamcrest.core.IsInstanceOf.instanceOf; - import android.os.Bundle; import android.os.Parcel; import android.support.annotation.Nullable; import android.util.Base64; -import java.nio.charset.StandardCharsets; -import java.util.Date; -import java.util.Map; + import org.hamcrest.collection.IsEmptyCollection; import org.hamcrest.core.IsCollectionContaining; -import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -28,39 +14,52 @@ import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; +import java.nio.charset.Charset; +import java.util.Date; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.hamcrest.collection.IsMapContaining.hasEntry; +import static org.hamcrest.core.IsCollectionContaining.hasItems; +import static org.hamcrest.core.IsInstanceOf.instanceOf; + @RunWith(RobolectricTestRunner.class) @Config(sdk = 23) public class JWTTest { - private static final String CHARSET_UTF_8 = "UTF-8"; - @Rule public ExpectedException exception = ExpectedException.none(); // Exceptions @Test - public void shouldThrowIfLessThan3Parts() throws Exception { + public void shouldThrowIfLessThan3Parts() { exception.expect(DecodeException.class); exception.expectMessage("The token was expected to have 3 parts, but got 2."); new JWT("two.parts"); } @Test - public void shouldThrowIfMoreThan3Parts() throws Exception { + public void shouldThrowIfMoreThan3Parts() { exception.expect(DecodeException.class); exception.expectMessage("The token was expected to have 3 parts, but got 4."); new JWT("this.has.four.parts"); } @Test - public void shouldThrowIfItsNotBase64Encoded() throws Exception { + public void shouldThrowIfItsNotBase64Encoded() { exception.expect(DecodeException.class); exception.expectMessage("Received bytes didn't correspond to a valid Base64 encoded string."); new JWT("thisIsNot.Base64_Enc.oded"); } @Test - public void shouldThrowIfPayloadHasInvalidJSONFormat() throws Exception { + public void shouldThrowIfPayloadHasInvalidJSONFormat() { exception.expect(DecodeException.class); exception.expectMessage("The token's payload had an invalid JSON format."); new JWT("eyJhbGciOiJIUzI1NiJ9.e30ijfe923.XmNK3GpH3Ys_7lyQ"); @@ -68,7 +67,7 @@ public void shouldThrowIfPayloadHasInvalidJSONFormat() throws Exception { // toString @Test - public void shouldGetStringToken() throws Exception { + public void shouldGetStringToken() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"); assertThat(jwt, is(notNullValue())); assertThat(jwt.toString(), is(notNullValue())); @@ -78,7 +77,7 @@ public void shouldGetStringToken() throws Exception { // Parts @Test - public void shouldGetHeader() throws Exception { + public void shouldGetHeader() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getHeader(), is(instanceOf(Map.class))); @@ -86,14 +85,14 @@ public void shouldGetHeader() throws Exception { } @Test - public void shouldGetSignature() throws Exception { + public void shouldGetSignature() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getSignature(), is("XmNK3GpH3Ys_7wsYBfq4C3M6goz71I7dTgUkuIa5lyQ")); } @Test - public void shouldGetEmptySignature() throws Exception { + public void shouldGetEmptySignature() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30."); assertThat(jwt, is(notNullValue())); assertThat(jwt.getSignature(), is("")); @@ -102,37 +101,37 @@ public void shouldGetEmptySignature() throws Exception { // Public Claims @Test - public void shouldGetIssuer() throws Exception { + public void shouldGetIssuer() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJKb2huIERvZSJ9.SgXosfRR_IwCgHq5lF3tlM-JHtpucWCRSaVuoHTbWbQ"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getIssuer(), is("John Doe")); } @Test - public void shouldGetNullIssuerIfMissing() throws Exception { + public void shouldGetNullIssuerIfMissing() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.something"); assertThat(jwt, is(notNullValue())); - Assert.assertThat(jwt.getIssuer(), is(nullValue())); + assertThat(jwt.getIssuer(), is(nullValue())); } @Test - public void shouldGetSubject() throws Exception { + public void shouldGetSubject() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJUb2szbnMifQ.RudAxkslimoOY3BLl2Ghny3BrUKu9I1ZrXzCZGDJtNs"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getSubject(), is("Tok3ns")); } @Test - public void shouldGetNullSubjectIfMissing() throws Exception { + public void shouldGetNullSubjectIfMissing() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.something"); assertThat(jwt, is(notNullValue())); - Assert.assertThat(jwt.getSubject(), is(nullValue())); + assertThat(jwt.getSubject(), is(nullValue())); } @Test - public void shouldGetArrayAudience() throws Exception { + public void shouldGetArrayAudience() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOlsiSG9wZSIsIlRyYXZpcyIsIlNvbG9tb24iXX0.Tm4W8WnfPjlmHSmKFakdij0on2rWPETpoM7Sh0u6-S4"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getAudience(), is(hasSize(3))); @@ -140,7 +139,7 @@ public void shouldGetArrayAudience() throws Exception { } @Test - public void shouldGetStringAudience() throws Exception { + public void shouldGetStringAudience() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJKYWNrIFJleWVzIn0.a4I9BBhPt1OB1GW67g2P1bEHgi6zgOjGUL4LvhE9Dgc"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getAudience(), is(hasSize(1))); @@ -148,15 +147,15 @@ public void shouldGetStringAudience() throws Exception { } @Test - public void shouldGetEmptyListAudienceIfMissing() throws Exception { + public void shouldGetEmptyListAudienceIfMissing() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.something"); assertThat(jwt, is(notNullValue())); - Assert.assertThat(jwt.getAudience(), IsEmptyCollection.empty()); + assertThat(jwt.getAudience(), IsEmptyCollection.empty()); } @Test - public void shouldDeserializeDatesUsingLong() throws Exception { + public void shouldDeserializeDatesUsingLong() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjIxNDc0OTM2NDcsIm5iZiI6MjE0NzQ5MzY0NywiZXhwIjoyMTQ3NDkzNjQ3LCJjdG0iOjIxNDc0OTM2NDd9.txmUJ0UCy2pqTFrEgj49eNDQCWUSW_XRMjMaRqcrgLg"); assertThat(jwt, is(notNullValue())); @@ -169,7 +168,7 @@ public void shouldDeserializeDatesUsingLong() throws Exception { } @Test - public void shouldGetExpirationTime() throws Exception { + public void shouldGetExpirationTime() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJleHAiOiIxNDc2NzI3MDg2In0.XwZztHlQwnAgmnQvrcWXJloLOUaLZGiY0HOXJCKRaks"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getExpiresAt(), is(instanceOf(Date.class))); @@ -180,15 +179,15 @@ public void shouldGetExpirationTime() throws Exception { } @Test - public void shouldGetNullExpirationTimeIfMissing() throws Exception { + public void shouldGetNullExpirationTimeIfMissing() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.something"); assertThat(jwt, is(notNullValue())); - Assert.assertThat(jwt.getExpiresAt(), is(nullValue())); + assertThat(jwt.getExpiresAt(), is(nullValue())); } @Test - public void shouldGetNotBefore() throws Exception { + public void shouldGetNotBefore() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJuYmYiOiIxNDc2NzI3MDg2In0.pi3Fi3oFiXk5A5AetDdL0hjVx_rt6F5r_YiG6HoCYDw"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getNotBefore(), is(instanceOf(Date.class))); @@ -199,15 +198,15 @@ public void shouldGetNotBefore() throws Exception { } @Test - public void shouldGetNullNotBeforeIfMissing() throws Exception { + public void shouldGetNullNotBeforeIfMissing() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.something"); assertThat(jwt, is(notNullValue())); - Assert.assertThat(jwt.getNotBefore(), is(nullValue())); + assertThat(jwt.getNotBefore(), is(nullValue())); } @Test - public void shouldGetIssuedAt() throws Exception { + public void shouldGetIssuedAt() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOiIxNDc2NzI3MDg2In0.u6BxwrO7S0sqDY8-1cUOLzU2uejAJBzQQF8g_o5BAgo"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getIssuedAt(), is(instanceOf(Date.class))); @@ -218,104 +217,105 @@ public void shouldGetIssuedAt() throws Exception { } @Test - public void shouldGetNullIssuedAtIfMissing() throws Exception { + public void shouldGetNullIssuedAtIfMissing() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.something"); assertThat(jwt, is(notNullValue())); - Assert.assertThat(jwt.getIssuedAt(), is(nullValue())); + assertThat(jwt.getIssuedAt(), is(nullValue())); } @Test - public void shouldGetId() throws Exception { + public void shouldGetId() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMjM0NTY3ODkwIn0.m3zgEfVUFOd-CvL3xG5BuOWLzb0zMQZCqiVNQQOPOvA"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getId(), is("1234567890")); } @Test - public void shouldGetNullIdIfMissing() throws Exception { + public void shouldGetNullIdIfMissing() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.something"); assertThat(jwt, is(notNullValue())); - Assert.assertThat(jwt.getId(), is(nullValue())); + assertThat(jwt.getId(), is(nullValue())); } @Test - public void shouldNotBeDeemedExpiredWithoutDateClaims() throws Exception { + public void shouldNotBeDeemedExpiredWithoutDateClaims() { JWT jwt = customTimeJWT(null, null); assertThat(jwt.isExpired(0), is(false)); } @Test - public void shouldNotBeDeemedExpired() throws Exception { + public void shouldNotBeDeemedExpired() { JWT jwt = customTimeJWT(null, new Date().getTime() + 2000); assertThat(jwt.isExpired(0), is(false)); } @Test - public void shouldBeDeemedExpired() throws Exception { + public void shouldBeDeemedExpired() { JWT jwt = customTimeJWT(null, new Date().getTime() - 2000); assertThat(jwt.isExpired(0), is(true)); } @Test - public void shouldNotBeDeemedExpiredByLeeway() throws Exception { + public void shouldNotBeDeemedExpiredByLeeway() { JWT jwt = customTimeJWT(null, new Date().getTime() - 1000); assertThat(jwt.isExpired(2), is(false)); } @Test - public void shouldBeDeemedExpiredByLeeway() throws Exception { + public void shouldBeDeemedExpiredByLeeway() { JWT jwt = customTimeJWT(null, new Date().getTime() - 2000); assertThat(jwt.isExpired(1), is(true)); } @Test - public void shouldNotBeDeemedFutureIssued() throws Exception { + public void shouldNotBeDeemedFutureIssued() { JWT jwt = customTimeJWT(new Date().getTime() - 2000, null); assertThat(jwt.isExpired(0), is(false)); } @Test - public void shouldBeDeemedFutureIssued() throws Exception { + public void shouldBeDeemedFutureIssued() { JWT jwt = customTimeJWT(new Date().getTime() + 2000, null); assertThat(jwt.isExpired(0), is(true)); } @Test - public void shouldNotBeDeemedFutureIssuedByLeeway() throws Exception { + public void shouldNotBeDeemedFutureIssuedByLeeway() { JWT jwt = customTimeJWT(new Date().getTime() + 1000, null); assertThat(jwt.isExpired(2), is(false)); } @Test - public void shouldBeDeemedFutureIssuedByLeeway() throws Exception { + public void shouldBeDeemedFutureIssuedByLeeway() { JWT jwt = customTimeJWT(new Date().getTime() + 2000, null); assertThat(jwt.isExpired(1), is(true)); } @Test - public void shouldBeDeemedNotTimeValid() throws Exception { + public void shouldBeDeemedNotTimeValid() { JWT jwt = customTimeJWT(new Date().getTime() + 1000, new Date().getTime() - 1000); assertThat(jwt.isExpired(0), is(true)); } @Test - public void shouldBeDeemedTimeValid() throws Exception { + public void shouldBeDeemedTimeValid() { JWT jwt = customTimeJWT(new Date().getTime() - 1000, new Date().getTime() + 1000); assertThat(jwt.isExpired(0), is(false)); } @Test - public void shouldThrowIfLeewayIsNegative() throws Exception { + public void shouldThrowIfLeewayIsNegative() { exception.expect(IllegalArgumentException.class); exception.expectMessage("The leeway must be a positive value."); JWT jwt = customTimeJWT(null, null); jwt.isExpired(-1); } + @SuppressWarnings("ConstantConditions") @Test - public void shouldNotRemoveKnownPublicClaimsFromTree() throws Exception { + public void shouldNotRemoveKnownPublicClaimsFromTree() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCIsInN1YiI6ImVtYWlscyIsImF1ZCI6InVzZXJzIiwiaWF0IjoxMDEwMTAxMCwiZXhwIjoxMTExMTExMSwibmJmIjoxMDEwMTAxMSwianRpIjoiaWRpZCIsInJvbGVzIjoiYWRtaW4ifQ.jCchxb-mdMTq5EpeVMSQyTp6zSwByKnfl9U-Zc9kg_w"); assertThat(jwt, is(notNullValue())); @@ -341,7 +341,7 @@ public void shouldNotRemoveKnownPublicClaimsFromTree() throws Exception { //Private Claims @Test - public void shouldGetBaseClaimIfClaimIsMissing() throws Exception { + public void shouldGetBaseClaimIfClaimIsMissing() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.K17vlwhE8FCMShdl1_65jEYqsQqBOVMPUU9IgG-QlTM"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getClaim("notExisting"), is(notNullValue())); @@ -350,7 +350,7 @@ public void shouldGetBaseClaimIfClaimIsMissing() throws Exception { } @Test - public void shouldGetClaim() throws Exception { + public void shouldGetClaim() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJvYmplY3QiOnsibmFtZSI6ImpvaG4ifX0.lrU1gZlOdlmTTeZwq0VI-pZx2iV46UWYd5-lCjy6-c4"); assertThat(jwt, is(notNullValue())); assertThat(jwt.getClaim("object"), is(notNullValue())); @@ -358,7 +358,7 @@ public void shouldGetClaim() throws Exception { } @Test - public void shouldGetAllClaims() throws Exception { + public void shouldGetAllClaims() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.eyJvYmplY3QiOnsibmFtZSI6ImpvaG4ifSwic3ViIjoiYXV0aDAifQ.U20MgOAV81c54mRelwYDJiLllb5OVwUAtMGn-eUOpTA"); assertThat(jwt, is(notNullValue())); Map claims = jwt.getClaims(); @@ -372,7 +372,7 @@ public void shouldGetAllClaims() throws Exception { } @Test - public void shouldGetEmptyAllClaims() throws Exception { + public void shouldGetEmptyAllClaims() { JWT jwt = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.ZRrHA1JJJW8opsbCGfG_HACGpVUMN_a9IV7pAx_Zmeo"); assertThat(jwt, is(notNullValue())); Map claims = jwt.getClaims(); @@ -382,8 +382,9 @@ public void shouldGetEmptyAllClaims() throws Exception { //Parcelable + @SuppressWarnings("ConstantConditions") @Test - public void shouldBeParceled() throws Exception { + public void shouldBeParceled() { JWT jwtOrigin = new JWT("eyJhbGciOiJIUzI1NiJ9.e30.K17vlwhE8FCMShdl1_65jEYqsQqBOVMPUU9IgG-QlTM"); assertThat(jwtOrigin, is(notNullValue())); @@ -435,9 +436,7 @@ private JWT customTimeJWT(@Nullable Long iatMs, @Nullable Long expMs) { private String encodeString(String source) { byte[] bytes = Base64.encode(source.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING); - String res = ""; - res = new String(bytes, StandardCharsets.UTF_8); - return res; + return new String(bytes, Charset.defaultCharset()); } } diff --git a/lib/src/test/java/com/auth0/android/jwt/UserPojo.java b/lib/src/test/java/com/auth0/android/jwt/UserPojo.java index 9812cf8..31fe500 100644 --- a/lib/src/test/java/com/auth0/android/jwt/UserPojo.java +++ b/lib/src/test/java/com/auth0/android/jwt/UserPojo.java @@ -1,10 +1,12 @@ package com.auth0.android.jwt; -public class UserPojo { - String name; - int id; +import java.util.Objects; - public UserPojo(String name, int id) { +class UserPojo { + private String name; + private int id; + + UserPojo(String name, int id) { this.name = name; this.id = id; } @@ -17,6 +19,6 @@ public boolean equals(Object o) { UserPojo userPojo = (UserPojo) o; if (id != userPojo.id) return false; - return name != null ? name.equals(userPojo.name) : userPojo.name == null; + return Objects.equals(name, userPojo.name); } }