2018-03-14 02:25:11 -04:00
package com.moparisthebest.jdbc.codegen ;
import org.junit.Test ;
2018-03-14 02:30:03 -04:00
import java.util.Arrays ;
import java.util.Collection ;
import java.util.Collections ;
import java.util.Iterator ;
2018-03-14 02:25:11 -04:00
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 ;
2018-03-14 02:30:03 -04:00
import static org.junit.Assert.assertFalse ;
2018-03-14 02:25:11 -04:00
public class ParamPatternTest {
@Test
public void testPattern ( ) {
2018-03-14 02:30:03 -04:00
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 " ) ) ) ;
2018-03-14 02:25:11 -04:00
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 ( ) ) ;
2019-02-07 01:09:15 -05:00
testMatch ( " {sql:sqlStatement} " , s ( null , null , null , null , " sql: " , null , " sqlStatement " ) ) ;
testMatch ( " {sql: sqlStatement} " , s ( null , null , null , null , " sql: " , null , " sqlStatement " ) ) ;
testMatch ( " {SQL: sqlStatement} " , s ( null , null , null , null , " SQL: " , null , " sqlStatement " ) ) ;
testMatch ( " {Sql: sqlStatement} " , s ( null , null , null , null , " Sql: " , null , " sqlStatement " ) ) ;
testMatch ( " {Sql : sqlStatement} " , s ( null , null , null , null , " Sql : " , null , " sqlStatement " ) ) ;
testMatch ( " {sql:person:sqlStatement} " , s ( null , null , null , null , " sql:person: " , " person: " , " sqlStatement " ) ) ;
testMatch ( " {sql:JOIN person ON p.person_no = b.person_no:sqlStatement} " , s ( null , null , null , null , " sql:JOIN person ON p.person_no = b.person_no: " , " JOIN person ON p.person_no = b.person_no: " , " sqlStatement " ) ) ;
2018-03-14 02:25:11 -04:00
}
2018-03-14 02:30:03 -04:00
private static void testMatch ( final String sql , final Collection < String [ ] > expected ) {
2018-03-14 02:25:11 -04:00
final Matcher bindParamMatcher = paramPattern . matcher ( sql ) ;
2018-03-14 02:30:03 -04:00
final Iterator < String [ ] > it = expected . iterator ( ) ;
2018-03-14 02:25:11 -04:00
while ( bindParamMatcher . find ( ) ) {
final String [ ] matches = new String [ bindParamMatcher . groupCount ( ) ] ;
for ( int x = 0 ; x < matches . length ; )
matches [ x ] = bindParamMatcher . group ( + + x ) ;
2018-03-14 02:30:03 -04:00
//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 ) ;
2018-03-14 02:25:11 -04:00
}
2018-03-14 02:30:03 -04:00
assertFalse ( it . hasNext ( ) ) ;
}
private static Collection < String [ ] > s ( final String . . . s ) {
return Collections . singleton ( s ) ;
}
private static String [ ] a ( final String . . . a ) {
return a ;
}
private static Collection < String [ ] > c ( final String [ ] . . . c ) {
return Arrays . asList ( c ) ;
2018-03-14 02:25:11 -04:00
}
}