1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.jdbc;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.util.List;
21
22 import org.apache.commons.lang3.StringUtils;
23 import org.junit.Assert;
24 import org.junit.Test;
25 import org.kuali.common.util.LocationUtils;
26 import org.kuali.common.util.Str;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class DefaultSqlReaderTest {
31
32 final Logger logger = LoggerFactory.getLogger(DefaultSqlReaderTest.class);
33
34 @Test
35 public void mySQLDumpTest() throws IOException {
36 try {
37 SqlReader sqlReader = new MySQLDumpReader();
38
39 BufferedReader reader = LocationUtils.getBufferedReader("classpath:mysqldump.sql");
40 List<String> sql = sqlReader.getSql(reader);
41 while (sql != null) {
42 for (String s : sql) {
43 logger.info(s);
44 }
45 sql = sqlReader.getSql(reader);
46 }
47 } catch (Exception e) {
48 e.printStackTrace();
49 }
50 }
51
52 @Test
53 public void simpleWayToBreakThingsTest() throws IOException {
54 SqlReader sqlReader = new DefaultSqlReader();
55 BufferedReader reader = LocationUtils.getBufferedReaderFromString(getSql4());
56 try {
57
58 List<String> sql = sqlReader.getSql(reader);
59 String s = sql.get(0);
60 Assert.assertEquals("SELECT '\n/\n'", s);
61 } catch (AssertionError e) {
62 ;
63 }
64 }
65
66 protected String getFirst(SqlReader reader, BufferedReader in) throws IOException {
67 List<String> sql = reader.getSql(in);
68 return sql.get(0);
69 }
70
71 @Test
72 public void simpleCommentTest() throws IOException {
73 SqlReader sqlReader = new DefaultSqlReader();
74 BufferedReader reader = LocationUtils.getBufferedReaderFromString(getSqlWithComment1());
75 Assert.assertEquals("SELECT 1", getFirst(sqlReader, reader));
76 reader = LocationUtils.getBufferedReaderFromString(getSqlWithComment2());
77 Assert.assertEquals("SELECT 1", getFirst(sqlReader, reader));
78 reader = LocationUtils.getBufferedReaderFromString(getSqlWithComment3());
79 Assert.assertEquals("SELECT '\n-- Howdy'", getFirst(sqlReader, reader));
80 }
81
82 @Test
83 public void simpleTest() throws IOException {
84 SqlReader sqlReader = new DefaultSqlReader();
85 String sql = "SELECT 1\r/\nSELECT 1\n/\nSELECT 1\r\n/";
86 BufferedReader reader = LocationUtils.getBufferedReaderFromString(sql);
87 String s = getFirst(sqlReader, reader);
88 while (s != null) {
89 logger.info("[" + Str.flatten(s) + "]");
90 s = getFirst(sqlReader, reader);
91 }
92 }
93
94 @Test
95 public void isBlankTest() {
96 Assert.assertTrue(StringUtils.isBlank(" "));
97 Assert.assertTrue(StringUtils.isBlank(System.getProperty("line.separator")));
98 for (LineSeparator ls : LineSeparator.values()) {
99 Assert.assertTrue(StringUtils.isBlank(ls.getValue()));
100 }
101 }
102
103 protected String getSqlWithComment1() {
104 StringBuilder sb = new StringBuilder();
105 sb.append("-- Howdy\n");
106 sb.append("SELECT 1");
107 return sb.toString();
108 }
109
110 protected String getSqlWithComment2() {
111 StringBuilder sb = new StringBuilder();
112 sb.append("# Howdy\n");
113 sb.append("SELECT 1");
114 return sb.toString();
115 }
116
117 protected String getSqlWithComment3() {
118 StringBuilder sb = new StringBuilder();
119 sb.append("-- Howdy\n");
120 sb.append("SELECT '\n-- Howdy'");
121 return sb.toString();
122 }
123
124 protected String getSql4() {
125 StringBuilder sb = new StringBuilder();
126 sb.append("SELECT '\n/\n'");
127 return sb.toString();
128 }
129
130 }