source: trunk/src/test/java/omq/test/calculator/CalculatorTest.java @ 84

Last change on this file since 84 was 84, checked in by stoda, 11 years ago

Default queues added, default exchange enabled, more control in remote queues added.
Tests verified and changed Persistent test to show how to make persistent messages.

File size: 3.5 KB
Line 
1package omq.test.calculator;
2
3import static org.junit.Assert.assertEquals;
4
5import java.util.Arrays;
6import java.util.Collection;
7import java.util.Properties;
8
9import omq.common.broker.Broker;
10import omq.common.util.ParameterQueue;
11import omq.common.util.Serializer;
12
13import org.junit.After;
14import org.junit.BeforeClass;
15import org.junit.Test;
16import org.junit.runner.RunWith;
17import org.junit.runners.Parameterized;
18import org.junit.runners.Parameterized.Parameters;
19
20/**
21 *
22 * @author Sergi Toda <sergi.toda@estudiants.urv.cat>
23 *
24 */
25@RunWith(value = Parameterized.class)
26public class CalculatorTest {
27
28        private static Broker broker;
29        private static Calculator remoteCalc;
30        private static Calculator remoteCalc2;
31
32        public CalculatorTest(String type) throws Exception {
33                Properties env = new Properties();
34                env.setProperty(ParameterQueue.USER_NAME, "guest");
35                env.setProperty(ParameterQueue.USER_PASS, "guest");
36
37                // Set host info of rabbimq (where it is)
38                env.setProperty(ParameterQueue.RABBIT_HOST, "127.0.0.1");
39                env.setProperty(ParameterQueue.RABBIT_PORT, "5672");
40                env.setProperty(ParameterQueue.DURABLE_QUEUE, "false");
41                env.setProperty(ParameterQueue.PROXY_SERIALIZER, type);
42                env.setProperty(ParameterQueue.ENABLE_COMPRESSION, "false");
43
44                // Set info about where the message will be sent
45                env.setProperty(ParameterQueue.RPC_EXCHANGE, "rpc_exchange");
46
47                // Set info about the queue & the exchange where the ResponseListener
48                // will listen to.
49                env.setProperty(ParameterQueue.RPC_REPLY_QUEUE, "reply_queue");
50
51                broker = new Broker(env);
52                remoteCalc = broker.lookup("calculator1", Calculator.class);
53                remoteCalc2 = broker.lookup("calculator2", Calculator.class);
54        }
55
56        @Parameters
57        public static Collection<Object[]> data() {
58                Object[][] data = new Object[][] { { Serializer.JAVA }, { Serializer.GSON }, { Serializer.KRYO } };
59                return Arrays.asList(data);
60        }
61
62        @BeforeClass
63        public static void server() throws Exception {
64                Properties env = new Properties();
65                env.setProperty(ParameterQueue.USER_NAME, "guest");
66                env.setProperty(ParameterQueue.USER_PASS, "guest");
67
68                // Get host info of rabbimq (where it is)
69                env.setProperty(ParameterQueue.RABBIT_HOST, "127.0.0.1");
70                env.setProperty(ParameterQueue.RABBIT_PORT, "5672");
71                env.setProperty(ParameterQueue.DURABLE_QUEUE, "false");
72                env.setProperty(ParameterQueue.ENABLE_COMPRESSION, "false");
73
74                // Set info about where the message will be sent
75                env.setProperty(ParameterQueue.RPC_EXCHANGE, "rpc_exchange");
76                env.setProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000");
77
78                CalculatorImpl calc = new CalculatorImpl();
79                CalculatorImpl calc2 = new CalculatorImpl();
80
81                Broker broker = new Broker(env);
82                broker.bind("calculator1", calc);
83                broker.bind("calculator2", calc2);
84
85                System.out.println("Server started");
86        }
87
88        @After
89        public void stop() throws Exception {
90                broker.stopBroker();
91        }
92
93        @Test
94        public void add() throws Exception {
95                int x = 10;
96                int y = 20;
97
98                int sync = remoteCalc.add(x, y);
99                int sum = x + y;
100
101                assertEquals(sum, sync);
102        }
103
104        @Test
105        public void add2() throws Exception {
106                int x = 10;
107                int y = 20;
108
109                int sync = remoteCalc2.add(x, y);
110                int sum = x + y;
111
112                assertEquals(sum, sync);
113        }
114
115        @Test
116        public void mult() throws Exception {
117                int x = 5;
118                int y = 15;
119
120                remoteCalc.mult(x, y);
121                Thread.sleep(200);
122        }
123
124        @Test
125        public void sendMessage() throws Exception {
126                Message m = new Message(2334, "Hello objectmq");
127                remoteCalc.sendMessage(m);
128        }
129
130        @Test(expected = ArithmeticException.class)
131        public void divideByZero() {
132                remoteCalc.divideByZero();
133        }
134}
Note: See TracBrowser for help on using the repository browser.