LogAlignmentTest.java revision 5551
606N/A/*
606N/A * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
606N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
606N/A *
1265N/A * This code is free software; you can redistribute it and/or modify it
606N/A * under the terms of the GNU General Public License version 2 only, as
606N/A * published by the Free Software Foundation.
919N/A *
919N/A * This code is distributed in the hope that it will be useful, but WITHOUT
919N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
919N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
919N/A * version 2 for more details (a copy is included in the LICENSE file that
919N/A * accompanied this code).
919N/A *
919N/A * You should have received a copy of the GNU General Public License version
919N/A * 2 along with this work; if not, write to the Free Software Foundation,
919N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
919N/A *
919N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
919N/A * or visit www.oracle.com if you need additional information or have any
919N/A * questions.
919N/A */
919N/A
919N/A/* @test
606N/A * @bug 4094889
606N/A * @summary rmid can have a corrupted log
606N/A *
606N/A * @run main LogAlignmentTest
606N/A */
606N/A
970N/A/* Fault: ReliableLog used RandomAccessFile.skipBytes() to seek past the end
970N/A * of a file. Unfortunately, skipBytes() doesn't do that, so the file was
970N/A * being misaligned.
970N/A *
970N/A * Reproduced by writing an odd number of bytes into an update, and then
606N/A * reloading.
1265N/A */
606N/A
911N/Aimport java.io.File;
1265N/Aimport java.io.PrintStream;
1265N/Aimport java.io.Serializable;
911N/Aimport sun.rmi.log.LogHandler;
606N/Aimport sun.rmi.log.ReliableLog;
947N/A
947N/A//import javasoft.sqe.harness.Status;
947N/A//import javasoft.sqe.harness.Test;
947N/A
606N/Apublic class LogAlignmentTest
606N/A extends LogHandler
606N/A implements Serializable //, Test
606N/A{
606N/A static public void main (String[] argv)
606N/A {
606N/A LogAlignmentTest test = new LogAlignmentTest();
606N/A //Status status = test.run (argv, System.err, System.out);
606N/A //status.exit();
606N/A test.run (argv, System.err, System.out);
606N/A }
1097N/A
606N/A //public Status run (String argv[], PrintStream log, PrintStream out)
1097N/A public void run (String argv[], PrintStream log, PrintStream out)
606N/A {
1088N/A try {
957N/A regtest(130, "./logalign_tmp", log, out);
606N/A regtest(131, "./logalign_tmp", log, out);
606N/A } catch (Exception e) {
e.printStackTrace (log);
//return (Status.failed
throw (new RuntimeException
("Exception in regression test for bugid 4094889"));
}
//return (Status.passed ("OKAY"));
}
static private void regtest(int updatesz, String dir, PrintStream lg, PrintStream out)
throws Exception
{
try {
LogAlignmentTest handler = new LogAlignmentTest();
ReliableLog log = new ReliableLog (dir, handler, false);
// Write a preamble update
String c = "[";
handler.basicUpdate (c);
log.update (c, true);
// Generate the requested size update (single chars)
char[] up = new char[updatesz];
int i;
for (i = 0; i < updatesz; i++) {
up[i] = (char)(65 + (i % 26));
}
c = new String (up);
handler.basicUpdate (c);
log.update (c, true);
// Write it again, so we can misalign
handler.basicUpdate (c);
log.update (c, true);
// Write the suffix
c = "]";
handler.basicUpdate (c);
log.update (c, true);
// Read it back using a new context.
LogAlignmentTest handler2 = new LogAlignmentTest();
ReliableLog carbon = new ReliableLog (dir, handler2, false);
Object thingy = carbon.recover();
// The report bit
String orig = handler.contents;
String news = ((LogAlignmentTest)thingy).contents;
lg.println ("Original as saved: " + orig);
lg.println ("As restored : " + news);
if (orig.compareTo (news) != 0) {
throw new RuntimeException ("Restored string was different from saved string");
} else {
lg.println ("Matched OK. Test element passed.");
}
} finally {
// Trash the log directory, so a new snap will be taken in the next test
try {
File vs = new File (dir, "Version_Number");
vs.delete();
} catch (Exception e) {
}
try {
File vs = new File (dir, "New_Version_Number");
vs.delete();
} catch (Exception e) {
}
}
}
private String contents;
public LogAlignmentTest()
{
super();
this.contents = "?";
}
// implements LogHandler.initialSnapshot()
public Object initialSnapshot()
throws Exception
{
this.contents = "";
return (this);
}
// implements LogHandler.applyUpdate()
public Object applyUpdate (Object update, Object state)
throws Exception
{
// basicUpdate appends the string
((LogAlignmentTest)state).basicUpdate ((String)update);
return (state);
}
// an "update" is a short string to append to this.contents (must ignore state)
public void basicUpdate (String extra)
{
this.contents = this.contents + extra;
}
}