PageRenderTime 62ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/commons/src/test/java/com/navercorp/pinpoint/common/util/DefaultSqlParserTest.java

https://gitlab.com/hansol6566/pinpoint
Java | 369 lines | 263 code | 82 blank | 24 comment | 1 complexity | 7cedc56ef52a09e5a59a7438dbbf1cb2 MD5 | raw file
  1. /*
  2. * Copyright 2014 NAVER Corp.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.navercorp.pinpoint.common.util;
  17. import org.junit.Assert;
  18. import org.junit.Test;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import java.util.List;
  22. /**
  23. * @author emeroad
  24. */
  25. public class DefaultSqlParserTest {
  26. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  27. private SqlParser sqlParser = new DefaultSqlParser();
  28. private OutputParameterParser outputParameterParser = new OutputParameterParser();
  29. @Test
  30. public void normalizedSql() {
  31. NormalizedSql parsingResult = sqlParser.normalizedSql("select * from table a = 1 and b=50 and c=? and d='11'");
  32. String s = parsingResult.getNormalizedSql();
  33. logger.debug(s);
  34. logger.debug(parsingResult.getParseParameter());
  35. NormalizedSql parsingResult2 = sqlParser.normalizedSql(" ");
  36. String s2 = parsingResult2.getNormalizedSql();
  37. logger.debug(s2);
  38. logger.debug("{}", (char) -1);
  39. String str = "s";
  40. logger.debug("{}", str.codePointAt(0));
  41. logger.debug("{}", (int) str.charAt(0));
  42. logger.debug("high:{}", Character.MAX_HIGH_SURROGATE);
  43. logger.debug("low:{}", Character.MIN_LOW_SURROGATE);
  44. logger.debug("{}", (int) Character.MIN_LOW_SURROGATE);
  45. logger.debug("{}", (int) Character.MAX_HIGH_SURROGATE);
  46. NormalizedSql parsingResult3 = sqlParser.normalizedSql("''");
  47. String s3 = parsingResult3.getNormalizedSql();
  48. logger.debug("s3:{}", s3);
  49. logger.debug("sb3:{}", parsingResult3.getParseParameter());
  50. }
  51. @Test
  52. public void nullCheck() {
  53. sqlParser.normalizedSql(null);
  54. }
  55. @Test
  56. public void complex() {
  57. assertEqual("select * from table a = 1 and b=50 and c=? and d='11'",
  58. "select * from table a = 0# and b=1# and c=? and d='2$'", "1,50,11");
  59. assertEqual("select * from table a = -1 and b=-50 and c=? and d='-11'",
  60. "select * from table a = -0# and b=-1# and c=? and d='2$'", "1,50,-11");
  61. assertEqual("select * from table a = +1 and b=+50 and c=? and d='+11'",
  62. "select * from table a = +0# and b=+1# and c=? and d='2$'", "1,50,+11");
  63. assertEqual("select * from table a = 1/*test*/ and b=50/*test*/ and c=? and d='11'",
  64. "select * from table a = 0#/*test*/ and b=1#/*test*/ and c=? and d='2$'", "1,50,11");
  65. assertEqual("select ZIPCODE,CITY from ZIPCODE");
  66. assertEqual("select a.ZIPCODE,a.CITY from ZIPCODE as a");
  67. assertEqual("select ZIPCODE,123 from ZIPCODE",
  68. "select ZIPCODE,0# from ZIPCODE", "123");
  69. assertEqual("SELECT * from table a=123 and b='abc' and c=1-3",
  70. "SELECT * from table a=0# and b='1$' and c=2#-3#", "123,abc,1,3");
  71. assertEqual("SYSTEM_RANGE(1, 10)",
  72. "SYSTEM_RANGE(0#, 1#)", "1,10");
  73. }
  74. @Test
  75. public void etcState() {
  76. assertEqual("test.abc", "test.abc", "");
  77. assertEqual("test.abc123", "test.abc123", "");
  78. assertEqual("test.123", "test.123", "");
  79. }
  80. @Test
  81. public void objectEquals() {
  82. assertEqualObject("test.abc");
  83. assertEqualObject("test.abc123");
  84. assertEqualObject("test.123");
  85. }
  86. @Test
  87. public void numberState() {
  88. assertEqual("123", "0#", "123");
  89. // just converting numbers as it is too much work to find out if '-' represents a negative number, or is part of the SQL expression
  90. assertEqual("-123", "-0#", "123");
  91. assertEqual("+123", "+0#", "123");
  92. assertEqual("1.23", "0#", "1.23");
  93. assertEqual("1.23.34", "0#", "1.23.34");
  94. assertEqual("123 456", "0# 1#", "123,456");
  95. assertEqual("1.23 4.56", "0# 1#", "1.23,4.56");
  96. assertEqual("1.23-4.56", "0#-1#", "1.23,4.56");
  97. assertEqual("1<2", "0#<1#", "1,2");
  98. assertEqual("1< 2", "0#< 1#", "1,2");
  99. assertEqual("(1< 2)", "(0#< 1#)", "1,2");
  100. assertEqual("-- 1.23", "-- 1.23", "");
  101. assertEqual("- -1.23", "- -0#", "1.23");
  102. assertEqual("--1.23", "--1.23", "");
  103. assertEqual("/* 1.23 */", "/* 1.23 */", "");
  104. assertEqual("/*1.23*/", "/*1.23*/", "");
  105. assertEqual("/* 1.23 \n*/", "/* 1.23 \n*/", "");
  106. assertEqual("test123", "test123", "");
  107. assertEqual("test_123", "test_123", "");
  108. assertEqual("test_ 123", "test_ 0#", "123");
  109. // this is effectively an impossible token
  110. assertEqual("123tst", "0#tst", "123");
  111. }
  112. @Test
  113. public void numberState2() {
  114. assertEqual("1.23e", "0#", "1.23e");
  115. assertEqual("1.23E", "0#", "1.23E");
  116. // just converting numbers as it is too much work to find out if '-' represents a negative number, or is part of the SQL expression
  117. assertEqual("1.4e-10", "0#-1#", "1.4e,10");
  118. }
  119. @Test
  120. public void singleLineCommentState() {
  121. assertEqual("--", "--", "");
  122. assertEqual("//", "//", "");
  123. assertEqual("--123", "--123", "");
  124. assertEqual("//123", "//123", "");
  125. assertEqual("--test", "--test");
  126. assertEqual("//test", "//test");
  127. assertEqual("--test\ntest", "--test\ntest", "");
  128. assertEqual("--test\t\n", "--test\t\n", "");
  129. assertEqual("--test\n123 test", "--test\n0# test", "123");
  130. }
  131. @Test
  132. public void multiLineCommentState() {
  133. assertEqual("/**/", "/**/", "");
  134. assertEqual("/* */", "/* */", "");
  135. assertEqual("/* */abc", "/* */abc", "");
  136. assertEqual("/* * */", "/* * */", "");
  137. assertEqual("/* * */", "/* * */", "");
  138. assertEqual("/* abc", "/* abc", "");
  139. assertEqual("select * from table", "select * from table", "");
  140. }
  141. @Test
  142. public void symbolState() {
  143. assertEqual("''", "''", "");
  144. assertEqual("'abc'", "'0$'", "abc");
  145. assertEqual("'a''bc'", "'0$'", "a''bc");
  146. assertEqual("'a' 'bc'", "'0$' '1$'", "a,bc");
  147. assertEqual("'a''bc' 'a''bc'", "'0$' '1$'", "a''bc,a''bc");
  148. assertEqual("select * from table where a='a'", "select * from table where a='0$'", "a");
  149. }
  150. // @Test
  151. public void charout() {
  152. for (int i = 11; i < 67; i++) {
  153. logger.debug("{}", (char) i);
  154. }
  155. }
  156. @Test
  157. public void commentAndSymbolCombine() {
  158. assertEqual("/* 'test' */", "/* 'test' */", "");
  159. assertEqual("/* 'test'' */", "/* 'test'' */", "");
  160. assertEqual("/* '' */", "/* '' */");
  161. assertEqual("/* */ 123 */", "/* */ 0# */", "123");
  162. assertEqual("' /* */'", "'0$'", " /* */");
  163. }
  164. @Test
  165. public void separatorTest() {
  166. assertEqual("1234 456,7", "0# 1#,2#", "1234,456,7");
  167. assertEqual("'1234 456,7'", "'0$'", "1234 456,,7");
  168. assertEqual("'1234''456,7'", "'0$'", "1234''456,,7");
  169. NormalizedSql parsingResult2 = this.sqlParser.normalizedSql("'1234''456,7'");
  170. logger.debug("{}", parsingResult2);
  171. // for string token
  172. assertEqual("'1234' '456,7'", "'0$' '1$'", "1234,456,,7");
  173. }
  174. @Test
  175. public void combineTest() {
  176. assertCombine("123 345", "0# 1#", "123,345");
  177. assertCombine("123 345 'test'", "0# 1# '2$'", "123,345,test");
  178. assertCombine("1 2 3 4 5 6 7 8 9 10 11", "0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10#", "1,2,3,4,5,6,7,8,9,10,11");
  179. }
  180. @Test
  181. public void combineErrorTest() {
  182. assertCombineErrorCase("123 10#", "0# 10#", "123,345");
  183. assertCombineErrorCase("1 3 10#", "0# 2# 10#", "1,2,3");
  184. assertCombineErrorCase("1 2 3", "0# 2 3", "1,2,3");
  185. assertCombineErrorCase("1 2 10", "0# 2 10", "1,2,3");
  186. assertCombineErrorCase("1 2 201", "0# 2 201", "1,2,3");
  187. assertCombineErrorCase("1 2 11", "0# 2 10#", "1,2,3,4,5,6,7,8,9,10,11");
  188. }
  189. @Test
  190. public void combineBindValue() {
  191. OutputParameterParser parameterParser = new OutputParameterParser();
  192. String sql = "select * from table a = 1 and b=50 and c=? and d='11'";
  193. String expected = "select * from table a = 1 and b=50 and c='foo' and d='11'";
  194. List<String> bindValues = parameterParser.parseOutputParameter("foo");
  195. String result = sqlParser.combineBindValues(sql, bindValues);
  196. Assert.assertEquals(expected, result);
  197. sql = "select * from table a = ? and b=? and c=? and d=?";
  198. expected = "select * from table a = '1' and b='50' and c='foo' and d='11'";
  199. bindValues = parameterParser.parseOutputParameter("1, 50, foo, 11");
  200. result = sqlParser.combineBindValues(sql, bindValues);
  201. Assert.assertEquals(expected, result);
  202. sql = "select * from table id = \"foo ? bar\" and number=?";
  203. expected = "select * from table id = \"foo ? bar\" and number='99'";
  204. bindValues = parameterParser.parseOutputParameter("99");
  205. result = sqlParser.combineBindValues(sql, bindValues);
  206. Assert.assertEquals(expected, result);
  207. sql = "select * from table id = 'hi ? name''s foo' and number=?";
  208. expected = "select * from table id = 'hi ? name's foo' and number='99'";
  209. bindValues = parameterParser.parseOutputParameter("99");
  210. result = sqlParser.combineBindValues(sql, bindValues);
  211. Assert.assertEquals(expected, result);
  212. sql = "/** comment ? */ select * from table id = ?";
  213. expected = "/** comment ? */ select * from table id = 'foo,bar'";
  214. bindValues = parameterParser.parseOutputParameter("foo,,bar");
  215. result = sqlParser.combineBindValues(sql, bindValues);
  216. Assert.assertEquals(expected, result);
  217. sql = "select /*! STRAIGHT_JOIN ? */ * from table id = ?";
  218. expected = "select /*! STRAIGHT_JOIN ? */ * from table id = 'foo,bar'";
  219. bindValues = parameterParser.parseOutputParameter("foo,,bar");
  220. result = sqlParser.combineBindValues(sql, bindValues);
  221. Assert.assertEquals(expected, result);
  222. sql = "select * from table id = ?; -- This ? comment";
  223. expected = "select * from table id = 'foo'; -- This ? comment";
  224. bindValues = parameterParser.parseOutputParameter("foo");
  225. result = sqlParser.combineBindValues(sql, bindValues);
  226. Assert.assertEquals(expected, result);
  227. }
  228. private void assertCombine(String result, String sql, String outputParams) {
  229. List<String> output = this.outputParameterParser.parseOutputParameter(outputParams);
  230. NormalizedSql parsingResult = this.sqlParser.normalizedSql(result);
  231. Assert.assertEquals("sql", parsingResult.getNormalizedSql(), sql);
  232. String combine = this.sqlParser.combineOutputParams(sql, output);
  233. Assert.assertEquals("combine", result, combine);
  234. }
  235. private void assertCombineErrorCase(String expectedError, String sql, String outputParams) {
  236. List<String> output = this.outputParameterParser.parseOutputParameter(outputParams);
  237. // ParsingResult parsingResult = this.sqlParser.normalizedSql(result);
  238. String combine = this.sqlParser.combineOutputParams(sql, output);
  239. Assert.assertEquals("combine", expectedError, combine);
  240. }
  241. private void assertEqual(String expected) {
  242. NormalizedSql parsingResult = sqlParser.normalizedSql(expected);
  243. String normalizedSql = parsingResult.getNormalizedSql();
  244. try {
  245. Assert.assertEquals(expected, normalizedSql);
  246. } catch (AssertionError e) {
  247. logger.warn("Original :{}", expected);
  248. throw e;
  249. }
  250. }
  251. private void assertEqual(String expected, String actual) {
  252. NormalizedSql parsingResult = sqlParser.normalizedSql(expected);
  253. String normalizedSql = parsingResult.getNormalizedSql();
  254. try {
  255. Assert.assertEquals(actual, normalizedSql);
  256. } catch (AssertionError e) {
  257. logger.warn("Original :{}", expected);
  258. throw e;
  259. }
  260. }
  261. private void assertEqual(String expected, String actual, String outputExpected) {
  262. NormalizedSql parsingResult = sqlParser.normalizedSql(expected);
  263. String normalizedSql = parsingResult.getNormalizedSql();
  264. String output = parsingResult.getParseParameter();
  265. List<String> outputParams = outputParameterParser.parseOutputParameter(output);
  266. String s = sqlParser.combineOutputParams(normalizedSql, outputParams);
  267. logger.debug("combine:" + s);
  268. try {
  269. Assert.assertEquals("normalizedSql check", actual, normalizedSql);
  270. } catch (AssertionError e) {
  271. logger.warn("Original :{}", expected);
  272. throw e;
  273. }
  274. Assert.assertEquals("outputParam check", outputExpected, parsingResult.getParseParameter());
  275. }
  276. private void assertEqualObject(String expected) {
  277. NormalizedSql parsingResult = sqlParser.normalizedSql(expected);
  278. String normalizedSql = parsingResult.getNormalizedSql();
  279. try {
  280. Assert.assertEquals("normalizedSql check", expected, normalizedSql);
  281. Assert.assertSame(expected, normalizedSql);
  282. } catch (AssertionError e) {
  283. logger.warn("Original :{}", expected);
  284. throw e;
  285. }
  286. }
  287. }