jdk/test/sun/rmi/log/ReliableLog/LogAlignmentTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
   @bug 4094889
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary rmid can have a corrupted log
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/* Fault: ReliableLog used RandomAccessFile.skipBytes() to seek past the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * of a file.  Unfortunately, skipBytes() doesn't do that, so the file was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * being misaligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Reproduced by writing an odd number of bytes into an update, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * reloading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.rmi.log.LogHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.rmi.log.ReliableLog;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
//import javasoft.sqe.harness.Status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
//import javasoft.sqe.harness.Test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class LogAlignmentTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    extends LogHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    implements Serializable //, Test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static public void main (String[] argv)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        LogAlignmentTest test = new LogAlignmentTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        //Status status = test.run (argv, System.err, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        //status.exit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        test.run (argv, System.err, System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    //public Status run (String argv[], PrintStream log, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public void run (String argv[], PrintStream log, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            regtest(130, "./logalign_tmp", log, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            regtest(131, "./logalign_tmp", log, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            e.printStackTrace (log);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            //return (Status.failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throw (new RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    ("Exception in regression test for bugid 4094889"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        //return (Status.passed ("OKAY"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    static private void regtest(int updatesz, String dir, PrintStream lg, PrintStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            LogAlignmentTest handler = new LogAlignmentTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            ReliableLog log = new ReliableLog (dir, handler, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            // Write a preamble update
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            String c = "[";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            handler.basicUpdate (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            log.update (c, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // Generate the requested size update (single chars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            char[] up = new char[updatesz];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            for (i = 0; i < updatesz; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                up[i] = (char)(65 + (i % 26));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            c = new String (up);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            handler.basicUpdate (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            log.update (c, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // Write it again, so we can misalign
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            handler.basicUpdate (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            log.update (c, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // Write the suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            c = "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            handler.basicUpdate (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            log.update (c, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // Read it back using a new context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            LogAlignmentTest handler2 = new LogAlignmentTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            ReliableLog carbon = new ReliableLog (dir, handler2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            Object thingy = carbon.recover();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            // The report bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            String orig = handler.contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            String news = ((LogAlignmentTest)thingy).contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            lg.println ("Original as saved: " + orig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            lg.println ("As restored      : " + news);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (orig.compareTo (news) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                throw new RuntimeException ("Restored string was different from saved string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                lg.println ("Matched OK.  Test element passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // Trash the log directory, so a new snap will be taken in the next test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                File vs = new File (dir, "Version_Number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                vs.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                File vs = new File (dir, "New_Version_Number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                vs.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private String contents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public LogAlignmentTest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.contents = "?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // implements LogHandler.initialSnapshot()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public Object initialSnapshot()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.contents = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return (this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    // implements LogHandler.applyUpdate()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Object applyUpdate (Object update, Object state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // basicUpdate appends the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        ((LogAlignmentTest)state).basicUpdate ((String)update);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return (state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // an "update" is a short string to append to this.contents (must ignore state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void basicUpdate (String extra)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.contents = this.contents + extra;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}