Skip to content

Commit

Permalink
[RESTEASY-1662] (#1178)
Browse files Browse the repository at this point in the history
* Add QueryParam char primitive tests

Signed-off-by: NicoNes <nicolas.nesmon@gmail.com>

* Add HeaderParam char primitive tests

Signed-off-by: NicoNes <nicolas.nesmon@gmail.com>

* Add UriParam char primitive tests

Signed-off-by: NicoNes <nicolas.nesmon@gmail.com>

* Add MatrixParam char primitive tests

Signed-off-by: NicoNes <nicolas.nesmon@gmail.com>

* Handle String conversion to char and Character

Signed-off-by: NicoNes <nicolas.nesmon@gmail.com>
  • Loading branch information
NicoNes authored and asoldano committed Jul 12, 2017
1 parent 1b5363e commit 202e213
Show file tree
Hide file tree
Showing 68 changed files with 599 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
@SuppressWarnings(value = "unchecked")
public class StringParameterInjector
{
private static final ParamConverter<Character> characterParamConverter = new ParamConverter<Character>() {

@Override
public Character fromString(String value) {
if(value != null && value.length() == 1)
{
return value.charAt(0);
}
return null;
}

@Override
public String toString(Character value) {
return null;
}

};

protected Class type;
protected Class baseType;
protected Type baseGenericType;
Expand Down Expand Up @@ -218,7 +236,14 @@ else if (valueOf == null)
}
if (valueOf == null)
{
throw new RuntimeException(Messages.MESSAGES.unableToFindConstructor(getParamSignature(), target, baseType.getName()));
if(Character.class.equals(baseType))
{
paramConverter = characterParamConverter;
}
else
{
throw new RuntimeException(Messages.MESSAGES.unableToFindConstructor(getParamSignature(), target, baseType.getName()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ public static Object stringToPrimitiveBoxType(Class primitiveType, String value)
if (value == null) return Boolean.FALSE;
return Boolean.valueOf(value);
}
else if (primitiveType.equals(char.class))
{
if(value==null)
{
return Character.valueOf(Character.MIN_VALUE);
}
else if (value.length() != 1) {
throw new IllegalArgumentException();
}
return Character.valueOf(value.charAt(0));
}
if (value == null) value = "0";
if (primitiveType.equals(int.class)) return Integer.valueOf(value);
if (primitiveType.equals(long.class)) return Long.valueOf(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,42 @@ public void testGetDoublePrimitiveWrappersDefault() {
public void testGetDoublePrimitiveListDefault() {
testListDefault("double", "3.14159265358979");
}

/**
* @tpTestDetails Basic test for char
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetChar() {
basicTest("char", "a");
}

/**
* @tpTestDetails Basic test for char, test default value
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitivesDefault() {
testDefault("char", "a");
}

/**
* @tpTestDetails Basic test for char, use wrapper
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitiveWrappersDefault() {
testWrappersDefault("char", "a");
}

/**
* @tpTestDetails Basic test for list of char, do not use wrapper
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitiveListDefault() {
testListDefault("char", "a");
}

/**
* @tpTestDetails Negative test for int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,43 @@ public void testGetDoublePrimitiveListDefault() {
testListDefault("double", "3.14159265358979");
testArrayDefault("double", "3.14159265358979");
}

/**
* @tpTestDetails Basic test for char
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetChar() {
basicTest("char", "a");
}

/**
* @tpTestDetails Test default value of char
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitivesDefault() {
testDefault("char", "a");
}

/**
* @tpTestDetails Test default value of char with wrapper
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitiveWrappersDefault() {
testWrappersDefault("char", "a");
}

/**
* @tpTestDetails Test default value of list and array of char
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitiveListDefault() {
testListDefault("char", "a");
testArrayDefault("char", "a");
}

/**
* @tpTestDetails Test wrong data stored in int format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,51 @@ public void testGetDoublePrimitiveWrappersDefault() {
public void testGetDoublePrimitiveListDefault() {
testListDefault("double", "3.14159265358979");
}

/**
* @tpTestDetails Test char primitive object for get method
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetChar() {
basicTest("char", "a");
}

/**
* @tpTestDetails Test char primitive object
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitivesDefault() {
testDefault("char", "a");
}

/**
* @tpTestDetails Test char primitive object with proxy
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitiveWrappersDefault() {
testWrappersDefault("char", "a");
}

/**
* @tpTestDetails Test char primitive objects in list
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitiveListDefault() {
testListDefault("char", "a");
}

/**
* @tpTestDetails Test char primitive objects in array
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetCharPrimitiveArrayDefault() {
testArrayDefault("char", "a");
}

/**
* @tpTestDetails Negative testing: accept only int, but string is provided. Error is excepted.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.jboss.resteasy.test.resource.param;


import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
Expand All @@ -13,6 +16,8 @@
import org.jboss.resteasy.test.resource.param.resource.UriParamAsPrimitiveResourceUriByte;
import org.jboss.resteasy.test.resource.param.resource.UriParamAsPrimitiveResourceUriByteInterface;
import org.jboss.resteasy.test.resource.param.resource.UriParamAsPrimitiveResourceUriByteWrapper;
import org.jboss.resteasy.test.resource.param.resource.UriParamAsPrimitiveResourceUriChar;
import org.jboss.resteasy.test.resource.param.resource.UriParamAsPrimitiveResourceUriCharWrapper;
import org.jboss.resteasy.test.resource.param.resource.UriParamAsPrimitiveResourceUriDouble;
import org.jboss.resteasy.test.resource.param.resource.UriParamAsPrimitiveResourceUriDoubleWrapper;
import org.jboss.resteasy.test.resource.param.resource.UriParamAsPrimitiveResourceUriFloat;
Expand All @@ -34,9 +39,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;

/**
* @tpSubChapter Parameters
* @tpChapter Integration tests
Expand Down Expand Up @@ -73,13 +75,15 @@ public static Archive<?> deploy() throws Exception {
UriParamAsPrimitiveResourceUriLong.class,
UriParamAsPrimitiveResourceUriFloat.class,
UriParamAsPrimitiveResourceUriDouble.class,
UriParamAsPrimitiveResourceUriChar.class,
UriParamAsPrimitiveResourceUriBooleanWrapper.class,
UriParamAsPrimitiveResourceUriByteWrapper.class,
UriParamAsPrimitiveResourceUriShortWrapper.class,
UriParamAsPrimitiveResourceUriIntWrapper.class,
UriParamAsPrimitiveResourceUriLongWrapper.class,
UriParamAsPrimitiveResourceUriFloatWrapper.class,
UriParamAsPrimitiveResourceUriDoubleWrapper.class);
UriParamAsPrimitiveResourceUriDoubleWrapper.class,
UriParamAsPrimitiveResourceUriCharWrapper.class);
}

private static String generateURL(String path) {
Expand Down Expand Up @@ -184,4 +188,13 @@ public void testGetFloat() {
public void testGetDouble() {
basicTest("double", "3.14159265358979");
}

/**
* @tpTestDetails Test char primitive object
* @tpSince RESTEasy 3.0.16
*/
@Test
public void testGetChar() {
basicTest("char", "a");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public interface HeaderParamsAsPrimitivesDefaultNullProxy {
@GET
@Produces("application/double")
String doGet();

@GET
@Produces("application/char")
String doGetChar();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public interface HeaderParamsAsPrimitivesDefaultOverrideProxy {
@GET
@Produces("application/double")
String doGet(@HeaderParam("double") @DefaultValue("0.0") double v);

@GET
@Produces("application/char")
String doGet(@HeaderParam("char") @DefaultValue("b") char v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public interface HeaderParamsAsPrimitivesDefaultProxy {
@GET
@Produces("application/double")
String doGetDouble();

@GET
@Produces("application/char")
String doGetChar();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public interface HeaderParamsAsPrimitivesListDefaultNullProxy {
@GET
@Produces("application/double")
String doGetDouble();

@GET
@Produces("application/char")
String doGetCharacter();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@ public interface HeaderParamsAsPrimitivesListDefaultOverrideProxy {
@GET
@Produces("application/double")
String doGetDouble(@HeaderParam("double") @DefaultValue("0.0") List<Double> v);

@GET
@Produces("application/char")
String doGetCharacter(@HeaderParam("char") @DefaultValue("b") List<Character> v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public interface HeaderParamsAsPrimitivesListDefaultProxy {
@GET
@Produces("application/double")
String doGetDouble();

@GET
@Produces("application/char")
String doGetCharacter();
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ public interface HeaderParamsAsPrimitivesListProxy {
@GET
@Produces("application/double")
String doGetDouble(@HeaderParam("double") List<Double> v);

@GET
@Produces("application/char")
String doGetCharacter(@HeaderParam("char") List<Character> v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public interface HeaderParamsAsPrimitivesPrimitivesProxy {
@GET
@Produces("application/double")
String doGet(@HeaderParam("double") double v);

@GET
@Produces("application/char")
String doGet(@HeaderParam("char") char v);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@ public String doGet(@HeaderParam("double") @DefaultValue("3.14159265358979") dou
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 3.14159265358979d, v, 0.0);
return "content";
}

@GET
@Produces("application/char")
public String doGet(@HeaderParam("char") @DefaultValue("a") char v) {
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 'a', v);
return "content";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jboss.resteasy.test.resource.param.HeaderParamsAsPrimitivesTest;
import org.junit.Assert;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -58,4 +59,11 @@ public String doGet(@HeaderParam("double") double v) {
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 0.0d, v, 0.0);
return "content";
}

@GET
@Produces("application/char")
public String doGet(@HeaderParam("char") char v) {
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, Character.MIN_VALUE, v);
return "content";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,11 @@ public String doGet(@HeaderParam("double") @DefaultValue("0.0") double v) {
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 3.14159265358979d, v, 0.0);
return "content";
}

@GET
@Produces("application/char")
public String doGet(@HeaderParam("char") @DefaultValue("b") char v) {
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 'a', v);
return "content";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@ public String doGetDouble(@HeaderParam("double") List<Double> v) {
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 3.14159265358979d, v.get(2).doubleValue(), 0.0);
return "content";
}

@GET
@Produces("application/char")
public String doGetCharacter(@HeaderParam("char") List<Character> v) {
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 'a', v.get(0).charValue());
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 'a', v.get(1).charValue());
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 'a', v.get(2).charValue());
return "content";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@ public String doGetDouble(@HeaderParam("double") @DefaultValue("3.14159265358979
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 3.14159265358979d, v.get(0).doubleValue(), 0.0);
return "content";
}

@GET
@Produces("application/char")
public String doGetCharacter(@HeaderParam("char") @DefaultValue("a") List<Character> v) {
Assert.assertEquals(HeaderParamsAsPrimitivesTest.ERROR_MESSAGE, 'a', v.get(0).charValue());
return "content";
}
}

0 comments on commit 202e213

Please sign in to comment.