001: /*
002: * Copyright 2007 The Kuali Foundation
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License"); you may not use this file except in
005: * compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.opensource.org/licenses/ecl1.php
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS
010: * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
011: * language governing permissions and limitations under the License.
012: */
013: package edu.iu.uis.eden.messaging;
014:
015: import javax.xml.namespace.QName;
016:
017: import org.junit.Test;
018: import org.kuali.bus.services.KSBServiceLocator;
019: import org.kuali.bus.test.KSBTestCase;
020: import org.kuali.rice.RiceConstants;
021: import org.kuali.rice.core.Core;
022:
023: import edu.iu.uis.eden.messaging.remotedservices.TestHarnessSharedTopic;
024:
025: /**
026: * Tests {@link MessageFetcher}. Turn messaging off but leave persistence on. this will result in messages being persisted
027: * to db but not delivered. from there we start up the {@link MessageFetcher} and make sure he does his job.
028: *
029: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
030: *
031: */
032: public class MessageFetcherTest extends KSBTestCase {
033:
034: @Override
035: public void setUp() throws Exception {
036: super .setUp();
037: Core.getCurrentContextConfig().overrideProperty(
038: RiceConstants.MESSAGING_OFF, "true");
039: TestHarnessSharedTopic.CALL_COUNT = 0;
040: }
041:
042: @Override
043: public void tearDown() throws Exception {
044: TestHarnessSharedTopic.CALL_COUNT = 0;
045: }
046:
047: @Test
048: public void testRequeueMessages() throws Exception {
049:
050: // this number is way over the top but we're going to see if it works in an overworked CI env.
051: TestHarnessSharedTopic.CALL_COUNT_NOTIFICATION_THRESHOLD = 500;
052:
053: for (int i = 0; i < TestHarnessSharedTopic.CALL_COUNT_NOTIFICATION_THRESHOLD; i++) {
054: sendMessage();
055: }
056:
057: turnOnMessaging();
058: new MessageFetcher((Integer) null).run();
059: synchronized (TestHarnessSharedTopic.LOCK) {
060: TestHarnessSharedTopic.LOCK.wait(5 * 60 * 1000);
061: }
062:
063: assertTrue(
064: "Service not called by message fetcher",
065: TestHarnessSharedTopic.CALL_COUNT == TestHarnessSharedTopic.CALL_COUNT_NOTIFICATION_THRESHOLD);
066: }
067:
068: private void sendMessage() {
069: QName serviceName = QName
070: .valueOf("{testAppsSharedTopic}sharedTopic");
071: KEWJavaService testJavaAsyncService = (KEWJavaService) KSBServiceLocator
072: .getMessageHelper().getServiceAsynchronously(
073: serviceName);
074: testJavaAsyncService
075: .invoke(new ClientAppServiceSharedPayloadObj(
076: "message content", false));
077: }
078:
079: private void turnOnMessaging() {
080: Core.getCurrentContextConfig().overrideProperty(
081: RiceConstants.MESSAGING_OFF, "false");
082: }
083:
084: @Test
085: public void testRequeueSingleMessage() throws Exception {
086: sendMessage();
087: sendMessage();
088: PersistedMessage message = KSBServiceLocator
089: .getRouteQueueService().getNextDocuments(null).get(0);
090: assertNotNull("message should have been persisted", message);
091: turnOnMessaging();
092: new MessageFetcher(message.getRouteQueueId()).run();
093: synchronized (TestHarnessSharedTopic.LOCK) {
094: TestHarnessSharedTopic.LOCK.wait(3 * 1000);
095: }
096:
097: assertTrue(
098: "Service not called by message fetcher corrent number of times",
099: 1 == TestHarnessSharedTopic.CALL_COUNT);
100: for (int i = 0; i < 10; i++) {
101: if (KSBServiceLocator.getRouteQueueService()
102: .getNextDocuments(null).size() == 1) {
103: break;
104: }
105: Thread.sleep(1000);
106: }
107: assertEquals(
108: "Message Queue should have a single remaining message because only single message was resent",
109: new Integer(1), KSBServiceLocator
110: .getRouteQueueService().getNextDocuments(null)
111: .size());
112: }
113:
114: }
|