001: /*
002: * $Id: TestTruncateCommand.java,v 1.5 2005/12/20 18:32:27 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 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 junit.framework.Test;
044: import junit.framework.TestSuite;
045:
046: import org.axiondb.AxionCommand;
047: import org.axiondb.AxionException;
048: import org.axiondb.Row;
049: import org.axiondb.RowIterator;
050: import org.axiondb.Table;
051: import org.axiondb.engine.rows.SimpleRow;
052:
053: /**
054: * @version $Revision: 1.5 $ $Date: 2005/12/20 18:32:27 $
055: * @author Ahimanikya Satapathy
056: */
057: public class TestTruncateCommand extends BaseAxionCommandTest {
058:
059: //------------------------------------------------------------ Conventional
060:
061: public TestTruncateCommand(String testName) {
062: super (testName);
063: }
064:
065: public static Test suite() {
066: return new TestSuite(TestTruncateCommand.class);
067: }
068:
069: //--------------------------------------------------------------- Lifecycle
070:
071: public void setUp() throws Exception {
072: super .setUp();
073: {
074: CreateTableCommand cmd = new CreateTableCommand("FOO");
075: cmd.addColumn("ID", "integer");
076: cmd.addColumn("NAME", "varchar", "10");
077: cmd.execute(getDatabase());
078: }
079: populateTable();
080: }
081:
082: public void tearDown() throws Exception {
083: super .tearDown();
084: }
085:
086: protected AxionCommand makeCommand() {
087: return new TruncateCommand();
088: }
089:
090: protected void populateTable() throws Exception {
091: Table table = getDatabase().getTable("FOO");
092: {
093: Row row = new SimpleRow(2);
094: row.set(0, new Integer(1));
095: row.set(1, "one");
096: table.addRow(row);
097: }
098: {
099: Row row = new SimpleRow(2);
100: row.set(0, new Integer(2));
101: row.set(1, "two");
102: table.addRow(row);
103: }
104: }
105:
106: //------------------------------------------------------------------- Tests
107:
108: public void testExecute() throws Exception {
109: Table table = getDatabase().getTable("FOO");
110: RowIterator iter = table.getRowIterator(true);
111: assertNotNull(iter);
112: assertTrue(iter.hasNext());
113: assertNotNull(iter.next());
114: assertTrue(iter.hasNext());
115: assertNotNull(iter.next());
116: assertTrue(!iter.hasNext());
117:
118: TruncateCommand cmd = new TruncateCommand();
119: try {
120: cmd.setObjectName("bogus");
121: cmd.execute(getDatabase());
122: fail("Exception Excepted");
123: } catch (AxionException e) {
124: // expected
125: }
126:
127: cmd.setObjectName("FOO");
128: assertTrue(!cmd.execute(getDatabase()));
129:
130: table = getDatabase().getTable("FOO");
131: iter = table.getRowIterator(true);
132: assertNotNull(iter);
133: assertTrue(!iter.hasNext());
134:
135: cmd = new TruncateCommand();
136: cmd.setObjectName("FOO");
137: assertTrue(!cmd.execute(getDatabase()));
138:
139: table = getDatabase().getTable("FOO");
140: iter = table.getRowIterator(true);
141: assertNotNull(iter);
142: assertTrue(!iter.hasNext());
143: populateTable();
144:
145: iter = table.getRowIterator(true);
146: assertNotNull(iter);
147: assertTrue(iter.hasNext());
148: assertNotNull(iter.next());
149: assertTrue(iter.hasNext());
150: assertNotNull(iter.next());
151: assertTrue(!iter.hasNext());
152: }
153:
154: public void testExecuteQuery() throws Exception {
155: TruncateCommand cmd = new TruncateCommand();
156: cmd.setObjectName("FOO");
157: assertExecuteQueryIsNotSupported(cmd);
158: }
159:
160: }
|