001: /*
002: * $Id: TestForeignKeyConstraint.java,v 1.4 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.List;
045:
046: import junit.framework.Test;
047: import junit.framework.TestCase;
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.Table;
054: import org.axiondb.TableIdentifier;
055: import org.axiondb.engine.rows.SimpleRow;
056: import org.axiondb.engine.tables.MemoryTable;
057: import org.axiondb.event.RowEvent;
058: import org.axiondb.event.RowInsertedEvent;
059: import org.axiondb.types.IntegerType;
060:
061: /**
062: * @version $Revision: 1.4 $ $Date: 2005/12/20 18:32:41 $
063: * @author Rodney Waldhoff
064: */
065: public class TestForeignKeyConstraint extends TestCase {
066:
067: //------------------------------------------------------------ Conventional
068:
069: public TestForeignKeyConstraint(String testName) {
070: super (testName);
071: }
072:
073: public static Test suite() {
074: return new TestSuite(TestForeignKeyConstraint.class);
075: }
076:
077: //---------------------------------------------------------- TestConstraint
078:
079: protected Constraint createConstraint() {
080: return new ForeignKeyConstraint(null);
081: }
082:
083: protected Constraint createConstraint(String name) {
084: return new ForeignKeyConstraint(name);
085: }
086:
087: //--------------------------------------------------------------- Lifecycle
088:
089: public void setUp() throws Exception {
090: _ftable = new MemoryTable("A");
091: _ftable.addColumn(new Column("ID", new IntegerType()));
092: _ftable.addRow(new SimpleRow(new Object[] { new Integer(1) }));
093: _ftable.addRow(new SimpleRow(new Object[] { new Integer(2) }));
094:
095: _table = new MemoryTable("B");
096: _table.addColumn(new Column("ID", new IntegerType()));
097: _table.addColumn(new Column("FID", new IntegerType()));
098: }
099:
100: public void tearDown() throws Exception {
101: super .tearDown();
102: _table = null;
103: _ftable = null;
104: }
105:
106: protected SimpleRow createRow(int id, int fid) {
107: SimpleRow row = new SimpleRow(2);
108: row.set(0, new Integer(id));
109: row.set(1, new Integer(fid));
110: return row;
111: }
112:
113: private Table _table = null;
114: private Table _ftable = null;
115:
116: //------------------------------------------------------------------- Tests
117:
118: public void testCheckWithNoNewRow() throws Exception {
119: ForeignKeyConstraint constraint = new ForeignKeyConstraint("C1");
120:
121: List list = new ArrayList(1);
122: list.add(new ColumnIdentifier(new TableIdentifier("B"), "FID",
123: null, new IntegerType()));
124: constraint.addColumns(list);
125:
126: constraint.setParentTable(_ftable);
127: constraint.setChildTable(_table);
128: constraint.setParentTableName(_ftable.getName());
129: constraint.setChildTableName(_table.getName());
130:
131: List list2 = new ArrayList(1);
132: list2.add(new ColumnIdentifier(new TableIdentifier("A"), "ID",
133: null, new IntegerType()));
134: constraint.addForeignColumns(list2);
135:
136: SimpleRow row = createRow(1, 1);
137: RowEvent event = new RowInsertedEvent(_table, row, null);
138: assertTrue(constraint.evaluate(event));
139:
140: event = new RowInsertedEvent(_table, row, row);
141: assertTrue(constraint.evaluate(event));
142:
143: row = createRow(1, 2);
144: event = new RowInsertedEvent(_table, row, row);
145: assertTrue(constraint.evaluate(event));
146:
147: row = createRow(1, 5);
148: event = new RowInsertedEvent(_table, row, row);
149: assertFalse(constraint.evaluate(event));
150:
151: }
152:
153: public void testGeneratedName() throws Exception {
154: Constraint constraint = createConstraint();
155: assertNotNull(constraint.getName());
156: assertEquals(constraint.getName().toUpperCase(), constraint
157: .getName());
158: Constraint constraint2 = createConstraint();
159: assertNotNull(constraint2.getName());
160: assertEquals(constraint2.getName().toUpperCase(), constraint2
161: .getName());
162: assertTrue(!constraint.getName().equals(constraint2.getName()));
163: }
164:
165: public void testNameIsUpperCase() throws Exception {
166: Constraint constraint = createConstraint("FOO");
167: assertEquals("FOO", constraint.getName());
168: }
169:
170: public void testGetType() throws Exception {
171: Constraint constraint = createConstraint("FOO");
172: assertNotNull(constraint.getType());
173: }
174:
175: }
|