KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > MessageCompressionTest


1 /**
2  *
3  * Copyright 2005-2006 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.command;
18
19 import java.net.URI JavaDoc;
20
21 import javax.jms.JMSException JavaDoc;
22 import javax.jms.MessageConsumer JavaDoc;
23 import javax.jms.MessageProducer JavaDoc;
24 import javax.jms.Session JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.apache.activemq.ActiveMQConnection;
29 import org.apache.activemq.ActiveMQConnectionFactory;
30 import org.apache.activemq.broker.BrokerService;
31 import org.apache.activemq.broker.TransportConnector;
32
33 public class MessageCompressionTest extends TestCase {
34     
35     protected BrokerService broker;
36     private ActiveMQQueue queue;
37     private static final String JavaDoc BROKER_URL = "tcp://localhost:61216";
38
39     // The following text should compress well
40
private static final String JavaDoc TEXT =
41          "The quick red fox jumped over the lazy brown dog. " +
42          "The quick red fox jumped over the lazy brown dog. " +
43          "The quick red fox jumped over the lazy brown dog. " +
44          "The quick red fox jumped over the lazy brown dog. " +
45          "The quick red fox jumped over the lazy brown dog. " +
46          "The quick red fox jumped over the lazy brown dog. " +
47          "The quick red fox jumped over the lazy brown dog. " +
48          "The quick red fox jumped over the lazy brown dog. " +
49          "The quick red fox jumped over the lazy brown dog. " +
50          "The quick red fox jumped over the lazy brown dog. " +
51          "The quick red fox jumped over the lazy brown dog. " +
52          "The quick red fox jumped over the lazy brown dog. " +
53          "The quick red fox jumped over the lazy brown dog. " +
54          "The quick red fox jumped over the lazy brown dog. " +
55          "The quick red fox jumped over the lazy brown dog. " +
56          "The quick red fox jumped over the lazy brown dog. " +
57          "The quick red fox jumped over the lazy brown dog. ";
58     
59     protected void setUp() throws Exception JavaDoc {
60         broker = new BrokerService();
61         
62         TransportConnector tc = new TransportConnector();
63         tc.setUri(new URI JavaDoc(BROKER_URL));
64         tc.setName("tcp");
65
66         queue = new ActiveMQQueue("TEST."+System.currentTimeMillis());
67         
68         broker.addConnector(tc);
69         broker.start();
70
71     }
72     
73     protected void tearDown() throws Exception JavaDoc {
74         if (broker != null) {
75             broker.stop();
76         }
77     }
78     
79     public void testTextMessageCompression() throws Exception JavaDoc {
80         
81         ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(BROKER_URL);
82         factory.setUseCompression(true);
83         sendTestMessage(factory, TEXT);
84         ActiveMQTextMessage message = receiveTestMessage(factory);
85         int compressedSize = message.getContent().getLength();
86         
87         factory = new ActiveMQConnectionFactory(BROKER_URL);
88         factory.setUseCompression(false);
89         sendTestMessage(factory, TEXT);
90         message = receiveTestMessage(factory);
91         int unCompressedSize = message.getContent().getLength();
92
93         assertTrue("expected: compressed Size '"+compressedSize+"' < unCompressedSize '"+unCompressedSize+"'", compressedSize < unCompressedSize);
94     }
95
96     private void sendTestMessage(ActiveMQConnectionFactory factory, String JavaDoc message) throws JMSException JavaDoc {
97         ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
98         Session JavaDoc session = connection.createSession(false, 0);
99         MessageProducer JavaDoc producer = session.createProducer(queue);
100         producer.send(session.createTextMessage(message));
101         connection.close();
102     }
103     
104     private ActiveMQTextMessage receiveTestMessage(ActiveMQConnectionFactory factory) throws JMSException JavaDoc {
105         ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
106         connection.start();
107         Session JavaDoc session = connection.createSession(false, 0);
108         MessageConsumer JavaDoc consumer = session.createConsumer(queue);
109         ActiveMQTextMessage rc = (ActiveMQTextMessage) consumer.receive();
110         connection.close();
111         return rc;
112     }
113     
114     
115 // public void testJavaUtilZip() throws Exception {
116
// String str = "When the going gets weird, the weird turn pro.";
117
// byte[] bytes = str.getBytes();
118
//
119
// ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
120
// DeflaterOutputStream dos = new DeflaterOutputStream(baos);
121
// dos.
122
// }
123
}
124
Popular Tags