Author: cbegin Date: Sat Feb 13 23:48:12 2010 New Revision: 909932 URL: http://svn.apache.org/viewvc?rev=909932&view=rev Log: Added simple name resolution to configuration.
Modified: ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/session/Configuration.java ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/session/SqlSessionTest.java Modified: ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/session/Configuration.java URL: http://svn.apache.org/viewvc/ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/session/Configuration.java?rev=909932&r1=909931&r2=909932&view=diff ============================================================================== --- ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/session/Configuration.java (original) +++ ibatis/java/ibatis-3/trunk/ibatis-3-core/src/main/java/org/apache/ibatis/session/Configuration.java Sat Feb 13 23:48:12 2010 @@ -366,7 +366,8 @@ return mappedStatements.containsKey(statementName); } - protected static class StrictMap<J, K> extends HashMap<J, K> { + + protected static class StrictMap<J extends String, K extends Object> extends HashMap<J, K> { private String name; @@ -392,15 +393,47 @@ public K put(J key, K value) { if (containsKey(key)) throw new IllegalArgumentException(name + " already contains value for " + key); + if (key.contains(".")) { + final String shortKey = getShortName(key); + if (super.get(shortKey) == null) { + super.put((J)shortKey, value); + } else { + super.put((J)shortKey, (K)new Ambiguity(shortKey)); + } + } return super.put(key, value); } public K get(Object key) { K value = super.get(key); - if (value == null) throw new IllegalArgumentException(name + " does not contain value for " + key); + if (value == null) { + value = super.get(getShortName((J)key)); + if (value == null) { + throw new IllegalArgumentException(name + " does not contain value for " + key); + } + } + if (value instanceof Ambiguity) { + throw new IllegalArgumentException(((Ambiguity)value).getSubject() + + " is ambiguous in " + name + " (try using the full name including the namespace, or rename one of the entries)"); + } return value; } + private String getShortName(J key) { + final String[] keyparts = key.split("\\."); + final String shortKey = keyparts[keyparts.length-1]; + return shortKey; + } + + protected static class Ambiguity { + private String subject; + public Ambiguity(String subject) { + this.subject = subject; + } + public String getSubject() { + return subject; + } + } } } Modified: ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/session/SqlSessionTest.java URL: http://svn.apache.org/viewvc/ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/session/SqlSessionTest.java?rev=909932&r1=909931&r2=909932&view=diff ============================================================================== --- ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/session/SqlSessionTest.java (original) +++ ibatis/java/ibatis-3/trunk/ibatis-3-core/src/test/java/org/apache/ibatis/session/SqlSessionTest.java Sat Feb 13 23:48:12 2010 @@ -4,6 +4,7 @@ import domain.blog.mappers.AuthorMapper; import domain.blog.mappers.BlogMapper; import org.apache.ibatis.BaseDataTest; +import org.apache.ibatis.cache.impl.PerpetualCache; import org.apache.ibatis.io.Resources; import static org.junit.Assert.*; import org.junit.BeforeClass; @@ -27,6 +28,68 @@ } @Test + public void shouldResolveBothSimpleNameAndFullyQualifiedName() { + Configuration c = new Configuration(); + final String fullName = "com.mycache.MyCache"; + final String shortName = "MyCache"; + final PerpetualCache cache = new PerpetualCache(fullName); + c.addCache(cache); + assertEquals(cache, c.getCache(fullName)); + assertEquals(cache, c.getCache(shortName)); + } + + @Test + public void shouldFailOverToMostApplicableSimpleName() { + Configuration c = new Configuration(); + final String fullName = "com.mycache.MyCache"; + final String invalidName = "unknown.namespace.MyCache"; + final PerpetualCache cache = new PerpetualCache(fullName); + c.addCache(cache); + assertEquals(cache, c.getCache(fullName)); + assertEquals(cache, c.getCache(invalidName)); + } + + @Test + public void shouldSucceedWhenFullyQualifiedButFailDueToAmbiguity() { + Configuration c = new Configuration(); + + final String name1 = "com.mycache.MyCache"; + final PerpetualCache cache1 = new PerpetualCache(name1); + c.addCache(cache1); + + final String name2 = "com.other.MyCache"; + final PerpetualCache cache2 = new PerpetualCache(name2); + c.addCache(cache2); + + final String shortName = "MyCache"; + + assertEquals(cache1, c.getCache(name1)); + assertEquals(cache2, c.getCache(name2)); + + try { + c.getCache(shortName); + fail("Exception expected."); + } catch (Exception e) { + assertTrue(e.getMessage().contains("ambiguous")); + } + + } + + @Test + public void shouldFailToAddDueToNameConflict() { + Configuration c = new Configuration(); + final String fullName = "com.mycache.MyCache"; + final PerpetualCache cache = new PerpetualCache(fullName); + try { + c.addCache(cache); + c.addCache(cache); + fail("Exception expected."); + } catch (Exception e) { + assertTrue(e.getMessage().contains("already contains value")); + } + } + + @Test public void shouldSelectAllAuthors() throws Exception { SqlSession session = sqlMapper.openSession(TransactionIsolationLevel.SERIALIZABLE); try {