View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 org.kuali.common.jdbc;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  
21  import org.apache.commons.lang3.StringUtils;
22  import org.junit.Assert;
23  import org.junit.Test;
24  import org.kuali.common.util.LocationUtils;
25  import org.kuali.common.util.Str;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  public class SqlReaderTest {
30  	final Logger logger = LoggerFactory.getLogger(SqlReaderTest.class);
31  
32  	@Test
33  	public void simpleWayToBreakThingsTest() throws IOException {
34  		SqlReader sqlReader = new DefaultSqlReader();
35  		BufferedReader reader = LocationUtils.getBufferedReaderFromString(getSql4());
36  		try {
37  			// This one is too complicated for the default sql reader
38  			Assert.assertEquals("SELECT '\n/\n'", sqlReader.getSqlStatement(reader));
39  		} catch (AssertionError e) {
40  			; // ignore
41  		}
42  	}
43  
44  	@Test
45  	public void simpleCommentTest() throws IOException {
46  		SqlReader sqlReader = new DefaultSqlReader();
47  		BufferedReader reader = LocationUtils.getBufferedReaderFromString(getSqlWithComment1());
48  		Assert.assertEquals("SELECT 1", sqlReader.getSqlStatement(reader));
49  		reader = LocationUtils.getBufferedReaderFromString(getSqlWithComment2());
50  		Assert.assertEquals("SELECT 1", sqlReader.getSqlStatement(reader));
51  		reader = LocationUtils.getBufferedReaderFromString(getSqlWithComment3());
52  		Assert.assertEquals("SELECT '\n-- Howdy'", sqlReader.getSqlStatement(reader));
53  	}
54  
55  	@Test
56  	public void simpleTest() throws IOException {
57  		SqlReader sqlReader = new DefaultSqlReader();
58  		String sql = "SELECT 1\r/\nSELECT 1\n/\nSELECT 1\r\n/";
59  		BufferedReader reader = LocationUtils.getBufferedReaderFromString(sql);
60  		String s = sqlReader.getSqlStatement(reader);
61  		while (s != null) {
62  			logger.info("[" + Str.flatten(s) + "]");
63  			s = sqlReader.getSqlStatement(reader);
64  		}
65  	}
66  
67  	@Test
68  	public void isBlankTest() {
69  		Assert.assertTrue(StringUtils.isBlank(" "));
70  		Assert.assertTrue(StringUtils.isBlank(System.getProperty("line.separator")));
71  		for (LineSeparator ls : LineSeparator.values()) {
72  			Assert.assertTrue(StringUtils.isBlank(ls.getValue()));
73  		}
74  	}
75  
76  	protected String getSqlWithComment1() {
77  		StringBuilder sb = new StringBuilder();
78  		sb.append("-- Howdy\n");
79  		sb.append("SELECT 1");
80  		return sb.toString();
81  	}
82  
83  	protected String getSqlWithComment2() {
84  		StringBuilder sb = new StringBuilder();
85  		sb.append("# Howdy\n");
86  		sb.append("SELECT 1");
87  		return sb.toString();
88  	}
89  
90  	protected String getSqlWithComment3() {
91  		StringBuilder sb = new StringBuilder();
92  		sb.append("-- Howdy\n");
93  		sb.append("SELECT '\n-- Howdy'");
94  		return sb.toString();
95  	}
96  
97  	protected String getSql4() {
98  		StringBuilder sb = new StringBuilder();
99  		sb.append("SELECT '\n/\n'");
100 		return sb.toString();
101 	}
102 
103 }