001: /*
002: * $Id: TestCreateSequenceCommand.java,v 1.7 2005/04/24 21:36:00 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.engine.commands;
042:
043: import java.math.BigInteger;
044:
045: import junit.framework.Test;
046: import junit.framework.TestCase;
047: import junit.framework.TestSuite;
048:
049: import org.axiondb.AxionCommand;
050: import org.axiondb.Selectable;
051: import org.axiondb.Sequence;
052: import org.axiondb.SequenceEvaluator;
053: import org.axiondb.engine.MemoryDatabase;
054: import org.axiondb.engine.visitors.ResolveSelectableVisitor;
055:
056: /**
057: * @version $Revision: 1.7 $ $Date: 2005/04/24 21:36:00 $
058: * @author Chuck Burdick
059: * @author Ahimanikya Satapathy
060: */
061: public class TestCreateSequenceCommand extends TestCase {
062:
063: //------------------------------------------------------------ Conventional
064:
065: public TestCreateSequenceCommand(String testName) {
066: super (testName);
067: }
068:
069: public static void main(String args[]) {
070: String[] testCaseName = { TestCreateSequenceCommand.class
071: .getName() };
072: junit.textui.TestRunner.main(testCaseName);
073: }
074:
075: public static Test suite() {
076: return new TestSuite(TestCreateSequenceCommand.class);
077: }
078:
079: //--------------------------------------------------------------- Lifecycle
080: org.axiondb.Database _db = null;
081:
082: public void setUp() throws Exception {
083: _db = new MemoryDatabase();
084: }
085:
086: public void tearDown() throws Exception {
087: }
088:
089: //------------------------------------------------------------------- Tests
090:
091: public void testCreateSequence() throws Exception {
092: assertNull("Should not find sequence", _db
093: .getSequence("foo_seq"));
094: AxionCommand create = new CreateSequenceCommand("foo_seq", 0);
095: create.execute(_db);
096: assertNotNull("Should find sequence", _db
097: .getSequence("foo_seq"));
098: assertEquals("Should have correct initial value", BigInteger
099: .valueOf(0), _db.getSequence("foo_seq").getValue());
100: }
101:
102: public void testCreateSequenceStartVal() throws Exception {
103: assertNull("Should not find sequence", _db
104: .getSequence("foo_seq"));
105: AxionCommand create = new CreateSequenceCommand("foo_seq", 23);
106: create.execute(_db);
107: assertNotNull("Should find sequence", _db
108: .getSequence("foo_seq"));
109: assertEquals("Should have correct initial value", BigInteger
110: .valueOf(23), _db.getSequence("foo_seq").getValue());
111: }
112:
113: public void testCreateSequenceAll() throws Exception {
114: assertNull("Should not find sequence", _db
115: .getSequence("foo_seq"));
116: CreateSequenceCommand create = new CreateSequenceCommand(
117: "foo_seq", 23);
118: create.setIncrementBy("10");
119: create.setMinValue("0");
120: create.setMaxValue("40");
121: create.setCycle(true);
122: create.execute(_db);
123: Sequence seq = _db.getSequence("foo_seq");
124: assertNotNull("Should find sequence", seq);
125: assertEquals("Should have correct initial value", BigInteger
126: .valueOf(23), seq.getValue());
127: assertEquals("Should have correct next value", new Integer(23),
128: seq.evaluate());
129: assertEquals("Should have correct next value", new Integer(33),
130: seq.evaluate());
131: assertEquals("Should have correct next value", new Integer(0),
132: seq.evaluate());
133: assertEquals("Should have correct next value", new Integer(10),
134: seq.evaluate());
135: }
136:
137: public void testDefaultStartValue() throws Exception {
138: assertNull("Should not find sequence", _db
139: .getSequence("foo_seq"));
140: CreateSequenceCommand create = new CreateSequenceCommand();
141: create.setObjectName("foo_seq");
142: create.setIncrementBy("10");
143: create.setMinValue("0");
144: create.setMaxValue("40");
145: create.setCycle(true);
146: create.execute(_db);
147: Sequence seq = _db.getSequence("foo_seq");
148: assertNotNull("Should find sequence", seq);
149: assertEquals("Should have correct initial value", BigInteger
150: .valueOf(0), seq.getValue());
151: assertEquals("Should have correct next value", new Integer(0),
152: seq.evaluate());
153: assertEquals("Should have correct next value", new Integer(10),
154: seq.evaluate());
155: assertEquals("Should have correct next value", new Integer(20),
156: seq.evaluate());
157: assertEquals("Should have correct next value", new Integer(30),
158: seq.evaluate());
159: assertEquals("Should have correct next value", new Integer(40),
160: seq.evaluate());
161: assertEquals("Should have correct next value", new Integer(0),
162: seq.evaluate());
163: }
164:
165: public void testDefaultStartValue2() throws Exception {
166: assertNull("Should not find sequence", _db
167: .getSequence("foo_seq"));
168: CreateSequenceCommand create = new CreateSequenceCommand();
169: create.setObjectName("foo_seq");
170: create.setIncrementBy("-10");
171: create.setMinValue("0");
172: create.setMaxValue("40");
173: create.setCycle(true);
174: create.execute(_db);
175: Sequence seq = _db.getSequence("foo_seq");
176: assertNotNull("Should find sequence", seq);
177: assertEquals("Should have correct initial value", BigInteger
178: .valueOf(40), seq.getValue());
179: assertEquals("Should have correct next value", new Integer(40),
180: seq.evaluate());
181: assertEquals("Should have correct next value", new Integer(30),
182: seq.evaluate());
183: assertEquals("Should have correct next value", new Integer(20),
184: seq.evaluate());
185: assertEquals("Should have correct next value", new Integer(10),
186: seq.evaluate());
187: assertEquals("Should have correct next value", new Integer(0),
188: seq.evaluate());
189: assertEquals("Should have correct next value", new Integer(40),
190: seq.evaluate());
191: }
192:
193: public void testCreateSequenceBad() throws Exception {
194: assertNull("Should not find sequence", _db
195: .getSequence("foo_seq"));
196: CreateSequenceCommand create = new CreateSequenceCommand(
197: "foo_seq", 23);
198: create.setIncrementBy("10");
199: create.setMinValue("0");
200: create.setMaxValue("40");
201: create.setCycle(false); // NO CYCLE
202: create.execute(_db);
203: Sequence seq = _db.getSequence("foo_seq");
204: assertNotNull("Should find sequence", seq);
205: assertEquals("Should have correct initial value", BigInteger
206: .valueOf(23), seq.getValue());
207: assertEquals("Should have correct next value", new Integer(23),
208: seq.evaluate());
209:
210: try {
211: seq.evaluate();
212: fail("No more next value expected...");
213: } catch (IllegalStateException ex) {
214: // expected
215: }
216:
217: try {
218: create.execute(_db);
219: fail("already exists");
220: } catch (Exception ex) {
221: // expected
222: }
223:
224: try {
225: _db.dropSequence("foo_seq");
226: create.setMaxValue("10000000000000000");
227: create.execute(_db);
228: fail("too big number exception expected");
229: } catch (Exception ex) {
230: // expected
231: }
232:
233: }
234:
235: public void testResolveAsSelectable() throws Exception {
236: ResolveSelectableVisitor visitor = new ResolveSelectableVisitor(
237: _db);
238: Sequence seq = new Sequence("foo", 0);
239: Selectable sel = new SequenceEvaluator(seq, "NEXTVAL");
240:
241: try {
242: visitor.visit(sel, null, null);
243: fail("Expected Exception: Couldn't resolve Selectable");
244: } catch (Exception e) {
245: // expected
246: }
247: }
248: }
|