View Javadoc
1   /**
2    * Copyright 2005-2015 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  package org.kuali.rice.krad.data.platform;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.junit.After;
20  import org.junit.Before;
21  import org.junit.Test;
22  import org.junit.runner.RunWith;
23  import org.kuali.rice.core.api.config.property.Config;
24  import org.kuali.rice.core.api.config.property.ConfigContext;
25  import org.kuali.rice.core.framework.config.property.SimpleConfig;
26  import org.mockito.Mock;
27  import org.mockito.runners.MockitoJUnitRunner;
28  import org.springframework.dao.DataAccessException;
29  import org.springframework.jdbc.support.incrementer.AbstractColumnMaxValueIncrementer;
30  import org.springframework.jdbc.support.incrementer.AbstractDataFieldMaxValueIncrementer;
31  import org.springframework.jdbc.support.incrementer.AbstractSequenceMaxValueIncrementer;
32  import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
33  import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;
34  
35  import javax.sql.DataSource;
36  import java.io.IOException;
37  import java.sql.Connection;
38  import java.sql.DatabaseMetaData;
39  import java.sql.SQLException;
40  import java.util.Map;
41  import java.util.Properties;
42  
43  import static org.junit.Assert.*;
44  import static org.mockito.Mockito.mock;
45  import static org.mockito.Mockito.when;
46  
47  /**
48   * Unit test for {@link MaxValueIncrementerFactory}.
49   *
50   * @author Kuali Rice Team (rice.collab@kuali.org)
51   */
52  @RunWith(MockitoJUnitRunner.class)
53  public class MaxValueIncrementerFactoryTest {
54  
55      @Mock private DataSource mysql;
56      @Mock private DataSource oracle;
57      @Mock private DataSource bad;
58  
59      @Before
60      public void setUp() throws Exception {
61          setUpMetaData(mysql, DatabasePlatforms.MYSQL, 5);
62          setUpMetaData(oracle, DatabasePlatforms.ORACLE, 11);
63          setUpMetaData(bad, "BAD!!!!!", 1);
64      }
65  
66      private void setUpMetaData(DataSource dataSource, String platformName, int version) throws SQLException {
67          DatabaseMetaData metaData = mock(DatabaseMetaData.class);
68          when(metaData.getDatabaseProductName()).thenReturn(platformName);
69          when(metaData.getDatabaseMajorVersion()).thenReturn(version);
70          Connection connection = mock(Connection.class);
71          when(connection.getMetaData()).thenReturn(metaData);
72          when(dataSource.getConnection()).thenReturn(connection);
73      }
74  
75  
76      @Test
77      public void testGetIncrementer_Oracle() throws Exception {
78          DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE");
79          assertTrue(incrementer instanceof OracleSequenceMaxValueIncrementer);
80          OracleSequenceMaxValueIncrementer oracleIncrementer = (OracleSequenceMaxValueIncrementer)incrementer;
81          assertEquals("MY_SEQUENCE", oracleIncrementer.getIncrementerName());
82  
83          // ensure that it's caching the incrementer
84          assertSame(incrementer, MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE"));
85          // ensure that different sequence gives a different incrementer
86          assertNotSame(incrementer, MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE_2"));
87  
88      }
89  
90      @Test
91      public void testGetIncrementer_MySQL() throws Exception {
92          DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE");
93          assertTrue(incrementer instanceof MaxValueIncrementerFactory.EnhancedMySQLMaxValueIncrementer);
94          MaxValueIncrementerFactory.EnhancedMySQLMaxValueIncrementer mysqlIncrementer =
95                  (MaxValueIncrementerFactory.EnhancedMySQLMaxValueIncrementer)incrementer;
96          assertEquals("MY_SEQUENCE", mysqlIncrementer.getIncrementerName());
97          assertEquals("ID", mysqlIncrementer.getColumnName());
98  
99          // ensure that it's caching the incrementer
100         assertSame(incrementer, MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE"));
101         // ensure that different sequence gives a different incrementer
102         assertNotSame(incrementer, MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE_2"));
103 
104     }
105 
106     @Test(expected = UnsupportedDatabasePlatformException.class)
107     public void testGetIncrementer_Bad() throws Exception {
108         MaxValueIncrementerFactory.getIncrementer(bad, "MY_SEQUENCE");
109     }
110 
111     @Test
112     public void testGetIncrementer_CaseInsensitive() throws Exception {
113         DataFieldMaxValueIncrementer incrementer1 = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE");
114         DataFieldMaxValueIncrementer incrementer2 = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_sequence");
115         DataFieldMaxValueIncrementer incrementer3 = MaxValueIncrementerFactory.getIncrementer(mysql, "my_sequence");
116         assertSame(incrementer1, incrementer2);
117         assertSame(incrementer2, incrementer3);
118     }
119 
120     @Test
121     public void testGetIncrementer_CacheByDataSource() throws Exception {
122         DataFieldMaxValueIncrementer incrementer1 = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE");
123         DataFieldMaxValueIncrementer incrementer2 = MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE");
124         assertNotSame(incrementer1, incrementer2);
125     }
126 
127     @Test(expected = IllegalArgumentException.class)
128     public void testGetIncrementer_NullDataSource() throws Exception {
129         MaxValueIncrementerFactory.getIncrementer(null, "MY_SEQUENCE");
130     }
131 
132     @Test(expected = IllegalArgumentException.class)
133     public void testGetIncrementer_NullIncrementerName() throws Exception {
134         MaxValueIncrementerFactory.getIncrementer(mysql, null);
135     }
136 
137     @Test(expected = IllegalArgumentException.class)
138     public void testGetIncrementer_BlankIncrementerName() throws Exception {
139         MaxValueIncrementerFactory.getIncrementer(mysql, "");
140     }
141 
142     @Test
143     public void testCustomIncrementerDatasourceVersion() throws Exception {
144         SimpleConfig config = new SimpleConfig();
145         config.putProperty("rice.krad.data.platform.incrementer.mysql.5",
146                 "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerMySQLVersion5");
147         config.putProperty("rice.krad.data.platform.incrementer.oracle.11",
148                 "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerOracleVersion11");
149         ConfigContext.init(config);
150 
151         DataFieldMaxValueIncrementer mySQLMaxVal = MaxValueIncrementerFactory.getIncrementer(mysql,"test_mySQL");
152         assertTrue("Found MySQL custom incrementer",mySQLMaxVal != null);
153         assertTrue("Custom incrementer for MySQL should be mySQL5 for String val",
154                         StringUtils.equals(mySQLMaxVal.nextStringValue(),"mySQL5"));
155 
156         DataFieldMaxValueIncrementer oracleMaxVal = MaxValueIncrementerFactory.getIncrementer(oracle,"test_oracle");
157         assertTrue("Found Oracle custom incrementer", oracleMaxVal != null);
158     }
159 
160     @Test
161     public void testCustomIncrementerDatasourceNoVersion() throws Exception {
162         SimpleConfig config = new SimpleConfig();
163         config.putProperty("rice.krad.data.platform.incrementer.mysql",
164                 "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerMySQLVersion5");
165         config.putProperty("rice.krad.data.platform.incrementer.oracle",
166                 "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerOracleVersion11");
167         ConfigContext.init(config);
168 
169         DataFieldMaxValueIncrementer mySQLMaxVal = MaxValueIncrementerFactory.getIncrementer(mysql,"test_mySQL");
170         assertTrue("Found MySQL custom incrementer",mySQLMaxVal != null);
171         assertTrue("Custom incrementer for MySQL should be mySQL5 for String val",
172                 StringUtils.equals(mySQLMaxVal.nextStringValue(),"mySQL5"));
173 
174         DataFieldMaxValueIncrementer oracleMaxVal = MaxValueIncrementerFactory.getIncrementer(oracle,"test_oracle");
175         assertTrue("Found Oracle custom incrementer", oracleMaxVal != null);
176     }
177 
178     @Test(expected = InstantiationError.class)
179     public void testCustomIncrementerDatasourceInvalidClass() throws Exception {
180         SimpleConfig config = new SimpleConfig();
181         config.putProperty("rice.krad.data.platform.incrementer.mysql",
182                 "org.kuali.rice.krad.data.platform.testincrementers.NonExistent");
183         ConfigContext.init(config);
184 
185         DataFieldMaxValueIncrementer mySQLMaxVal = MaxValueIncrementerFactory.getIncrementer(mysql,"test_mySQL");
186         assertTrue("Cannot create incrementer", mySQLMaxVal == null);
187     }
188 
189     @After
190     public void clearContext(){
191         ConfigContext.destroy();
192     }
193 }