KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > streams > JMSInputStreamTest


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

18 package org.apache.activemq.streams;
19 import java.io.DataInputStream JavaDoc;
20 import java.io.DataOutputStream JavaDoc;
21
22 import javax.jms.Destination JavaDoc;
23
24 import junit.framework.Test;
25
26 import org.apache.activemq.ActiveMQConnection;
27 import org.apache.activemq.JmsTestSupport;
28 import org.apache.activemq.command.ActiveMQQueue;
29 import org.apache.activemq.command.ActiveMQTopic;
30
31 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
32
33 /**
34  * JMSInputStreamTest
35  */

36 public class JMSInputStreamTest extends JmsTestSupport {
37     
38     protected DataOutputStream JavaDoc out;
39     protected DataInputStream JavaDoc in;
40     private ActiveMQConnection connection2;
41
42     public Destination JavaDoc destination;
43     
44     public static Test suite() {
45         return suite(JMSInputStreamTest.class);
46     }
47
48     public static void main(String JavaDoc[] args) {
49         junit.textui.TestRunner.run(suite());
50     }
51     
52     public void initCombos() {
53         addCombinationValues("destination", new Object JavaDoc[] {
54                 new ActiveMQQueue("TEST.QUEUE"),
55                 new ActiveMQTopic("TEST.TOPIC") });
56     }
57
58     /*
59      * @see TestCase#setUp()
60      */

61     protected void setUp() throws Exception JavaDoc {
62         super.setAutoFail(true);
63         super.setUp();
64         connection2 = (ActiveMQConnection) factory.createConnection(userName, password);
65         connections.add(connection2);
66         out = new DataOutputStream JavaDoc(connection.createOutputStream(destination));
67         in = new DataInputStream JavaDoc(connection2.createInputStream(destination));
68     }
69
70     /*
71      * @see TestCase#tearDown()
72      */

73     protected void tearDown() throws Exception JavaDoc {
74         super.tearDown();
75     }
76
77     public void testStreams() throws Exception JavaDoc {
78         out.writeInt(4);
79         out.flush();
80         assertTrue(in.readInt() == 4);
81         out.writeFloat(2.3f);
82         out.flush();
83         assertTrue(in.readFloat() == 2.3f);
84         String JavaDoc str = "this is a test string";
85         out.writeUTF(str);
86         out.flush();
87         assertTrue(in.readUTF().equals(str));
88         for (int i = 0;i < 100;i++) {
89             out.writeLong(i);
90         }
91         out.flush();
92         for (int i = 0;i < 100;i++) {
93             assertTrue(in.readLong() == i);
94         }
95     }
96
97     public void testLarge() throws Exception JavaDoc {
98         final int TEST_DATA = 23;
99         final int DATA_LENGTH = 4096;
100         final int COUNT = 1024;
101         byte[] data = new byte[DATA_LENGTH];
102         for (int i = 0;i < data.length;i++) {
103             data[i] = TEST_DATA;
104         }
105         final AtomicBoolean JavaDoc complete = new AtomicBoolean JavaDoc(false);
106         Thread JavaDoc runner = new Thread JavaDoc(new Runnable JavaDoc() {
107             public void run() {
108                 try {
109                     for (int x = 0;x < COUNT;x++) {
110                         byte[] b = new byte[2048];
111                         in.readFully(b);
112                         for (int i = 0;i < b.length;i++) {
113                             assertTrue(b[i] == TEST_DATA);
114                         }
115                     }
116                     complete.set(true);
117                     synchronized(complete){
118                         complete.notify();
119                     }
120                 }
121                 catch (Exception JavaDoc ex) {
122                     ex.printStackTrace();
123                 }
124             }
125         });
126         runner.start();
127         for (int i = 0;i < COUNT;i++) {
128             out.write(data);
129         }
130         out.flush();
131         synchronized (complete) {
132             if (!complete.get()) {
133                 complete.wait(30000);
134             }
135         }
136         assertTrue(complete.get());
137     }
138 }
139
Popular Tags