001: /*
002: * $Id: TestCheckConstraint.java,v 1.16 2005/12/20 18:32:41 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-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.constraints;
042:
043: import junit.framework.Test;
044: import junit.framework.TestSuite;
045:
046: import org.axiondb.ColumnIdentifier;
047: import org.axiondb.Constraint;
048: import org.axiondb.Function;
049: import org.axiondb.Literal;
050: import org.axiondb.Row;
051: import org.axiondb.Selectable;
052: import org.axiondb.TableIdentifier;
053: import org.axiondb.event.RowEvent;
054: import org.axiondb.event.RowInsertedEvent;
055: import org.axiondb.functions.AndFunction;
056: import org.axiondb.functions.IsNotNullFunction;
057: import org.axiondb.functions.NotEqualFunction;
058: import org.axiondb.types.CharacterVaryingType;
059:
060: /**
061: * @version $Revision: 1.16 $ $Date: 2005/12/20 18:32:41 $
062: * @author Rodney Waldhoff
063: * @author Jonathan Giron
064: */
065: public class TestCheckConstraint extends BaseConstraintTest {
066:
067: //------------------------------------------------------------ Conventional
068:
069: public TestCheckConstraint(String testName) {
070: super (testName);
071: }
072:
073: public static Test suite() {
074: return new TestSuite(TestCheckConstraint.class);
075: }
076:
077: //---------------------------------------------------------- TestConstraint
078:
079: protected Constraint createConstraint() {
080: return new CheckConstraint(null);
081: }
082:
083: protected Constraint createConstraint(String name) {
084: return new CheckConstraint(name);
085: }
086:
087: //--------------------------------------------------------------- Lifecycle
088:
089: public void setUp() throws Exception {
090: super .setUp();
091: }
092:
093: public void tearDown() throws Exception {
094: super .tearDown();
095: }
096:
097: //-------------------------------------------------------------------- Util
098:
099: private Constraint makeConstraint(String name, Selectable condition) {
100: CheckConstraint c = new CheckConstraint(name);
101: c.setCondition(condition);
102: return c;
103: }
104:
105: private Selectable makeWhereNameIsNotEmpty() {
106: Function where = new NotEqualFunction();
107: where.addArgument(new ColumnIdentifier(new TableIdentifier(
108: "FOO"), "NAME", null, new CharacterVaryingType(10)));
109: where.addArgument(new Literal("", new CharacterVaryingType(1)));
110: return where;
111: }
112:
113: private Selectable makeWhereNameIsNotNull() {
114: Function where = new IsNotNullFunction();
115: where.addArgument(new ColumnIdentifier(new TableIdentifier(
116: "FOO"), "NAME", null, new CharacterVaryingType(10)));
117: return where;
118: }
119:
120: private Selectable makeWhereNameIsNotEmptyAndNameIsNotNull() {
121: AndFunction fn = new AndFunction();
122: fn.addArgument(makeWhereNameIsNotEmpty());
123: fn.addArgument(makeWhereNameIsNotNull());
124: return fn;
125: }
126:
127: //------------------------------------------------------------------- Tests
128:
129: public void testNotEmptyCheckSuccess() throws Exception {
130: Constraint constraint = makeConstraint("NOT_EMPTY",
131: makeWhereNameIsNotEmpty());
132: {
133: Row row = createRow(" ", null);
134: RowEvent event = new RowInsertedEvent(getTable(), null, row);
135: assertTrue(constraint.evaluate(event));
136: }
137: {
138: Row row = createRow("test", null);
139: RowEvent event = new RowInsertedEvent(getTable(), null, row);
140: assertTrue(constraint.evaluate(event));
141: }
142: }
143:
144: public void testCheckWithNoNewRow() throws Exception {
145: Constraint constraint = makeConstraint("NOT_EMPTY",
146: makeWhereNameIsNotEmpty());
147: Row row = createRow(null, null);
148: RowEvent event = new RowInsertedEvent(getTable(), row, null);
149: assertTrue(constraint.evaluate(event));
150: }
151:
152: public void testNotEmptyCheckFailure() throws Exception {
153: Constraint constraint = makeConstraint("NOT_EMPTY",
154: makeWhereNameIsNotEmpty());
155: {
156: Row row = createRow("", null);
157: RowEvent event = new RowInsertedEvent(getTable(), null, row);
158: assertTrue(!constraint.evaluate(event));
159: }
160: }
161:
162: public void testNotNullCheckSuccess() throws Exception {
163: Constraint constraint = makeConstraint("NOT_NULL",
164: makeWhereNameIsNotNull());
165: {
166: Row row = createRow("", null);
167: RowEvent event = new RowInsertedEvent(getTable(), null, row);
168: assertTrue(constraint.evaluate(event));
169: }
170: {
171: Row row = createRow(" ", null);
172: RowEvent event = new RowInsertedEvent(getTable(), null, row);
173: assertTrue(constraint.evaluate(event));
174: }
175: {
176: Row row = createRow("test", null);
177: RowEvent event = new RowInsertedEvent(getTable(), null, row);
178: assertTrue(constraint.evaluate(event));
179: }
180: }
181:
182: public void testNotNullCheckFailure() throws Exception {
183: Constraint constraint = makeConstraint("NOT_NULL",
184: makeWhereNameIsNotNull());
185: {
186: Row row = createRow(null, null);
187: RowEvent event = new RowInsertedEvent(getTable(), null, row);
188: assertTrue(!constraint.evaluate(event));
189: }
190: }
191:
192: public void testAndCheckSuccess() throws Exception {
193: Constraint constraint = makeConstraint("NOT_NULL_OR_EMPTY",
194: makeWhereNameIsNotEmptyAndNameIsNotNull());
195: {
196: Row row = createRow(" ", null);
197: RowEvent event = new RowInsertedEvent(getTable(), null, row);
198: assertTrue(constraint.evaluate(event));
199: }
200: {
201: Row row = createRow("test", null);
202: RowEvent event = new RowInsertedEvent(getTable(), null, row);
203: assertTrue(constraint.evaluate(event));
204: }
205: }
206:
207: public void testAndCheckFailure() throws Exception {
208: Constraint constraint = makeConstraint("NOT_NULL_OR_EMPTY",
209: makeWhereNameIsNotEmptyAndNameIsNotNull());
210: {
211: Row row = createRow("", null);
212: RowEvent event = new RowInsertedEvent(getTable(), null, row);
213: assertFalse(constraint.evaluate(event));
214: }
215: {
216: Row row = createRow(null, null);
217: RowEvent event = new RowInsertedEvent(getTable(), null, row);
218: assertFalse(constraint.evaluate(event));
219: }
220: }
221: }
|