001: /*
002: * $Id: TestBase64EncodeFunction.java,v 1.3 2005/05/02 22:32:02 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2005 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.functions;
042:
043: import java.io.IOException;
044: import java.io.InputStream;
045: import java.math.BigDecimal;
046: import java.sql.Blob;
047: import java.sql.SQLException;
048: import java.util.HashMap;
049: import java.util.Map;
050:
051: import junit.framework.Test;
052: import junit.framework.TestSuite;
053:
054: import org.apache.commons.codec.binary.Base64;
055: import org.axiondb.AxionException;
056: import org.axiondb.ColumnIdentifier;
057: import org.axiondb.Literal;
058: import org.axiondb.RowDecorator;
059: import org.axiondb.TableIdentifier;
060: import org.axiondb.engine.rows.SimpleRow;
061: import org.axiondb.types.BigDecimalType;
062: import org.axiondb.types.ByteArrayBlob;
063: import org.axiondb.types.CharacterVaryingType;
064:
065: /**
066: * @version $Revision: 1.3 $ $Date: 2005/05/02 22:32:02 $
067: * @author Ahimanikya Satapathy
068: */
069: public class TestBase64EncodeFunction extends BaseFunctionTest {
070:
071: //------------------------------------------------------------ Conventional
072:
073: public TestBase64EncodeFunction(String testName) {
074: super (testName);
075: }
076:
077: public static Test suite() {
078: TestSuite suite = new TestSuite(TestBase64EncodeFunction.class);
079: return suite;
080: }
081:
082: //--------------------------------------------------------------- Lifecycle
083:
084: //--------------------------------------------------------------- Framework
085:
086: protected ConcreteFunction makeFunction() {
087: return new ABSFunction();
088: }
089:
090: //------------------------------------------------------------------- Tests
091:
092: public void testMakekNwInstance() {
093: Base64EncodeFunction function = new Base64EncodeFunction();
094: assertTrue(function.makeNewInstance() instanceof Base64EncodeFunction);
095: assertTrue(function.makeNewInstance() != function
096: .makeNewInstance());
097: }
098:
099: public void testFunctionEval() throws Exception {
100: Base64EncodeFunction function = new Base64EncodeFunction();
101: ColumnIdentifier sel1 = new ColumnIdentifier("arg1");
102: function.addArgument(sel1);
103: Map map = new HashMap();
104: map.put(sel1, new Integer(0));
105: RowDecorator dec = new RowDecorator(map);
106:
107: dec.setRow(new SimpleRow(new Object[] { "string".getBytes() }));
108: assertEquals(new String(Base64
109: .encodeBase64("string".getBytes())), function
110: .evaluate(dec));
111:
112: dec.setRow(new SimpleRow(new Object[] { null }));
113: assertNull(function.evaluate(dec));
114:
115: try {
116: dec.setRow(new SimpleRow(
117: new Object[] { new Object[] { "invalid" } }));
118: function.evaluate(dec);
119: fail("Expected type conversion error");
120: } catch (AxionException e) {
121: // Expected
122: }
123:
124: try {
125: dec.setRow(new SimpleRow(new Object[] { new Literal(
126: new BigDecimal(23), new BigDecimalType()) }));
127: function.evaluate(dec);
128: fail("Expected type conversion error");
129: } catch (AxionException e) {
130: // Expected
131: }
132: }
133:
134: public void testBadArguments() throws Exception {
135: Base64EncodeFunction function = new Base64EncodeFunction();
136: ColumnIdentifier sel1 = new ColumnIdentifier(
137: new TableIdentifier(), "arg1", null, new BadDataType1());
138: function.addArgument(sel1);
139:
140: Map map = new HashMap();
141: map.put(sel1, new Integer(0));
142: RowDecorator dec = new RowDecorator(map);
143:
144: try {
145: dec.setRow(new SimpleRow(
146: new Object[] { "string".getBytes() }));
147: function.evaluate(dec);
148: fail("Expected bad input steam exception");
149: } catch (AxionException e) {
150: // Expected
151: }
152:
153: sel1 = new ColumnIdentifier(new TableIdentifier(), "arg1",
154: null, new BadDataType2());
155: function.setArgument(0, sel1);
156:
157: try {
158: dec.setRow(new SimpleRow(
159: new Object[] { "string".getBytes() }));
160: function.evaluate(dec);
161: fail("Expected io exception");
162: } catch (AxionException e) {
163: // Expected
164: }
165: }
166:
167: public void testFunctionInvalid() throws Exception {
168: Base64EncodeFunction function = new Base64EncodeFunction();
169: assertTrue(!function.isValid());
170: function.addArgument(new ColumnIdentifier("arg1"));
171: function.addArgument(new ColumnIdentifier("arg1"));
172: assertTrue(!function.isValid());
173: }
174:
175: public void testFunctionValid() throws Exception {
176: Base64EncodeFunction function = new Base64EncodeFunction();
177: function.addArgument(new ColumnIdentifier("arg1"));
178: assertTrue(function.isValid());
179: }
180:
181: private class BadDataType1 extends CharacterVaryingType {
182: public Blob toBlob(Object val) {
183: return new BadBlob1("string".getBytes());
184: }
185: }
186:
187: private class BadBlob1 extends ByteArrayBlob {
188: public BadBlob1(byte[] value) {
189: super (value);
190: }
191:
192: public InputStream getBinaryStream() throws SQLException {
193: throw new SQLException("simulate for test");
194: }
195: }
196:
197: private class BadDataType2 extends CharacterVaryingType {
198: public Blob toBlob(Object val) {
199: return new BadBlob2("string".getBytes());
200: }
201: }
202:
203: private class BadBlob2 extends ByteArrayBlob {
204: public BadBlob2(byte[] value) {
205: super (value);
206: }
207:
208: public InputStream getBinaryStream() throws SQLException {
209: return new BadInputStream();
210: }
211: }
212:
213: private class BadInputStream extends InputStream {
214:
215: public int read() throws IOException {
216: throw new IOException("simulate for test");
217: }
218: }
219: }
|