View Javadoc
1   package org.kuali.rice.krad.data.platform;
2   
3   import org.junit.Test;
4   import org.kuali.rice.krad.test.KRADTestCase;
5   import org.kuali.rice.test.TestHarnessServiceLocator;
6   import org.springframework.dao.DataAccessException;
7   import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
8   
9   import javax.sql.DataSource;
10  
11  import static org.junit.Assert.assertEquals;
12  import static org.junit.Assert.assertNotNull;
13  import static org.junit.Assert.assertTrue;
14  
15  /**
16   * An integration test for the {@link MaxValueIncrementerFactory}.
17   *
18   * @author Kuali Rice Team (rice.collab@kuali.org)
19   */
20  public class MaxValueIncrementerFactoryIntegrationTest extends KRADTestCase {
21  
22      private static final String ARBITRARY_SEQUENCE = "TRVL_ID_SEQ";
23  
24      /**
25       * Tests that the incrementer returned from the factory returns the proper next int, long, and String values.
26       */
27      @Test
28      public void testGetIncrementer_nextValues() {
29          DataSource dataSource = TestHarnessServiceLocator.getDataSource();
30          DataFieldMaxValueIncrementer incrementer =
31                  MaxValueIncrementerFactory.getIncrementer(dataSource, ARBITRARY_SEQUENCE);
32          assertNotNull(incrementer);
33  
34          // now that we have our incrementer, let's get the next value
35          int nextIntValue = incrementer.nextIntValue();
36          assertTrue("nextIntValue should be greater than 0", nextIntValue > 0);
37  
38          // do it again, should be 1 larger!
39          int nextNextIntValue = incrementer.nextIntValue();
40          assertEquals("Next value should be one higher", nextIntValue + 1, nextNextIntValue);
41  
42          // try getting the next value as a long
43          long nextLongValue = incrementer.nextLongValue();
44          assertEquals(nextNextIntValue + 1, nextLongValue);
45  
46          // try getting it as a String now
47          String nextStringValue = incrementer.nextStringValue();
48          assertEquals(nextLongValue + 1, Long.parseLong(nextStringValue));
49      }
50  
51      /**
52       * Tests that the sequence name is case insensitive. We will do this by using the same sequence name as the
53       * previous test, but changing the case to all lowercase.
54       */
55      @Test
56      public void testGetIncrementer_CaseInsensitive() {
57          DataSource dataSource = TestHarnessServiceLocator.getDataSource();
58          DataFieldMaxValueIncrementer incrementer =
59                  MaxValueIncrementerFactory.getIncrementer(dataSource, ARBITRARY_SEQUENCE.toLowerCase());
60          assertNotNull(incrementer);
61  
62          // now that we have our incrementer, let's get the next value
63          int nextIntValue = incrementer.nextIntValue();
64          assertTrue("nextIntValue should be greater than 0", nextIntValue > 0);
65      }
66  
67      /**
68       * Tests that if you try to use the factory with an invalid sequence name, it will throw a DataAccessException.
69       */
70      @Test(expected = DataAccessException.class)
71      public void testGetIncrementer_BadSequence() {
72          DataSource dataSource = TestHarnessServiceLocator.getDataSource();
73          DataFieldMaxValueIncrementer incrementer =
74                  MaxValueIncrementerFactory.getIncrementer(dataSource, "OH_NO_YOU_DIDNT!");
75  
76          // the incrementer *may* still be retrieved successfully (depending on the database this integration test is run against)
77          assertNotNull(incrementer);
78  
79          // but at the very least it should throw an exception when executed
80          incrementer.nextLongValue();
81      }
82  
83  }