-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handled generic require for Date. Added ability to specify required t…
…ype on get method of claim
- Loading branch information
Showing
7 changed files
with
214 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2014 jsonwebtoken.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.jsonwebtoken; | ||
|
||
/** | ||
* Exception thrown when {@link Claims#get(String, Class)} is called and the value does not match the type of the | ||
* {@code Class} argument. | ||
* | ||
* @since 0.6 | ||
*/ | ||
public class RequiredTypeException extends JwtException { | ||
public RequiredTypeException(String message) { | ||
super(message); | ||
} | ||
|
||
public RequiredTypeException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/test/groovy/io/jsonwebtoken/RequiredTypeExceptionTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.jsonwebtoken | ||
|
||
import org.junit.Test | ||
|
||
import static org.junit.Assert.assertEquals | ||
import static org.junit.Assert.assertSame | ||
|
||
class RequiredTypeExceptionTest { | ||
@Test | ||
void testOverloadedConstructor() { | ||
def msg = 'foo' | ||
def cause = new NullPointerException() | ||
|
||
def ex = new RequiredTypeException(msg, cause) | ||
|
||
assertEquals ex.message, msg | ||
assertSame ex.cause, cause | ||
} | ||
|
||
} |
78 changes: 78 additions & 0 deletions
78
src/test/groovy/io/jsonwebtoken/impl/DefaultClaimsTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (C) 2015 jsonwebtoken.io | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.jsonwebtoken.impl | ||
|
||
import io.jsonwebtoken.Claims | ||
import io.jsonwebtoken.RequiredTypeException | ||
import org.junit.Before | ||
import org.junit.Test | ||
import static org.junit.Assert.* | ||
|
||
class DefaultClaimsTest { | ||
|
||
Claims claims | ||
|
||
@Before | ||
void setup() { | ||
claims = new DefaultClaims() | ||
} | ||
|
||
@Test | ||
void testGetClaimWithRequiredType_Null_Success() { | ||
claims.put("aNull", null) | ||
Object result = claims.get("aNull", Integer.class) | ||
assertNull(result) | ||
} | ||
|
||
@Test | ||
void testGetClaimWithRequiredType_Exception() { | ||
claims.put("anInteger", new Integer(5)) | ||
try { | ||
claims.get("anInteger", String.class) | ||
fail() | ||
} catch (RequiredTypeException e) { | ||
assertEquals( | ||
"Expected value to be of type: class java.lang.String, but was class java.lang.Integer", | ||
e.getMessage() | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
void testGetClaimWithRequiredType_Success() { | ||
claims.put("anInteger", new Integer(5)) | ||
Object result = claims.get("anInteger", Integer.class) | ||
|
||
assertTrue(result instanceof Integer) | ||
} | ||
|
||
@Test | ||
void testGetClaimWithRequiredType_Date_Success() { | ||
def actual = new Date(); | ||
claims.put("aDate", actual) | ||
Date expected = claims.get("aDate", Date.class); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
void testGetClaimWithRequiredType_DateWithLong_Success() { | ||
def actual = new Date(); | ||
// note that Long is stored in claim | ||
claims.put("aDate", actual.getTime()) | ||
Date expected = claims.get("aDate", Date.class); | ||
assertEquals(expected, actual); | ||
} | ||
} |