2
* Copyright 2005-2012 Restlet S.A.S.
4
* The contents of this file are subject to the terms of one of the following
5
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
6
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
7
* not use this file except in compliance with one of these Licenses.
9
* You can obtain a copy of the Apache 2.0 license at
10
* http://www.opensource.org/licenses/apache-2.0
12
* You can obtain a copy of the LGPL 3.0 license at
13
* http://www.opensource.org/licenses/lgpl-3.0
15
* You can obtain a copy of the LGPL 2.1 license at
16
* http://www.opensource.org/licenses/lgpl-2.1
18
* You can obtain a copy of the CDDL 1.0 license at
19
* http://www.opensource.org/licenses/cddl1
21
* You can obtain a copy of the EPL 1.0 license at
22
* http://www.opensource.org/licenses/eclipse-1.0
24
* See the Licenses for the specific language governing permissions and
25
* limitations under the Licenses.
27
* Alternatively, you can obtain a royalty free commercial license with less
28
* limitations, transferable or non-transferable, directly at
29
* http://www.restlet.com/products/restlet-framework
31
* Restlet is a registered trademark of Restlet S.A.S.
34
package org.restlet.test.ext.jaxb;
36
import java.util.ArrayList;
37
import java.util.Arrays;
38
import java.util.List;
40
import org.restlet.test.RestletTestCase;
42
import org.restlet.Application;
43
import org.restlet.Client;
44
import org.restlet.Component;
45
import org.restlet.Context;
46
import org.restlet.Restlet;
47
import org.restlet.Server;
48
import org.restlet.data.MediaType;
49
import org.restlet.data.Method;
50
import org.restlet.data.Preference;
51
import org.restlet.data.Protocol;
52
import org.restlet.ext.jaxb.JaxbRepresentation;
53
import org.restlet.Request;
54
import org.restlet.Response;
55
import org.restlet.resource.ClientResource;
56
import org.restlet.resource.Get;
57
import org.restlet.resource.Post;
58
import org.restlet.resource.Put;
59
import org.restlet.resource.ServerResource;
60
import org.restlet.routing.Router;
63
* Simple Integration Tests that uses the JAXB Converter to perform POST, PUT
66
* Note: You must have registered the JaxbConverter in
67
* META-INF/services/org.restlet.engine.converter.ConverterHelper
69
* @author Sanjay Acharya
71
public class JaxbIntegrationConverterTestCase extends RestletTestCase {
72
private static final String IN_STRING = "foo";
74
private static final String HELLO_OUT_STRING = "Hello World " + IN_STRING;
76
private Component component;
80
public void setUp() throws Exception {
82
this.component = new Component();
83
final Server server = this.component.getServers().add(Protocol.HTTP, 0);
84
final Application application = createApplication(this.component);
85
this.component.getDefaultHost().attach(application);
86
this.component.start();
87
uri = "http://localhost:" + server.getEphemeralPort() + "/test";
90
public void tearDown() throws Exception {
92
if (component != null) {
96
this.component = null;
99
protected Application createApplication(Component component) {
100
final Application application = new Application() {
102
public Restlet createInboundRoot() {
103
final Router router = new Router(getContext());
104
router.attach("/test", SampleResource.class);
113
* Test POST, PUT and GET using the Client class
117
public void testIntegration() throws Exception {
118
Client client = new Client(new Context(), Arrays.asList(Protocol.HTTP));
119
Request request = new Request(Method.POST, uri);
120
request.setEntity(new JaxbRepresentation<Sample>(new Sample(IN_STRING)));
122
Response response = client.handle(request);
124
JaxbRepresentation<Sample> resultRepresentation = new JaxbRepresentation<Sample>(
125
response.getEntity(), Sample.class);
126
Sample sample = resultRepresentation.getObject();
127
assertEquals(HELLO_OUT_STRING, sample.getVal());
129
request = new Request(Method.PUT, uri);
130
request.setEntity(new JaxbRepresentation<Sample>(new Sample(IN_STRING)));
132
response = client.handle(request);
133
resultRepresentation = new JaxbRepresentation<Sample>(
134
response.getEntity(), Sample.class);
135
sample = resultRepresentation.getObject();
136
assertEquals(HELLO_OUT_STRING, sample.getVal());
138
request = new Request(Method.GET, uri);
139
response = client.handle(request);
140
resultRepresentation = new JaxbRepresentation<Sample>(
141
response.getEntity(), Sample.class);
142
sample = resultRepresentation.getObject();
143
assertEquals(IN_STRING, sample.getVal());
149
* Test POST, PUT and GET using the ClientResource class
153
public void testWithClientResource() throws Exception {
154
ClientResource sampleResource = new ClientResource(uri);
155
List<Preference<MediaType>> m = new ArrayList<Preference<MediaType>>();
156
m.add(new Preference<MediaType>(MediaType.APPLICATION_XML));
157
sampleResource.getClientInfo().setAcceptedMediaTypes(m);
159
Sample sample = new Sample(IN_STRING);
160
sample = sampleResource.post(sample, Sample.class);
161
assertEquals(HELLO_OUT_STRING, sample.getVal());
163
sampleResource.put(sample);
164
assertTrue(sampleResource.getStatus().isSuccess());
166
sample = sampleResource.put(sample, Sample.class);
167
assertEquals(HELLO_OUT_STRING, sample.getVal());
169
sample = sampleResource.get(Sample.class);
170
assertEquals(IN_STRING, sample.getVal());
173
public static class SampleResource extends ServerResource {
175
public Sample post(Sample sample) {
176
assertNotNull(sample);
177
return new Sample(HELLO_OUT_STRING);
181
public Sample getSample() {
182
return new Sample(IN_STRING);
186
public JaxbRepresentation<Sample> putSample(Sample sample) {
187
assertNotNull(sample);
188
return new JaxbRepresentation<Sample>(new Sample(HELLO_OUT_STRING));