001 /**
002 * Copyright 2005-2011 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.ksb.messaging.exceptionhandling;
017
018 import static org.junit.Assert.assertEquals;
019 import static org.junit.Assert.assertFalse;
020 import static org.junit.Assert.assertNull;
021 import static org.junit.Assert.assertTrue;
022
023 import org.junit.Test;
024 import org.kuali.rice.core.api.config.property.ConfigContext;
025 import org.kuali.rice.ksb.api.bus.support.JavaServiceConfiguration;
026 import org.kuali.rice.ksb.api.messaging.AsynchronousCall;
027 import org.kuali.rice.ksb.messaging.PersistedMessageBO;
028 import org.kuali.rice.ksb.messaging.PersistedMessagePayload;
029 import org.kuali.rice.ksb.test.KSBTestCase;
030 import org.kuali.rice.ksb.util.KSBConstants;
031
032
033 public class DefaultMessageExceptionHandlerTest extends KSBTestCase {
034
035 private PersistedMessageBO setupMessage(Integer retriesAttempted, Integer serviceMaxRetries) throws Exception {
036 PersistedMessageBO message = new PersistedMessageBO();
037 message.setRetryCount(retriesAttempted);
038 //ServiceInfo serviceInfo = new ServiceInfo();
039 //serviceInfo.setServiceDefinition(new JavaServiceDefinition());
040 //serviceInfo.getServiceDefinition().setRetryAttempts(serviceMaxRetries);
041 JavaServiceConfiguration.Builder builder = JavaServiceConfiguration.Builder.create();
042 builder.setRetryAttempts(serviceMaxRetries);
043 AsynchronousCall methodCall = new AsynchronousCall(new Class[0], new Object[0], builder.build(), "", null, null);
044 message.setPayload(new PersistedMessagePayload(methodCall, message));
045 message.setMethodCall(methodCall);
046 return message;
047 }
048
049 private void setMaxRetries(String maxRetries) {
050 ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_OVERRIDE_KEY, maxRetries);
051 }
052
053 @Test public void testGetGlobalMaxRetryAttempts() throws Exception {
054 DefaultMessageExceptionHandler exceptionHandler = new DefaultMessageExceptionHandler();
055
056 this.setMaxRetries("0");
057
058
059 // test non-numeric
060 this.setMaxRetries("B");
061 assertNull("Method should return null if app constant is non-numeric.", exceptionHandler.getGlobalMaxRetryAttempts());
062
063 // test large negative number
064 this.setMaxRetries("-10");
065 assertNull("Method should return null if app constant is negative number.", exceptionHandler.getGlobalMaxRetryAttempts());
066
067 // test -1
068 this.setMaxRetries("-1");
069 assertNull("Method should return null if app constant is negative number.", exceptionHandler.getGlobalMaxRetryAttempts());
070
071 // test 0
072 this.setMaxRetries("0");
073 assertEquals("Method should return app constant value if app constant is numeric and greater than or equal to zero.", new Integer(0), exceptionHandler.getGlobalMaxRetryAttempts());
074
075 // test 1
076 this.setMaxRetries("1");
077 assertEquals("Method should return app constant value if app constant is numeric and greater than or equal to zero.", new Integer(1), exceptionHandler.getGlobalMaxRetryAttempts());
078
079 // test 5
080 this.setMaxRetries("5");
081 assertEquals("Method should return app constant value if app constant is numeric and greater than or equal to zero.", new Integer(5), exceptionHandler.getGlobalMaxRetryAttempts());
082 }
083
084 @Test public void testIsInException() throws Exception {
085 DefaultMessageExceptionHandler exceptionHandler = new DefaultMessageExceptionHandler();
086 PersistedMessageBO message = null;
087
088 this.setMaxRetries("-10");
089
090 message = setupMessage(0, 1);
091 assertFalse(exceptionHandler.isInException(message));
092
093 message.setRetryCount(1);
094 assertTrue(exceptionHandler.isInException(message));
095
096 message.setRetryCount(2);
097 assertTrue(exceptionHandler.isInException(message));
098
099 this.setMaxRetries("5");
100
101 message.setRetryCount(4);
102 assertFalse(exceptionHandler.isInException(message));
103
104 message.setRetryCount(5);
105 assertTrue(exceptionHandler.isInException(message));
106
107 message.setRetryCount(6);
108 assertTrue(exceptionHandler.isInException(message));
109
110 this.setMaxRetries("0");
111
112 message.setRetryCount(0);
113 assertTrue(exceptionHandler.isInException(message));
114
115 message.setRetryCount(1);
116 assertTrue(exceptionHandler.isInException(message));
117
118 this.setMaxRetries("-1");
119
120 message.setRetryCount(0);
121 assertFalse(exceptionHandler.isInException(message));
122
123 message = setupMessage(1, 5);
124 assertFalse(exceptionHandler.isInException(message));
125
126 this.setMaxRetries("B");
127
128 message.setRetryCount(0);
129 assertFalse(exceptionHandler.isInException(message));
130 }
131
132 }