PoolUtil { public static String fillLogParams(String sql, Map<Object, Object> logParams) { StringBuilder result = new StringBuilder(); Map<Object, Object> tmpLogParam = (logParams == null ? new HashMap<Object, Object>() : logParams); Iterator<Object> it = tmpLogParam.values().iterator(); boolean inQuote = false; boolean inQuote2 = false; char[] sqlChar = sql != null ? sql.toCharArray() : new char[]{}; for (int i=0; i < sqlChar.length; i++){ if (sqlChar[i] == '\''){ inQuote = !inQuote; } if (sqlChar[i] == '"'){ inQuote2 = !inQuote2; } if (sqlChar[i] == '?' && !(inQuote || inQuote2)){ if (it.hasNext()){ result.append(prettyPrint(it.next())); } else { result.append('?'); } } else { result.append(sqlChar[i]); } } return result.toString(); }  static String fillLogParams(String sql, Map<Object, Object> logParams); static String stringifyException(Throwable t); static SQLException generateSQLException(String reason, Throwable t);  }
@Test public void testPoolUtil() throws SQLException{ Maps.newHashMap(); Map<Object, Object> logParams = new LinkedHashMap<Object, Object>(); logParams.put("1", "123"); logParams.put("2", "456"); logParams.put("3", new Blob() { public void truncate(long len) throws SQLException { } public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException { return 0; } public int setBytes(long pos, byte[] bytes) throws SQLException { return 0; } public OutputStream setBinaryStream(long pos) throws SQLException { return null; } public long position(Blob pattern, long start) throws SQLException { return 0; } public long position(byte[] pattern, long start) throws SQLException { return 0; } public long length() throws SQLException { return 5; } public byte[] getBytes(long pos, int length) throws SQLException { return null; } public InputStream getBinaryStream(long pos, long length) throws SQLException { return null; } public InputStream getBinaryStream() throws SQLException { return null; } public void free() throws SQLException { } }); logParams.put("4", new Clob() { public void truncate(long len) throws SQLException { } public int setString(long pos, String str, int offset, int len) throws SQLException { return 0; } public int setString(long pos, String str) throws SQLException { return 0; } public Writer setCharacterStream(long pos) throws SQLException { return null; } public OutputStream setAsciiStream(long pos) throws SQLException { return null; } public long position(Clob searchstr, long start) throws SQLException { return 0; } public long position(String searchstr, long start) throws SQLException { return 0; } public long length() throws SQLException { return 5; } public String getSubString(long pos, int length) throws SQLException { return null; } public Reader getCharacterStream(long pos, long length) throws SQLException { return null; } public Reader getCharacterStream() throws SQLException { return null; } public InputStream getAsciiStream() throws SQLException { return null; } public void free() throws SQLException { } }); logParams.put("5", new Array() { public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) throws SQLException { return null; } public ResultSet getResultSet(long index, int count) throws SQLException { return null; } public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException { return null; } public ResultSet getResultSet() throws SQLException { return null; } public String getBaseTypeName() throws SQLException { return "Base type name"; } public int getBaseType() throws SQLException { return 14; } public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException { return null; } public Object getArray(long index, int count) throws SQLException { return null; } public Object getArray(Map<String, Class<?>> map) throws SQLException { return null; } public Object getArray() throws SQLException { return null; } public void free() throws SQLException { } }); logParams.put("6", new Ref() { public void setObject(Object value) throws SQLException { } public Object getObject(Map<String, Class<?>> map) throws SQLException { return null; } public Object getObject() throws SQLException { return null; } public String getBaseTypeName() throws SQLException { return "type"; } }); logParams.put("7", new Integer(999)); assertEquals("ID='123' AND FOO='?' and LALA=\"BOO\" '456' (blob of length 5) (cblob of length 5) (array of type14) (ref of type4) 999 ?", PoolUtil.fillLogParams("ID=? AND FOO='?' and LALA=\"BOO\" ? ? ? ? ? ? ?", logParams)); }
@Test public void testPoolUtilExceptions() throws SQLException{ Map<Object, Object> logParams = new LinkedHashMap<Object, Object>(); logParams.put("1", "123"); logParams.put("2", null); Blob mockBlob = createNiceMock(Blob.class); expect(mockBlob.length()).andThrow(new SQLException()); replay(mockBlob); logParams.put("3", mockBlob); Clob mockClob = createNiceMock(Clob.class); expect(mockClob.length()).andThrow(new SQLException()); replay(mockClob); logParams.put("4", mockClob); Array mockArray = createNiceMock(java.sql.Array.class); expect(mockArray.getBaseTypeName()).andThrow(new SQLException()); replay(mockArray); logParams.put("5", mockArray); Ref mockSerialRef = createNiceMock(Ref.class); expect(mockSerialRef.getBaseTypeName()).andThrow(new SQLException()); replay(mockSerialRef); logParams.put("6", mockSerialRef); assertEquals("ID='123' AND FOO='?' and LALA=\"BOO\" NULL (blob of unknown length) (cblob of unknown length) (array of unknown type) (ref of unknown type) ?", PoolUtil.fillLogParams("ID=? AND FOO='?' and LALA=\"BOO\" ? ? ? ? ? ?", logParams)); }
@Test public void testPoolUtilNull() throws SQLException{ PoolUtil.fillLogParams(null, null); }