KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > transport > tcp > WireformatNegociationTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.transport.tcp;
18
19 import java.io.IOException JavaDoc;
20 import java.net.URI JavaDoc;
21 import java.net.URISyntaxException JavaDoc;
22
23 import org.apache.activemq.CombinationTestSupport;
24 import org.apache.activemq.command.CommandTypes;
25 import org.apache.activemq.command.WireFormatInfo;
26 import org.apache.activemq.openwire.OpenWireFormat;
27 import org.apache.activemq.transport.Transport;
28 import org.apache.activemq.transport.TransportAcceptListener;
29 import org.apache.activemq.transport.TransportFactory;
30 import org.apache.activemq.transport.TransportListener;
31 import org.apache.activemq.transport.TransportServer;
32
33 import javax.net.SocketFactory;
34
35 import java.util.concurrent.CountDownLatch JavaDoc;
36 import java.util.concurrent.TimeUnit JavaDoc;
37 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
38 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
39 import java.util.concurrent.atomic.AtomicReference JavaDoc;
40
41
42 public class WireformatNegociationTest extends CombinationTestSupport {
43     
44     private TransportServer server;
45     private Transport clientTransport;
46     private Transport serverTransport;
47     
48     private final AtomicReference JavaDoc<WireFormatInfo> clientWF = new AtomicReference JavaDoc<WireFormatInfo>();
49     private final AtomicReference JavaDoc<WireFormatInfo> serverWF = new AtomicReference JavaDoc<WireFormatInfo>();
50     private final AtomicReference JavaDoc<Exception JavaDoc> asyncError = new AtomicReference JavaDoc<Exception JavaDoc>();
51     private final AtomicBoolean JavaDoc ignoreAsycError = new AtomicBoolean JavaDoc();
52     
53     private final CountDownLatch JavaDoc negociationCounter = new CountDownLatch JavaDoc(2);
54             
55     protected void setUp() throws Exception JavaDoc {
56         super.setUp();
57     }
58
59     /**
60      * @throws Exception
61      * @throws URISyntaxException
62      */

63     private void startClient(String JavaDoc uri) throws Exception JavaDoc, URISyntaxException JavaDoc {
64         clientTransport = TransportFactory.connect(new URI JavaDoc(uri));
65         clientTransport.setTransportListener(new TransportListener() {
66             public void onCommand(Object JavaDoc command) {
67                 if( command instanceof WireFormatInfo ) {
68                     clientWF.set((WireFormatInfo) command);
69                     negociationCounter.countDown();
70                 }
71             }
72             public void onException(IOException JavaDoc error) {
73                 if( !ignoreAsycError.get() ) {
74                     log.info("Client transport error: ", error);
75                     asyncError.set(error);
76                     negociationCounter.countDown();
77                 }
78             }
79             public void transportInterupted() {
80             }
81             public void transportResumed() {
82             }});
83         clientTransport.start();
84     }
85
86     /**
87      * @throws IOException
88      * @throws URISyntaxException
89      * @throws Exception
90      */

91     private void startServer(String JavaDoc uri ) throws IOException JavaDoc, URISyntaxException JavaDoc, Exception JavaDoc {
92         server = TransportFactory.bind("localhost", new URI JavaDoc(uri));
93         server.setAcceptListener(new TransportAcceptListener(){
94             public void onAccept(Transport transport) {
95                 try {
96                     log.info("["+getName()+"] Server Accepted a Connection");
97                     serverTransport = transport;
98                     serverTransport.setTransportListener(new TransportListener() {
99                         public void onCommand(Object JavaDoc command) {
100                             if( command instanceof WireFormatInfo ) {
101                                 serverWF.set((WireFormatInfo) command);
102                                 negociationCounter.countDown();
103                             }
104                         }
105                         public void onException(IOException JavaDoc error) {
106                             if( !ignoreAsycError.get() ) {
107                                 log.info("Server transport error: ", error);
108                                 asyncError.set(error);
109                                 negociationCounter.countDown();
110                             }
111                         }
112                         public void transportInterupted() {
113                         }
114                         public void transportResumed() {
115                         }});
116                     serverTransport.start();
117                 } catch (Exception JavaDoc e) {
118                     e.printStackTrace();
119                 }
120             }
121
122             public void onAcceptError(Exception JavaDoc error) {
123                 error.printStackTrace();
124             }
125         });
126         server.start();
127     }
128     
129     protected void tearDown() throws Exception JavaDoc {
130         ignoreAsycError.set(true);
131         try {
132             if( clientTransport!=null )
133                 clientTransport.stop();
134             if( serverTransport!=null )
135                 serverTransport.stop();
136             if( server!=null )
137                 server.stop();
138         } catch (Throwable JavaDoc e) {
139             e.printStackTrace();
140         }
141         super.tearDown();
142     }
143     
144     
145     /**
146      * @throws Exception
147      */

148     public void testWireFomatInfoSeverVersion1() throws Exception JavaDoc {
149         
150         startServer("tcp://localhost:61616?wireFormat.version=1");
151         startClient("tcp://localhost:61616");
152         
153         assertTrue("Connect timeout", negociationCounter.await(10, TimeUnit.SECONDS));
154         assertNull("Async error: "+asyncError, asyncError.get());
155         
156         assertNotNull(clientWF.get());
157         assertEquals(1, clientWF.get().getVersion());
158         
159         assertNotNull(serverWF.get());
160         assertEquals(1, serverWF.get().getVersion());
161     }
162     
163     /**
164      * @throws Exception
165      */

166     public void testWireFomatInfoClientVersion1() throws Exception JavaDoc {
167         
168         startServer("tcp://localhost:61616");
169         startClient("tcp://localhost:61616?wireFormat.version=1");
170         
171         assertTrue("Connect timeout", negociationCounter.await(10, TimeUnit.SECONDS));
172         assertNull("Async error: "+asyncError, asyncError.get());
173         
174         assertNotNull(clientWF.get());
175         assertEquals(1, clientWF.get().getVersion());
176         
177         assertNotNull(serverWF.get());
178         assertEquals(1, serverWF.get().getVersion());
179     }
180
181     /**
182      * @throws Exception
183      */

184     public void testWireFomatInfoCurrentVersion() throws Exception JavaDoc {
185         
186         startServer("tcp://localhost:61616");
187         startClient("tcp://localhost:61616");
188         
189         assertTrue("Connect timeout", negociationCounter.await(10, TimeUnit.SECONDS));
190         assertNull("Async error: "+asyncError, asyncError.get());
191         
192         assertNotNull(clientWF.get());
193         assertEquals(CommandTypes.PROTOCOL_VERSION, clientWF.get().getVersion());
194         
195         assertNotNull(serverWF.get());
196         assertEquals(CommandTypes.PROTOCOL_VERSION, serverWF.get().getVersion());
197     }
198
199 }
200
Popular Tags