[47] | 1 | package omq.test.calculator; |
---|
| 2 | |
---|
| 3 | import static org.junit.Assert.assertEquals; |
---|
| 4 | |
---|
| 5 | import java.util.Arrays; |
---|
| 6 | import java.util.Collection; |
---|
| 7 | import java.util.Properties; |
---|
| 8 | |
---|
| 9 | import omq.common.broker.Broker; |
---|
| 10 | import omq.common.util.ParameterQueue; |
---|
| 11 | import omq.common.util.Serializer; |
---|
| 12 | |
---|
| 13 | import org.junit.After; |
---|
[54] | 14 | import org.junit.BeforeClass; |
---|
[47] | 15 | import org.junit.Test; |
---|
| 16 | import org.junit.runner.RunWith; |
---|
| 17 | import org.junit.runners.Parameterized; |
---|
| 18 | import org.junit.runners.Parameterized.Parameters; |
---|
| 19 | |
---|
[83] | 20 | /** |
---|
| 21 | * |
---|
| 22 | * @author Sergi Toda <sergi.toda@estudiants.urv.cat> |
---|
| 23 | * |
---|
| 24 | */ |
---|
[47] | 25 | @RunWith(value = Parameterized.class) |
---|
[54] | 26 | public class CalculatorTest { |
---|
[47] | 27 | |
---|
[54] | 28 | private static Broker broker; |
---|
[47] | 29 | private static Calculator remoteCalc; |
---|
| 30 | private static Calculator remoteCalc2; |
---|
| 31 | |
---|
[54] | 32 | public CalculatorTest(String type) throws Exception { |
---|
[47] | 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) |
---|
[77] | 38 | env.setProperty(ParameterQueue.RABBIT_HOST, "127.0.0.1"); |
---|
| 39 | env.setProperty(ParameterQueue.RABBIT_PORT, "5672"); |
---|
[84] | 40 | env.setProperty(ParameterQueue.DURABLE_QUEUE, "false"); |
---|
[77] | 41 | env.setProperty(ParameterQueue.PROXY_SERIALIZER, type); |
---|
| 42 | env.setProperty(ParameterQueue.ENABLE_COMPRESSION, "false"); |
---|
[47] | 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 | |
---|
[54] | 51 | broker = new Broker(env); |
---|
| 52 | remoteCalc = broker.lookup("calculator1", Calculator.class); |
---|
| 53 | remoteCalc2 = broker.lookup("calculator2", Calculator.class); |
---|
[47] | 54 | } |
---|
| 55 | |
---|
| 56 | @Parameters |
---|
| 57 | public static Collection<Object[]> data() { |
---|
[62] | 58 | Object[][] data = new Object[][] { { Serializer.JAVA }, { Serializer.GSON }, { Serializer.KRYO } }; |
---|
[47] | 59 | return Arrays.asList(data); |
---|
| 60 | } |
---|
| 61 | |
---|
[54] | 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) |
---|
[77] | 69 | env.setProperty(ParameterQueue.RABBIT_HOST, "127.0.0.1"); |
---|
| 70 | env.setProperty(ParameterQueue.RABBIT_PORT, "5672"); |
---|
[84] | 71 | env.setProperty(ParameterQueue.DURABLE_QUEUE, "false"); |
---|
[77] | 72 | env.setProperty(ParameterQueue.ENABLE_COMPRESSION, "false"); |
---|
[54] | 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 | |
---|
[47] | 88 | @After |
---|
| 89 | public void stop() throws Exception { |
---|
[54] | 90 | broker.stopBroker(); |
---|
[47] | 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 | } |
---|