Skip to content

Commit c75f92c

Browse files
committed
Merge pull request #110 from aslakknutsen/jaxrs-samples2
Jaxrs samples2
2 parents 56031d8 + e2f69c5 commit c75f92c

4 files changed

Lines changed: 78 additions & 19 deletions

File tree

jaxrs/mapping-exceptions/src/test/java/org/javaee7/jaxrs/mapping/exceptions/MyResourceTest.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,50 @@
66

77
package org.javaee7.jaxrs.mapping.exceptions;
88

9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.fail;
11+
12+
import java.net.MalformedURLException;
13+
import java.net.URL;
14+
915
import javax.ws.rs.ClientErrorException;
1016
import javax.ws.rs.client.Client;
1117
import javax.ws.rs.client.ClientBuilder;
1218
import javax.ws.rs.client.WebTarget;
19+
20+
import org.jboss.arquillian.container.test.api.Deployment;
21+
import org.jboss.arquillian.junit.Arquillian;
22+
import org.jboss.arquillian.test.api.ArquillianResource;
23+
import org.jboss.shrinkwrap.api.ShrinkWrap;
24+
import org.jboss.shrinkwrap.api.spec.WebArchive;
1325
import org.junit.Before;
1426
import org.junit.Test;
15-
import static org.junit.Assert.*;
27+
import org.junit.runner.RunWith;
1628

1729
/**
1830
*
1931
* @author argupta
2032
*/
33+
@RunWith(Arquillian.class)
2134
public class MyResourceTest {
22-
WebTarget target;
35+
36+
@Deployment(testable = false)
37+
public static WebArchive createDeployment() {
38+
return ShrinkWrap.create(WebArchive.class)
39+
.addClasses(
40+
MyApplication.class, MyResource.class,
41+
OrderNotFoundException.class, OrderNotFoundExceptionMapper.class);
42+
}
43+
@ArquillianResource
44+
private URL base;
45+
46+
private WebTarget target;
2347

2448
@Before
25-
public void setUp() {
49+
public void setUp() throws MalformedURLException {
2650
Client client = ClientBuilder.newClient();
2751
target = client
28-
.target("http://localhost:8080/mapping-exceptions/webresources/order");
52+
.target(new URL(base, "webresources/order").toExternalForm());
2953
}
3054

3155
/**

jaxrs/server-negotiation/src/main/java/org/javaee7/jaxrs/server/negotiation/MyResource.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
package org.javaee7.jaxrs.server.negotiation;
4141

42+
import java.util.List;
43+
4244
import javax.ws.rs.GET;
4345
import javax.ws.rs.Path;
4446
import javax.ws.rs.Produces;
@@ -51,11 +53,11 @@ public class MyResource {
5153
@GET
5254
@Produces({"application/xml; qs=0.75", "application/json; qs=1.0"})
5355
// @Produces({"application/xml", "application/json"})
54-
public Person[] getList() {
55-
Person[] list = new Person[3];
56-
list[0] = new Person("Penny", 1);
57-
list[1] = new Person("Leonard", 2);
58-
list[2] = new Person("Sheldon", 3);
56+
public List<Person> getList() {
57+
People list = new People();
58+
list.add(new Person("Penny", 1));
59+
list.add(new Person("Leonard", 2));
60+
list.add(new Person("Sheldon", 3));
5961

6062
return list;
6163
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.javaee7.jaxrs.server.negotiation;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.xml.bind.annotation.XmlElement;
7+
import javax.xml.bind.annotation.XmlRootElement;
8+
9+
@XmlRootElement
10+
public class People extends ArrayList<Person> {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
@XmlElement(name = "person")
15+
public List<Person> getPeople() {
16+
return this;
17+
}
18+
}

jaxrs/server-negotiation/src/test/java/org/javaee7/jaxrs/server/negotiation/MyResourceTest.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,48 @@
77
package org.javaee7.jaxrs.server.negotiation;
88

99
import java.io.IOException;
10+
import java.net.MalformedURLException;
11+
import java.net.URL;
12+
1013
import javax.ws.rs.client.Client;
1114
import javax.ws.rs.client.ClientBuilder;
1215
import javax.ws.rs.client.WebTarget;
16+
1317
import org.custommonkey.xmlunit.XMLAssert;
18+
import org.jboss.arquillian.container.test.api.Deployment;
19+
import org.jboss.arquillian.junit.Arquillian;
20+
import org.jboss.arquillian.test.api.ArquillianResource;
21+
import org.jboss.shrinkwrap.api.ShrinkWrap;
22+
import org.jboss.shrinkwrap.api.spec.WebArchive;
1423
import org.json.JSONException;
15-
import org.junit.After;
16-
import org.junit.AfterClass;
1724
import org.junit.Before;
18-
import org.junit.BeforeClass;
1925
import org.junit.Test;
20-
import static org.junit.Assert.*;
26+
import org.junit.runner.RunWith;
2127
import org.skyscreamer.jsonassert.JSONAssert;
2228
import org.skyscreamer.jsonassert.JSONCompareMode;
2329
import org.xml.sax.SAXException;
2430

2531
/**
2632
* @author Arun Gupta
2733
*/
34+
@RunWith(Arquillian.class)
2835
public class MyResourceTest {
2936

30-
WebTarget target;
31-
37+
@Deployment(testable = false)
38+
public static WebArchive createDeployment() {
39+
return ShrinkWrap.create(WebArchive.class)
40+
.addClasses(MyApplication.class, MyResource.class, People.class, Person.class);
41+
}
42+
43+
@ArquillianResource
44+
private URL base;
45+
46+
private WebTarget target;
47+
3248
@Before
33-
public void setUp() {
49+
public void setUp() throws MalformedURLException {
3450
Client client = ClientBuilder.newClient();
35-
target = client
36-
.target("http://localhost:8080/server-negotiation/webresources/persons");
51+
target = client.target(new URL(base, "webresources/persons").toExternalForm());
3752
}
3853

3954
@Test
@@ -55,7 +70,7 @@ public void testJson2() throws JSONException {
5570
@Test
5671
public void testXml() throws JSONException, SAXException, IOException {
5772
String response = target.request().accept("application/xml").get(String.class);
58-
XMLAssert.assertXMLEqual("<collection><person><age>1</age><name>Penny</name></person><person><age>2</age><name>Leonard</name></person><person><age>3</age><name>Sheldon</name></person></collection>",
73+
XMLAssert.assertXMLEqual("<people><person><age>1</age><name>Penny</name></person><person><age>2</age><name>Leonard</name></person><person><age>3</age><name>Sheldon</name></person></people>",
5974
response);
6075
}
6176

0 commit comments

Comments
 (0)