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

Last change on this file since 62 was 62, checked in by gguerrero, 11 years ago
File size: 3.7 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@RunWith(value = Parameterized.class)
21public class CalculatorTest {
22
23        private static Broker broker;
24        private static Calculator remoteCalc;
25        private static Calculator remoteCalc2;
26
27        public CalculatorTest(String type) throws Exception {
28                Properties env = new Properties();
29                env.setProperty(ParameterQueue.USER_NAME, "guest");
30                env.setProperty(ParameterQueue.USER_PASS, "guest");
31
32                // Set host info of rabbimq (where it is)
33                env.setProperty(ParameterQueue.SERVER_HOST, "127.0.0.1");
34                env.setProperty(ParameterQueue.SERVER_PORT, "5672");
35                env.setProperty(ParameterQueue.DURABLE_QUEUES, "false");
36                env.setProperty(ParameterQueue.SERIALIZER_NAME, type);
37                env.setProperty(ParameterQueue.ENABLECOMPRESSION, "false");
38
39                // Set info about where the message will be sent
40                env.setProperty(ParameterQueue.RPC_EXCHANGE, "rpc_exchange");
41                // env.setProperty(ParameterQueue.DEBUGFILE, "c:\\middlewareDebug");
42
43                // Set info about the queue & the exchange where the ResponseListener
44                // will listen to.
45                env.setProperty(ParameterQueue.RPC_REPLY_QUEUE, "reply_queue");
46                env.setProperty(ParameterQueue.EVENT_REPLY_QUEUE, "event_queue");
47
48                broker = new Broker(env);
49                remoteCalc = broker.lookup("calculator1", Calculator.class);
50                remoteCalc2 = broker.lookup("calculator2", Calculator.class);
51        }
52
53        @Parameters
54        public static Collection<Object[]> data() {
55                Object[][] data = new Object[][] { { Serializer.JAVA }, { Serializer.GSON }, { Serializer.KRYO } };
56                return Arrays.asList(data);
57        }
58
59        @BeforeClass
60        public static void server() throws Exception {
61                Properties env = new Properties();
62                env.setProperty(ParameterQueue.USER_NAME, "guest");
63                env.setProperty(ParameterQueue.USER_PASS, "guest");
64
65                // Get host info of rabbimq (where it is)
66                env.setProperty(ParameterQueue.SERVER_HOST, "127.0.0.1");
67                env.setProperty(ParameterQueue.SERVER_PORT, "5672");
68                env.setProperty(ParameterQueue.DURABLE_QUEUES, "false");
69                env.setProperty(ParameterQueue.ENABLECOMPRESSION, "false");
70
71                // Set info about where the message will be sent
72                env.setProperty(ParameterQueue.RPC_EXCHANGE, "rpc_exchange");
73                env.setProperty(ParameterQueue.RETRY_TIME_CONNECTION, "2000");
74
75                CalculatorImpl calc = new CalculatorImpl();
76                CalculatorImpl calc2 = new CalculatorImpl();
77
78                Broker broker = new Broker(env);
79                broker.bind("calculator1", calc);
80                broker.bind("calculator2", calc2);
81
82                System.out.println("Server started");
83        }
84
85        @After
86        public void stop() throws Exception {
87                broker.stopBroker();
88        }
89
90        @Test
91        public void add() throws Exception {
92                int x = 10;
93                int y = 20;
94
95                int sync = remoteCalc.add(x, y);
96                int sum = x + y;
97
98                assertEquals(sum, sync);
99        }
100
101        @Test
102        public void add2() throws Exception {
103                int x = 10;
104                int y = 20;
105
106                int sync = remoteCalc2.add(x, y);
107                int sum = x + y;
108
109                assertEquals(sum, sync);
110        }
111
112        @Test
113        public void mult() throws Exception {
114                int x = 5;
115                int y = 15;
116
117                remoteCalc.mult(x, y);
118                Thread.sleep(200);
119        }
120
121        @Test
122        public void notifyEvent() throws Exception {
123                ZeroListener zL = new ZeroListener("zero-event");
124
125                remoteCalc.addListener(zL);
126
127                remoteCalc.asyncDivideByZero();
128
129                Thread.sleep(200);
130        }
131
132        @Test
133        public void sendMessage() throws Exception {
134                Message m = new Message(2334, "Hello objectmq");
135                remoteCalc.sendMessage(m);
136        }
137
138        @Test(expected = ArithmeticException.class)
139        public void divideByZero() {
140                remoteCalc.divideByZero();
141        }
142}
Note: See TracBrowser for help on using the repository browser.