Skip to content

Commit

Permalink
New test case to show how to use enum constants in mapped statements
Browse files Browse the repository at this point in the history
  • Loading branch information
mnesarco committed Apr 18, 2013
1 parent fb56e7a commit 9c2fb55
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/test/java/mybatis-velocity.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
additional.context.attributes=trailingWildCardFormatter:org.mybatis.scripting.velocity.use.TrailingWildCardFormatter
additional.context.attributes=trailingWildCardFormatter:org.mybatis.scripting.velocity.use.TrailingWildCardFormatter,enumBinder:org.mybatis.scripting.velocity.use.EnumBinder
userdirective=org.mybatis.scripting.velocity.use.CustomUserDirective
28 changes: 28 additions & 0 deletions src/test/java/org/mybatis/scripting/velocity/use/EnumBinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2013 MyBatis.org.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.scripting.velocity.use;

/**
*
* @author mnesarco
*/
public class EnumBinder {

public EnumWrapper bind(String className) throws ClassNotFoundException {
return new EnumWrapper(Class.forName(className));
}

}
38 changes: 38 additions & 0 deletions src/test/java/org/mybatis/scripting/velocity/use/EnumWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2013 MyBatis.org.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.scripting.velocity.use;

import java.util.HashMap;

/**
*
* @author mnesarco
*/
public class EnumWrapper extends HashMap<String, Integer> {

public EnumWrapper(Class<?> e) {
if (e.isEnum()) {
Object[] consts = e.getEnumConstants();
for (int i = 0; i<consts.length; i++) {
put(consts[i].toString(), i);
}
}
else {
throw new IllegalArgumentException("Supplied argument is not an enum class");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public class VelocityLanguageTest {

protected static SqlSessionFactory sqlSessionFactory;

public enum IDS {
ZERO, ONE, TWO, THREE, FOUR, FIVE
}

@BeforeClass
public static void setUp() throws Exception {
Connection conn = null;
Expand Down Expand Up @@ -145,6 +149,22 @@ public void testSelectNamesWithFormattedParam() {
}
}

@Test
public void testEnumBinding() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {

List<Name> answer = sqlSession.selectList("org.mybatis.scripting.velocity.use.selectEnumBinding");
assertEquals(3, answer.size());
for (Name n : answer) {
assertEquals("Flintstone", n.getLastName());
}

} finally {
sqlSession.close();
}
}

@Test
public void testSelectNamesWithFormattedParamSafe() {
SqlSession sqlSession = sqlSessionFactory.openSession();
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/org/mybatis/scripting/velocity/use/mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,12 @@
VALUES (@{firstName}, @{lastName})
</insert>

<select id="selectEnumBinding" resultType="org.mybatis.scripting.velocity.use.Name">
#set( $ids = $enumBinder.bind('org.mybatis.scripting.velocity.use.VelocityLanguageTest$IDS') )
SELECT *
FROM names
WHERE
id IN (@{ids.ONE}, @{ids.TWO}, @{ids.THREE})
</select>

</mapper>

0 comments on commit 9c2fb55

Please sign in to comment.