View Javadoc
1   /*
2    * Copyright 2006-2014 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.krad.data.platform;
18  
19  import org.junit.Test;
20  import org.kuali.rice.krad.test.KRADTestCase;
21  import org.kuali.rice.test.TestHarnessServiceLocator;
22  import org.springframework.context.ConfigurableApplicationContext;
23  import org.springframework.context.support.ClassPathXmlApplicationContext;
24  import org.springframework.dao.DataAccessException;
25  import org.springframework.jdbc.BadSqlGrammarException;
26  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
27  
28  import javax.sql.DataSource;
29  
30  import static org.junit.Assert.*;
31  
32  public class MaxValueIncrementerFactoryBeanIntegrationTest extends KRADTestCase {
33  
34      private final static String TEST_INCREMENTER = "testIncrementer";
35      private final static String INVALID_TEST_INCREMENTER = "invalidTestIncrementer";
36  
37      private ConfigurableApplicationContext context;
38  
39      @Override
40      public void setUpInternal() throws Exception {
41          super.setUpInternal();
42          context = new ClassPathXmlApplicationContext("MaxValueIncrementerFactoryBeanTest.xml", getClass());
43      }
44  
45      @Override
46      public void tearDown() throws Exception {
47          if (context != null) {
48              context.close();
49          }
50          super.tearDown();
51      }
52  
53      /**
54       * Tests that the incrementer returned from the factory returns the proper next int, long, and String values.
55       */
56      @Test
57      public void testGetIncrementer_nextValues() {
58          DataFieldMaxValueIncrementer incrementer = (DataFieldMaxValueIncrementer) context.getBean(TEST_INCREMENTER);
59          assertNotNull(incrementer);
60  
61          // now that we have our incrementer, let's get the next value
62          int nextIntValue = incrementer.nextIntValue();
63          assertTrue("nextIntValue should be greater than 0", nextIntValue > 0);
64  
65          // do it again, should be 1 larger!
66          int nextNextIntValue = incrementer.nextIntValue();
67          assertEquals("Next value should be one higher", nextIntValue + 1, nextNextIntValue);
68  
69          // try getting the next value as a long
70          long nextLongValue = incrementer.nextLongValue();
71          assertEquals(nextNextIntValue + 1, nextLongValue);
72  
73          // try getting it as a String now
74          String nextStringValue = incrementer.nextStringValue();
75          assertEquals(nextLongValue + 1, Long.parseLong(nextStringValue));
76      }
77  
78      /**
79       * Tests that the sequence name is case insensitive. We will do this by using the same sequence name as the
80       * previous test, but changing the case to all lowercase.
81       */
82      @Test
83      public void testGetIncrementer_CaseInsensitive() {
84          DataFieldMaxValueIncrementer incrementer = (DataFieldMaxValueIncrementer) context.getBean(TEST_INCREMENTER);
85          assertNotNull(incrementer);
86  
87          // now that we have our incrementer, let's get the next value
88          int nextIntValue = incrementer.nextIntValue();
89          assertTrue("nextIntValue should be greater than 0", nextIntValue > 0);
90      }
91  
92      /**
93       * Tests that if you try to use the factory with an invalid sequence name, it will throw a DataAccessException.
94       */
95      @Test(expected = DataAccessException.class)
96      public void testGetIncrementer_BadSequence() {
97          DataFieldMaxValueIncrementer incrementer = (DataFieldMaxValueIncrementer) context.getBean(INVALID_TEST_INCREMENTER);
98  
99          // the incrementer *may* still be retrieved successfully (depending on the database this integration test is run against)
100         assertNotNull(incrementer);
101 
102         // but at the very least it should throw an exception when executed
103         incrementer.nextLongValue();
104     }
105 }