001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.db.explorer.actions;
029:
030: import java.io.File;
031: import java.io.FileInputStream;
032: import java.io.ObjectInputStream;
033: import java.sql.Types;
034: import java.util.Iterator;
035: import java.util.Vector;
036: import org.netbeans.lib.ddl.impl.CreateTable;
037: import org.netbeans.lib.ddl.impl.TableColumn;
038: import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo;
039: import org.netbeans.modules.db.util.DBTestBase;
040: import org.netbeans.modules.db.util.InfoHelper;
041:
042: /**
043: * @author David Van Couvering
044: */
045: public class GrabTableHelperTest extends DBTestBase {
046:
047: public GrabTableHelperTest(String name) {
048: super (name);
049: }
050:
051: public void testGrabTable() throws Exception {
052: File file = null;
053: InfoHelper infoHelper = new InfoHelper(spec, drvSpec, conn);
054:
055: try {
056: String tablename = "grabtable";
057: String pkName = "id";
058: String col1 = "col1";
059: String col2 = "col2";
060: String filename = "grabtable.grab";
061:
062: if (dblocation != null && dblocation.length() > 0) {
063: filename = dblocation + "/" + filename;
064: }
065:
066: file = new File(filename);
067: if (file.exists()) {
068: file.delete();
069: }
070:
071: createBasicTable(tablename, pkName);
072: addBasicColumn(tablename, col1, Types.VARCHAR, 255);
073: addBasicColumn(tablename, col2, Types.INTEGER, 0);
074:
075: // Fix the identifiers based on how the database converts
076: // the casing of identifiers. This is because we still
077: // quote existing (versus) new identifiers, so we need to
078: // make sure they are quoted correctly.
079: tablename = fixIdentifier(tablename);
080: col1 = fixIdentifier(col1);
081: col2 = fixIdentifier(col2);
082: pkName = fixIdentifier(pkName);
083:
084: // Initialize the table information in the format required
085: // by the helper. This is done by creating a DatabaseNodeInfo
086: // for the table
087: DatabaseNodeInfo tableInfo = infoHelper
088: .getTableInfo(tablename);
089:
090: new GrabTableHelper().execute(spec, tablename, tableInfo
091: .getChildren().elements(), file);
092:
093: assertTrue(file.exists());
094:
095: // Now recreate the table info and make sure it's accurate
096: FileInputStream fstream = new FileInputStream(file);
097: ObjectInputStream istream = new ObjectInputStream(fstream);
098: CreateTable cmd = (CreateTable) istream.readObject();
099: istream.close();
100: cmd.setSpecification(spec);
101: cmd.setObjectOwner(SCHEMA);
102:
103: assertEquals(tablename, cmd.getObjectName());
104:
105: Vector cols = cmd.getColumns();
106: assertTrue(cols.size() == 3);
107:
108: Iterator it = cols.iterator();
109:
110: while (it.hasNext()) {
111: TableColumn col = (TableColumn) it.next();
112:
113: if (col.getColumnName().equals(pkName)) {
114: assertEquals(col.getColumnType(), Types.INTEGER);
115: assertEquals(col.getObjectType(),
116: TableColumn.PRIMARY_KEY);
117: } else if (col.getColumnName().equals(col1)) {
118: assertEquals(col.getColumnType(), Types.VARCHAR);
119: assertEquals(col.getColumnSize(), 255);
120: } else if (col.getColumnName().equals(col2)) {
121: assertEquals(col.getColumnType(), Types.INTEGER);
122: } else {
123: fail("Unexpected column with name "
124: + col.getColumnName());
125: }
126: }
127:
128: // OK, now see if we can actually create this guy
129: dropTable(tablename);
130: cmd.execute();
131:
132: assertFalse(cmd.wasException());
133:
134: dropTable(tablename);
135: } finally {
136: if (file != null && file.exists()) {
137: file.delete();
138: }
139: }
140: }
141: }
|