/* This file is part of VoltDB. * Copyright (C) 2008-2010 VoltDB L.L.C. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ package org.voltdb.utils; import java.util.List; import org.voltdb.benchmark.tpcc.TPCCProjectBuilder; import org.voltdb.catalog.*; import org.voltdb.types.ConstraintType; import junit.framework.TestCase; public class TestCatalogUtil extends TestCase { protected Catalog catalog; protected Database catalog_db; @Override protected void setUp() throws Exception { catalog = TPCCProjectBuilder.getTPCCSchemaCatalog(); assertNotNull(catalog); catalog_db = catalog.getClusters().get("cluster").getDatabases().get("database"); assertNotNull(catalog_db); } /** * */ public void testGetSortedCatalogItems() { for (Table catalog_tbl : catalog_db.getTables()) { int last_idx = -1; List<Column> columns = CatalogUtil.getSortedCatalogItems(catalog_tbl.getColumns(), "index"); assertFalse(columns.isEmpty()); assertEquals(catalog_tbl.getColumns().size(), columns.size()); for (Column catalog_col : columns) { assertTrue(catalog_col.getIndex() > last_idx); last_idx = catalog_col.getIndex(); } } } /** * */ public void testToSchema() { String search_str = ""; // Simple check to make sure things look ok... for (Table catalog_tbl : catalog_db.getTables()) { String sql = CatalogUtil.toSchema(catalog_tbl); assertTrue(sql.startsWith("CREATE TABLE " + catalog_tbl.getTypeName())); // Columns for (Column catalog_col : catalog_tbl.getColumns()) { assertTrue(sql.indexOf(catalog_col.getTypeName()) != -1); } // Constraints for (Constraint catalog_const : catalog_tbl.getConstraints()) { ConstraintType const_type = ConstraintType.get(catalog_const.getType()); Index catalog_idx = catalog_const.getIndex(); // 2011-07-15: Skip all foreign keys if (catalog_const.getType() == ConstraintType.FOREIGN_KEY.getValue()) continue; assertNotNull("Missing index for " + catalog_const.fullName(), catalog_idx); List<ColumnRef> columns = CatalogUtil.getSortedCatalogItems(catalog_idx.getColumns(), "index"); if (!columns.isEmpty()) { search_str = ""; String add = ""; for (ColumnRef catalog_colref : columns) { search_str += add + catalog_colref.getColumn().getTypeName(); add = ", "; } assertTrue(sql.indexOf(search_str) != -1); } switch (const_type) { case PRIMARY_KEY: assertTrue(sql.indexOf("PRIMARY KEY") != -1); break; case FOREIGN_KEY: search_str = "REFERENCES " + catalog_const.getForeignkeytable().getTypeName(); assertTrue(sql.indexOf(search_str) != -1); break; } } } } }