nashorn/test/script/nosecurity/JDK-util.js
author sundar
Tue, 12 Jul 2016 21:18:13 +0530
changeset 39662 e2b36a3779b9
parent 36689 3370f6f942ff
permissions -rw-r--r--
8149929: Nashorn Parser API needs to be updated for ES6 Reviewed-by: mhaupt, hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36689
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
     1
/*
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
     4
 *
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
     8
 *
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    13
 * accompanied this code).
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    14
 *
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    18
 *
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    21
 * questions.
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    22
 */
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    23
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    24
/**
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    25
 * This file contains utility functions used by other tests.
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    26
 * @subtest
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    27
 */
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    28
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    29
var Files = Java.type('java.nio.file.Files'),
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    30
    Paths = Java.type('java.nio.file.Paths'),
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    31
    System = Java.type('java.lang.System')
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    32
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    33
var File = java.io.File
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    34
var windows = System.getProperty("os.name").startsWith("Windows")
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    35
var winCyg = false
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    36
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    37
var outPath = {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    38
    windows:0, //C:\dir
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    39
    mixed:1    //C:/dir
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    40
}
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    41
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    42
if (windows) {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    43
    //Is there any better way to diffrentiate between cygwin/command prompt on windows
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    44
    var term = System.getenv("TERM")
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    45
    winCyg = term ? true:false
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    46
}
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    47
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    48
/**
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    49
 * Returns which command is selected from PATH
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    50
 * @param cmd name of the command searched from PATH
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    51
 */
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    52
function which(cmd) {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    53
    var path = System.getenv("PATH")
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    54
    var st = new java.util.StringTokenizer(path, File.pathSeparator)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    55
    while (st.hasMoreTokens()) {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    56
        var file = new File(st.nextToken(), cmd)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    57
        if (file.exists()) {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    58
            return (file.getAbsolutePath())
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    59
        }
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    60
    }
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    61
}
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    62
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    63
/**
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    64
 * Removes a given file
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    65
 * @param pathname name of the file
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    66
 */
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    67
function rm(pathname) {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    68
    var Path = Paths.get(pathname)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    69
    if (!Files.deleteIfExists(Path))
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    70
        print("File \"${pathname}\" doesn't exist")
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    71
}
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    72
    
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    73
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    74
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    75
/**
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    76
 * Unix cygpath implementation
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    77
 * Supports only two outputs,windows(C:\dir\) and mixed(C:/dir/)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    78
 */
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    79
function cygpath(path,mode) {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    80
   
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    81
    var newpath = path
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    82
    if(path.startsWith("/cygdrive/")){
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    83
        var str = path.substring(10)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    84
        var index = str.indexOf('/',0)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    85
        var drv = str.substring(0,index)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    86
        var rstr = drv.toUpperCase() + ":"
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    87
        newpath = str.replaceFirst(drv,rstr)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    88
    }
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    89
    if (mode == outPath.windows)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    90
        return Paths.get(newpath).toString()
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    91
    else {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    92
        newpath = newpath.replaceAll('\\\\','/')
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    93
        return newpath
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    94
    }                   
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    95
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    96
}
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    97
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    98
/**
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
    99
 * convert given path based on underlying shell programme runs on
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   100
 */
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   101
function toShellPath(path) {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   102
    if (windows) {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   103
        if (winCyg) {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   104
            return cygpath(path,outPath.mixed)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   105
        }else {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   106
         var path = cygpath(path,outPath.windows)
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   107
         //convert '\' ->'\\',cmd shell expects this.
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   108
         return path.replaceAll('\\\\','\\\\\\\\')
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   109
       }
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   110
    }else {
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   111
        return path
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   112
    }
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   113
} 
3370f6f942ff 8147613: enable jjs tests on Windows
sdama
parents:
diff changeset
   114