diff --git a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java index 336be7c..c78e13d 100644 --- a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java +++ b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java @@ -34,7 +34,7 @@ import static com.moparisthebest.jdbc.codegen.JdbcMapperFactory.SUFFIX; @SupportedSourceVersion(SourceVersion.RELEASE_5) public class JdbcMapperProcessor extends AbstractProcessor { - public static final Pattern paramPattern = Pattern.compile("\\{(([^\\s]+)\\s+(([Nn][Oo][Tt]\\s+)?[Ii][Nn]\\s+))?([BbCc][Ll][Oo][Bb]\\s*:\\s*([^:]+\\s*:\\s*)?)?([^}]+)\\}"); + public static final Pattern paramPattern = Pattern.compile("\\{(([^\\s]+)\\s+(([Nn][Oo][Tt]\\s+)?[Ii][Nn]\\s+))?([BbCc][Ll][Oo][Bb]\\s*:\\s*([^:}]+\\s*:\\s*)?)?([^}]+)\\}"); public static final SourceVersion RELEASE_8; public static boolean java8; diff --git a/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/ParamPatternTest.java b/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/ParamPatternTest.java index bf473da..ea147df 100644 --- a/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/ParamPatternTest.java +++ b/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/ParamPatternTest.java @@ -2,38 +2,58 @@ package com.moparisthebest.jdbc.codegen; import org.junit.Test; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; import java.util.regex.Matcher; import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.paramPattern; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; public class ParamPatternTest { @Test public void testPattern() { - testMatch("{personNo}", null, null, null, null, null, null, "personNo"); - testMatch("{last_name IN lastNames}", "last_name IN ", "last_name", "IN ", null, null, null, "lastNames"); - testMatch("{last_name not in lastNames}", "last_name not in ", "last_name", "not in ", "not ", null, null, "lastNames"); - testMatch("{clob:comment}", null, null, null, null, "clob:", null, "comment"); - testMatch("{clob: comment}", null, null, null, null, "clob: ", null, "comment"); - testMatch("{blob: comment}", null, null, null, null, "blob: ", null, "comment"); - testMatch("{Blob: comment}", null, null, null, null, "Blob: ", null, "comment"); - testMatch("{blob:utf-16:comment}", null, null, null, null, "blob:utf-16:", "utf-16:", "comment"); + testMatch("{personNo}", s(null, null, null, null, null, null, "personNo")); + testMatch("{last_name IN lastNames}", s("last_name IN ", "last_name", "IN ", null, null, null, "lastNames")); + testMatch("{last_name not in lastNames}", s("last_name not in ", "last_name", "not in ", "not ", null, null, "lastNames")); + testMatch("{clob:comment}", s(null, null, null, null, "clob:", null, "comment")); + testMatch("{clob: comment}", s(null, null, null, null, "clob: ", null, "comment")); + testMatch("{blob: comment}", s(null, null, null, null, "blob: ", null, "comment")); + testMatch("{Blob: comment}", s(null, null, null, null, "Blob: ", null, "comment")); + testMatch("{blob:utf-16:comment}", s(null, null, null, null, "blob:utf-16:", "utf-16:", "comment")); + testMatch("{clob:comment} {clob:action}", c(a(null, null, null, null, "clob:", null, "comment"), a(null, null, null, null, "clob:", null, "action"))); String blobCharset = "utf-16:"; assertEquals("utf-16", blobCharset.substring(0, blobCharset.indexOf(':')).trim()); blobCharset = "utf-16 : "; assertEquals("utf-16", blobCharset.substring(0, blobCharset.indexOf(':')).trim()); } - private static void testMatch(final String sql, final String... expected) { + private static void testMatch(final String sql, final Collection expected) { final Matcher bindParamMatcher = paramPattern.matcher(sql); + final Iterator it = expected.iterator(); while (bindParamMatcher.find()) { final String[] matches = new String[bindParamMatcher.groupCount()]; for (int x = 0; x < matches.length; ) matches[x] = bindParamMatcher.group(++x); - //System.out.println(bindParamMatcher.group() + ": " + java.util.Arrays.toString(matches)); - assertArrayEquals(bindParamMatcher.group(), expected, matches); + //System.out.printf("sql: '%s', whole group: '%s', matches: '%s'%n", sql, bindParamMatcher.group(), java.util.Arrays.toString(matches)); + assertArrayEquals(bindParamMatcher.group(), it.next(), matches); } + assertFalse(it.hasNext()); + } + + private static Collection s(final String... s) { + return Collections.singleton(s); + } + + private static String[] a(final String... a) { + return a; + } + + private static Collection c(final String[]... c) { + return Arrays.asList(c); } }