Skip to content

Commit 995175a

Browse files
committed
Adding an interim version of meged singleton tests - annotated and application
1 parent 86962eb commit 995175a

File tree

6 files changed

+469
-0
lines changed

6 files changed

+469
-0
lines changed

jaxrs/singleton/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>jaxrs-samples</artifactId>
6+
<groupId>org.javaee7.jaxrs</groupId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.jaxrs</groupId>
12+
<artifactId>singleton</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
<name>singleton</name>
16+
</project>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.jaxrs.singleton;
41+
42+
import java.util.ArrayList;
43+
import java.util.List;
44+
import javax.inject.Singleton;
45+
import javax.ws.rs.Consumes;
46+
import javax.ws.rs.DELETE;
47+
import javax.ws.rs.GET;
48+
import javax.ws.rs.POST;
49+
import javax.ws.rs.PUT;
50+
import javax.ws.rs.Path;
51+
import javax.ws.rs.PathParam;
52+
import javax.ws.rs.Produces;
53+
import javax.ws.rs.core.MediaType;
54+
55+
/**
56+
* @author Arun Gupta
57+
*/
58+
@Singleton
59+
@Path("annotated")
60+
public class AnnotatedSingletonResource {
61+
// Ideally this state should be stored in a database
62+
// But this is a singleton resource and so state can be saved here too
63+
List<String> strings;
64+
65+
public AnnotatedSingletonResource() {
66+
strings = new ArrayList<>();
67+
System.out.println("******* init");
68+
}
69+
70+
@GET
71+
@Produces(MediaType.TEXT_PLAIN)
72+
public String getAll() {
73+
return strings.toString();
74+
}
75+
76+
@GET
77+
@Produces(MediaType.TEXT_PLAIN)
78+
@Path("{id}")
79+
public String getString(@PathParam("id")int id) {
80+
return strings.get(id);
81+
}
82+
83+
@POST
84+
@Consumes(MediaType.TEXT_PLAIN)
85+
public void postString(String content) {
86+
strings.add(content);
87+
}
88+
89+
@PUT
90+
@Consumes(MediaType.TEXT_PLAIN)
91+
public void putToList(String content) {
92+
strings.add(content);
93+
}
94+
95+
@DELETE
96+
@Path("{content}")
97+
public void deleteFromList(@PathParam("content") String content) {
98+
if (strings.contains(content))
99+
strings.remove(content);
100+
}
101+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.jaxrs.singleton;
41+
42+
import java.util.ArrayList;
43+
import java.util.List;
44+
import javax.ws.rs.Consumes;
45+
import javax.ws.rs.DELETE;
46+
import javax.ws.rs.GET;
47+
import javax.ws.rs.POST;
48+
import javax.ws.rs.PUT;
49+
import javax.ws.rs.Path;
50+
import javax.ws.rs.PathParam;
51+
import javax.ws.rs.Produces;
52+
import javax.ws.rs.core.MediaType;
53+
54+
/**
55+
* @author Arun Gupta
56+
*/
57+
@Path("application")
58+
public class ApplicationSingletonResource {
59+
// Ideally this state should be stored in a database
60+
// But this is a singleton resource and so state can be saved here too
61+
List<String> strings;
62+
63+
public ApplicationSingletonResource() {
64+
strings = new ArrayList<>();
65+
}
66+
67+
@GET
68+
@Produces(MediaType.TEXT_PLAIN)
69+
public String getAll() {
70+
return strings.toString();
71+
}
72+
73+
@GET
74+
@Produces(MediaType.TEXT_PLAIN)
75+
@Path("{id}")
76+
public String getString(@PathParam("id")int id) {
77+
return strings.get(id);
78+
}
79+
80+
@POST
81+
@Consumes(MediaType.TEXT_PLAIN)
82+
public void postString(String content) {
83+
strings.add(content);
84+
}
85+
86+
@PUT
87+
@Consumes(MediaType.TEXT_PLAIN)
88+
public void putToList(String content) {
89+
strings.add(content);
90+
}
91+
92+
@DELETE
93+
@Path("{content}")
94+
public void deleteFromList(@PathParam("content") String content) {
95+
if (strings.contains(content))
96+
strings.remove(content);
97+
}
98+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.jaxrs.singleton;
41+
42+
import java.util.Set;
43+
import javax.ws.rs.ApplicationPath;
44+
import javax.ws.rs.core.Application;
45+
46+
/**
47+
* @author Arun Gupta
48+
*/
49+
@ApplicationPath("webresources")
50+
public class MyApplication extends Application {
51+
52+
// @Override
53+
// public Set<Object> getSingletons() {
54+
// Set<Object> resources = new java.util.HashSet<>();
55+
// resources.add(new ApplicationSingletonResource());
56+
// return resources;
57+
// }
58+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package org.javaee7.jaxrs.singleton;
7+
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
10+
import java.util.StringTokenizer;
11+
import javax.ws.rs.client.Client;
12+
import javax.ws.rs.client.ClientBuilder;
13+
import javax.ws.rs.client.Entity;
14+
import javax.ws.rs.client.WebTarget;
15+
import org.jboss.arquillian.container.test.api.Deployment;
16+
import org.jboss.arquillian.junit.Arquillian;
17+
import org.jboss.arquillian.test.api.ArquillianResource;
18+
import org.jboss.shrinkwrap.api.ShrinkWrap;
19+
import org.jboss.shrinkwrap.api.spec.WebArchive;
20+
import org.junit.After;
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import static org.junit.Assert.*;
24+
import org.junit.FixMethodOrder;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.MethodSorters;
27+
28+
/**
29+
* @author Arun Gupta
30+
*/
31+
@RunWith(Arquillian.class)
32+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
33+
public class AnnotatedSingletonResourceTest {
34+
35+
@ArquillianResource
36+
private URL base;
37+
38+
Client client;
39+
WebTarget target;
40+
41+
@Before
42+
public void setUp() throws MalformedURLException {
43+
client = ClientBuilder.newClient();
44+
target = client.target(new URL(base, "webresources/annotated").toExternalForm());
45+
// target = client.target("http://localhost:8080/singleton/webresources/annotated");
46+
}
47+
48+
@After
49+
public void tearDown() {
50+
client.close();
51+
}
52+
53+
@Deployment(testable=false)
54+
public static WebArchive createDeployment() {
55+
return ShrinkWrap.create(WebArchive.class)
56+
.addClasses(
57+
MyApplication.class,
58+
AnnotatedSingletonResource.class,
59+
ApplicationSingletonResource.class);
60+
}
61+
62+
@Test
63+
public void test1Post() {
64+
target.request().post(Entity.text("pineapple"));
65+
target.request().post(Entity.text("mango"));
66+
target.request().post(Entity.text("kiwi"));
67+
target.request().post(Entity.text("passion fruit"));
68+
69+
String list = target.request().get(String.class);
70+
System.out.println("--> " + list);
71+
StringTokenizer tokens = new StringTokenizer(list, ",");
72+
assertEquals(4, tokens.countTokens());
73+
}
74+
75+
// @Test
76+
public void test2Get() {
77+
String response = target.path("2").request().get(String.class);
78+
assertEquals("kiwi", response);
79+
}
80+
81+
// @Test
82+
public void test3Delete() {
83+
target.path("kiwi").request().delete();
84+
85+
String list = target.request().get(String.class);
86+
StringTokenizer tokens = new StringTokenizer(list, ",");
87+
assertEquals(3, tokens.countTokens());
88+
}
89+
90+
// @Test
91+
public void test4Put() {
92+
target.request().put(Entity.text("apple"));
93+
94+
String list = target.request().get(String.class);
95+
StringTokenizer tokens = new StringTokenizer(list, ",");
96+
assertEquals(4, tokens.countTokens());
97+
}
98+
99+
}

0 commit comments

Comments
 (0)