KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > store > T_Util


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.store.T_Util
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.store;
23
24 import org.apache.derby.iapi.store.raw.*;
25
26 import org.apache.derby.iapi.services.io.FormatableBitSet;
27
28 import org.apache.derby.iapi.reference.Property;
29
30 // impl imports are the preferred way to create unit tests.
31
import org.apache.derbyTesting.unitTests.harness.T_MultiThreadedIterations;
32 import org.apache.derbyTesting.unitTests.harness.T_Fail;
33
34 import org.apache.derby.iapi.services.context.ContextService;
35 import org.apache.derby.iapi.services.context.ContextManager;
36 import org.apache.derby.iapi.services.locks.*;
37 import org.apache.derby.iapi.services.sanity.SanityManager;
38
39 import org.apache.derby.iapi.error.StandardException;
40
41 import org.apache.derby.iapi.store.access.AccessFactoryGlobals;
42 import org.apache.derby.iapi.store.access.Qualifier;
43
44 import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo;
45
46 import org.apache.derby.iapi.types.DataValueDescriptor;
47
48
49 import org.apache.derby.iapi.reference.Attribute;
50 import org.apache.derby.iapi.services.property.PropertyUtil;
51 import org.apache.derby.iapi.error.ExceptionSeverity;
52 import java.io.*;
53 import java.util.Properties JavaDoc;
54 import org.apache.derby.iapi.types.SQLChar;
55
56
57 /*
58   Utility class to help test raw store functionality.
59
60   If you write a raw store unit test, be that a protocol test or an
61   implementation test, and find youself needing to do certain operations over
62   and over again, chances are that functionality is either in here or should be
63   added. This class is here entirely for the convenience of people writing
64   unit tests for the RawStore.
65 */

66 public class T_Util
67 {
68
69     RawStoreFactory rsFactory;
70     LockFactory lFactory;
71     ContextService csFactory;
72
73     private int openContainerMode; // mode flags used in openContainer
74

75     public T_Util(RawStoreFactory rsf, LockFactory lf,
76                   ContextService csf)
77     {
78         rsFactory = rsf;
79         lFactory = lf;
80         csFactory = csf;
81
82         openContainerMode = 0; // logged by default
83
}
84
85     public void setOpenMode(int newMode) {
86         openContainerMode = newMode;
87     }
88
89     /*
90      * function that checks for a condition, throws T_Fail exception if the condition
91      * is not met.
92      */

93
94     /*
95      * check that transaction does not hold any lock
96      */

97     public void t_checkNullLockCount(Transaction t) throws T_Fail {
98         if (lFactory.areLocksHeld(t))
99             throw T_Fail.testFailMsg("Previous action did not clean up all locks.");
100     }
101
102     /*
103      * check that page number on the page matches the input page number
104      */

105     public static void t_checkPageNumber(Page page, long pageNumber) throws T_Fail {
106         if (page.getPageNumber() != pageNumber)
107             throw T_Fail.testFailMsg("page number expected to be " + pageNumber + ", is " +
108                 page.getPageNumber());
109     }
110
111     /*
112      * check that the number of record on the page matches input.
113      * @param page the page in question
114      * @param count the total number of record - this include deleted as well as non-deleted
115      * @param nonDeleted the number of non-deleted record
116      */

117     public static void t_checkRecordCount(Page page, int count, int nonDeleted) throws T_Fail, StandardException {
118         if (page.recordCount() != count)
119             throw T_Fail.testFailMsg("recordCount() expected to be " + count + ", is " + page.recordCount());
120
121         if (page.nonDeletedRecordCount() != nonDeleted)
122             throw T_Fail.testFailMsg("nonDeletedRecordCount() expected to be " + nonDeleted + ", is " + page.nonDeletedRecordCount());
123     }
124
125     /*
126      * check the number of fields in the slot
127      */

128     public static void t_checkFieldCount(Page page, int slot, int count) throws T_Fail, StandardException {
129         if (page.fetchNumFieldsAtSlot(slot) != count)
130             throw T_Fail.testFailMsg("number of fields at slot " + slot + " expected to be " + count
131                                      + ", is " + page.fetchNumFieldsAtSlot(slot));
132     }
133
134     /**
135         Fetch a record that is expected to exist using a record handle.
136         The record has a T_RawStoreRow of 1 column and this column as value as
137         specified by data, which could be null.
138
139         Calls recordExists() before fetch to ensure that the record
140         is there.
141
142         @param page the page in question
143         @param rh the record handle
144         @param data the string value that is expected in the row
145
146         @exception T_Fail Implementation failed expectation
147         @exception StandardException Unexpected exception from the implementation
148
149         @see Page#recordExists
150         @see Page#fetch
151     */

152     public static void t_checkFetch(Page page, RecordHandle rh, String JavaDoc data, int stringLen)
153         throws T_Fail, StandardException {
154
155         t_checkFetch(page, rh, T_Util.getStringFromData(data, stringLen));
156     }
157     
158     public static void t_checkFetch(Page page, RecordHandle rh, String JavaDoc data)
159         throws T_Fail, StandardException {
160
161         if (!page.recordExists(rh, false))
162             throw T_Fail.testFailMsg("Record does not exist");
163
164         T_RawStoreRow readRow = new T_RawStoreRow((String JavaDoc) null);
165
166         int slot = page.getSlotNumber(rh);
167
168         RecordHandle rhf =
169             page.fetchFromSlot(
170                 rh, slot, readRow.getRow(),
171                 (FetchDescriptor) null,
172                 false);
173
174         if (rhf == null)
175             throw T_Fail.testFailMsg("Failed to read record");
176
177         if ((data == null) || readRow.getStorableColumn(0).isNull()) {
178
179             if ((data == null) && readRow.getStorableColumn(0).isNull())
180                 return;
181
182             throw T_Fail.testFailMsg("Record's value incorrect");
183         }
184
185         if (!readRow.toString().equals(data))
186             throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString());
187     }
188
189     /**
190         Fetch a record from a container that is expected to exist using a record handle.
191         Calls recordExists() before fetch to ensure that the record
192         is there.
193
194         @exception T_Fail Implementation failed expectation
195         @exception StandardException Unexpected exception from the implementation
196
197         @see Page#recordExists
198         @see Page#fetch
199     */

