001: /*
002: * $Id: TestUniqueConstraint.java,v 1.13 2005/12/20 18:32:41 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002 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 java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046:
047: import junit.framework.Test;
048: import junit.framework.TestSuite;
049:
050: import org.axiondb.Column;
051: import org.axiondb.ColumnIdentifier;
052: import org.axiondb.Constraint;
053: import org.axiondb.Row;
054: import org.axiondb.Selectable;
055: import org.axiondb.Table;
056: import org.axiondb.TableIdentifier;
057: import org.axiondb.engine.rows.SimpleRow;
058: import org.axiondb.engine.tables.MemoryTable;
059: import org.axiondb.event.RowEvent;
060: import org.axiondb.event.RowInsertedEvent;
061: import org.axiondb.types.CharacterVaryingType;
062: import org.axiondb.types.IntegerType;
063:
064: /**
065: * @version $Revision: 1.13 $ $Date: 2005/12/20 18:32:41 $
066: * @author Rodney Waldhoff
067: */
068: public class TestUniqueConstraint extends BaseConstraintTest {
069:
070: //------------------------------------------------------------ Conventional
071:
072: public TestUniqueConstraint(String testName) {
073: super (testName);
074: }
075:
076: public static Test suite() {
077: return new TestSuite(TestUniqueConstraint.class);
078: }
079:
080: //---------------------------------------------------------- TestConstraint
081:
082: protected Constraint createConstraint() {
083: return new UniqueConstraint(null);
084: }
085:
086: protected Constraint createConstraint(String name) {
087: return new UniqueConstraint(name);
088: }
089:
090: //--------------------------------------------------------------- Lifecycle
091:
092: private Table _table = null;
093:
094: public void setUp() throws Exception {
095: super .setUp();
096: _table = new MemoryTable("FOO");
097: _table.addColumn(new Column("NAME",
098: new CharacterVaryingType(10)));
099: _table.addColumn(new Column("NUM", new IntegerType()));
100: }
101:
102: public void tearDown() throws Exception {
103: super .tearDown();
104: _table = null;
105: }
106:
107: //-------------------------------------------------------------------- Util
108:
109: private SimpleRow addRow(Table table, String name, Integer num)
110: throws Exception {
111: SimpleRow row = createRow(name, num);
112: table.addRow(row);
113: return row;
114: }
115:
116: private List getNameColIdList() {
117: List list = new ArrayList();
118: list.add(new ColumnIdentifier(new TableIdentifier("FOO"),
119: "NAME", null, new CharacterVaryingType(10)));
120: return list;
121: }
122:
123: private List getNumColIdList() {
124: List list = new ArrayList();
125: list.add(new ColumnIdentifier(new TableIdentifier("FOO"),
126: "NUM", null, new IntegerType()));
127: return list;
128: }
129:
130: private List getNameNumColIdList() {
131: List list = new ArrayList();
132: list.add(new ColumnIdentifier(new TableIdentifier("FOO"),
133: "NAME", null, new CharacterVaryingType(10)));
134: list.add(new ColumnIdentifier(new TableIdentifier("FOO"),
135: "NUM", null, new IntegerType()));
136: return list;
137: }
138:
139: private Constraint makeConstraint(String name, List columns) {
140: UniqueConstraint c = new UniqueConstraint(name);
141: for (Iterator iter = columns.iterator(); iter.hasNext();) {
142: c.addSelectable((Selectable) (iter.next()));
143: }
144: return c;
145: }
146:
147: //------------------------------------------------------------------- Tests
148:
149: public void testCheckWithNoNewRow() throws Exception {
150: Constraint constraint = makeConstraint("C1", getNumColIdList());
151: Row row = createRow(null, null);
152: RowEvent event = new RowInsertedEvent(_table, row, null);
153: assertTrue(constraint.evaluate(event));
154: }
155:
156: public void testEvaluateInsertAgainstEmptyTable() throws Exception {
157: Constraint intconstraint = makeConstraint("C1",
158: getNumColIdList());
159: Constraint strconstraint = makeConstraint("C2",
160: getNameColIdList());
161: Constraint bothconstraint = makeConstraint("C3",
162: getNameNumColIdList());
163: {
164: Row row = createRow(null, null);
165: RowEvent event = new RowInsertedEvent(_table, null, row);
166: assertTrue(intconstraint.evaluate(event));
167: assertTrue(strconstraint.evaluate(event));
168: assertTrue(bothconstraint.evaluate(event));
169: }
170: {
171: Row row = createRow("17", new Integer(17));
172: RowEvent event = new RowInsertedEvent(_table, null, row);
173: assertTrue(intconstraint.evaluate(event));
174: assertTrue(strconstraint.evaluate(event));
175: assertTrue(bothconstraint.evaluate(event));
176: }
177: }
178:
179: public void testEvaluateUniqueInsert() throws Exception {
180: Constraint intconstraint = makeConstraint("C1",
181: getNumColIdList());
182: Constraint strconstraint = makeConstraint("C2",
183: getNameColIdList());
184: Constraint bothconstraint = makeConstraint("C3",
185: getNameNumColIdList());
186: addRow(_table, "3", new Integer(3));
187: addRow(_table, null, null);
188: {
189: Row row = createRow(null, null);
190: RowEvent event = new RowInsertedEvent(_table, null, row);
191: assertTrue(intconstraint.evaluate(event));
192: assertTrue(strconstraint.evaluate(event));
193: assertTrue(bothconstraint.evaluate(event));
194: }
195: {
196: Row row = createRow("17", new Integer(17));
197: RowEvent event = new RowInsertedEvent(_table, null, row);
198: assertTrue(intconstraint.evaluate(event));
199: assertTrue(strconstraint.evaluate(event));
200: assertTrue(bothconstraint.evaluate(event));
201: }
202: }
203:
204: public void testEvaluateInsertWhereOnlyPairIsUnique()
205: throws Exception {
206: Constraint intconstraint = makeConstraint("C1",
207: getNumColIdList());
208: Constraint strconstraint = makeConstraint("C2",
209: getNameColIdList());
210: Constraint bothconstraint = makeConstraint("C3",
211: getNameNumColIdList());
212: addRow(_table, "17", new Integer(3));
213: {
214: Row row = createRow("3", new Integer(3));
215: RowEvent event = new RowInsertedEvent(_table, null, row);
216: assertTrue(!intconstraint.evaluate(event));
217: assertTrue(strconstraint.evaluate(event));
218: assertTrue(bothconstraint.evaluate(event));
219: }
220: {
221: Row row = createRow("17", new Integer(17));
222: RowEvent event = new RowInsertedEvent(_table, null, row);
223: assertTrue(intconstraint.evaluate(event));
224: assertTrue(!strconstraint.evaluate(event));
225: assertTrue(bothconstraint.evaluate(event));
226: }
227: }
228:
229: public void testEvaluateInsertNotUnique() throws Exception {
230: Constraint intconstraint = makeConstraint("C1",
231: getNumColIdList());
232: Constraint strconstraint = makeConstraint("C2",
233: getNameColIdList());
234: Constraint bothconstraint = makeConstraint("C3",
235: getNameNumColIdList());
236: addRow(_table, "17", new Integer(3));
237: addRow(_table, "3", new Integer(17));
238: addRow(_table, null, null);
239: {
240: Row row = createRow("3", new Integer(3));
241: RowEvent event = new RowInsertedEvent(_table, null, row);
242: assertTrue(!intconstraint.evaluate(event));
243: assertTrue(!strconstraint.evaluate(event));
244: assertTrue(bothconstraint.evaluate(event));
245: }
246: {
247: Row row = createRow("17", new Integer(17));
248: RowEvent event = new RowInsertedEvent(_table, null, row);
249: assertTrue(!intconstraint.evaluate(event));
250: assertTrue(!strconstraint.evaluate(event));
251: assertTrue(bothconstraint.evaluate(event));
252: }
253: }
254:
255: public void testEvaluateUniqueUpdate() throws Exception {
256: Constraint intconstraint = makeConstraint("C1",
257: getNumColIdList());
258: Constraint strconstraint = makeConstraint("C2",
259: getNameColIdList());
260: Constraint bothconstraint = makeConstraint("C3",
261: getNameNumColIdList());
262: Row old = addRow(_table, "3", new Integer(3));
263: {
264: Row row = createRow(null, null);
265: row.setIdentifier(old.getIdentifier());
266: RowEvent event = new RowInsertedEvent(_table, old, row);
267: assertTrue(intconstraint.evaluate(event));
268: assertTrue(strconstraint.evaluate(event));
269: assertTrue(bothconstraint.evaluate(event));
270: }
271: {
272: Row row = createRow("17", new Integer(17));
273: row.setIdentifier(old.getIdentifier());
274: RowEvent event = new RowInsertedEvent(_table, old, row);
275: assertTrue(intconstraint.evaluate(event));
276: assertTrue(strconstraint.evaluate(event));
277: assertTrue(bothconstraint.evaluate(event));
278: }
279: }
280:
281: public void testEvaluateUpdateSame() throws Exception {
282: Constraint intconstraint = makeConstraint("C1",
283: getNumColIdList());
284: Constraint strconstraint = makeConstraint("C2",
285: getNameColIdList());
286: Constraint bothconstraint = makeConstraint("C3",
287: getNameNumColIdList());
288: Row old = addRow(_table, "3", new Integer(3));
289: Row row = createRow("3", new Integer(3));
290: row.setIdentifier(old.getIdentifier());
291: RowEvent event = new RowInsertedEvent(_table, old, row);
292: assertTrue(intconstraint.evaluate(event));
293: assertTrue(strconstraint.evaluate(event));
294: assertTrue(bothconstraint.evaluate(event));
295: }
296:
297: public void testEvaluateDelete() throws Exception {
298: Constraint intconstraint = makeConstraint("C1",
299: getNumColIdList());
300: Constraint strconstraint = makeConstraint("C2",
301: getNameColIdList());
302: Constraint bothconstraint = makeConstraint("C3",
303: getNameNumColIdList());
304: Row old = addRow(_table, "3", new Integer(3));
305: RowEvent event = new RowInsertedEvent(_table, old, null);
306: assertTrue(intconstraint.evaluate(event));
307: assertTrue(strconstraint.evaluate(event));
308: assertTrue(bothconstraint.evaluate(event));
309: }
310: }
|