View | Details | Raw Unified | Return to bug 55664
Collapse All | Expand All

(-)test/org/apache/tomcat/websocket/pojo/TestEncodingDecoding.java (-3 / +147 lines)
Lines 47-55 Link Here
47
import javax.websocket.server.ServerEndpointConfig;
47
import javax.websocket.server.ServerEndpointConfig;
48
48
49
49
50
50
import org.junit.Assert;
51
import org.junit.Assert;
51
import org.junit.Test;
52
import org.junit.Test;
52
53
import org.apache.catalina.Context;
53
import org.apache.catalina.Context;
54
import org.apache.catalina.servlets.DefaultServlet;
54
import org.apache.catalina.servlets.DefaultServlet;
55
import org.apache.catalina.startup.Tomcat;
55
import org.apache.catalina.startup.Tomcat;
Lines 64-69 Link Here
64
    private static final String MESSAGE_ONE = "message-one";
64
    private static final String MESSAGE_ONE = "message-one";
65
    private static final String PATH_PROGRAMMATIC_EP = "/echoProgrammaticEP";
65
    private static final String PATH_PROGRAMMATIC_EP = "/echoProgrammaticEP";
66
    private static final String PATH_ANNOTATED_EP = "/echoAnnotatedEP";
66
    private static final String PATH_ANNOTATED_EP = "/echoAnnotatedEP";
67
	private static final String PATH_GENERICS_EP = "/echoGenericsEP";
67
68
68
69
69
    @Test
70
    @Test
Lines 141-147 Link Here
141
        MsgString msg1 = new MsgString();
142
        MsgString msg1 = new MsgString();
142
        msg1.setData(MESSAGE_ONE);
143
        msg1.setData(MESSAGE_ONE);
143
        session.getBasicRemote().sendObject(msg1);
144
        session.getBasicRemote().sendObject(msg1);
144
145
        
145
        // Should not take very long
146
        // Should not take very long
146
        int i = 0;
147
        int i = 0;
147
        while (i < 20) {
148
        while (i < 20) {
Lines 172-179 Link Here
172
        i = testEvent(MsgByteEncoder.class.getName()+":destroy", i);
173
        i = testEvent(MsgByteEncoder.class.getName()+":destroy", i);
173
        i = testEvent(MsgByteDecoder.class.getName()+":destroy", i);
174
        i = testEvent(MsgByteDecoder.class.getName()+":destroy", i);
174
    }
175
    }
176
    
177
    
178
    
179
    @Test
180
    public void testGenericsCoders() throws Exception {
181
        // Set up utility classes
182
        GenericsServer server = new GenericsServer();
183
        SingletonConfigurator.setInstance(server);
184
        ServerConfigListener.setPojoClazz(GenericsServer.class);
175
185
186
        Tomcat tomcat = getTomcatInstance();
187
        // Must have a real docBase - just use temp
188
        Context ctx =
189
            tomcat.addContext("", System.getProperty("java.io.tmpdir"));
190
        ctx.addApplicationListener(new ApplicationListener(
191
                ServerConfigListener.class.getName(), false));
192
        Tomcat.addServlet(ctx, "default", new DefaultServlet());
193
        ctx.addServletMapping("/", "default");
176
194
195
        WebSocketContainer wsContainer =
196
                ContainerProvider.getWebSocketContainer();
197
198
        tomcat.start();
199
200
        GenericsClient client = new GenericsClient();
201
        URI uri = new URI("ws://localhost:" + getPort() + PATH_GENERICS_EP);
202
        Session session = wsContainer.connectToServer(client, uri);
203
        
204
        
205
        
206
        ArrayList<String> list = new ArrayList<String>(2);
207
        list.add("str1");
208
        list.add("str2");
209
        session.getBasicRemote().sendObject(list);
210
        
211
     // Should not take very long
212
        int i = 0;
213
        while (i < 20) {
214
            if (server.received.size() > 0 && client.received.size() > 0) {
215
                break;
216
            }
217
            Thread.sleep(100);
218
        }
219
220
        
221
       // Check messages were received
222
        Assert.assertEquals(1, server.received.size());
223
        Assert.assertEquals(server.received.peek().toString(), "[str1, str2]");
224
      
225
        Assert.assertEquals(1, client.received.size());
226
        Assert.assertEquals(client.received.peek().toString(), "[str1, str2]");
227
        
228
        session.close();
229
        
230
        
231
        
232
    }