200     public void t_checkFetch(ContainerHandle c, RecordHandle rh, String JavaDoc data)
201         throws T_Fail, StandardException {
202
203         Page page = t_getPage(c, rh.getPageNumber());
204
205         try
206         {
207             t_checkFetch(page, rh, data);
208         }
209         finally
210         {
211             page.unlatch();
212         }
213     }
214
215     /**
216         Check to make sure record is NOT there
217
218         @exception T_Fail Implementation failed expectation
219         @exception StandardException Unexpected exception from the implementation
220      */

221     public void t_checkFetchFail(ContainerHandle c, RecordHandle rh)
222         throws T_Fail, StandardException
223     {
224         Page page = t_getPage(c, rh.getPageNumber());
225
226         try
227         {
228             if (page.recordExists(rh, true))
229                 throw T_Fail.testFailMsg("Record Exists");
230         }
231         finally
232         {
233             page.unlatch();
234         }
235     }
236
237     /**
238         Fetch a deleted record from a container using a record handle.
239
240         @exception T_Fail Implementation failed expectation
241         @exception StandardException Unexpected exception from the implementation
242
243         @see Page#recordExists
244         @see Page#fetch
245     */

246     public void t_checkFetchDeleted(ContainerHandle c, RecordHandle rh,
247                                     String JavaDoc data)
248         throws T_Fail, StandardException
249     {
250         Page p = t_getPage(c, rh.getPageNumber());
251         if (p == null)
252             throw T_Fail.testFailMsg("Page not found " + rh);
253
254         T_RawStoreRow readRow = new T_RawStoreRow((String JavaDoc) null);
255
256         try
257         {
258             int slot = p.getSlotNumber(rh);
259             if (p.fetchFromSlot(
260                     rh, slot, readRow.getRow(),
261                     (FetchDescriptor) null,
262                     false) != null)
263             {
264                 throw T_Fail.testFailMsg(
265                     "Record at slot " + slot + " not deleted");
266             }
267         }
268         finally
269         {
270             p.unlatch();
271         }
272     }
273
274
275     /*
276         Fetch a record that is expected to exist using a record handle.
277         The record contains the values in the passed in row, which is a
278         T_RawStoreRow. A T_RawStoreRow of the same number of columns will be made and fetched
279         from the page and compared with the passed in row.
280
281     */

282     public static void t_checkFetch(Page page, RecordHandle rh, T_RawStoreRow row)
283         throws T_Fail, StandardException
284     {
285         if (!page.recordExists(rh, false))
286             throw T_Fail.testFailMsg("Record does not exist");
287
288         // try to fetch the same number of columns as the passed in row
289
int ncol = row.nColumns();
290         T_RawStoreRow readRow = new T_RawStoreRow(ncol);
291         for (int i = 0; i < ncol; i++)
292             readRow.setColumn(i, (String JavaDoc) null);
293
294         RecordHandle rhf = page.fetch(rh, readRow.getRow(), (FormatableBitSet) null, false);
295         if (rhf == null)
296             throw T_Fail.testFailMsg("Failed to read record");
297         if (!readRow.toString().equals(row.toString()))
298             throw T_Fail.testFailMsg("Record's value incorrect, expected :" +
299                                      row.toString() + ": - got :" + readRow.toString());
300     }
301
302
303     /*
304         Using sparse row representation:
305         Fetch a column of a record that is expected to exist, using a record
306         handle and a FormatableBitSet object.
307         Check that column colNum has value data.
308     */

309     public static void t_checkFetchCol(Page page, RecordHandle rh, int colNum,
310                                        int numCols, String JavaDoc data)
311         throws T_Fail, StandardException
312     {
313         if (!page.recordExists(rh, false))
314             throw T_Fail.testFailMsg("Record does not exist");
315
316         T_RawStoreRow readRow = new T_RawStoreRow(numCols);
317         for (int i = 0; i < numCols; i++)
318             readRow.setColumn(i, (String JavaDoc) null);
319         FormatableBitSet colList = new FormatableBitSet(numCols);
320         colList.set(colNum);
321
322         RecordHandle rhf = page.fetch(rh, readRow.getRow(), colList, false);
323         if (rhf == null)
324             throw T_Fail.testFailMsg("Failed to read record");
325         String JavaDoc col = readRow.getStorableColumn(colNum).toString();
326         if (!col.equals(data))
327             throw T_Fail.testFailMsg("Record's value for column " + colNum +
328                                      " incorrect, expected :" + data +
329                                      ": - got :" + readRow.toString());
330     }
331
332
333     /*
334      * the following is a sequence of fetches, fetching the first row, fetching
335      * the next and previous rows, and fetching the last row in the page.
336      *
337      * The row is assumed to be a T_RawStoreRow with 1 column, which value is the
338      * string specified in data.
339      */

340
341     /*
342      * fetch and check the first row in the page.
343      * Return the first row's recordHandle.
344      */

345     public static RecordHandle t_checkFetchFirst(Page page, String JavaDoc data)
346         throws T_Fail, StandardException {
347         T_RawStoreRow readRow = new T_RawStoreRow((String JavaDoc) null);
348
349         int slot = 0;
350         while (page.isDeletedAtSlot(slot))
351         {
352             slot++;
353         }
354
355         RecordHandle rhf =
356             page.fetchFromSlot(
357                 (RecordHandle) null, slot,
358                 readRow.getRow(),
359                 (FetchDescriptor) null,
360                 false);
361
362         if (rhf == null)
363             throw T_Fail.testFailMsg("Failed to read record");
364         if (!readRow.toString().equals(data))
365             throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString());
366
367         return rhf;
368     }
369
370     /*
371      * Fetch and check the next (next to rh) row in the page.
372      * Return the next row's recordHandle
373      */

