File: TestVMOptionsFile.java

package info (click to toggle)
openjdk-11 11.0.29%2B7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,596 kB
  • sloc: java: 5,209,028; xml: 1,192,267; cpp: 1,143,585; ansic: 462,354; javascript: 162,416; sh: 16,740; objc: 13,730; python: 4,757; asm: 3,570; makefile: 2,969; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (653 lines) | stat: -rw-r--r-- 27,587 bytes parent folder | download | duplicates (4)
1
2
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
28
29
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
63
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
127
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
 * 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
 * @bug 8061999 8135195 8136552
 * @summary Test "-XX:VMOptionsFile" VM option
 * @library /test/lib
 * @modules java.base/jdk.internal.misc
 * @modules jdk.management
 * @run main TestVMOptionsFile
 */

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermissions;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclEntryType;
import java.nio.file.attribute.AclFileAttributeView;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import jdk.test.lib.Asserts;
import jdk.test.lib.management.DynamicVMOption;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;

public class TestVMOptionsFile {

    /* Various valid VM Option files */
    private static final String VM_OPTION_FILE_EMPTY = "optionfile_empty";
    private static final String VM_OPTION_FILE_TABS_AND_SPACES = "optionfile_only_tabsandspaces";
    private static final String VM_OPTION_FILE_1 = "optionfile_1";
    private static final String VM_OPTION_FILE_2 = "optionFILE_2";
    private static final String VM_OPTION_FILE_3 = "optionfile_3";
    private static final String VM_OPTION_FILE_QUOTE = "optionfile_quote";
    private static final String VM_OPTION_FILE_BIG = "optionfile_big";
    private static final int REPEAT_COUNT = 512;
    /* Name of the file with flags for VM_OPTION_FILE_2 Option file */
    private static final String FLAGS_FILE = "flags_file";
    /* VM Option file with a lot of options with quote on separate lines */
    private static final String VM_OPTION_FILE_LOT_OF_OPTIONS_QUOTE = "optionfile_lot_of_options_quote";
    /* Number of properties defined in VM_OPTION_FILE_LOT_OF_OPTIONS_QUOTE */
    private static final int NUM_OF_PROP_IN_FILE_LOT_OF_OPTIONS_QUOTE = 70;
    /* VM Option file with long property */
    private static final String VM_OPTION_FILE_WITH_LONG_PROPERTY = "optionfile_long_property";
    private static final String LONG_PROPERTY_NAME = "veryl'" + String.format("%1536s", "").replace(' ', 'o') + "ng'name";
    private static final String LONG_PROPERTY_VALUE = String.format("%2096s", "").replaceAll("    ", "long");
    /* 2 VM Option files with unmatched quotes */
    private static final String VM_OPTION_FILE_UNMATCHED_QUOTE_1 = "optionfile_unmatched_quote_1";
    private static final String VM_OPTION_FILE_UNMATCHED_QUOTE_2 = "optionfile_unmatched_quote_2";
    /* VM Option file with bad option in it */
    private static final String VM_OPTION_FILE_WITH_BAD_OPTION = "optionfile_bad_option";
    /* VM Option file with "-XX:VMOptionsFile=" option in it */
    private static final String VM_OPTION_FILE_WITH_VM_OPTION_FILE = "optionfile_with_optionfile";
    /* VM Option file with "-XX:VMOptionsFile=" option in it, where file is the same option file */
    private static final String VM_OPTION_FILE_WITH_SAME_VM_OPTION_FILE = "optionfile_with_same_optionfile";
    /* VM Option file without read permissions(not accessible) */
    private static final String VM_OPTION_FILE_WITHOUT_READ_PERMISSIONS = "optionfile_wo_read_perm";
    /* VM Option file which does not exist */
    private static final String NOT_EXISTING_FILE = "not_exist_junk2123";

    /* JAVA_TOOL_OPTIONS environment variable */
    private static final String JAVA_TOOL_OPTIONS = "JAVA_TOOL_OPTIONS";
    /* _JAVA_OPTIONS environment variable */
    private static final String JAVA_OPTIONS = "_JAVA_OPTIONS";

    /* Exit code for JVM, zero - for success, non-zero for failure */
    private static final int JVM_SUCCESS = 0;
    private static final int JVM_FAIL_WITH_EXIT_CODE_1 = 1;

    /* Current working directory */
    private static final String CURRENT_DIR = System.getProperty("user.dir");

