View Javadoc

1   /*
2    * Copyright 2006-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.kuali.rice.ksb.messaging.exceptionhandling;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import org.junit.Test;
25  import org.kuali.rice.core.api.config.property.ConfigContext;
26  import org.kuali.rice.ksb.api.bus.support.JavaServiceConfiguration;
27  import org.kuali.rice.ksb.api.messaging.AsynchronousCall;
28  import org.kuali.rice.ksb.messaging.PersistedMessageBO;
29  import org.kuali.rice.ksb.messaging.PersistedMessagePayload;
30  import org.kuali.rice.ksb.test.KSBTestCase;
31  import org.kuali.rice.ksb.util.KSBConstants;
32  
33  
34  public class DefaultMessageExceptionHandlerTest extends KSBTestCase {
35      
36      private PersistedMessageBO setupMessage(Integer retriesAttempted, Integer serviceMaxRetries) throws Exception {
37          PersistedMessageBO message = new PersistedMessageBO();
38          message.setRetryCount(retriesAttempted);
39          //ServiceInfo serviceInfo = new ServiceInfo();
40          //serviceInfo.setServiceDefinition(new JavaServiceDefinition());
41          //serviceInfo.getServiceDefinition().setRetryAttempts(serviceMaxRetries);
42          JavaServiceConfiguration.Builder builder = JavaServiceConfiguration.Builder.create();
43          builder.setRetryAttempts(serviceMaxRetries);
44          AsynchronousCall methodCall = new AsynchronousCall(new Class[0], new Object[0], builder.build(), "", null, null);
45          message.setPayload(new PersistedMessagePayload(methodCall, message));
46          message.setMethodCall(methodCall);
47          return message;
48      }
49      
50      private void setMaxRetries(String maxRetries) {
51      	ConfigContext.getCurrentContextConfig().putProperty(KSBConstants.Config.ROUTE_QUEUE_MAX_RETRY_ATTEMPTS_OVERRIDE_KEY, maxRetries);
52      }
53      
54      @Test public void testGetGlobalMaxRetryAttempts() throws Exception {
55          DefaultMessageExceptionHandler exceptionHandler = new DefaultMessageExceptionHandler();
56          
57          this.setMaxRetries("0");
58          
59          
60          //  test non-numeric
61          this.setMaxRetries("B");
62          assertNull("Method should return null if app constant is non-numeric.", exceptionHandler.getGlobalMaxRetryAttempts());
63          
64          //  test large negative number
65          this.setMaxRetries("-10");
66          assertNull("Method should return null if app constant is negative number.", exceptionHandler.getGlobalMaxRetryAttempts());
67          
68          //  test -1
69          this.setMaxRetries("-1");
70          assertNull("Method should return null if app constant is negative number.", exceptionHandler.getGlobalMaxRetryAttempts());
71          
72          //  test 0
73          this.setMaxRetries("0");
74          assertEquals("Method should return app constant value if app constant is numeric and greater than or equal to zero.", new Integer(0), exceptionHandler.getGlobalMaxRetryAttempts());
75          
76          //  test 1
77          this.setMaxRetries("1");
78          assertEquals("Method should return app constant value if app constant is numeric and greater than or equal to zero.", new Integer(1), exceptionHandler.getGlobalMaxRetryAttempts());
79          
80          //  test 5
81          this.setMaxRetries("5");
82          assertEquals("Method should return app constant value if app constant is numeric and greater than or equal to zero.", new Integer(5), exceptionHandler.getGlobalMaxRetryAttempts());
83      }
84      
85      @Test public void testIsInException() throws Exception {
86          DefaultMessageExceptionHandler exceptionHandler = new DefaultMessageExceptionHandler();
87          PersistedMessageBO message = null;
88          
89          this.setMaxRetries("-10");
90  
91          message = setupMessage(0, 1);
92          assertFalse(exceptionHandler.isInException(message));
93          
94          message.setRetryCount(1);
95          assertTrue(exceptionHandler.isInException(message));
96          
97          message.setRetryCount(2);
98          assertTrue(exceptionHandler.isInException(message));
99          
100         this.setMaxRetries("5");
101         
102         message.setRetryCount(4);
103         assertFalse(exceptionHandler.isInException(message));
104         
105         message.setRetryCount(5);
106         assertTrue(exceptionHandler.isInException(message));
107         
108         message.setRetryCount(6);
109         assertTrue(exceptionHandler.isInException(message));
110         
111         this.setMaxRetries("0");
112         
113         message.setRetryCount(0);
114         assertTrue(exceptionHandler.isInException(message));
115         
116         message.setRetryCount(1);
117         assertTrue(exceptionHandler.isInException(message));
118         
119         this.setMaxRetries("-1");
120         
121         message.setRetryCount(0);
122         assertFalse(exceptionHandler.isInException(message));
123         
124         message = setupMessage(1, 5);
125         assertFalse(exceptionHandler.isInException(message));
126         
127         this.setMaxRetries("B");
128         
129         message.setRetryCount(0);
130         assertFalse(exceptionHandler.isInException(message));
131     }
132 
133 }