~ubuntu-branches/ubuntu/saucy/restlet/saucy

« back to all changes in this revision

Viewing changes to org.restlet.test/src/org/restlet/test/ext/jaxb/JaxbIntegrationConverterTestCase.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 16:25:45 UTC
  • Revision ID: package-import@ubuntu.com-20120611162545-5w2o0resi5y3pybc
Tags: upstream-2.0.14
ImportĀ upstreamĀ versionĀ 2.0.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2005-2012 Restlet S.A.S.
 
3
 * 
 
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.
 
8
 * 
 
9
 * You can obtain a copy of the Apache 2.0 license at
 
10
 * http://www.opensource.org/licenses/apache-2.0
 
11
 * 
 
12
 * You can obtain a copy of the LGPL 3.0 license at
 
13
 * http://www.opensource.org/licenses/lgpl-3.0
 
14
 * 
 
15
 * You can obtain a copy of the LGPL 2.1 license at
 
16
 * http://www.opensource.org/licenses/lgpl-2.1
 
17
 * 
 
18
 * You can obtain a copy of the CDDL 1.0 license at
 
19
 * http://www.opensource.org/licenses/cddl1
 
20
 * 
 
21
 * You can obtain a copy of the EPL 1.0 license at
 
22
 * http://www.opensource.org/licenses/eclipse-1.0
 
23
 * 
 
24
 * See the Licenses for the specific language governing permissions and
 
25
 * limitations under the Licenses.
 
26
 * 
 
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
 
30
 * 
 
31
 * Restlet is a registered trademark of Restlet S.A.S.
 
32
 */
 
33
 
 
34
package org.restlet.test.ext.jaxb;
 
35
 
 
36
import java.util.ArrayList;
 
37
import java.util.Arrays;
 
38
import java.util.List;
 
39
 
 
40
import org.restlet.test.RestletTestCase;
 
41
 
 
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;
 
61
 
 
62
/**
 
63
 * Simple Integration Tests that uses the JAXB Converter to perform POST, PUT
 
64
 * and GET operations.
 
65
 * 
 
66
 * Note: You must have registered the JaxbConverter in
 
67
 * META-INF/services/org.restlet.engine.converter.ConverterHelper
 
68
 * 
 
69
 * @author Sanjay Acharya
 
70
 */
 
71
public class JaxbIntegrationConverterTestCase extends RestletTestCase {
 
72
    private static final String IN_STRING = "foo";
 
73
 
 
74
    private static final String HELLO_OUT_STRING = "Hello World " + IN_STRING;
 
75
 
 
76
    private Component component;
 
77
 
 
78
    private String uri;
 
79
 
 
80
    public void setUp() throws Exception {
 
81
        super.setUp();
 
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";
 
88
    }
 
89
 
 
90
    public void tearDown() throws Exception {
 
91
        super.tearDown();
 
92
        if (component != null) {
 
93
            component.stop();
 
94
        }
 
95
 
 
96
        this.component = null;
 
97
    }
 
98
 
 
99
    protected Application createApplication(Component component) {
 
100
        final Application application = new Application() {
 
101
            @Override
 
102
            public Restlet createInboundRoot() {
 
103
                final Router router = new Router(getContext());
 
104
                router.attach("/test", SampleResource.class);
 
105
                return router;
 
106
            }
 
107
        };
 
108
 
 
109
        return application;
 
110
    }
 
111
 
 
112
    /**
 
113
     * Test POST, PUT and GET using the Client class
 
114
     * 
 
115
     * @throws Exception
 
116
     */
 
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)));
 
121
 
 
122
        Response response = client.handle(request);
 
123
 
 
124
        JaxbRepresentation<Sample> resultRepresentation = new JaxbRepresentation<Sample>(
 
125
                response.getEntity(), Sample.class);
 
126
        Sample sample = resultRepresentation.getObject();
 
127
        assertEquals(HELLO_OUT_STRING, sample.getVal());
 
128
 
 
129
        request = new Request(Method.PUT, uri);
 
130
        request.setEntity(new JaxbRepresentation<Sample>(new Sample(IN_STRING)));
 
131
 
 
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());
 
137
 
 
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());
 
144
 
 
145
        client.stop();
 
146
    }
 
147
 
 
148
    /**
 
149
     * Test POST, PUT and GET using the ClientResource class
 
150
     * 
 
151
     * @throws Exception
 
152
     */
 
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);
 
158
 
 
159
        Sample sample = new Sample(IN_STRING);
 
160
        sample = sampleResource.post(sample, Sample.class);
 
161
        assertEquals(HELLO_OUT_STRING, sample.getVal());
 
162
 
 
163
        sampleResource.put(sample);
 
164
        assertTrue(sampleResource.getStatus().isSuccess());
 
165
 
 
166
        sample = sampleResource.put(sample, Sample.class);
 
167
        assertEquals(HELLO_OUT_STRING, sample.getVal());
 
168
 
 
169
        sample = sampleResource.get(Sample.class);
 
170
        assertEquals(IN_STRING, sample.getVal());
 
171
    }
 
172
 
 
173
    public static class SampleResource extends ServerResource {
 
174
        @Post("xml")
 
175
        public Sample post(Sample sample) {
 
176
            assertNotNull(sample);
 
177
            return new Sample(HELLO_OUT_STRING);
 
178
        }
 
179
 
 
180
        @Get("xml")
 
181
        public Sample getSample() {
 
182
            return new Sample(IN_STRING);
 
183
        }
 
184
 
 
185
        @Put("xml:xml")
 
186
        public JaxbRepresentation<Sample> putSample(Sample sample) {
 
187
            assertNotNull(sample);
 
188
            return new JaxbRepresentation<Sample>(new Sample(HELLO_OUT_STRING));
 
189
        }
 
190
    }
 
191
}