Skip to main content
summaryrefslogtreecommitdiffstats
blob: 16d086b067ed33c7d249e593d9938f2df0a3cfb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*******************************************************************************
 * Copyright (c) 2010 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.messaging.internal;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import org.apache.activemq.broker.BrokerService;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.messaging.ConnectionNode;
import org.eclipse.osee.framework.messaging.MessageService;
import org.eclipse.osee.framework.messaging.OseeMessagingListener;
import org.eclipse.osee.framework.messaging.ReplyConnection;
import org.eclipse.osee.framework.messaging.SystemTopic;
import org.eclipse.osee.framework.messaging.test.msg.TestMessage;

/**
 * @author Andrew M. Finkbeiner
 */
public class BaseBrokerTesting {

   private MessageServiceProviderImpl messageServiceProviderImpl = null;
   private ConcurrentHashMap<String, BrokerService> brokers;
   private Thread[] threads;

   @org.junit.Before
   public void beforeTest() {
      messageServiceProviderImpl = new MessageServiceProviderImpl(Thread.currentThread().getContextClassLoader());
      brokers = new ConcurrentHashMap<String, BrokerService>();
      try {
         messageServiceProviderImpl.start();
      } catch (Exception ex) {
         ex.printStackTrace();
      }

   }

   @org.junit.After
   public void afterTest() {
      try {
         messageServiceProviderImpl.stop();
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }

   protected void startEmbeddedBroker(String brokerName, String brokerURI) throws Exception {
      BrokerService broker = new BrokerService();
      broker.setBrokerName(brokerName);
      broker.setPersistent(false);
      broker.setUseShutdownHook(true);
      broker.addConnector(brokerURI);
      broker.start();
      brokers.put(brokerURI, broker);
   }

   public void testWait(long timeMS) {
      try {
         Thread.sleep(timeMS);
      } catch (InterruptedException ex) {
         ex.printStackTrace();
      }
   }

   protected void stopEmbeddedBroker(String brokerName, String brokerURI) throws Exception {
      BrokerService broker = brokers.get(brokerURI);
      if (broker != null) {
         broker.stop();
      }
   }

   protected void startBroker() {
      try {
         String really = "file:C:\\Program Files\\OSEE_3.5.1\\0_9_0\\";// System.getProperty("eclipse.home.location");
         URL url = new URL(really);
         String exe = null;
         if (Lib.isWindows()) {
            exe = "eclipse.exe";
         } else {
            exe = "eclipse";
         }
         ProcessBuilder builder =
            new ProcessBuilder(url.getPath() + exe, "-console", "-nosplash", "-application",
               "jms.activemq.launch.RunActiveMq", DefaultNodeInfos.OSEE_JMS_DEFAULT_PORT);
         builder.directory(new File(url.getPath()));
         builder.redirectErrorStream(true);
         Process process = builder.start();
         Thread th = new Thread(new OutputReader(System.out, process.getInputStream()));
         th.start();
         //         threads = Lib.handleProcessNoWait(process, new PrintWriter(System.out));
         Thread.sleep(30000);
      } catch (MalformedURLException ex) {
         OseeLog.log(BaseBrokerTesting.class, Level.SEVERE, ex);
         fail(ex.getMessage());
      } catch (IOException ex) {
         OseeLog.log(BaseBrokerTesting.class, Level.SEVERE, ex);
         fail(ex.getMessage());
      } catch (InterruptedException ex) {
         OseeLog.log(BaseBrokerTesting.class, Level.SEVERE, ex);
         fail(ex.getMessage());
      }
   }

   public class OutputReader implements Runnable {

      private final PrintStream printStream;
      private final InputStream input;

      public OutputReader(PrintStream printStream, InputStream input) {
         super();
         this.printStream = printStream;
         this.input = input;

      }

      @Override
      public void run() {
         try {
            final byte[] buffer = new byte[4096];
            int size;
            while ((size = input.read(buffer)) != -1) {
               printStream.print(new String(buffer, 0, size, "UTF-8"));
               printStream.flush();
            }
         } catch (IOException ex) {
            ex.printStackTrace();
         } finally {
            try {
               input.close();
               printStream.flush();
            } catch (IOException ex) {
               ex.printStackTrace();
            }
         }
      }
   }

   protected void stopBroker() {
      try {
         getMessaging().get(DefaultNodeInfos.OSEE_JMS_DEFAULT).send(SystemTopic.KILL_TEST_JMS_BROKER, "kill",
            new MessageStatusTest(true));
         Thread.sleep(10000);
      } catch (InterruptedException ex) {
         OseeLog.log(BaseBrokerTesting.class, Level.SEVERE, ex);
         // fail(ex.getMessage());
      } catch (OseeCoreException ex) {
         OseeLog.log(BaseBrokerTesting.class, Level.SEVERE, ex);
         // fail(ex.getMessage());
      }
   }

   protected final MessageService getMessaging() {
      MessageService messaging = null;
      try {
         messaging = messageServiceProviderImpl.getMessageService();
      } catch (OseeCoreException ex) {
         fail("Failed to get messaging service. " + ex.getMessage());
      }
      assertTrue(messaging != null);
      return messaging;
   }

   protected void testJMSSendShouldFail(MessageService messaging) {
      MessageStatusTest status = new MessageStatusTest(false);
      try {
         messaging.get(DefaultNodeInfos.OSEE_JMS_DEFAULT).send(TestMessages.test, "test", status);
      } catch (OseeCoreException ex) {
         OseeLog.log(BaseBrokerTesting.class, Level.SEVERE, ex);
      }
      status.waitForStatus(5000);
   }

   protected void testJMSSendShouldPass(MessageService messaging) {
      MessageStatusTest status = new MessageStatusTest(true);
      try {
         messaging.get(DefaultNodeInfos.OSEE_JMS_DEFAULT).send(TestMessages.test, "test", status);
      } catch (OseeCoreException ex) {
         OseeLog.log(BaseBrokerTesting.class, Level.SEVERE, ex);
      }
      status.waitForStatus(5000);
   }

   protected void testJMSSubscribeShouldFail(MessageService messaging) throws OseeCoreException {
      MessageStatusTest status = new MessageStatusTest(false);
      OseeMessagingListener listener = new OseeMessagingListener(TestMessage.class) {
         @Override
         public void process(Object message, Map<String, Object> headers, ReplyConnection replyConnection) {
            TestMessage msg = (TestMessage) message;
            System.out.println(msg.getMessage());
         }
      };

      messaging.get(DefaultNodeInfos.OSEE_JMS_DEFAULT).subscribe(TestMessages.test2, listener, status);
      status.waitForStatus(5000);
      messaging.get(DefaultNodeInfos.OSEE_JMS_DEFAULT).unsubscribe(TestMessages.test2, listener, status);//we have to remove so we don't get a false fail later on
   }

   protected void testJMSSubscribeShouldPass(MessageService messaging) throws OseeCoreException {
      MessageStatusTest status = new MessageStatusTest(true);
      ConnectionNode node = messaging.get(DefaultNodeInfos.OSEE_JMS_DEFAULT);
      node.subscribe(TestMessages.test2, new OseeMessagingListener(TestMessage.class) {
         @Override
         public void process(Object message, Map<String, Object> headers, ReplyConnection replyConnection) {
            TestMessage msg = (TestMessage) message;
            System.out.println(msg.getMessage());
         }
      }, status);
      status.waitForStatus(5000);
   }
}

Back to the top