    /* Source directory */
    private static final String SOURCE_DIR = System.getProperty("test.src", ".");

    /* VM Options which are passed to the JVM */
    private static final List<String> VMParams = new ArrayList<>();
    /* Argument passed to the PrintPropertyAndOptions.main */
    private static final Set<String> appParams = new LinkedHashSet<>();

    private static OutputAnalyzer output;

    private static final String PRINT_PROPERTY_FORMAT = "Property %s=%s";
    private static final String PRINT_VM_OPTION_FORMAT = "Virtual Machine option %s=%s";

    /*
     * Get absoulte path to file from folder with sources
     */
    private static String getAbsolutePathFromSource(String fileName) {
        return SOURCE_DIR + File.separator + fileName;
    }

    /*
     * Make file non-readable by modifying its permissions.
     * If file supports "posix" attributes, then modify it.
     * Otherwise check for "acl" attributes.
     */
    private static void makeFileNonReadable(String file) throws IOException {
        Path filePath = Paths.get(file);
        Set<String> supportedAttr = filePath.getFileSystem().supportedFileAttributeViews();

        if (supportedAttr.contains("posix")) {
            Files.setPosixFilePermissions(filePath, PosixFilePermissions.fromString("-w--w----"));
        } else if (supportedAttr.contains("acl")) {
            UserPrincipal fileOwner = Files.getOwner(filePath);

            AclFileAttributeView view = Files.getFileAttributeView(filePath, AclFileAttributeView.class);

            AclEntry entry = AclEntry.newBuilder()
                    .setType(AclEntryType.DENY)
                    .setPrincipal(fileOwner)
                    .setPermissions(AclEntryPermission.READ_DATA)
                    .build();

            List<AclEntry> acl = view.getAcl();
            acl.add(0, entry);
            view.setAcl(acl);
        }
    }

    private static void copyFromSource(String fileName) throws IOException {
        Files.copy(Paths.get(getAbsolutePathFromSource(fileName)),
                Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING);
    }

    private static void createOptionFiles() throws IOException {
        FileWriter fw = new FileWriter(VM_OPTION_FILE_WITH_VM_OPTION_FILE);

        /* Create VM option file with following parameters "-XX:VMOptionFile=<absolute_path_to_the_VM_option_file> */
        fw.write("-XX:VMOptionsFile=" + getAbsolutePathFromSource(VM_OPTION_FILE_1));
        fw.close();

        /* Create VM option file with following parameters "-XX:MinHeapFreeRatio=12 -XX:VMOptionFile=<absolute_path_to_the_same_VM_option_file> */
        fw = new FileWriter(VM_OPTION_FILE_WITH_SAME_VM_OPTION_FILE);
        fw.write("-XX:MinHeapFreeRatio=12 -XX:VMOptionsFile=" + (new File(VM_OPTION_FILE_WITH_SAME_VM_OPTION_FILE)).getCanonicalPath());
        fw.close();

        /* Create VM option file with long property */
        fw = new FileWriter(VM_OPTION_FILE_WITH_LONG_PROPERTY);
        fw.write("-D" + LONG_PROPERTY_NAME + "=" + LONG_PROPERTY_VALUE);
        fw.close();

        /* Create big VM option file */
        fw = new FileWriter(VM_OPTION_FILE_BIG);
        fw.write("-XX:MinHeapFreeRatio=17\n");
        for (int i = 0; i < REPEAT_COUNT; i++) {
            if (i == REPEAT_COUNT / 2) {
                fw.write("-XX:+PrintVMOptions ");
            }
            fw.write("-Dmy.property=value" + (i + 1) + "\n");
        }
        fw.write("-XX:MaxHeapFreeRatio=85\n");
        fw.close();

        /* Copy valid VM option file and change its permission to make it not accessible */
        Files.copy(Paths.get(getAbsolutePathFromSource(VM_OPTION_FILE_1)),
                Paths.get(VM_OPTION_FILE_WITHOUT_READ_PERMISSIONS),
                StandardCopyOption.REPLACE_EXISTING);

        makeFileNonReadable(VM_OPTION_FILE_WITHOUT_READ_PERMISSIONS);

        /* Copy valid VM option file to perform test with relative path */
        copyFromSource(VM_OPTION_FILE_2);

        /* Copy flags file to the current working folder */
        copyFromSource(FLAGS_FILE);

        /* Create a new empty file */
        new File(VM_OPTION_FILE_EMPTY).createNewFile();
    }

