RandomGeneratorTest.java 5.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @summary Verify correctnes of the random generator from Utility.java
 * @library /testlibrary
28 29
 * @modules java.base/sun.misc
 *          java.management
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 * @run driver RandomGeneratorTest SAME_SEED
 * @run driver RandomGeneratorTest NO_SEED
 * @run driver RandomGeneratorTest DIFFERENT_SEED
 */

import com.oracle.java.testlibrary.ProcessTools;
import com.oracle.java.testlibrary.Utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * The test verifies correctness of work {@link com.oracle.java.testlibrary.Utils#getRandomInstance()}.
 * Test works in three modes: same seed provided, no seed provided and
 * different seed provided. In the first case the test expects that all random numbers
 * will be repeated in all next iterations. For other two modes test expects that
 * randomly generated numbers differ from original.
 */
public class RandomGeneratorTest {
    private static final String SEED_VM_OPTION = "-D" + Utils.SEED_PROPERTY_NAME + "=";

    public static void main( String[] args) throws Throwable {
        if (args.length == 0) {
            throw new Error("TESTBUG: No test mode provided.");
        }
        SeedOption seedOpt = SeedOption.valueOf(args[0]);
        List<String> jvmArgs = new ArrayList<String>();
        String optStr = seedOpt.getSeedOption();
        if (optStr != null) {
            jvmArgs.add(optStr);
        }
        jvmArgs.add(RandomRunner.class.getName());
        String[] cmdLineArgs = jvmArgs.toArray(new String[jvmArgs.size()]);
63
        String etalon = ProcessTools.executeTestJvm(cmdLineArgs).getStdout().trim();
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        seedOpt.verify(etalon, cmdLineArgs);
    }

    /**
     * The utility enum helps to generate an appropriate string that should be passed
     * to the command line depends on the testing mode. It is also responsible for the result
     * validation.
     */
    private enum SeedOption {
        SAME_SEED {
            @Override
            public String getSeedOption() {
                return SEED_VM_OPTION + Utils.SEED;
            }

            @Override
            protected boolean isOutputExpected(String orig, String output) {
                return output.equals(orig);
            }
        },
        DIFFERENT_SEED {
            @Override
            public String getSeedOption() {
                return SEED_VM_OPTION + Utils.getRandomInstance().nextLong();
            }

            @Override
            public void verify(String orig, String[] cmdLine) {
                cmdLine[0] = getSeedOption();
                super.verify(orig, cmdLine);
            }
        },
        NO_SEED {
            @Override
            public String getSeedOption() {
                return null;
            }
        };

        /**
         * Generates a string to be added as a command line argument.
         * It contains "-D" prefix, system property name, '=' sign
         * and seed value.
         * @return command line argument
         */
        public abstract String getSeedOption();

        protected boolean isOutputExpected(String orig, String output) {
            return !output.equals(orig);
        }

        /**
         * Verifies that the original output meets expectations
         * depending on the test mode. It compares the output of second execution
         * to original one.
         * @param orig original output
         * @param cmdLine command line arguments
         * @throws Throwable - Throws an exception in case test failure.
         */
        public void verify(String orig, String[] cmdLine) {
            String lastLineOrig = getLastLine(orig);
            String lastLine;
            try {
127
                lastLine = getLastLine(ProcessTools.executeTestJvm(cmdLine).getStdout().trim());
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
            } catch (Throwable t) {
                throw new Error("TESTBUG: Unexpedted exception during jvm execution.", t);
            }
            if (!isOutputExpected(lastLineOrig, lastLine)) {
                    throw new AssertionError("Unexpected random number sequence for mode: " + this.name());
            }
        }

        private static String getLastLine(String output) {
            return output.substring(output.lastIndexOf(Utils.NEW_LINE)).trim();
        }
    }

    /**
     * The helper class generates several random numbers
     * and prints them out.
     */
    public static class RandomRunner {
        private static final int COUNT = 10;
        public static void main(String[] args) {
            StringBuilder sb = new StringBuilder();
            Random rng = Utils.getRandomInstance();
            for (int i = 0; i < COUNT; i++) {
                sb.append(rng.nextLong()).append(' ');
            }
            System.out.println(sb.toString());
        }
    }
}