Skip to content

Commit

Permalink
RESTEASY-1137: In resteasy-validator-provider-11,
Browse files Browse the repository at this point in the history
ResteasyViolationException is now a subclass of
javax.validation.ConstraintViolationException.
  • Loading branch information
ronsigal committed Jul 22, 2015
1 parent be75806 commit 04eb0e9
Show file tree
Hide file tree
Showing 13 changed files with 794 additions and 135 deletions.
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jboss.resteasy.resteasy1137;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import javax.ws.rs.ext.Provider;

/**
*
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
* @version $Revision: 1.1 $
*
* Copyright Jul 20, 2015
*/
@Provider
@ApplicationPath("/test")
public class TestApplication extends Application
{
}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jboss.resteasy.resteasy1137;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;
import javax.ws.rs.Path;

@Path("/")
@Documented
@Constraint(validatedBy = TestClassValidator.class)
@Target({TYPE})
@Retention(RUNTIME)
public @interface TestClassConstraint
{
String message() default "Concatenation of s and t must have length > {value}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
int value();
}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jboss.resteasy.resteasy1137;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class TestClassValidator implements ConstraintValidator<TestClassConstraint, TestResource>
{
int length;

public void initialize(TestClassConstraint constraintAnnotation)
{
length = constraintAnnotation.value();
}

public boolean isValid(TestResource value, ConstraintValidatorContext context)
{
boolean b = value.retrieveS().length() + value.getT().length() >= length;
return b;
}
}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.jboss.resteasy.resteasy1137;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="testReport")
@XmlAccessorType(XmlAccessType.FIELD)
public class TestReport
{
private int fieldViolations;
private int propertyViolations;
private int classViolations;
private int parameterViolations;
private int returnValueViolations;
public int getFieldViolations()
{
return fieldViolations;
}
public void setFieldViolations(int fieldViolations)
{
this.fieldViolations = fieldViolations;
}
public int getPropertyViolations()
{
return propertyViolations;
}
public void setPropertyViolations(int propertyViolations)
{
this.propertyViolations = propertyViolations;
}
public int getClassViolations()
{
return classViolations;
}
public void setClassViolations(int classViolations)
{
this.classViolations = classViolations;
}
public int getParameterViolations()
{
return parameterViolations;
}
public void setParameterViolations(int parameterViolations)
{
this.parameterViolations = parameterViolations;
}
public int getReturnValueViolations()
{
return returnValueViolations;
}
public void setReturnValueViolations(int returnValueViolations)
{
this.returnValueViolations = returnValueViolations;
}
}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jboss.resteasy.resteasy1137;

import javax.validation.constraints.Size;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

@Path("all")
@TestClassConstraint(5)
public class TestResource
{
@Size(min=2, max=4)
@PathParam("s")
String s;

private String t;

@Size(min=3, max=5)
public String getT()
{
return t;
}

public String retrieveS()
{
return s;
}

@PathParam("t")
public void setT(String t)
{
this.t = t;
}

@GET
@Path("{s}/{t}/{u}")
@Size(max=3)
public String test(@Size(min=4, max=6) @PathParam("u") String u)
{
return s + t + u;
}
}
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* This package (along with @see org.jboss.resteasy.test.resteasy1137) tests versioning compatibility
* of the class org.jboss.resteasy.api.validation.ResteasyViolationException in module resteasy-validator-provider11.
*
* As of release 3.0.12.Final, ResteasyViolationException was changed from a subclass of javax.validation.ValidationException
* to a subclass of javax.validation.ConstraintViolationException, which is a subclass of javax.validation.ValidationException.
*
* The jar validation-versioning.jar in src/test/resources/1137 contains the class
* org.jboss.resteasy.test.validation.versioning.CustomExceptionMapper, with the method
*
* <p>
* <pre>
* {@code
* public Response toResponse(ResteasyViolationException rve);
* }
* </pre>
* <p>
*
* which was compiled with the previous version of ResteasyViolationException. The test in these two packages shows
* that the two versions of ResteasyViolationException are binary compatible.
*
*/
package org.jboss.resteasy.resteasy1137;
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.jboss.resteasy.test.resteasy1137;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.resteasy.resteasy1137.TestApplication;
import org.jboss.resteasy.resteasy1137.TestClassConstraint;
import org.jboss.resteasy.resteasy1137.TestClassValidator;
import org.jboss.resteasy.resteasy1137.TestReport;
import org.jboss.resteasy.resteasy1137.TestResource;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* RESTEASY-1137
*
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
* @version $Revision: 1.1 $
*
* Copyright Jul 20, 2015
*/
@RunWith(Arquillian.class)
public class TestCustomExceptionMapper
{
@Deployment
public static Archive<?> createTestArchive()
{
WebArchive war = ShrinkWrap.create(WebArchive.class, "RESTEASY-1137.war")
.addClasses(TestApplication.class, TestResource.class)
.addClasses(TestClassConstraint.class, TestClassValidator.class, TestReport.class)
.addAsLibrary("1137/validation-versioning.jar", "validation-versioning.jar")
.addAsWebInfResource("1137/web.xml", "web.xml")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
;
System.out.println(war.toString(true));
return war;
}

@Test
public void testExceptionMapperInputViolations() throws Exception
{
Client client = ClientBuilder.newClient();
Builder builder = client.target("http://localhost:8080/RESTEASY-1137/test/all/a/b/c").request();
builder.accept(MediaType.APPLICATION_XML);
Response response = builder.get();
System.out.println("status: " + response.getStatus());
Assert.assertEquals(444, response.getStatus());
TestReport report = response.readEntity(TestReport.class);
countViolations(report, 1, 1, 1, 1, 0);
}

@Test
public void testExceptionMapperOutputViolations() throws Exception
{
Client client = ClientBuilder.newClient();
Builder builder = client.target("http://localhost:8080/RESTEASY-1137/test/all/abc/defg/hijkl").request();
builder.accept(MediaType.APPLICATION_XML);
Response response = builder.get();
System.out.println("status: " + response.getStatus());
Assert.assertEquals(444, response.getStatus());
TestReport report = response.readEntity(TestReport.class);
countViolations(report, 0, 0, 0, 0, 1);
}


protected boolean countViolations(TestReport report, int fieldCount, int propertyCount, int classCount, int parameterCount, int returnValueCount)
{
return report.getFieldViolations() == fieldCount
&& report.getPropertyViolations() == propertyCount
&& report.getClassViolations() == classCount
&& report.getParameterViolations() == parameterCount
&& report.getReturnValueViolations() == returnValueCount;
}
}
Binary file not shown.
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,5 @@
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

</web-app>
Loading

0 comments on commit 04eb0e9

Please sign in to comment.