    /*
     * Add parameters to the VM Parameters list
     */
    private static void addVMParam(String... params) {
        VMParams.addAll(Arrays.asList(params));
    }

    /*
     * Add VM option name to the application arguments list
     */
    private static void addVMOptionsToCheck(String... params) {
        for (String param : params) {
            appParams.add("vmoption=" + param);
        }
    }

    /*
     * Add property to the VM Params list and to the application arguments list
     */
    private static void addProperty(String propertyName, String propertyValue) {
        addVMParam("-D" + propertyName + "=" + propertyValue);
    }

    /*
     * Add "-XX:VMOptionsfile" parameter to the VM Params list
     */
    private static void addVMOptionsFile(String fileName) {
        addVMParam("-XX:VMOptionsFile=" + fileName);
    }

    private static void outputShouldContain(String expectedString) {
        output.shouldContain(expectedString);
    }

    private static void outputShouldNotContain(String expectedString) {
        output.shouldNotContain(expectedString);
    }

    private static ProcessBuilder createProcessBuilder() throws Exception {
        ProcessBuilder pb;
        List<String> runJava = new ArrayList<>();

        runJava.addAll(VMParams);
        runJava.add(PrintPropertyAndOptions.class.getName());
        runJava.addAll(appParams);

        pb = ProcessTools.createJavaProcessBuilder(runJava);

        VMParams.clear();
        appParams.clear();

        return pb;
    }

    private static void runJavaCheckExitValue(ProcessBuilder pb, int expectedExitValue) throws Exception {
        output = new OutputAnalyzer(pb.start());
        output.shouldHaveExitValue(expectedExitValue);
    }

    private static void runJavaCheckExitValue(int expectedExitValue) throws Exception {
        runJavaCheckExitValue(createProcessBuilder(), expectedExitValue);
    }

    /*
     * Update environment variable in passed ProcessBuilder object to the passed value
     */
    private static void updateEnvironment(ProcessBuilder pb, String name, String value) {
        pb.environment().put(name, value);
    }

    /*
     * Check property value by examining output
     */
    private static void checkProperty(String property, String expectedValue) {
        outputShouldContain(String.format(PRINT_PROPERTY_FORMAT, property, expectedValue));
    }

    /*
     * Check VM Option value by examining output
     */
    private static void checkVMOption(String vmOption, String expectedValue) {
        outputShouldContain(String.format(PRINT_VM_OPTION_FORMAT, vmOption, expectedValue));
    }

