001: /*
002: * Copyright (C) 2007 Rob Manning
003: * manningr@users.sourceforge.net
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.sourceforge.squirrel_sql.fw.util;
020:
021: import java.io.IOException;
022: import java.sql.DataTruncation;
023: import java.sql.SQLException;
024: import java.sql.SQLWarning;
025:
026: import net.sourceforge.squirrel_sql.BaseSQuirreLJUnit4TestCase;
027: import net.sourceforge.squirrel_sql.fw.sql.SQLExecutionException;
028:
029: import static org.easymock.EasyMock.*;
030: import org.junit.After;
031: import org.junit.Assert;
032: import org.junit.Before;
033: import org.junit.Test;
034:
035: /**
036: * @author manningr
037: */
038: public class DefaultExceptionFormatterTest extends
039: BaseSQuirreLJUnit4TestCase {
040:
041: DefaultExceptionFormatter formatterUnderTest;
042:
043: /**
044: * @throws java.lang.Exception
045: */
046: @Before
047: public void setUp() throws Exception {
048: formatterUnderTest = new DefaultExceptionFormatter();
049: }
050:
051: /**
052: * @throws java.lang.Exception
053: */
054: @After
055: public void tearDown() throws Exception {
056: formatterUnderTest = null;
057: }
058:
059: /**
060: * Test method for {@link net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter#format(java.lang.Throwable)}.
061: */
062: @Test(expected=IllegalArgumentException.class)
063: public final void testNullThrowableFormat() {
064: formatterUnderTest.format(null);
065: }
066:
067: /**
068: * Test method for {@link net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter#format(java.lang.Throwable)}.
069: */
070: @Test
071: public final void testDefaultFormatForDataTruncationRead() {
072: DataTruncation dt = new DataTruncation(1, true, true, 20, 3);
073: Assert.assertTrue(formatterUnderTest.formatsException(dt));
074: String formattedException = formatterUnderTest.format(dt);
075:
076: //" a read "
077: StringBuilder expectedMessage = new StringBuilder(
078: "Data Truncation error occured on");
079: expectedMessage.append(" a read ");
080: expectedMessage.append(" of column ");
081: expectedMessage.append(1);
082: expectedMessage.append("Data was ");
083: expectedMessage.append(20);
084: expectedMessage.append(" bytes long and ");
085: expectedMessage.append(3);
086: expectedMessage.append(" bytes were transferred.");
087:
088: Assert.assertEquals(expectedMessage.toString(),
089: formattedException);
090: }
091:
092: /**
093: * Test method for {@link net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter#format(java.lang.Throwable)}.
094: */
095: @Test
096: public void testDefaultFormatForDataTruncationWrite() {
097: DataTruncation dt = new DataTruncation(1, true, false, 20, 3);
098: Assert.assertTrue(formatterUnderTest.formatsException(dt));
099:
100: StringBuilder expectedMessage = new StringBuilder(
101: "Data Truncation error occured on");
102: expectedMessage.append(" a write ");
103: expectedMessage.append(" of column ");
104: expectedMessage.append(1);
105: expectedMessage.append("Data was ");
106: expectedMessage.append(20);
107: expectedMessage.append(" bytes long and ");
108: expectedMessage.append(3);
109: expectedMessage.append(" bytes were transferred.");
110:
111: String formattedException = formatterUnderTest.format(dt);
112: Assert.assertEquals(expectedMessage.toString(),
113: formattedException);
114: }
115:
116: /**
117: * Test method for {@link net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter#format(java.lang.Throwable)}.
118: */
119: @Test
120: public void testDefaultFormatForSQLException() {
121: SQLException ex = new SQLException("table not found",
122: "FooState", 1000);
123: Assert.assertTrue(formatterUnderTest.formatsException(ex));
124:
125: StringBuilder expectedMessage = new StringBuilder(
126: "Error: table not found\n");
127: expectedMessage.append("SQLState: FooState\n");
128: expectedMessage.append("ErrorCode: 1000");
129:
130: String formattedException = formatterUnderTest.format(ex);
131: Assert.assertEquals(expectedMessage.toString(),
132: formattedException);
133: }
134:
135: /**
136: * Test method for {@link net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter#format(java.lang.Throwable)}.
137: */
138: @Test
139: public void testDefaultFormatForSQLWarning() {
140: SQLWarning ex = new SQLWarning("low on memory", "WarningState",
141: 1000);
142: SQLExecutionException ee = new SQLExecutionException(ex,
143: "postError");
144: Assert.assertTrue(formatterUnderTest.formatsException(ex));
145: Assert.assertTrue(formatterUnderTest.formatsException(ee));
146:
147: StringBuilder expectedMessage = new StringBuilder(
148: "Warning: low on memory\n");
149: expectedMessage.append("SQLState: WarningState\n");
150: expectedMessage.append("ErrorCode: 1000\n");
151: expectedMessage.append("postError");
152:
153: String formattedException = formatterUnderTest.format(ee);
154: Assert.assertEquals(expectedMessage.toString(),
155: formattedException);
156: }
157:
158: /**
159: * Test method for {@link net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter#formatsException(java.lang.Throwable)}.
160: */
161: @Test
162: public final void testFormatsException() {
163: Assert.assertTrue(formatterUnderTest
164: .formatsException(new Throwable()));
165: Assert.assertTrue(formatterUnderTest
166: .formatsException(new SQLException()));
167: Assert.assertTrue(formatterUnderTest
168: .formatsException(new SQLExecutionException(
169: new SQLException(), "")));
170: Assert.assertTrue(formatterUnderTest
171: .formatsException(new SQLExecutionException(
172: new SQLException(), "some error")));
173: Assert.assertTrue(formatterUnderTest
174: .formatsException(new SQLExecutionException(
175: new SQLException(), null)));
176: Assert.assertTrue(formatterUnderTest
177: .formatsException(new IOException()));
178: Assert.assertTrue(formatterUnderTest
179: .formatsException(new DataTruncation(0, true, true, 0,
180: 0)));
181: Assert.assertTrue(formatterUnderTest
182: .formatsException(new SQLWarning()));
183: }
184:
185: @Test(expected=IllegalArgumentException.class)
186: public final void testSetNullCustomExceptionFormatter() {
187: formatterUnderTest.setCustomExceptionFormatter(null);
188: }
189:
190: /**
191: * Test method for {@link net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter#setCustomExceptionFormatter(net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter)}.
192: */
193: @Test
194: public final void testSetCustomExceptionFormatter()
195: throws Exception {
196: ExceptionFormatter formatter1 = createMock(ExceptionFormatter.class);
197: SQLException ex = new SQLException("table does not exist");
198: SQLExecutionException ee = new SQLExecutionException(ex, "");
199: expect(formatter1.formatsException(isA(SQLException.class)))
200: .andReturn(true).anyTimes();
201: expect(formatter1.format(ex)).andReturn("foo").anyTimes();
202: replay(formatter1);
203: formatterUnderTest.setCustomExceptionFormatter(formatter1);
204: String formattedEx = formatterUnderTest.format(ee);
205: verify(formatter1);
206: Assert.assertEquals("foo", formattedEx);
207: }
208:
209: /**
210: * Test method for {@link net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter#setCustomExceptionFormatter(net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter)}.
211: */
212: @Test
213: public final void testSetMultiCustomExceptionFormatter()
214: throws Exception {
215: ExceptionFormatter formatter1 = createMock(ExceptionFormatter.class);
216: ExceptionFormatter formatter2 = createMock(ExceptionFormatter.class);
217: SQLException ex = new SQLException("table does not exist");
218: SQLExecutionException ee = new SQLExecutionException(ex, "");
219: expect(formatter1.formatsException(isA(SQLException.class)))
220: .andReturn(true).anyTimes();
221: expect(formatter1.format(ex)).andReturn("formatter1")
222: .anyTimes();
223: expect(formatter2.formatsException(isA(SQLException.class)))
224: .andReturn(true).anyTimes();
225: expect(formatter2.format(ex)).andReturn("formatter2")
226: .anyTimes();
227: replay(formatter1);
228: replay(formatter2);
229: formatterUnderTest.setCustomExceptionFormatter(formatter1);
230: // This should just produce an error message.
231: formatterUnderTest.setCustomExceptionFormatter(formatter2);
232:
233: String formattedEx = formatterUnderTest.format(ee);
234: verify(formatter1);
235: verify(formatter2);
236: Assert.assertEquals("formatter1", formattedEx);
237: }
238:
239: }
|