1/*
2 * Copyright (c) 2014, 2016, 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/*
25 * @test
26 * @summary Verify correctnes of the random generator from Utility.java
27 * @library /test/lib
28 * @modules java.base/jdk.internal.misc
29 *          java.management
30 * @run driver RandomGeneratorTest SAME_SEED
31 * @run driver RandomGeneratorTest NO_SEED
32 * @run driver RandomGeneratorTest DIFFERENT_SEED
33 */
34
35import java.io.FileWriter;
36import java.io.IOException;
37import java.io.PrintWriter;
38import java.util.ArrayList;
39import java.util.List;
40import java.util.Random;
41import jdk.test.lib.process.OutputAnalyzer;
42import jdk.test.lib.process.ProcessTools;
43import jdk.test.lib.Utils;
44
45/**
46 * The test verifies correctness of work {@link jdk.test.lib.Utils#getRandomInstance()}.
47 * Test works in three modes: same seed provided, no seed provided and
48 * different seed provided. In the first case the test expects that all random numbers
49 * will be repeated in all next iterations. For other two modes test expects that
50 * randomly generated numbers differ from original.
51 */
52public class RandomGeneratorTest {
53    private static final String SEED_VM_OPTION = "-D" + Utils.SEED_PROPERTY_NAME + "=";
54
55    public static void main( String[] args) throws Throwable {
56        if (args.length == 0) {
57            throw new Error("TESTBUG: No test mode provided.");
58        }
59        SeedOption seedOpt = SeedOption.valueOf(args[0]);
60        List<String> jvmArgs = new ArrayList<String>();
61        String optStr = seedOpt.getSeedOption();
62        if (optStr != null) {
63            jvmArgs.add(optStr);
64        }
65        jvmArgs.add(RandomRunner.class.getName());
66        String origFileName = seedOpt.name() + "_orig";
67        jvmArgs.add(origFileName);
68        int fileNameIndex = jvmArgs.size() - 1;
69        String[] cmdLineArgs = jvmArgs.toArray(new String[jvmArgs.size()]);
70        ProcessTools.executeTestJvm(cmdLineArgs).shouldHaveExitValue(0);
71        String etalon = Utils.fileAsString(origFileName).trim();
72        cmdLineArgs[fileNameIndex] = seedOpt.name();
73        seedOpt.verify(etalon, cmdLineArgs);
74    }
75
76    /**
77     * The utility enum helps to generate an appropriate string that should be passed
78     * to the command line depends on the testing mode. It is also responsible for the result
79     * validation.
80     */
81    private enum SeedOption {
82        SAME_SEED {
83            @Override
84            public String getSeedOption() {
85                return SEED_VM_OPTION + Utils.SEED;
86            }
87
88            @Override
89            protected boolean isOutputExpected(String orig, String output) {
90                return output.equals(orig);
91            }
92        },
93        DIFFERENT_SEED {
94            @Override
95            public String getSeedOption() {
96                return SEED_VM_OPTION + Utils.getRandomInstance().nextLong();
97            }
98
99            @Override
100            public void verify(String orig, String[] cmdLine) {
101                cmdLine[0] = getSeedOption();
102                super.verify(orig, cmdLine);
103            }
104        },
105        NO_SEED {
106            @Override
107            public String getSeedOption() {
108                return null;
109            }
110        };
111
112        /**
113         * Generates a string to be added as a command line argument.
114         * It contains "-D" prefix, system property name, '=' sign
115         * and seed value.
116         * @return command line argument
117         */
118        public abstract String getSeedOption();
119
120        protected boolean isOutputExpected(String orig, String output) {
121            return !output.equals(orig);
122        }
123
124        /**
125         * Verifies that the original output meets expectations
126         * depending on the test mode. It compares the output of second execution
127         * to original one.
128         * @param orig original output
129         * @param cmdLine command line arguments
130         * @throws Throwable - Throws an exception in case test failure.
131         */
132        public void verify(String orig, String[] cmdLine) {
133            String output;
134            OutputAnalyzer oa;
135            try {
136                oa = ProcessTools.executeTestJvm(cmdLine);
137            } catch (Throwable t) {
138                throw new Error("TESTBUG: Unexpedted exception during jvm execution.", t);
139            }
140            oa.shouldHaveExitValue(0);
141            try {
142                output = Utils.fileAsString(name()).trim();
143            } catch (IOException ioe) {
144                throw new Error("TESTBUG: Problem during IO operation with file: " + name(), ioe);
145            }
146            if (!isOutputExpected(orig, output)) {
147                System.err.println("Initial output: " + orig);
148                System.err.println("Second run output: " + output);
149                throw new AssertionError("Unexpected random number sequence for mode: " + this.name());
150            }
151        }
152    }
153
154    /**
155     * The helper class generates several random numbers
156     * and put results to a file. The file name came as first
157     * command line argument.
158     */
159    public static class RandomRunner {
160        private static final int COUNT = 10;
161        public static void main(String[] args) {
162            StringBuilder sb = new StringBuilder();
163            Random rng = Utils.getRandomInstance();
164            for (int i = 0; i < COUNT; i++) {
165                sb.append(rng.nextLong()).append(' ');
166            }
167            try (PrintWriter pw = new PrintWriter(new FileWriter(args[0]))) {
168                pw.write(sb.toString());
169            } catch (IOException ioe) {
170                throw new Error("TESTBUG: Problem during IO operation with file: " + args[0], ioe);
171            }
172        }
173    }
174}
175