    private static void testVMOptions() throws Exception {
        ProcessBuilder pb;

        /* Check that empty VM Option file is accepted without errors */
        addVMOptionsFile(VM_OPTION_FILE_EMPTY);

        runJavaCheckExitValue(JVM_SUCCESS);

        /* Check that VM Option file with tabs and spaces is accepted without errors */
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_TABS_AND_SPACES));

        runJavaCheckExitValue(JVM_SUCCESS);

        /* Check that parameters are gotten from first VM Option file. Pass absolute path to the VM Option file */
        addVMParam("-showversion");
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_1));
        addVMOptionsToCheck("SurvivorRatio", "MinHeapFreeRatio");

        runJavaCheckExitValue(JVM_SUCCESS);
        outputShouldContain("interpreted mode");
        checkProperty("optfile_1", "option_file_1");
        checkVMOption("SurvivorRatio", "16");
        checkVMOption("MinHeapFreeRatio", "22");

        /*
         * Check that parameters are gotten from second VM Option file which also contains flags file.
         * Flags file and option file contains NewRatio, but since options from VM Option file
         * are processed later NewRatio should be set to value from VM Option file
         * Pass relative path to the VM Option file in form "vmoptionfile"
         */
        addVMOptionsFile(VM_OPTION_FILE_2);
        addVMOptionsToCheck("UseGCOverheadLimit", "NewRatio", "MinHeapFreeRatio", "MaxFDLimit", "AlwaysPreTouch");

        runJavaCheckExitValue(JVM_SUCCESS);
        checkProperty("javax.net.ssl.keyStorePassword", "someVALUE123+");
        checkVMOption("UseGCOverheadLimit", "true");
        checkVMOption("NewRatio", "4");
        checkVMOption("MinHeapFreeRatio", "3");
        checkVMOption("MaxFDLimit", "true");
        checkVMOption("AlwaysPreTouch", "false");

        /* Check that parameters are gotten from third VM Option file which contains a mix of the options */
        addVMParam("-showversion");
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_3));
        addVMOptionsToCheck("UseGCOverheadLimit", "NewRatio");

        runJavaCheckExitValue(JVM_SUCCESS);
        outputShouldContain("interpreted mode");
        checkProperty("other.secret.data", "qwerty");
        checkProperty("property", "second");
        checkVMOption("UseGCOverheadLimit", "false");
        checkVMOption("NewRatio", "16");

        /* Check that quotes are processed normally in VM Option file */
        addVMParam("-showversion");
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_QUOTE));
        addVMOptionsToCheck("ErrorFile");

        runJavaCheckExitValue(JVM_SUCCESS);

        outputShouldContain("interpreted mode");
        checkProperty("my.quote.single", "Property in single quote. Here a double qoute\" Add some slashes \\/");
        checkProperty("my.quote.double", "Double qoute. Include single '.");
        checkProperty("javax.net.ssl.trustStorePassword", "data @+NEW");
        checkVMOption("ErrorFile", "./my error file");

        /*
         * Verify that VM Option file accepts a file with 70 properties and with two options on separate
         * lines and properties that use quotes a lot.
         */
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_LOT_OF_OPTIONS_QUOTE));
        addVMOptionsToCheck("MinHeapFreeRatio", "MaxHeapFreeRatio");

        runJavaCheckExitValue(JVM_SUCCESS);

        for (int i = 1; i <= NUM_OF_PROP_IN_FILE_LOT_OF_OPTIONS_QUOTE; i++) {
            checkProperty(String.format("prop%02d", i), String.format("%02d", i));
        }
        checkVMOption("MinHeapFreeRatio", "7");
        checkVMOption("MaxHeapFreeRatio", "96");

        /*
         * Verify that VM Option file accepts a file with very long property.
         */
        addVMOptionsFile(VM_OPTION_FILE_WITH_LONG_PROPERTY);

        runJavaCheckExitValue(JVM_SUCCESS);

        checkProperty(LONG_PROPERTY_NAME.replaceAll("'", ""), LONG_PROPERTY_VALUE);

        /*
         * Verify that VM Option file accepts a big VM Option file
         */
        addVMOptionsFile(VM_OPTION_FILE_BIG);
        addVMOptionsToCheck("MinHeapFreeRatio");
        addVMOptionsToCheck("MaxHeapFreeRatio");

        runJavaCheckExitValue(JVM_SUCCESS);

        outputShouldContain("VM option '+PrintVMOptions'");
        checkProperty("my.property", "value" + REPEAT_COUNT);
        checkVMOption("MinHeapFreeRatio", "17");
        checkVMOption("MaxHeapFreeRatio", "85");

        /* Pass VM Option file in _JAVA_OPTIONS environment variable */
        addVMParam("-showversion");
        addVMOptionsToCheck("SurvivorRatio", "MinHeapFreeRatio");
        pb = createProcessBuilder();

        updateEnvironment(pb, JAVA_OPTIONS, "-XX:VMOptionsFile=" + getAbsolutePathFromSource(VM_OPTION_FILE_1));

        runJavaCheckExitValue(pb, JVM_SUCCESS);
        outputShouldContain("interpreted mode");
        checkProperty("optfile_1", "option_file_1");
        checkVMOption("SurvivorRatio", "16");
        checkVMOption("MinHeapFreeRatio", "22");

        /* Pass VM Option file in JAVA_TOOL_OPTIONS environment variable */
        addVMOptionsToCheck("UseGCOverheadLimit", "NewRatio", "MinHeapFreeRatio", "MaxFDLimit", "AlwaysPreTouch");
        pb = createProcessBuilder();

        updateEnvironment(pb, JAVA_TOOL_OPTIONS, "-XX:VMOptionsFile=" + VM_OPTION_FILE_2);

        runJavaCheckExitValue(pb, JVM_SUCCESS);
        checkProperty("javax.net.ssl.keyStorePassword", "someVALUE123+");
        checkVMOption("UseGCOverheadLimit", "true");
        checkVMOption("NewRatio", "4");
        checkVMOption("MinHeapFreeRatio", "3");
        checkVMOption("MaxFDLimit", "true");
        checkVMOption("AlwaysPreTouch", "false");
    }

    private static ProcessBuilder prepareTestCase(int testCase) throws Exception {
        ProcessBuilder pb;

        Asserts.assertTrue(0 < testCase && testCase < 6, "testCase should be from 1 to 5");

        addVMParam("-showversion");
        addVMOptionsToCheck("MinHeapFreeRatio", "SurvivorRatio", "NewRatio");

        if (testCase < 5) {
            addVMParam("-XX:Flags=flags_file", "-XX:-PrintVMOptions");
            addProperty("shared.property", "command_line_before");
            addProperty("clb", "unique_command_line_before");
            addVMParam("-XX:MinHeapFreeRatio=7");
        }

        if (testCase < 4) {
            addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_1));
        }

        if (testCase < 3) {
            addVMParam("-XX:MinHeapFreeRatio=9", "-XX:-PrintVMOptions");
            addProperty("shared.property", "command_line_after");
            addProperty("cla", "unique_command_line_after");
        }

        /* Create ProcessBuilder after all setup is done to update environment variables */
        pb = createProcessBuilder();

        if (testCase < 2) {
            updateEnvironment(pb, JAVA_OPTIONS, "-Dshared.property=somevalue -Djo=unique_java_options "
                    + "-XX:MinHeapFreeRatio=18 -Dshared.property=java_options -XX:MinHeapFreeRatio=11 -XX:+PrintVMOptions");
        }

        if (testCase < 6) {
            updateEnvironment(pb, JAVA_TOOL_OPTIONS, "-Dshared.property=qwerty -Djto=unique_java_tool_options "
                    + "-XX:MinHeapFreeRatio=15 -Dshared.property=java_tool_options -XX:MinHeapFreeRatio=6 -XX:+PrintVMOptions");
        }

        return pb;
    }

    private static void testVMOptionsLastArgumentsWins() throws Exception {
        ProcessBuilder pb;

        /*
         * "shared.property" property and "MinHeapFreeRatio" XX VM Option are defined
         * in flags file, JAVA_TOOL_OPTIONS and _JAVA_OPTIONS environment variables,
         * on command line before VM Option file, on command line after VM Option file
         * and also in VM Option file. Verify that last argument wins. Also check
         * unique properties and VM Options.
         * Here is the order of options processing and last argument wins:
         *    1) Flags file
         *    2) JAVA_TOOL_OPTIONS environment variables
         *    3) Pseudo command line from launcher
         *    4) _JAVA_OPTIONS
         * In every category arguments processed from left to right and from up to down
         * and the last processed arguments wins, i.e. if argument is defined several
         * times the value of argument will be equal to the last processed argument.
         *
         * "shared.property" property and "MinHeapFreeRatio" should be equal to the
         * value from _JAVA_OPTIONS environment variable
         */
        pb = prepareTestCase(1);

        runJavaCheckExitValue(pb, JVM_SUCCESS);

        outputShouldContain("interpreted mode");
        outputShouldContain("VM option '+PrintVMOptions'");
        checkProperty("shared.property", "java_options");
        checkVMOption("MinHeapFreeRatio", "11");
        /* Each category defines its own properties */
        checkProperty("jto", "unique_java_tool_options");
        checkProperty("jo", "unique_java_options");
        checkProperty("clb", "unique_command_line_before");
        checkProperty("optfile_1", "option_file_1");
        checkProperty("cla", "unique_command_line_after");
        /* SurvivorRatio defined only in VM Option file */
        checkVMOption("SurvivorRatio", "16");
        /* NewRatio defined only in flags file */
        checkVMOption("NewRatio", "5");

        /*
         * The same as previous but without _JAVA_OPTIONS environment variable.
         * "shared.property" property and "MinHeapFreeRatio" should be equal to the
         * value from pseudo command line after VM Option file
         */
        pb = prepareTestCase(2);

        runJavaCheckExitValue(pb, JVM_SUCCESS);

        outputShouldContain("interpreted mode");
        outputShouldNotContain("VM option '+PrintVMOptions'");
        checkProperty("shared.property", "command_line_after");
        checkVMOption("MinHeapFreeRatio", "9");

        /*
         * The same as previous but without arguments in pseudo command line after
         * VM Option file.
         * "shared.property" property and "MinHeapFreeRatio" should be equal to the
         * value from VM Option file.
         */
        pb = prepareTestCase(3);

        runJavaCheckExitValue(pb, JVM_SUCCESS);

        outputShouldContain("interpreted mode");
        outputShouldContain("VM option '+PrintVMOptions'");
        checkProperty("shared.property", "vmoptfile");
        checkVMOption("MinHeapFreeRatio", "22");

        /*
         * The same as previous but without arguments in VM Option file.
         * "shared.property" property and "MinHeapFreeRatio" should be equal to the
         * value from pseudo command line.
         */
        pb = prepareTestCase(4);

        runJavaCheckExitValue(pb, JVM_SUCCESS);

        outputShouldNotContain("VM option '+PrintVMOptions'");
        checkProperty("shared.property", "command_line_before");
        checkVMOption("MinHeapFreeRatio", "7");

        /*
         * The same as previous but without arguments from pseudo command line.
         * "shared.property" property and "MinHeapFreeRatio" should be equal to the
         * value from JAVA_TOOL_OPTIONS environment variable.
         */
        pb = prepareTestCase(5);

        runJavaCheckExitValue(pb, JVM_SUCCESS);

        outputShouldContain("VM option '+PrintVMOptions'");
        checkProperty("shared.property", "java_tool_options");
        checkVMOption("MinHeapFreeRatio", "6");
    }

    private static void testVMOptionsInvalid() throws Exception {
        ProcessBuilder pb;

        /* Pass directory instead of file */
        addVMOptionsFile(CURRENT_DIR);

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);

        /* Pass not existing file */
        addVMOptionsFile(getAbsolutePathFromSource(NOT_EXISTING_FILE));

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);
        outputShouldContain("Could not open options file");

        /* Pass VM option file with bad option */
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_WITH_BAD_OPTION));

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);
        outputShouldContain("Unrecognized VM option");

        /* Pass VM option file with same VM option file option in it */
        addVMOptionsFile(VM_OPTION_FILE_WITH_SAME_VM_OPTION_FILE);

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);
        outputShouldContain("A VM options file may not refer to a VM options file. Specification of '-XX:VMOptionsFile=<file-name>' in the options file");

        /* Pass VM option file with VM option file option in it */
        addVMOptionsFile(VM_OPTION_FILE_WITH_VM_OPTION_FILE);

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);
        outputShouldContain("A VM options file may not refer to a VM options file. Specification of '-XX:VMOptionsFile=<file-name>' in the options file");

        /* Pass VM option file which is not accessible (without read permissions) */
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_WITHOUT_READ_PERMISSIONS));

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);
        outputShouldContain("Could not open options file");

        /* Pass two VM option files */
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_1));
        addVMOptionsFile(VM_OPTION_FILE_2);

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);
        outputShouldContain("is already specified in the");

        /* Pass empty option file i.e. pass "-XX:VMOptionsFile=" */
        addVMOptionsFile("");

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);
        outputShouldContain("Could not open options file");

        /* Pass VM option file with unmatched single quote */
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_UNMATCHED_QUOTE_1));

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);
        outputShouldContain("Unmatched quote in");

        /* Pass VM option file with unmatched double quote in X option */
        addVMOptionsFile(getAbsolutePathFromSource(VM_OPTION_FILE_UNMATCHED_QUOTE_2));

        runJavaCheckExitValue(JVM_FAIL_WITH_EXIT_CODE_1);
        outputShouldContain("Unmatched quote in");
    }

    public static void main(String[] args) throws Exception {
        /*
         * Preparation before actual testing - create two VM Option files
         * which contains VM Option file in it and copy other files to the
         * current working folder
         */
        createOptionFiles();

        testVMOptions(); /* Test VM Option file general functionality */
        testVMOptionsLastArgumentsWins(); /* Verify that last argument wins */
        testVMOptionsInvalid(); /* Test invalid VM Option file functionality */

    }

    public static class PrintPropertyAndOptions {

        public static void main(String[] arguments) {
            String vmOption;
            Properties properties = System.getProperties();

            for (String propertyName : properties.stringPropertyNames()) {
                System.out.println(String.format(PRINT_PROPERTY_FORMAT, propertyName, System.getProperty(propertyName, "NOT DEFINED")));
            }

            for (String arg : arguments) {
                if (arg.startsWith("vmoption=")) {
                    vmOption = arg.substring(9);
                    System.out.println(String.format(PRINT_VM_OPTION_FORMAT, vmOption, new DynamicVMOption(vmOption).getValue()));
                }
            }
        }
    }
}