1/*
2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 4319866
26 * @summary Verify that ReliableLog.snapshotSize() returns correct snapshot
27 *          file size even if LogHandler doesn't flush.
28 *
29 * @modules java.rmi/sun.rmi.log
30 * @run main/othervm SnapshotSize
31 */
32
33import java.io.ByteArrayOutputStream;
34import java.io.OutputStream;
35import java.io.ObjectOutputStream;
36import java.io.IOException;
37import sun.rmi.log.LogHandler;
38import sun.rmi.log.ReliableLog;
39
40public class SnapshotSize extends LogHandler {
41
42    int lastSnapshotSize = -1;
43
44    public static void main(String[] args) throws Exception {
45        SnapshotSize handler = new SnapshotSize();
46        ReliableLog log = new ReliableLog(".", handler);
47        if (log.snapshotSize() != handler.lastSnapshotSize) {
48            throw new Error();
49        }
50
51        String[] snapshots = { "some", "sample", "objects", "to", "snapshot" };
52        for (int i = 0; i < snapshots.length; i++) {
53            log.snapshot(snapshots[i]);
54            if (log.snapshotSize() != handler.lastSnapshotSize) {
55                throw new Error();
56            }
57        }
58    }
59
60    public Object initialSnapshot() {
61        return "initial snapshot";
62    }
63
64    public void snapshot(OutputStream out, Object value) throws IOException {
65        ByteArrayOutputStream bout = new ByteArrayOutputStream();
66        ObjectOutputStream oout = new ObjectOutputStream(bout);
67        oout.writeObject(value);
68        oout.close();
69
70        byte[] buf = bout.toByteArray();
71        out.write(buf);         // leave unflushed
72        lastSnapshotSize = buf.length;
73    }
74
75    public Object applyUpdate(Object update, Object state) {
76        return state;
77    }
78}
79