374     public static RecordHandle t_checkFetchNext(Page page, RecordHandle rh, String JavaDoc data)
375         throws T_Fail, StandardException {
376
377         if (!page.recordExists(rh, false))
378             throw T_Fail.testFailMsg("Record does not exist");
379
380         T_RawStoreRow readRow = new T_RawStoreRow((String JavaDoc) null);
381
382         int slot = page.getSlotNumber(rh) + 1;
383         while (page.isDeletedAtSlot(slot))
384         {
385             slot++;
386         }
387
388         RecordHandle rhf =
389             page.fetchFromSlot(
390                 (RecordHandle) null,
391                 slot,
392                 readRow.getRow(),
393                 (FetchDescriptor) null,
394                 false);
395
396         if (rhf == null)
397             throw T_Fail.testFailMsg("Failed to read record");
398         if (!readRow.toString().equals(data))
399             throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString());
400
401         return rhf;
402     }
403     
404     /*
405      * Fetch and check the previous (previous to rh) row in the page.
406      * Return the previous row's recordHandle
407      */

408     public static RecordHandle t_checkFetchPrevious(Page page, RecordHandle rh, String JavaDoc data)
409         throws T_Fail, StandardException {
410
411         if (!page.recordExists(rh, false))
412             throw T_Fail.testFailMsg("Record does not exist");
413
414         T_RawStoreRow readRow = new T_RawStoreRow((String JavaDoc) null);
415
416         int slot = page.getSlotNumber(rh) - 1;
417
418         while (page.isDeletedAtSlot(slot) && slot >= 0)
419         {
420             slot--;
421         }
422
423         if (slot == -1)
424             return(null);
425
426
427         RecordHandle rhf =
428             page.fetchFromSlot(
429                 (RecordHandle) null,
430                 slot,
431                 readRow.getRow(),
432                 (FetchDescriptor) null,
433                 false);
434
435         if (rhf == null)
436             throw T_Fail.testFailMsg("Failed to read record");
437         if (!readRow.toString().equals(data))
438             throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString());
439
440         return rhf;
441     }
442
443     /*
444      * Fetch and check the last row in the page.
445      * Return the last row's recordHandle
446      */

447     public static RecordHandle t_checkFetchLast(Page page, String JavaDoc data)
448         throws T_Fail, StandardException {
449         T_RawStoreRow readRow = new T_RawStoreRow((String JavaDoc) null);
450
451         int slot = page.recordCount() - 1;
452         while (page.isDeletedAtSlot(slot) && slot >= 0)
453         {
454             slot--;
455         }
456
457         if (slot == -1)
458             return(null);
459
460         RecordHandle rhf =
461             page.fetchFromSlot(
462                 (RecordHandle) null,
463                 slot,
464                 readRow.getRow(),
465                 (FetchDescriptor) null,
466                 false);
467
468         if (rhf == null)
469             throw T_Fail.testFailMsg("Failed to read record");
470         if (!readRow.toString().equals(data))
471             throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString());
472
473         return rhf;
474     }
475
476
477     /*
478      * Fetch and check the slot on the page.
479      *
480      * The slot number is NOT a stable reference once the page is unlatched,
481      * this check is only valid if you know the page has not been unlatched
482      * since you put the row in, or you know nobody has touched the page since
483      * you determined the slot number
484      *
485      * The slot refers to a row in the page which has a T_RawStoreRow of 1 column, the
486      * column has the value of data input.
487      *
488      * @param page the page in question
489      * @param slot the slot number (see above)
490      * @param data the column value
491      * @param deleted if the row is deleted, set to true
492      * @param forUpdate If you want to lock the row for update, set forUpdate to true.
493      *
494      */

495     public static void t_checkFetchBySlot(Page page, int slot,
496                                     String JavaDoc data, boolean deleted,
497                                     boolean forUpdate)
498         throws T_Fail, StandardException
499     {
500         T_RawStoreRow readRow = new T_RawStoreRow((String JavaDoc) null);
501         RecordHandle rh =
502             page.fetchFromSlot(
503                 (RecordHandle) null, slot,
504                 readRow.getRow(),
505                 (FetchDescriptor) null,
506                 true);
507
508         if (rh == null)
509             throw T_Fail.testFailMsg("Failed to read record");
510         if (!readRow.toString().equals(data))
511             throw T_Fail.testFailMsg("Record's value incorrect, expected :" + data + ": - got :" + readRow.toString());
512
513         if (page.isDeletedAtSlot(slot) != deleted)
514             throw T_Fail.testFailMsg("Record at slot " + slot + " deleted=" +
515                                      page.isDeletedAtSlot(slot) + ", expect " + deleted);
516
517         // RESOLVE: check locking
518
}
519
520     /*
521      * check a column value from a slot on the page
522      *
523      * The slot number is NOT a stable reference once the page is unlatched,
524      * this check is only valid if you know the page has not been unlatched
525      * since you put the row in, or you know nobody has touched the page since
526      * you determined the slot number
527      *
528      * The storable in the specified column put into the input column and it
529      * is check for the same value as the input data
530      *
531      * @param page the page in question
532      * @param slot the slot number (see above)
533      * @param fieldId the field Id on the row
534      * @param column the storable to put the column in
535      * @param forUpdate true if you want to lock the row for update
536      * @param data the expected value in the column
537      */

