001: /*
002: * $Id: TestDMLWhenClause.java,v 1.2 2005/06/18 01:03:44 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2004-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.engine.commands;
042:
043: import java.util.Collections;
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: import junit.framework.Test;
048: import junit.framework.TestCase;
049: import junit.framework.TestSuite;
050:
051: import org.axiondb.AxionException;
052: import org.axiondb.ColumnIdentifier;
053: import org.axiondb.Database;
054: import org.axiondb.Literal;
055: import org.axiondb.RowDecorator;
056: import org.axiondb.TableIdentifier;
057: import org.axiondb.engine.MemoryDatabase;
058: import org.axiondb.engine.rows.SimpleRow;
059: import org.axiondb.functions.ConcreteFunction;
060: import org.axiondb.functions.EqualFunction;
061: import org.axiondb.types.BooleanType;
062:
063: /**
064: * @version $Revision: 1.2 $ $Date: 2005/06/18 01:03:44 $
065: * @author Rod Waldhoff
066: * @author Jonathan Giron
067: */
068: public class TestDMLWhenClause extends TestCase {
069:
070: //------------------------------------------------------------ Conventional
071:
072: public TestDMLWhenClause(String testName) {
073: super (testName);
074: }
075:
076: public static Test suite() {
077: return new TestSuite(TestDMLWhenClause.class);
078: }
079:
080: //--------------------------------------------------------------- Lifecycle
081: private static final Literal _true = new Literal(Boolean.TRUE,
082: new BooleanType());
083: private static final Literal _false = new Literal(Boolean.FALSE,
084: new BooleanType());
085: private RowDecorator _decorator = null;
086: private Database _db = null;
087:
088: public void setUp() throws Exception {
089: _decorator = new RowDecorator(Collections.EMPTY_MAP);
090: _decorator.setRow(1, new SimpleRow(0));
091: _db = new MemoryDatabase();
092: }
093:
094: public void tearDown() throws Exception {
095: _decorator = null;
096: _db = null;
097: }
098:
099: //------------------------------------------------------------------- Tests
100:
101: public void testEvaluateNullCondition() throws Exception {
102: DMLWhenClause clause = new DMLWhenClause(null);
103: try {
104: clause.evaluate(_decorator);
105: fail("Expected AxionException");
106: } catch (AxionException e) {
107: // expected
108: }
109: }
110:
111: public void testEvaluateTrue() throws Exception {
112: DMLWhenClause clause = new DMLWhenClause(_true);
113: assertTrue(clause.evaluate(_decorator));
114: }
115:
116: public void testEvaluateFalse() throws Exception {
117: DMLWhenClause clause = new DMLWhenClause(_false);
118: assertFalse(clause.evaluate(_decorator));
119: }
120:
121: public void testEvaluateConditionEvaluatingToNull()
122: throws Exception {
123: ConcreteFunction function = new EqualFunction();
124: ColumnIdentifier sel1 = new ColumnIdentifier("foo");
125: function.addArgument(sel1);
126: ColumnIdentifier sel2 = new ColumnIdentifier("bar");
127: function.addArgument(sel2);
128:
129: Map map = new HashMap();
130: map.put(sel1, new Integer(0));
131: map.put(sel2, new Integer(1));
132: _decorator = new RowDecorator(map);
133:
134: DMLWhenClause clause = new DMLWhenClause(function);
135:
136: _decorator.setRow(new SimpleRow(new Object[] { null,
137: new Integer(0) }));
138: assertFalse(clause.evaluate(_decorator));
139:
140: _decorator.setRow(new SimpleRow(new Object[] { new Integer(0),
141: null }));
142: assertFalse(clause.evaluate(_decorator));
143:
144: _decorator.setRow(new SimpleRow(new Object[] { null, null }));
145: assertFalse(clause.evaluate(_decorator));
146: }
147:
148: public void testResolve() throws Exception {
149: DMLWhenClause clause = new DMLWhenClause(_false);
150: clause.resolve(_db, new TableIdentifier[0]);
151: assertFalse(clause.evaluate(_decorator));
152: }
153: }
|