001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql;
022:
023: import java.io.ByteArrayInputStream;
024: import java.io.CharArrayReader;
025: import java.io.File;
026: import java.io.FileWriter;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.Reader;
030: import java.io.Writer;
031: import java.sql.SQLException;
032: import java.util.Arrays;
033:
034: import org.easymock.EasyMock;
035: import org.easymock.IMocksControl;
036: import org.testng.annotations.AfterMethod;
037: import org.testng.annotations.DataProvider;
038: import org.testng.annotations.Test;
039:
040: /**
041: * Unit test for {@link FileSupportImpl}.
042: *
043: * @author Paul Ferraro
044: * @since 1.1
045: */
046: @SuppressWarnings("nls")
047: public class TestFileSupportImpl implements FileSupport {
048: private IMocksControl control = EasyMock.createStrictControl();
049:
050: private FileSupport fileSupport = new FileSupportImpl();
051:
052: @AfterMethod
053: void reset() {
054: this .fileSupport.close();
055: this .control.reset();
056: }
057:
058: /**
059: * @see net.sf.hajdbc.sql.FileSupport#close()
060: */
061: @Test
062: public void close() {
063: try {
064: File file = this .fileSupport
065: .createFile(new ByteArrayInputStream(new byte[0]));
066:
067: assert file.exists();
068:
069: this .fileSupport.close();
070:
071: assert !file.exists();
072: } catch (SQLException e) {
073: assert false : e;
074: }
075: }
076:
077: @DataProvider(name="inputStream")
078: Object[][] inputStreamProvider() {
079: return new Object[][] { new Object[] { new ByteArrayInputStream(
080: new byte[] { 1, 2, 3, 4 }) } };
081: }
082:
083: /**
084: * @see net.sf.hajdbc.sql.FileSupport#createFile(java.io.InputStream)
085: */
086: @Test(dataProvider="inputStream")
087: public File createFile(InputStream inputStream) throws SQLException {
088: File file = this .fileSupport.createFile(inputStream);
089:
090: assert file != null;
091: assert file.exists();
092: assert file.getName().startsWith("ha-jdbc-") : file.getName();
093: assert file.getName().endsWith(".lob") : file.getName();
094: assert file.length() == 4 : file.length();
095:
096: return file;
097: }
098:
099: @DataProvider(name="reader")
100: Object[][] readerProvider() {
101: return new Object[][] { new Object[] { new CharArrayReader(
102: "abcd".toCharArray()) } };
103: }
104:
105: /**
106: * @see net.sf.hajdbc.sql.FileSupport#createFile(java.io.Reader)
107: */
108: @Test(dataProvider="reader")
109: public File createFile(Reader reader) throws SQLException {
110: File file = this .fileSupport.createFile(reader);
111:
112: assert file != null;
113: assert file.exists();
114: assert file.getName().startsWith("ha-jdbc-") : file.getName();
115: assert file.getName().endsWith(".lob") : file.getName();
116: assert file.length() == 4 : file.length();
117:
118: return file;
119: }
120:
121: @DataProvider(name="file")
122: Object[][] fileProvider() throws IOException {
123: File file = File.createTempFile("test", ".test");
124:
125: Writer writer = new FileWriter(file);
126: writer.write("abcd");
127: writer.flush();
128: writer.close();
129:
130: return new Object[][] { new Object[] { file } };
131: }
132:
133: /**
134: * @see net.sf.hajdbc.sql.FileSupport#getInputStream(java.io.File)
135: */
136: @Test(dataProvider="file")
137: public InputStream getInputStream(File file) throws SQLException {
138: InputStream inputStream = this .fileSupport.getInputStream(file);
139:
140: byte[] buffer = new byte[4];
141:
142: assert inputStream != null;
143:
144: try {
145: assert inputStream.read(buffer) == 4;
146: assert Arrays.equals(buffer, "abcd".getBytes());
147: assert inputStream.read(buffer) < 0;
148: } catch (IOException e) {
149: assert false : e;
150: }
151:
152: return inputStream;
153: }
154:
155: /**
156: * @see net.sf.hajdbc.sql.FileSupport#getReader(java.io.File)
157: */
158: @Test(dataProvider="file")
159: public Reader getReader(File file) throws SQLException {
160: Reader reader = this .fileSupport.getReader(file);
161:
162: char[] buffer = new char[4];
163:
164: assert reader != null;
165:
166: try {
167: assert reader.read(buffer) == 4;
168: assert new String(buffer).equals("abcd");
169: assert reader.read(buffer) < 0;
170: } catch (IOException e) {
171: assert false : e;
172: }
173:
174: return reader;
175: }
176: }
|