Skip to content

Commit 54a2719

Browse files
committed
Fixed and improved tests for jaxrs client and singleton due to TomEE failures.
1 parent 3195a28 commit 54a2719

File tree

7 files changed

+70
-70
lines changed

7 files changed

+70
-70
lines changed

jaxrs/jaxrs-client/src/test/java/org/javaee7/jaxrs/client/MyResourceTest.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import java.io.StringReader;
56
import java.net.MalformedURLException;
67
import java.net.URI;
78
import java.net.URL;
89

10+
import javax.json.Json;
11+
import javax.json.JsonArray;
12+
import javax.json.JsonObject;
13+
import javax.json.JsonReader;
914
import javax.ws.rs.client.Client;
1015
import javax.ws.rs.client.ClientBuilder;
1116
import javax.ws.rs.client.Entity;
@@ -132,7 +137,15 @@ public void test4Delete() {
132137
@Test
133138
public void test5ClientSideNegotiation() {
134139
String json = target.request().accept(MediaType.APPLICATION_JSON).get(String.class);
135-
assertEquals("[{\"name\":\"Penny\",\"age\":1},{\"name\":\"Leonard\",\"age\":2},{\"name\":\"Sheldon\",\"age\":3}]", json);
140+
141+
JsonReader reader = Json.createReader(new StringReader(json));
142+
JsonArray jsonArray = reader.readArray();
143+
assertEquals(1, jsonArray.getJsonObject(0).getInt("age"));
144+
assertEquals("Penny", jsonArray.getJsonObject(0).getString("name"));
145+
assertEquals(2, jsonArray.getJsonObject(1).getInt("age"));
146+
assertEquals("Leonard", jsonArray.getJsonObject(1).getString("name"));
147+
assertEquals(3, jsonArray.getJsonObject(2).getInt("age"));
148+
assertEquals("Sheldon", jsonArray.getJsonObject(2).getString("name"));
136149
}
137150

138151
@Test

jaxrs/singleton/src/main/java/org/javaee7/jaxrs/singleton/AnnotatedSingletonResource.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
public class AnnotatedSingletonResource {
2222
// Ideally this state should be stored in a database
2323
// But this is a singleton resource and so state can be saved here too
24-
List<String> strings;
24+
private List<String> strings;
2525

2626
public AnnotatedSingletonResource() {
2727
strings = new ArrayList<>();
28-
System.out.println("******* init");
2928
}
3029

3130
@GET

jaxrs/singleton/src/main/java/org/javaee7/jaxrs/singleton/ApplicationSingletonResource.java

+3-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
package org.javaee7.jaxrs.singleton;
22

3+
import javax.ws.rs.*;
4+
import javax.ws.rs.core.MediaType;
35
import java.util.ArrayList;
46
import java.util.List;
5-
import javax.ws.rs.Consumes;
6-
import javax.ws.rs.DELETE;
7-
import javax.ws.rs.GET;
8-
import javax.ws.rs.POST;
9-
import javax.ws.rs.PUT;
10-
import javax.ws.rs.Path;
11-
import javax.ws.rs.PathParam;
12-
import javax.ws.rs.Produces;
13-
import javax.ws.rs.core.MediaType;
147

158
/**
169
* @author Arun Gupta
@@ -19,7 +12,7 @@
1912
public class ApplicationSingletonResource {
2013
// Ideally this state should be stored in a database
2114
// But this is a singleton resource and so state can be saved here too
22-
List<String> strings;
15+
private List<String> strings;
2316

2417
public ApplicationSingletonResource() {
2518
strings = new ArrayList<>();

jaxrs/singleton/src/main/java/org/javaee7/jaxrs/singleton/MyAnnotatedApplication.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@
77
* @author Arun Gupta
88
*/
99
@ApplicationPath("webresources")
10-
public class MyAnnotatedApplication extends Application {
11-
}
10+
public class MyAnnotatedApplication extends Application {}

jaxrs/singleton/src/main/java/org/javaee7/jaxrs/singleton/MyApplication.java

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*/
1010
@ApplicationPath("webresources")
1111
public class MyApplication extends Application {
12-
1312
@Override
1413
public Set<Object> getSingletons() {
1514
Set<Object> resources = new java.util.HashSet<>();
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,60 @@
11
package org.javaee7.jaxrs.singleton;
22

3-
import static org.junit.Assert.assertEquals;
4-
5-
import java.net.MalformedURLException;
6-
import java.net.URI;
7-
import java.net.URL;
8-
import java.util.StringTokenizer;
9-
10-
import javax.ws.rs.client.Client;
11-
import javax.ws.rs.client.ClientBuilder;
12-
import javax.ws.rs.client.Entity;
13-
import javax.ws.rs.client.WebTarget;
14-
153
import org.jboss.arquillian.container.test.api.Deployment;
164
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.arquillian.junit.InSequence;
176
import org.jboss.arquillian.test.api.ArquillianResource;
187
import org.jboss.shrinkwrap.api.ShrinkWrap;
198
import org.jboss.shrinkwrap.api.spec.WebArchive;
209
import org.junit.After;
2110
import org.junit.Before;
22-
import org.junit.FixMethodOrder;
2311
import org.junit.Test;
2412
import org.junit.runner.RunWith;
25-
import org.junit.runners.MethodSorters;
13+
14+
import javax.ws.rs.client.Client;
15+
import javax.ws.rs.client.ClientBuilder;
16+
import javax.ws.rs.client.Entity;
17+
import javax.ws.rs.client.WebTarget;
18+
import java.net.MalformedURLException;
19+
import java.net.URI;
20+
import java.net.URL;
21+
import java.util.StringTokenizer;
22+
23+
import static org.junit.Assert.assertEquals;
2624

2725
/**
2826
* @author Arun Gupta
2927
*/
3028
@RunWith(Arquillian.class)
31-
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
3229
public class AnnotatedSingletonResourceTest {
33-
3430
@ArquillianResource
3531
private URL base;
3632

37-
Client client;
38-
WebTarget target;
33+
private Client client;
34+
private WebTarget target;
3935

4036
@Before
4137
public void setUp() throws MalformedURLException {
4238
client = ClientBuilder.newClient();
4339
target = client.target(URI.create(new URL(base, "webresources/annotated").toExternalForm()));
44-
// target = client.target("http://localhost:8080/singleton/webresources/annotated");
4540
}
4641

4742
@After
4843
public void tearDown() {
4944
client.close();
5045
}
5146

52-
@Deployment(testable=false)
47+
@Deployment(testable = false)
5348
public static WebArchive createDeployment() {
5449
return ShrinkWrap.create(WebArchive.class)
55-
.addClasses(
56-
MyAnnotatedApplication.class,
57-
AnnotatedSingletonResource.class);
50+
.addClasses(
51+
MyAnnotatedApplication.class,
52+
AnnotatedSingletonResource.class);
5853
}
5954

6055
@Test
61-
public void test1Post() {
56+
@InSequence(1)
57+
public void testPost() {
6258
target.request().post(Entity.text("pineapple"));
6359
target.request().post(Entity.text("mango"));
6460
target.request().post(Entity.text("kiwi"));
@@ -71,13 +67,15 @@ public void test1Post() {
7167
}
7268

7369
@Test
74-
public void test2Get() {
70+
@InSequence(2)
71+
public void testGet() {
7572
String response = target.path("2").request().get(String.class);
7673
assertEquals("kiwi", response);
7774
}
7875

7976
@Test
80-
public void test3Delete() {
77+
@InSequence(3)
78+
public void testDelete() {
8179
target.path("kiwi").request().delete();
8280

8381
String list = target.request().get(String.class);
@@ -86,12 +84,12 @@ public void test3Delete() {
8684
}
8785

8886
@Test
89-
public void test4Put() {
87+
@InSequence(4)
88+
public void testPut() {
9089
target.request().put(Entity.text("apple"));
9190

9291
String list = target.request().get(String.class);
9392
StringTokenizer tokens = new StringTokenizer(list, ",");
9493
assertEquals(4, tokens.countTokens());
9594
}
96-
9795
}
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
11
package org.javaee7.jaxrs.singleton;
22

3-
import static org.junit.Assert.assertEquals;
4-
5-
import java.net.MalformedURLException;
6-
import java.net.URI;
7-
import java.net.URL;
8-
import java.util.StringTokenizer;
9-
10-
import javax.ws.rs.client.Client;
11-
import javax.ws.rs.client.ClientBuilder;
12-
import javax.ws.rs.client.Entity;
13-
import javax.ws.rs.client.WebTarget;
14-
153
import org.jboss.arquillian.container.test.api.Deployment;
164
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.arquillian.junit.InSequence;
176
import org.jboss.arquillian.test.api.ArquillianResource;
187
import org.jboss.shrinkwrap.api.ShrinkWrap;
198
import org.jboss.shrinkwrap.api.spec.WebArchive;
209
import org.junit.After;
2110
import org.junit.Before;
22-
import org.junit.FixMethodOrder;
2311
import org.junit.Test;
2412
import org.junit.runner.RunWith;
25-
import org.junit.runners.MethodSorters;
13+
14+
import javax.ws.rs.client.Client;
15+
import javax.ws.rs.client.ClientBuilder;
16+
import javax.ws.rs.client.Entity;
17+
import javax.ws.rs.client.WebTarget;
18+
import java.net.MalformedURLException;
19+
import java.net.URI;
20+
import java.net.URL;
21+
import java.util.StringTokenizer;
22+
23+
import static org.junit.Assert.assertEquals;
2624

2725
/**
2826
* @author Arun Gupta
2927
*/
3028
@RunWith(Arquillian.class)
31-
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
3229
public class ApplicationSingletonResourceTest {
33-
3430
@ArquillianResource
3531
private URL base;
3632

37-
Client client;
38-
WebTarget target;
33+
private Client client;
34+
private WebTarget target;
3935

4036
@Before
4137
public void setUp() throws MalformedURLException {
@@ -51,13 +47,14 @@ public void tearDown() {
5147
@Deployment(testable = false)
5248
public static WebArchive createDeployment() {
5349
return ShrinkWrap.create(WebArchive.class)
54-
.addClasses(
55-
MyApplication.class,
56-
ApplicationSingletonResource.class);
50+
.addClasses(
51+
MyApplication.class,
52+
ApplicationSingletonResource.class);
5753
}
5854

5955
@Test
60-
public void test1Post() {
56+
@InSequence(1)
57+
public void testPost() {
6158
target.request().post(Entity.text("pineapple"));
6259
target.request().post(Entity.text("mango"));
6360
target.request().post(Entity.text("kiwi"));
@@ -69,13 +66,15 @@ public void test1Post() {
6966
}
7067

7168
@Test
72-
public void test2Get() {
69+
@InSequence(2)
70+
public void testGet() {
7371
String response = target.path("2").request().get(String.class);
7472
assertEquals("kiwi", response);
7573
}
7674

7775
@Test
78-
public void test3Delete() {
76+
@InSequence(3)
77+
public void testDelete() {
7978
target.path("kiwi").request().delete();
8079

8180
String list = target.request().get(String.class);
@@ -84,12 +83,12 @@ public void test3Delete() {
8483
}
8584

8685
@Test
87-
public void test4Put() {
86+
@InSequence(4)
87+
public void testPut() {
8888
target.request().put(Entity.text("apple"));
8989

9090
String list = target.request().get(String.class);
9191
StringTokenizer tokens = new StringTokenizer(list, ",");
9292
assertEquals(4, tokens.countTokens());
9393
}
94-
9594
}

0 commit comments

Comments
 (0)