538     public static void t_checkFetchColFromSlot(Page page,
539                                          int slot,
540                                          int fieldId,
541                                          DataValueDescriptor column,
542                                          boolean forUpdate,
543                                          String JavaDoc data,
544                                          int stringLen)
545          throws StandardException, T_Fail
546     {
547         t_checkFetchColFromSlot(page, slot, fieldId, column, forUpdate, T_Util.getStringFromData(data, stringLen));
548     }
549     
550     public static void t_checkFetchColFromSlot(
551     Page page,
552     int slot,
553     int fieldId,
554     DataValueDescriptor column,
555     boolean forUpdate,
556     String JavaDoc data)
557          throws StandardException, T_Fail
558     {
559         DataValueDescriptor[] fetch_row = new DataValueDescriptor[fieldId + 1];
560         fetch_row[fieldId] = column;
561         FormatableBitSet validCols = new FormatableBitSet(fieldId + 1);
562         validCols.set(fieldId);
563
564         RecordHandle rh =
565             page.fetchFromSlot(
566                 null, slot, fetch_row,
567                 new FetchDescriptor(
568                     fetch_row.length, validCols, (Qualifier[][]) null),
569                 true);
570
571         if (rh == null)
572             throw T_Fail.testFailMsg("Failed to fetch record: slot "
573                              + slot + " field " + fieldId);
574
575         // RESOLVE - how to check rh lock mode?
576

577         if (data == null)
578         {
579             if (!column.isNull())
580                 throw T_Fail.testFailMsg("Failed to fetch null column: slot "
581                                  + slot + " field " + fieldId + " column is " + column);
582         }
583         else
584         {
585             if (column.isNull())
586                 throw T_Fail.testFailMsg("expect non null column, got null: slot "
587                                  + slot + " field " + fieldId);
588             if (!column.toString().equals(data))
589                 throw T_Fail.testFailMsg("expect " + data + " got " + column.toString()
590                                  + ": slot " + slot + " field " + fieldId);
591         }
592     }
593
594
595     /**
596         Take an empty page and check it does actually seem to be empty.
597
598         @exception T_Fail Unexpected behaviour from the API
599         @exception StandardException Unexpected exception from the implementation
600     */

601     public static void t_checkEmptyPage(Page page) throws T_Fail, StandardException {
602
603         // check the counts
604
t_checkRecordCount(page, 0, 0);
605
606         try
607         {
608             page.fetchFromSlot(
609                 (RecordHandle) null, 0, null,
610                 (FetchDescriptor) null,
611                 false);
612
613             throw T_Fail.testFailMsg(
614                 "fetchFromSlot() must throw exception on fetch from slot 0 on an empty page");
615         }
616         catch (StandardException se)
617         {
618             // expected exception.
619
}
620
621         // check we can't get a record handle. NB here we are guessing that 0
622
// and RecordHandle.FIRST_RECORD_ID might be valid record identifiers,
623
// nothing in the API states that they will be. Eother way we
624
// shouldn't get a valid RecordHandle back.
625
if (page.getRecordHandle(0) != null)
626             throw T_Fail.testFailMsg("obtained a RecordHandle for an empty page");
627
628         if (page.getRecordHandle(RecordHandle.FIRST_RECORD_ID) != null)
629             throw T_Fail.testFailMsg("obtained a RecordHandle for an empty page");
630         
631         // should be no aux object
632
if (page.getAuxObject() != null)
633             throw T_Fail.testFailMsg("empty page has an aux object");
634
635         t_readOnlySlotOutOfRange(page, Page.FIRST_SLOT_NUMBER);
636
637         if (!page.spaceForInsert())
638             throw T_Fail.testFailMsg("spaceForInsert() returned false on an empty page");
639     }
640
641     /*
642         Check to see the correct behaviour for read only operations
643         that take a slot when the slot is out of range.
644     */

645     public static void t_readOnlySlotOutOfRange(Page page, int slot) throws T_Fail, StandardException {
646
647         try {
648             page.fetchFromSlot(
649                 (RecordHandle) null, slot,
650                 new DataValueDescriptor[0],
651                 (FetchDescriptor) null,
652                 true);
653
654             throw T_Fail.testFailMsg("fetchFromSlot succeeded on out of range slot " + slot);
655         } catch (StandardException se0) {
656             // Statement exception expected, throw if not a statement exception.
657
if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY)
658                 throw se0;
659         }
660         try {
661             page.isDeletedAtSlot(slot);
662             throw T_Fail.testFailMsg("isDeletedAtSlot succeeded on out of range slot " + slot);
663         } catch (StandardException se2) {
664             // Statement exception expected, throw if not a statement exception.
665
if (se2.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY)
666                 throw se2;
667         }
668     }
669
670     /*
671         Check to see the correct behaviour for update operations
672         that take a slot when the slot is out of range.
673     */

674     public static void t_updateSlotOutOfRange(Page page, int slot) throws T_Fail, StandardException {
675
676         try {
677             page.deleteAtSlot(slot, false, (LogicalUndo)null);
678             throw T_Fail.testFailMsg("deleteAtSlot succeeded on out of range slot " + slot);
679         } catch (StandardException se0) {
680             // Statement exception expected, throw if not a statement exception.
681
if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY)
682                 throw se0;
683         }
684         try {
685             page.deleteAtSlot(slot, true, (LogicalUndo)null);
686             throw T_Fail.testFailMsg("deleteAtSlot succeeded on out of range slot " + slot);
687         } catch (StandardException se0) {
688             // Statement exception expected, throw if not a statement exception.
689
if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY)
690                 throw se0;
691         }
692
693         T_RawStoreRow row = new T_RawStoreRow((String JavaDoc) null);
694
695         // insert at the last slot will succeed, so don't do it.
696
if (page.recordCount() != slot) {
697             try {
698                     page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null, (LogicalUndo)null,
699                         Page.INSERT_DEFAULT, 100);
700                     throw T_Fail.testFailMsg("insertAtSlot succeeded, on out of range slot " + slot);
701             } catch (StandardException se0) {
702                 // Statement exception expected, throw if not a statement exception.
703
if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY)
704                     throw se0;
705             }
706         }
707
708         try {
709             page.updateAtSlot(slot, row.getRow(), (FormatableBitSet) null);
710             throw T_Fail.testFailMsg("updateAtSlot succeeded on out of range slot " + slot);
711         } catch (StandardException se0) {
712             // Statement exception expected, throw if not a statement exception.
713
if (se0.getSeverity() > ExceptionSeverity.STATEMENT_SEVERITY)
714                 throw se0;
715         }
716     }
717
718
719     /*
720      * Save point checks
721      */

722
723     /**
724         Negative test - check that an invalid savepoint is detected.
725         
726         @exception T_Fail Unexpected behaviour from the API
727         @exception StandardException Unexpected exception from the implementation
728     */

729     public static void t_checkInvalidSavePoint(Transaction t, String JavaDoc name)
730         throws T_Fail, StandardException {
731
732         // check a non-existent save point is trapped
733
try {
734             t.rollbackToSavePoint(name, null);
735
736             throw T_Fail.testFailMsg("non existent save point did not cause exception on rollbackToSavePoint");
737         } catch (StandardException se) {
738             // we expected this ...
739
}
740         try {
741             t.releaseSavePoint(name, null);
742             throw T_Fail.testFailMsg("non existent save point did not cause exception on releaseSavePoint");
743
744         } catch (StandardException se) {
745             // we expected this ...
746
}
747     }
748
749     /*
750      * same as above, check an invalid savepoint in the given transaction
751      * context
752      */

753     public void t_checkInvalidSavePoint(T_TWC ctx, String JavaDoc name)
754         throws T_Fail, StandardException {
755         csFactory.setCurrentContextManager(ctx.cm);
756         try {
757         t_checkInvalidSavePoint(ctx.tran, name);
758         } finally {
759             csFactory.resetCurrentContextManager(ctx.cm);
760         }
761     }
762
763
764     /*
765      * function that actually do something, start, commit, abort a trasaction,
766      * get a page, insert a row, etc.
767      */

768
769
770     /*
771         Start a user transaction, ensures that the startTransaction method
772         does not return null (which it shouldn't).
773     */

774     public Transaction t_startTransaction()
775         throws StandardException, T_Fail {
776             
777             Transaction t1 =
778                 rsFactory.startTransaction(
779                     csFactory.getCurrentContextManager(),
780                     AccessFactoryGlobals.USER_TRANS_NAME);
781
782             if (t1 == null)
783                 throw T_Fail.testFailMsg("Start a transaction");
784             t_checkNullLockCount(t1);
785             return t1;
786     }
787
788     /*
789         Start a user transaction, ensures that the startTransaction method
790         does not return null (which it shouldn't).
791     */

792     public Transaction t_startGlobalTransaction(
793     int format_id,
794     byte[] global_id,
795     byte[] branch_id)
796         throws StandardException, T_Fail {
797
798             Transaction t1 =
799                 rsFactory.startGlobalTransaction(
800                     csFactory.getCurrentContextManager(),
801                     format_id, global_id, branch_id);
802
803             if (t1 == null)
804                 throw T_Fail.testFailMsg("Start a transaction");
805             t_checkNullLockCount(t1);
806             return t1;
807     }
808
809     /*
810      * start a user transaction with its own context (T_TWC)
811      */

812     public T_TWC t_startTransactionWithContext()
813         throws StandardException, T_Fail
814     {
815         T_TWC ctx = new T_TWC(csFactory, lFactory, rsFactory);
816         ctx.startUserTransaction();
817         return ctx;
818     }
819
820     /*
821      * start an internal transaction
822      */

823     public Transaction t_startInternalTransaction()
824         throws StandardException, T_Fail {
825
826             Transaction t1 = rsFactory.startInternalTransaction(csFactory.getCurrentContextManager());
827
828             if (t1 == null)
829                 throw T_Fail.testFailMsg("Failed to start an internal transaction");
830             t_checkNullLockCount(t1);
831             return t1;
832     }
833
834     /*
835      * commit a transaction
836      */

837     public void t_commit(Transaction t)
838         throws StandardException, T_Fail {
839         t.commit();
840         t_checkNullLockCount(t);
841     }
842
843     /*
844      * commit a transaction with context
845      */

846     public void t_commit(T_TWC ctx)
847         throws StandardException, T_Fail
848     {
849         csFactory.setCurrentContextManager(ctx.cm);
850         try {
851         t_commit(ctx.tran);
852         } finally {
853             csFactory.resetCurrentContextManager(ctx.cm);
854         }
855     }
856
857     /*
858      * close a transaction with context
859      */

860     public void t_close(T_TWC ctx)
861         throws StandardException, T_Fail
862     {
863         ctx.tran.close();
864         ctx.tran = null;
865         ctx.cm = null; // no need to close a context ???
866
}
867
868     /*
869      * abort a transaction
870      */

871     public void t_abort(Transaction t)
872         throws StandardException, T_Fail {
873         t.abort();
874         t_checkNullLockCount(t);
875     }
876
877     /*
878      * abort a transaction with context
879      */

880     public void t_abort(T_TWC ctx)
881         throws StandardException, T_Fail
882     {
883         csFactory.setCurrentContextManager(ctx.cm);
884         try {
885         t_abort(ctx.tran);
886         } finally {
887             csFactory.resetCurrentContextManager(ctx.cm);
888         }
889     }
890
891     /**
892         Add a new container in the transaction
893
894         @exception T_Fail Unexpected behaviour from the API
895         @exception StandardException Unexpected exception from the implementation
896     */

897     public long t_addContainer(Transaction t, long segmentId)
898         throws StandardException, T_Fail {
899         
900         long cid =
901             t.addContainer(
902                 segmentId, ContainerHandle.DEFAULT_ASSIGN_ID,
903                 ContainerHandle.MODE_DEFAULT, (Properties JavaDoc) null, 0);
904
905         if (cid < 0)
906             throw T_Fail.testFailMsg("add container");
907
908         return cid;
909     }
910
911     public long t_addContainer(T_TWC ctx, long segmentId)
912         throws StandardException, T_Fail
913     {
914         csFactory.setCurrentContextManager(ctx.cm);
915         try {
916         return t_addContainer(ctx.tran, segmentId);
917         } finally {
918             csFactory.resetCurrentContextManager(ctx.cm);
919         }
920     }
921
922     /**
923
924         Add a new container in the transaction with a specified page size
925
926         @exception T_Fail Unexpected behaviour from the API
927         @exception StandardException Unexpected exception from the implementation
928     */

929     public long t_addContainer(Transaction t, long segmentId, int pageSize)
930         throws StandardException, T_Fail {
931
932         Properties JavaDoc tableProperties = new Properties JavaDoc();
933         tableProperties.put(Property.PAGE_SIZE_PARAMETER, Integer.toString(pageSize));
934         
935         long cid =
936             t.addContainer(
937                 segmentId, ContainerHandle.DEFAULT_ASSIGN_ID,
938                 ContainerHandle.MODE_DEFAULT, tableProperties, 0);
939
940         if (cid < 0)
941             throw T_Fail.testFailMsg("add container");
942
943         return cid;
944     }
945
946     public long t_addContainer(T_TWC ctx, long segmentId, int pageSize)
947         throws StandardException, T_Fail {
948
949         csFactory.setCurrentContextManager(ctx.cm);
950         try {
951         return t_addContainer(ctx.tran, segmentId, pageSize);
952         } finally {
953             csFactory.resetCurrentContextManager(ctx.cm);
954         }
955     }
956
957     public long t_addContainer(Transaction t, long segmentId, Properties JavaDoc tableProperties)
958         throws StandardException, T_Fail {
959
960         long cid =
961             t.addContainer(
962                 segmentId, ContainerHandle.DEFAULT_ASSIGN_ID,
963                 ContainerHandle.MODE_DEFAULT, tableProperties, 0);
964
965         if (cid < 0)
966             throw T_Fail.testFailMsg("add container");
967
968         return cid;
969     }
970                 
971
972     /**
973
974         Add a new container in the transaction with specified
975         pageSize, spareSpace, minimumRecordSize, and reusableRecordId
976
977         @exception T_Fail Unexpected behaviour from the API
978         @exception StandardException Unexpected exception from the implementation
979     */

980     public long t_addContainer(Transaction t, long segmentId, int pageSize, int spareSpace,
981             int minimumRecordSize, boolean reusableRecordId)
982         throws StandardException, T_Fail {
983
984         Properties JavaDoc tableProperties = new Properties JavaDoc();
985         tableProperties.put(Property.PAGE_SIZE_PARAMETER, Integer.toString(pageSize));
986         tableProperties.put(RawStoreFactory.PAGE_RESERVED_SPACE_PARAMETER, Integer.toString(spareSpace));
987         tableProperties.put(RawStoreFactory.MINIMUM_RECORD_SIZE_PARAMETER, Integer.toString(minimumRecordSize));
988
989         if (reusableRecordId) {
990             tableProperties.put(RawStoreFactory.PAGE_REUSABLE_RECORD_ID, "true");
991         }
992         
993         long cid =
994             t.addContainer(
995                 segmentId, ContainerHandle.DEFAULT_ASSIGN_ID,
996                 ContainerHandle.MODE_DEFAULT, tableProperties, 0);
997
998         if (cid < 0)
999             throw T_Fail.testFailMsg("add container");
1000
1001        return cid;
1002    }
1003
1004    public long t_addContainer(T_TWC ctx, long segmentId, int pageSize, int spareSpace, int minimumRecordSize)
1005        throws StandardException, T_Fail {
1006        csFactory.setCurrentContextManager(ctx.cm);
1007        try {
1008        return t_addContainer(ctx.tran, segmentId, pageSize, spareSpace, minimumRecordSize, false);
1009        } finally {
1010            csFactory.resetCurrentContextManager(ctx.cm);
1011        }
1012    }
1013
1014    /**
1015        Open a container.
1016
1017        @exception T_Fail Unexpected behaviour from the API
1018        @exception StandardException Unexpected exception from the implementation
1019    */

1020    
1021    public ContainerHandle t_openContainer(Transaction t, long segmentId, long containerId, boolean forUpdate)
1022        throws StandardException, T_Fail
1023    {
1024        ContainerKey id = new ContainerKey(segmentId, containerId);
1025        ContainerHandle c = t.openContainer(id,
1026            forUpdate ? (ContainerHandle.MODE_FORUPDATE | openContainerMode) : ContainerHandle.MODE_READONLY);
1027        if (c == null)
1028            throw T_Fail.testFailMsg("ContainerHandle failed to open: (" +
1029                                     segmentId + "," + containerId + ")");
1030
1031        return c;
1032    }
1033    public ContainerHandle t_openContainer(T_TWC ctx, long segmentId, long containerId, boolean forUpdate)
1034        throws StandardException, T_Fail
1035    {
1036        csFactory.setCurrentContextManager(ctx.cm);
1037        try {
1038            return t_openContainer(ctx.tran, segmentId, containerId, forUpdate);
1039        } finally {
1040            csFactory.resetCurrentContextManager(ctx.cm);
1041        }
1042    }
1043
1044    /**
1045        Drop a container
1046
1047        @exception T_Fail Unexpected behaviour from the API
1048        @exception StandardException Unexpected exception from the implementation
1049    */

1050    public void t_dropContainer(Transaction t, long segmentId, long containerId)
1051         throws StandardException, T_Fail
1052    {
1053        t.dropContainer(new ContainerKey(segmentId, containerId));
1054    }
1055
1056    /**
1057        Get the last page in a container.
1058        Always returns a valid page or null if there is no page in the container.
1059
1060        @exception T_Fail Unexpected behaviour from the API
1061        @exception StandardException Unexpected exception from the implementation
1062    */

1063    public Page t_getLastPage(ContainerHandle c) throws T_Fail, StandardException {
1064
1065        Page page = c.getFirstPage();
1066        if (page != null)
1067        {
1068            Page nextPage;
1069            while((nextPage = c.getNextPage(page.getPageNumber())) != null)
1070            {
1071                page.unlatch();
1072                page = nextPage;
1073            }
1074        }
1075
1076        return page;
1077    }
1078
1079
1080    /**
1081        Get a specific page in a container.
1082        Always returns a valid page.
1083
1084        @exception T_Fail Unexpected behaviour from the API
1085        @exception StandardException Unexpected exception from the implementation
1086    */

1087    public Page t_getPage(ContainerHandle c, long pageNumber) throws T_Fail, StandardException {
1088
1089        Page page = c.getPage(pageNumber);
1090        if (page == null)
1091            throw T_Fail.testFailMsg("fail to get page " + pageNumber + " from container " + c);
1092
1093        if (page.getPageNumber() != pageNumber)
1094            throw T_Fail.testFailMsg("page expected to have page number " +
1095                pageNumber + ", has " + page.getPageNumber() + " Container " + c);
1096
1097        return page;
1098    }
1099
1100    /**
1101        Add a page to a container.
1102
1103        @exception T_Fail Unexpected behaviour from the API
1104        @exception StandardException Unexpected exception from the implementation
1105    */

1106    public Page t_addPage(ContainerHandle c) throws T_Fail, StandardException {
1107
1108        Page page = c.addPage();
1109
1110        if (page == null)
1111            throw T_Fail.testFailMsg("addPage() returned null");
1112
1113        return page;
1114    }
1115
1116    /**
1117        Remove a page from a container.
1118
1119        @exception T_Fail Record handle returned is null.
1120        @exception StandardException Unexpected exception from the implementation
1121    */

1122    public void t_removePage(ContainerHandle c, Page p) throws T_Fail, StandardException
1123    {
1124        long pnum = p.getPageNumber();
1125        c.removePage(p);
1126
1127        // we should not be able to get this page
1128
Page badp = c.getPage(pnum);
1129        if (badp != null)
1130            throw T_Fail.testFailMsg("got a deallcated page back");
1131    }
1132
1133
1134    /**
1135        Call page.insert() and ensure that the return record handle is not null.
1136        This assumes the caller has called spaceForInsert.
1137
1138        @exception T_Fail Record handle returned is null.
1139        @exception StandardException Unexpected exception from the implementation
1140
1141        @see Page#insert
1142    */

1143    public static RecordHandle t_insert(Page page, T_RawStoreRow row)
1144        throws T_Fail, StandardException {
1145        
1146        RecordHandle rh = page.insert(row.getRow(), (FormatableBitSet) null, Page.INSERT_DEFAULT, 100);
1147
1148        return rh;
1149    }
1150
1151    /**
1152        Call page.insert() and ensure that the return record handle is not null.
1153        This assumes the caller has called spaceForInsert.
1154
1155        @exception T_Fail Record handle returned is null.
1156        @exception StandardException Unexpected exception from the implementation
1157
1158        @see Page#insert
1159    */

1160    public static RecordHandle t_insertAtSlot(Page page, int slot, T_RawStoreRow row)
1161        throws T_Fail, StandardException {
1162        
1163        RecordHandle rh = page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null,
1164            (LogicalUndo) null, Page.INSERT_DEFAULT, 100);
1165
1166        return rh;
1167    }
1168
1169    /**
1170        Call page.insert() and ensure that the return record handle is not null.
1171        This assumes the caller has called spaceForInsert.
1172
1173        @exception T_Fail Record handle returned is null.
1174        @exception StandardException Unexpected exception from the implementation
1175
1176        @see Page#insert
1177    */

1178    public static RecordHandle t_insertAtSlot(Page page, int slot, T_RawStoreRow row, byte insertFlag)
1179        throws T_Fail, StandardException {
1180        
1181        RecordHandle rh = page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null,
1182            (LogicalUndo) null, insertFlag, 100);
1183
1184        return rh;
1185    }
1186
1187    /**
1188        Call page.insert() and ensure that the return record handle is not null.
1189        This assumes the caller has called spaceForInsert.
1190
1191        @exception T_Fail Record handle returned is null.
1192        @exception StandardException Unexpected exception from the implementation
1193
1194        @see Page#insert
1195    */

1196    public static RecordHandle t_insertAtSlot(Page page, int slot, T_RawStoreRow row, byte insertFlag,
1197            int overflowThreshold) throws T_Fail, StandardException {
1198        
1199        RecordHandle rh = page.insertAtSlot(slot, row.getRow(), (FormatableBitSet) null,
1200            (LogicalUndo) null, insertFlag, overflowThreshold);
1201
1202        return rh;
1203    }
1204
1205    /**
1206        Insert a record on the last page, if the row doesn't fit on the
1207        last page create a new page and insert there.
1208
1209        @exception T_Fail Record handle returned is null.
1210        @exception StandardException Unexpected exception from the implementation
1211
1212        @see Page#insert
1213    */

1214    public RecordHandle t_insert(ContainerHandle c, T_RawStoreRow row)
1215        throws T_Fail, StandardException {
1216
1217        Page page = c.getPageForInsert(0);
1218        boolean addedPage = false;
1219
1220        if (page == null)
1221        {
1222            page = t_addPage(c);
1223            addedPage = true;
1224        }
1225        else if (!page.spaceForInsert(row.getRow(), (FormatableBitSet) null, 100)) {
1226            page.unlatch();
1227            page = t_addPage(c);
1228            addedPage = true;
1229        }
1230
1231        RecordHandle rh = t_insert(page, row);
1232        page.unlatch();
1233
1234        if (rh == null) {
1235            if (addedPage)
1236                throw T_Fail.testFailMsg("insert returned null on an empty page");
1237
1238            page = t_addPage(c);
1239            rh = t_insert(page, row);
1240            page.unlatch();
1241        }
1242        return rh;
1243    }
1244
1245    /**
1246        Update a record.
1247
1248        @exception T_Fail Record handle returned is null.
1249        @exception StandardException Unexpected exception from the implementation
1250
1251        @see Page#update
1252    */

1253    public void t_update(ContainerHandle c, RecordHandle rh, T_RawStoreRow row)
1254         throws T_Fail, StandardException
1255    {
1256        Page page = t_getPage(c, rh.getPageNumber());
1257        try
1258        {
1259            if (!page.update(rh, row.getRow(), (FormatableBitSet)null))
1260                throw T_Fail.testFailMsg("update failed");
1261
1262            t_checkFetch(page, rh, row);
1263        }
1264        finally
1265        {
1266            page.unlatch();
1267        }
1268    }
1269
1270
1271
1272    /**
1273        Using sparse representation:
1274        Update a column of a record and check resulting value.
1275
1276        @exception T_Fail Record handle returned is null.
1277        @exception StandardException Unexpected exception from the implementation
1278
1279        @see Page#update
1280    */

1281    public void t_checkUpdateCol(Page page, RecordHandle rh, int colNum, int
1282                                 numCols, String JavaDoc data)
1283         throws T_Fail, StandardException
1284    {
1285        if (!page.recordExists(rh, false))
1286            throw T_Fail.testFailMsg("Record does not exist");
1287
1288        T_RawStoreRow writeRow = new T_RawStoreRow(numCols);
1289        for (int i = 0; i < numCols; i++)
1290            writeRow.setColumn(i, (String JavaDoc) null);
1291        writeRow.setColumn(colNum, data);
1292        FormatableBitSet colList = new FormatableBitSet(numCols);
1293        colList.set(colNum);
1294
1295        if (!page.update(rh, writeRow.getRow(), colList))
1296            throw T_Fail.testFailMsg("update failed");
1297        
1298        t_checkFetchCol(page, rh, colNum, numCols, data);
1299    }
1300
1301        
1302
1303    /**
1304        Delete a record.
1305
1306        @exception T_Fail Record handle returned is null.
1307        @exception StandardException Unexpected exception from the implementation
1308
1309        @see Page#delete
1310    */

1311    public void t_delete(ContainerHandle c, RecordHandle rh)
1312        throws T_Fail, StandardException {
1313
1314        Page page = t_getPage(c, rh.getPageNumber());
1315
1316        try
1317        {
1318            if (!page.recordExists(rh, false))
1319                throw T_Fail.testFailMsg("record does not exist");
1320
1321            if (!page.delete(rh, (LogicalUndo)null))
1322                throw T_Fail.testFailMsg("delete failed");
1323
1324            if (page.recordExists(rh, false))
1325                throw T_Fail.testFailMsg("recordExists() returns true after a delete");
1326        }
1327        finally
1328        {
1329            page.unlatch();
1330        }
1331    }
1332
1333    /**
1334        Check to make sure a row (possibly with overflow) is of the correct length
1335
1336        @exception T_Fail Record handle returned is null.
1337        @exception StandardException Unexpected exception from the implementation
1338
1339     */

1340    public void t_checkStringLengthFetch(Page page, int slot, int expectedLength) throws T_Fail, StandardException {
1341
1342        T_RawStoreRow rr = new T_RawStoreRow((String JavaDoc) null);
1343
1344        page.fetchFromSlot(
1345            (RecordHandle) null, slot, rr.getRow(),
1346            (FetchDescriptor) null,
1347            true);
1348
1349        String JavaDoc s = ((SQLChar) (rr.getStorableColumn(0))).getString();
1350
1351
1352        if ((s == null) && (expectedLength < 0))
1353            return;
1354
1355        if ((s != null) && (expectedLength < 0))
1356            throw T_Fail.testFailMsg("Expected null string, fetched one of length " + s.length());
1357
1358        if (s == null)
1359            throw T_Fail.testFailMsg("Expected string length " + expectedLength + " got null string");
1360
1361        if (s.length() != expectedLength)
1362            throw T_Fail.testFailMsg("fetch string length incorrect expected " + expectedLength + " got " + s.length());
1363    }
1364
1365    /**
1366        Lazy people's random file generator:
1367        Generate a random file with specified name and file size
1368
1369        @exception T_Fail Record handle returned is null.
1370    */

1371    public void t_genRandomFile(String JavaDoc fileName, String JavaDoc mode, int size) throws T_Fail {
1372
1373        RandomAccessFile iFile = null;
1374        try {
1375            iFile = new RandomAccessFile(fileName, mode);
1376            for (int i = 0; i < size; i++){
1377                byte b = (byte) (i & 0xff);
1378                b = (byte) (((b >= ' ') && (b <= '~')) ? b : ' ');
1379                iFile.write(b);
1380            }
1381            iFile.close();
1382        } catch (FileNotFoundException fnfe) {
1383            throw T_Fail.testFailMsg("cannot create new file");
1384        } catch (IOException ioe) {
1385            throw T_Fail.testFailMsg("io error, test failed");
1386        }
1387
1388    }
1389
1390    /**
1391        Return a string of stringLen characters that starts with data
1392        and is padded with nulls.
1393    */

1394    public static String JavaDoc getStringFromData(String JavaDoc data, int stringLen) {
1395        char[] ca = new char[stringLen];
1396
1397        char[] sd = data.toCharArray();
1398
1399        System.arraycopy(sd, 0, ca, 0, sd.length);
1400        
1401        return new String JavaDoc(ca);
1402    }
1403
1404    /**
1405        Make this thread wait a bit, probably for post commit to finish
1406     */

1407    public static void t_wait(int milliSecond)
1408    {
1409        Thread.currentThread().yield();
1410        try
1411        {
1412            Thread.currentThread().sleep(milliSecond);
1413        }
1414        catch (InterruptedException JavaDoc ie)
1415        {
1416        }
1417    }
1418
1419    /**
1420        Add in encryption parameters to the startParam if "testDataEncryption"
1421        is set to a non-null string.
1422     */

1423    public static Properties JavaDoc setEncryptionParam(Properties JavaDoc startParams)
1424    {
1425        // see if we are testing encryption
1426
String JavaDoc encryptionPassword =
1427                    PropertyUtil.getSystemProperty("testDataEncryption");
1428        //look for alternate encryption provider
1429
String JavaDoc encryptionProvider =
1430                    PropertyUtil.getSystemProperty("testEncryptionProvider");
1431        if (encryptionPassword != null)
1432        {
1433            if (startParams == null)
1434                startParams = new Properties JavaDoc();
1435
1436            startParams.put(Attribute.DATA_ENCRYPTION, "true");
1437            startParams.put(Attribute.BOOT_PASSWORD, encryptionPassword);
1438            if (encryptionProvider != null) {
1439                startParams.put(Attribute.CRYPTO_PROVIDER, encryptionProvider);
1440            }
1441
1442            // System.out.println("Setting encryption password to " + encryptionPassword);
1443

1444        }
1445
1446        return startParams;
1447    }
1448
1449}
1450
Popular Tags