233
234
177
    private int testEvent(String name, int count) throws InterruptedException {
235
    private int testEvent(String name, int count) throws InterruptedException {
178
        int i = count;
236
        int i = count;
179
        while (i < 50) {
237
        while (i < 50) {
Lines 188-193 Link Here
188
    }
246
    }
189
247
190
248
249
    @ClientEndpoint(decoders={ListStringDecoder.class},
250
            encoders={ListStringEncoder.class})
251
    public static class GenericsClient {
252
    	private Queue<Object> received = new ConcurrentLinkedQueue<>();
253
254
        @OnMessage
255
        public void rx(List<String> in) {
256
            received.add(in);
257
        }
258
    }
259
    
191
    @ClientEndpoint(decoders={MsgStringDecoder.class, MsgByteDecoder.class},
260
    @ClientEndpoint(decoders={MsgStringDecoder.class, MsgByteDecoder.class},
192
            encoders={MsgStringEncoder.class, MsgByteEncoder.class})
261
            encoders={MsgStringEncoder.class, MsgByteEncoder.class})
193
    public static class Client {
262
    public static class Client {
Lines 206-211 Link Here
206
    }
275
    }
207
276
208
277
278
    @ServerEndpoint(value=PATH_GENERICS_EP,
279
            decoders={ListStringDecoder.class},
280
            encoders={ListStringEncoder.class},
281
            configurator=SingletonConfigurator.class)
282
    public static class GenericsServer {
283
284
        private Queue<Object> received = new ConcurrentLinkedQueue<>();
285
        
286
        
287
        @OnMessage
288
        public List<String> rx(List<String> in) {
289
            received.add(in);
290
            // Echo the message back
291
            return in;
292
        }
293
    }
294
    
295
    
209
    @ServerEndpoint(value=PATH_ANNOTATED_EP,
296
    @ServerEndpoint(value=PATH_ANNOTATED_EP,
210
            decoders={MsgStringDecoder.class, MsgByteDecoder.class},
297
            decoders={MsgStringDecoder.class, MsgByteDecoder.class},
211
            encoders={MsgStringEncoder.class, MsgByteEncoder.class},
298
            encoders={MsgStringEncoder.class, MsgByteEncoder.class},
Lines 228-234 Link Here
228
            // Echo the message back
315
            // Echo the message back
229
            return in;
316
            return in;
230
        }
317
        }
231
318
        
319
        
232
        public static void addLifeCycleEvent(String event){
320
        public static void addLifeCycleEvent(String event){
233
            lifeCyclesCalled.put(event, Boolean.TRUE);
321
            lifeCyclesCalled.put(event, Boolean.TRUE);
234
        }
322
        }
Lines 412-419 Link Here
412
            return false;
500
            return false;
413
        }
501
        }
414
    }
502
    }
503
    
504
    
505
    public static class ListStringEncoder implements Encoder.Text<List<String>> {
415
506
507
        @Override
508
        public void init(EndpointConfig endpointConfig) {
509
            Server.addLifeCycleEvent(getClass().getName() + ":init");
510
        }
416
511
512
        @Override
513
        public void destroy() {
514
            Server.addLifeCycleEvent(getClass().getName() + ":destroy");
515
        }
516
517
        @Override
518
        public String encode(List<String> str) throws EncodeException {
519
        	StringBuffer sbuf = new StringBuffer();
520
        	sbuf.append("[");
521
        	for (String s: str){
522
        		sbuf.append(s).append(",");
523
        	}
524
        	sbuf.deleteCharAt(sbuf.lastIndexOf(",")).append("]");
525
        	return sbuf.toString();
526
        }
527
    }
528
    
529
    
530
    public static class ListStringDecoder implements Decoder.Text<List<String>> {
531
532
        @Override
533
        public void init(EndpointConfig endpointConfig) {
534
             Server.addLifeCycleEvent(getClass().getName() + ":init");
535
        }
536
537
        @Override
538
        public void destroy() {
539
            Server.addLifeCycleEvent(getClass().getName() + ":destroy");
540
        }
541
542
        @Override
543
        public List<String> decode(String str) throws DecodeException {
544
            List<String> lst = new ArrayList<String>(1);
545
            str = str.substring(1,str.length()-1);
546
            String[] strings = str.split(",");
547
            for (String t : strings){
548
            	lst.add(t);
549
            }
550
            return lst;
551
        }
552
553
        @Override
554
        public boolean willDecode(String str) {
555
            return str.startsWith("[") && str.endsWith("]");
556
        }
557
    }
558
    
559
560
417
    public static class ProgramaticServerEndpointConfig extends WsContextListener {
561
    public static class ProgramaticServerEndpointConfig extends WsContextListener {
418
562
419
        @Override
563
        @Override

Return to bug 55664