MaxValueIncrementerFactory { public static DataFieldMaxValueIncrementer getIncrementer(DataSource dataSource, String incrementerName) { if (dataSource == null) { throw new IllegalArgumentException("DataSource must not be null"); } if (StringUtils.isBlank(incrementerName)) { throw new IllegalArgumentException("Incrementer name must not be null or blank"); } ConcurrentMap<String, DataFieldMaxValueIncrementer> incrementerCache = cache.get(dataSource); if (incrementerCache == null) { cache.put(dataSource, new ConcurrentHashMap<String, DataFieldMaxValueIncrementer>(8, 0.9f, 1)); if (incrementerCache == null) { incrementerCache = cache.get(dataSource); } } DataFieldMaxValueIncrementer incrementer = incrementerCache.get(incrementerName.toUpperCase()); if (incrementer == null) { incrementer = incrementerCache.putIfAbsent(incrementerName.toUpperCase(), createIncrementer(dataSource, incrementerName)); if (incrementer == null) { incrementer = incrementerCache.get(incrementerName.toUpperCase()); } } return incrementer; } private  MaxValueIncrementerFactory(); static DataFieldMaxValueIncrementer getIncrementer(DataSource dataSource, String incrementerName); static final String PLATFORM_INCREMENTER_PREFIX; }
@Test public void testGetIncrementer_Oracle() throws Exception { DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE"); assertTrue(incrementer instanceof OracleSequenceMaxValueIncrementer); OracleSequenceMaxValueIncrementer oracleIncrementer = (OracleSequenceMaxValueIncrementer)incrementer; assertEquals("MY_SEQUENCE", oracleIncrementer.getIncrementerName()); assertSame(incrementer, MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE")); assertNotSame(incrementer, MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE_2")); }
@Test public void testGetIncrementer_MySQL() throws Exception { DataFieldMaxValueIncrementer incrementer = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE"); assertTrue(incrementer instanceof MaxValueIncrementerFactory.EnhancedMySQLMaxValueIncrementer); MaxValueIncrementerFactory.EnhancedMySQLMaxValueIncrementer mysqlIncrementer = (MaxValueIncrementerFactory.EnhancedMySQLMaxValueIncrementer)incrementer; assertEquals("MY_SEQUENCE", mysqlIncrementer.getIncrementerName()); assertEquals("ID", mysqlIncrementer.getColumnName()); assertSame(incrementer, MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE")); assertNotSame(incrementer, MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE_2")); }
@Test(expected = UnsupportedDatabasePlatformException.class) public void testGetIncrementer_Bad() throws Exception { MaxValueIncrementerFactory.getIncrementer(bad, "MY_SEQUENCE"); }
@Test public void testGetIncrementer_CaseInsensitive() throws Exception { DataFieldMaxValueIncrementer incrementer1 = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE"); DataFieldMaxValueIncrementer incrementer2 = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_sequence"); DataFieldMaxValueIncrementer incrementer3 = MaxValueIncrementerFactory.getIncrementer(mysql, "my_sequence"); assertSame(incrementer1, incrementer2); assertSame(incrementer2, incrementer3); }
@Test public void testGetIncrementer_CacheByDataSource() throws Exception { DataFieldMaxValueIncrementer incrementer1 = MaxValueIncrementerFactory.getIncrementer(mysql, "MY_SEQUENCE"); DataFieldMaxValueIncrementer incrementer2 = MaxValueIncrementerFactory.getIncrementer(oracle, "MY_SEQUENCE"); assertNotSame(incrementer1, incrementer2); }
@Test(expected = IllegalArgumentException.class) public void testGetIncrementer_NullDataSource() throws Exception { MaxValueIncrementerFactory.getIncrementer(null, "MY_SEQUENCE"); }
@Test(expected = IllegalArgumentException.class) public void testGetIncrementer_NullIncrementerName() throws Exception { MaxValueIncrementerFactory.getIncrementer(mysql, null); }
@Test(expected = IllegalArgumentException.class) public void testGetIncrementer_BlankIncrementerName() throws Exception { MaxValueIncrementerFactory.getIncrementer(mysql, ""); }
@Test public void testCustomIncrementerDatasourceVersion() throws Exception { SimpleConfig config = new SimpleConfig(); config.putProperty("rice.krad.data.platform.incrementer.mysql.5", "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerMySQLVersion5"); config.putProperty("rice.krad.data.platform.incrementer.oracle.11", "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerOracleVersion11"); ConfigContext.init(config); DataFieldMaxValueIncrementer mySQLMaxVal = MaxValueIncrementerFactory.getIncrementer(mysql,"test_mySQL"); assertTrue("Found MySQL custom incrementer",mySQLMaxVal != null); assertTrue("Custom incrementer for MySQL should be mySQL5 for String val", StringUtils.equals(mySQLMaxVal.nextStringValue(),"mySQL5")); DataFieldMaxValueIncrementer oracleMaxVal = MaxValueIncrementerFactory.getIncrementer(oracle,"test_oracle"); assertTrue("Found Oracle custom incrementer", oracleMaxVal != null); }
@Test public void testCustomIncrementerDatasourceNoVersion() throws Exception { SimpleConfig config = new SimpleConfig(); config.putProperty("rice.krad.data.platform.incrementer.mysql", "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerMySQLVersion5"); config.putProperty("rice.krad.data.platform.incrementer.oracle", "org.kuali.rice.krad.data.platform.testincrementers.CustomIncrementerOracleVersion11"); ConfigContext.init(config); DataFieldMaxValueIncrementer mySQLMaxVal = MaxValueIncrementerFactory.getIncrementer(mysql,"test_mySQL"); assertTrue("Found MySQL custom incrementer",mySQLMaxVal != null); assertTrue("Custom incrementer for MySQL should be mySQL5 for String val", StringUtils.equals(mySQLMaxVal.nextStringValue(),"mySQL5")); DataFieldMaxValueIncrementer oracleMaxVal = MaxValueIncrementerFactory.getIncrementer(oracle,"test_oracle"); assertTrue("Found Oracle custom incrementer", oracleMaxVal != null); }
@Test(expected = InstantiationError.class) public void testCustomIncrementerDatasourceInvalidClass() throws Exception { SimpleConfig config = new SimpleConfig(); config.putProperty("rice.krad.data.platform.incrementer.mysql", "org.kuali.rice.krad.data.platform.testincrementers.NonExistent"); ConfigContext.init(config); DataFieldMaxValueIncrementer mySQLMaxVal = MaxValueIncrementerFactory.getIncrementer(mysql,"test_mySQL"); assertTrue("Cannot create incrementer", mySQLMaxVal == null); }