📄 t_accessfactory.java
字号:
/* Derby - Class org.apache.derbyTesting.unitTests.store.T_AccessFactory Copyright 1997, 2005 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derbyTesting.unitTests.store;import org.apache.derbyTesting.unitTests.harness.T_Generic;import org.apache.derbyTesting.unitTests.harness.T_Fail;import org.apache.derby.iapi.store.access.*;import org.apache.derby.iapi.types.SQLLongint;import org.apache.derby.iapi.services.context.ContextManager;import org.apache.derby.iapi.services.context.ContextService;import org.apache.derby.iapi.services.io.Storable;import org.apache.derby.iapi.services.monitor.Monitor;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.services.io.FormatIdUtil;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.RowLocation;import org.apache.derby.iapi.store.raw.RawStoreFactory;import org.apache.derby.iapi.store.raw.Transaction;import org.apache.derby.iapi.reference.Property;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.services.io.FormatableBitSet;import org.apache.derby.iapi.services.i18n.MessageService;import java.io.File;import java.io.Serializable;import java.util.Hashtable;import java.util.Properties;import org.apache.derby.iapi.types.SQLInteger;import org.apache.derby.iapi.types.SQLChar;public class T_AccessFactory extends T_Generic{ private static final String testService = "accessTest"; AccessFactory store = null; public T_AccessFactory() { super(); } /* ** Methods of UnitTest. */ /* ** Methods required by T_Generic */ public String getModuleToTestProtocolName() { return AccessFactory.MODULE; } /** @exception T_Fail Unexpected behaviour from the API */ protected void runTests() throws T_Fail { TransactionController tc = null; boolean pass = false; // Create a AccessFactory to test. // don't automatic boot this service if it gets left around if (startParams == null) { startParams = new Properties(); } startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString()); // remove the service directory to ensure a clean run startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE.toString()); // see if we are testing encryption startParams = T_Util.setEncryptionParam(startParams); try { store = (AccessFactory) Monitor.createPersistentService( getModuleToTestProtocolName(), testService, startParams); } catch (StandardException mse) { throw T_Fail.exceptionFail(mse); } if (store == null) { throw T_Fail.testFailMsg( getModuleToTestProtocolName() + " service not started."); } REPORT("(unitTestMain) Testing " + testService); try { ContextManager cm = ContextService.getFactory().getCurrentContextManager(); tc = store.getAndNameTransaction( cm, AccessFactoryGlobals.USER_TRANS_NAME); if ( dropTest(tc) && holdCursor(tc) && readUncommitted(tc) && updatelocks(tc) && nestedUserTransaction(tc) && sortCost(tc) && storeCost(tc) && partialScan(tc) && scanInfo(tc) && insertAndUpdateExample(tc) && insertAndFetchExample(tc) && scanExample(tc) && alterTable(tc) && tempTest(tc) && getTableProperties(tc) && insert_bench(tc) && transactionalProperties(tc) && commitTest(tc)) { pass = true; } // Make sure commitNoSync gets executed sometimes. tc.commitNoSync(TransactionController.RELEASE_LOCKS); tc.destroy(); if (!pass) throw T_Fail.testFailMsg("test failed"); } catch (StandardException e) { String msg = e.getMessage(); if (msg == null) msg = e.getClass().getName(); REPORT(msg); e.printStackTrace(); throw T_Fail.exceptionFail(e); } catch (Throwable t) { t.printStackTrace(); } } /* ** Methods of T_AccessFactory. */ private void flush_cache() throws StandardException { // flush and empty cache to make sure rereading stuff works. RawStoreFactory rawstore = (RawStoreFactory) Monitor.findServiceModule( this.store, RawStoreFactory.MODULE); rawstore.checkpoint(); } protected boolean insertAndFetchExample(TransactionController tc) throws StandardException, T_Fail { REPORT("(insertAndFetchExample)"); // First a negative test - make sure heap requires a template: try { // Create a heap conglomerate. long conglomid = tc.createConglomerate( "heap", // create a heap conglomerate null, // ERROR - Heap requires a template!!! null, // column sort order not required for heap null, // default properties TransactionController.IS_DEFAULT); // not temporary throw T_Fail.testFailMsg("Allowed heap create without template."); } catch (Throwable t) { // expected error, just continue. } // Create a heap conglomerate. T_AccessRow template_row = new T_AccessRow(1); long conglomid = tc.createConglomerate( "heap", // create a heap conglomerate template_row.getRowArray(), // 1 column template. null, // column sort order not required for heap null, // default properties TransactionController.IS_DEFAULT); // not temporary // Insert and fetch some values. if (insertAndFetch(tc, conglomid, 33) && insertAndFetch(tc, conglomid, -1) && insertAndFetch(tc, conglomid, -1000000000)) { return true; } else { return false; } } // Insert a single row with a single column containing // the argument integer, and fetch it back, making sure that // we read the correct value. // protected boolean insertAndFetch( TransactionController tc, long conglomid, int value) throws StandardException, T_Fail { StaticCompiledOpenConglomInfo static_info = tc.getStaticCompiledConglomInfo(conglomid); DynamicCompiledOpenConglomInfo dynamic_info = tc.getDynamicCompiledConglomInfo(conglomid); String curr_xact_name = tc.getTransactionIdString(); REPORT("(insertAndFetch) xact id = " + curr_xact_name); // Open the conglomerate. ConglomerateController cc = tc.openCompiledConglomerate( false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, static_info, dynamic_info); // Create a row. T_AccessRow r1 = new T_AccessRow(1); SQLInteger c1 = new SQLInteger(value); r1.setCol(0, c1); // Get a location template RowLocation rowloc = cc.newRowLocationTemplate(); // Insert the row and remember its location. cc.insertAndFetchLocation(r1.getRowArray(), rowloc); // quick test to make sure we can hash insert and find row location. Hashtable test_rowloc_hash = new Hashtable(); test_rowloc_hash.put(rowloc, rowloc); RowLocation hash_find = (RowLocation) test_rowloc_hash.get(rowloc); if (!hash_find.equals(rowloc)) throw T_Fail.testFailMsg("(insertAndFetch) bad hash lookup 1"); hash_find = (RowLocation) test_rowloc_hash.remove(rowloc); if (!hash_find.equals(rowloc)) throw T_Fail.testFailMsg("(insertAndFetch) bad hash lookup 2"); hash_find = (RowLocation) test_rowloc_hash.remove(rowloc); if (hash_find != null) throw T_Fail.testFailMsg("(insertAndFetch) bad hash lookup 3"); // Create a new row of the same type (since the interface expects // the callers to be keeping the row types straight), but with // a different column value. T_AccessRow r2 = new T_AccessRow(1); SQLInteger c2 = new SQLInteger(0); r2.setCol(0, c2); // Fetch the stored value. if (!cc.fetch(rowloc, r2.getRowArray(), (FormatableBitSet) null)) { throw T_Fail.testFailMsg("(insertAndFetch) fetch found no row."); } // Fetch using the fetch partial column interface SQLInteger c3 = new SQLInteger(0); FormatableBitSet singleColumn = new FormatableBitSet(1); singleColumn.set(0); DataValueDescriptor[] c3row = new DataValueDescriptor[1]; c3row[0] = c3; if (!cc.fetch(rowloc, c3row, singleColumn)) { throw T_Fail.testFailMsg("(insertAndFetch) fetch found no row."); } // Close the conglomerate. cc.close(); // Make sure we read back the value we wrote. if (c2.getInt() != value) throw T_Fail.testFailMsg("(insertAndFetch) Fetched value != inserted value."); if (c3.getInt() != value) throw T_Fail.testFailMsg("(insertAndFetch) Fetched value != inserted value."); return true; } protected boolean insertAndUpdateExample(TransactionController tc) throws StandardException, T_Fail { // Create a heap conglomerate. long conglomid = tc.createConglomerate( "heap", // create a heap conglomerate new T_AccessRow(1).getRowArray(), // 1 column template. null, // column sort order not required for heap null, // default properties TransactionController.IS_DEFAULT); // not temporary REPORT("(insertAndUpdateExample)"); // Insert and update some values if (insertAndUpdate(tc, conglomid, -1, -1003152) && insertAndUpdate(tc, conglomid, 0, 2000000000) && deletetest(tc, conglomid, 1, 2)) { return true; } return false; } // Insert a single row with a single column containing // the first argument integer, delete it, make sure subsequent // delete, replace, and replace a single column return false. // protected boolean deletetest( TransactionController tc, long conglomid, int value1, int value2) throws StandardException, T_Fail { boolean ret_val; // Open the conglomerate. ConglomerateController cc = tc.openConglomerate( conglomid, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE); // Create a row. T_AccessRow r1 = new T_AccessRow(1); r1.setCol(0, new SQLInteger(value1)); // Get a location template RowLocation rowloc = cc.newRowLocationTemplate(); // Insert the row and remember its location. cc.insertAndFetchLocation(r1.getRowArray(), rowloc); // delete it. if (!cc.delete(rowloc)) { throw T_Fail.testFailMsg("(deleteTest) delete of row failed"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -