### Question:
TxLog { public TxLog( String fileName ) throws IOException { if ( fileName == null ) { throw new IllegalArgumentException( "Null filename" ); } fileChannel = new RandomAccessFile( fileName, "rw" ).getChannel(); fileChannel.position( fileChannel.size() ); buffer = ByteBuffer.allocateDirect( (3 + Xid.MAXGTRIDSIZE + Xid.MAXBQUALSIZE) * 1000 ); this.name = fileName; } TxLog( String fileName ); String getName(); int getRecordCount(); void close(); void force(); synchronized void truncate(); synchronized void txStart( byte globalId[] ); synchronized void addBranch( byte globalId[], byte branchId[] ); synchronized void markAsCommitting( byte globalId[] ); synchronized void txDone( byte globalId[] ); synchronized Iterator<List<Record>> getDanglingRecords(); synchronized void switchToLogFile( String newFile ); static final byte TX_START; static final byte BRANCH_ADD; static final byte MARK_COMMIT; static final byte TX_DONE; }### Answer:
@Test public void testTxLog() throws IOException { File file = new File( txFile() ); if ( file.exists() ) { file.delete(); } try { TxLog txLog = new TxLog( txFile() ); assertTrue( !txLog.getDanglingRecords().hasNext() ); byte globalId[] = new byte[64]; byte branchId[] = new byte[45]; txLog.txStart( globalId ); txLog.addBranch( globalId, branchId ); assertEquals( 2, txLog.getRecordCount() ); List<?> lists[] = getRecordLists( txLog.getDanglingRecords() ); assertEquals( 1, lists.length ); List<?> records = lists[0]; assertEquals( 2, records.size() ); TxLog.Record record = (TxLog.Record) records.get( 0 ); assertEquals( TxLog.TX_START, record.getType() ); assertEqualByteArray( globalId, record.getGlobalId() ); assertTrue( null == record.getBranchId() ); record = (TxLog.Record) records.get( 1 ); assertEquals( TxLog.BRANCH_ADD, record.getType() ); assertEqualByteArray( globalId, record.getGlobalId() ); assertEqualByteArray( branchId, record.getBranchId() ); txLog.markAsCommitting( globalId ); assertEquals( 3, txLog.getRecordCount() ); txLog.close(); txLog = new TxLog( txFile() ); assertEquals( 0, txLog.getRecordCount() ); lists = getRecordLists( txLog.getDanglingRecords() ); assertEquals( 1, lists.length ); records = lists[0]; assertEquals( 3, records.size() ); record = (TxLog.Record) records.get( 0 ); assertEquals( TxLog.TX_START, record.getType() ); assertEqualByteArray( globalId, record.getGlobalId() ); assertTrue( null == record.getBranchId() ); record = (TxLog.Record) records.get( 1 ); assertEquals( TxLog.BRANCH_ADD, record.getType() ); assertEqualByteArray( globalId, record.getGlobalId() ); assertEqualByteArray( branchId, record.getBranchId() ); record = (TxLog.Record) records.get( 2 ); assertEquals( TxLog.MARK_COMMIT, record.getType() ); assertEqualByteArray( globalId, record.getGlobalId() ); assertTrue( null == record.getBranchId() ); txLog.txDone( globalId ); assertEquals( 1, txLog.getRecordCount() ); assertEquals( 0, getRecordLists( txLog.getDanglingRecords() ).length ); txLog.close(); txLog = new TxLog( txFile() ); assertEquals( 0, getRecordLists( txLog.getDanglingRecords() ).length ); txLog.close(); } finally { file = new File( txFile() ); if ( file.exists() ) { file.delete(); } } }