1 2 package net.sf.voruta; 3 4 import junit.framework.*; 5 import java.util.*; 6 import java.lang.reflect.*; 7 8 /*** 9 * 10 * @author baliuka 11 */ 12 public class SQLParserTest extends TestCase { 13 14 public SQLParserTest(java.lang.String testName) { 15 super(testName); 16 } 17 18 public static void main(java.lang.String[] args) { 19 junit.textui.TestRunner.run(suite()); 20 } 21 22 public static Test suite() { 23 TestSuite suite = new TestSuite(SQLParserTest.class); 24 return suite; 25 } 26 27 static class ClassFinderImpl implements ClassFinder{ 28 29 public Class findByName(String name) { 30 31 try{ 32 33 return Class.forName(name); 34 35 }catch( ClassNotFoundException cnfe ){ 36 throw new NoClassDefFoundError(cnfe.getMessage()); 37 } 38 } 39 40 } 41 42 public interface Methods{ 43 public static final ThreadLocal CALLER = new ThreadLocal(); 44 public static final int CONST = 0; 45 void method(); 46 void method1(String a); 47 void method2(String a,String b); 48 49 } 50 51 static SqlParser getParser( String query, 52 String jdbcString, 53 String methodName, Object args[])throws Exception{ 54 55 Method methods[] = Methods.class.getMethods(); 56 57 Method method = null; 58 for(int i = 0; i< methods.length; i++ ){ 59 60 if( methodName.equals(methods[i].getName()) ){ 61 method = methods[i]; 62 break; 63 } 64 } 65 assertTrue(method != null); 66 SqlParser parser = new SqlParser(); 67 parser.compile( 68 new ClassFinderImpl(), 69 method, 70 query, 71 new java.util.Properties()); 72 73 assertEquals(jdbcString,parser.generateSql(args) ); 74 75 return parser; 76 77 } 78 79 public void testStaticQuery(String query, String jdbcString)throws Exception{ 80 81 getParser(query,jdbcString,"method",new Object[]{}); 82 83 } 84 85 public void testDynamicQuery(String query, String jdbcString, Object args[])throws Exception{ 86 87 getParser(query,jdbcString,"method" + args.length,args); 88 89 } 90 91 public void testThreadLocalParam()throws Exception{ 92 Methods.CALLER.set("baliuka"); 93 SqlParser p = getParser( "$CALLER", "?","method", new Object[]{}); 94 assertEquals( Methods.CALLER.get(), p.prepare(null)[0] ); 95 p = getParser( "$" + Methods.class.getName() + ".CALLER", "?","method", new Object[]{}); 96 assertEquals( Methods.CALLER.get(), p.prepare(null)[0] ); 97 98 } 99 100 public void testParser()throws Exception{ 101 102 testStaticQuery("$$$$$$","$$$"); 103 testStaticQuery("$1","?"); 104 testStaticQuery("$1$1","??"); 105 testStaticQuery("$$1$1","$1?"); 106 testStaticQuery("${CONST}","0"); 107 testStaticQuery("${CONST}$1","0?"); 108 testDynamicQuery("${1}${2}","12",new String[]{"1","2"}); 109 testDynamicQuery("${CONST}${1}${2}","012",new String[]{"1","2"}); 110 testDynamicQuery("${CONST}TEST${1}","0TEST1",new String[]{"1"}); 111 112 } 113 114 115 }

This page was automatically generated by Maven