test/java/lang/ProcessBuilder/Basic.java

Print this page




   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  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
  27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
  28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
  29  *      4947220 7018606
  30  * @summary Basic tests for Process and Environment Variable code
  31  * @run main/othervm/timeout=300 Basic
  32  * @author Martin Buchholz
  33  */
  34 
  35 import java.lang.ProcessBuilder.Redirect;
  36 import static java.lang.ProcessBuilder.Redirect.*;
  37 
  38 import java.io.*;
  39 import java.util.*;
  40 import java.util.concurrent.CountDownLatch;
  41 import java.security.*;
  42 import java.util.regex.Pattern;
  43 import static java.lang.System.getenv;
  44 import static java.lang.System.out;
  45 import static java.lang.Boolean.TRUE;
  46 import static java.util.AbstractMap.SimpleImmutableEntry;
  47 
  48 public class Basic {
  49 


1423         try {
1424             Set<String> env1 = new HashSet<String>
1425                 (Arrays.asList(nativeEnv((String[])null).split("\n")));
1426 
1427             ProcessBuilder pb = new ProcessBuilder();
1428             pb.environment().put("QwErTyUiOp","AsDfGhJk");
1429 
1430             Set<String> env2 = new HashSet<String>
1431                 (Arrays.asList(nativeEnv(pb).split("\n")));
1432 
1433             check(env2.size() == env1.size() + 1);
1434             env1.add("QwErTyUiOp=AsDfGhJk");
1435             check(env1.equals(env2));
1436         } catch (Throwable t) { unexpected(t); }
1437 
1438         //----------------------------------------------------------------
1439         // Test Runtime.exec(...envp...)
1440         // Check for sort order of environment variables on Windows.
1441         //----------------------------------------------------------------
1442         try {

1443             // '+' < 'A' < 'Z' < '_' < 'a' < 'z' < '~'
1444             String[]envp = {"FOO=BAR","BAZ=GORP","QUUX=",
1445                             "+=+", "_=_", "~=~"};
1446             String output = nativeEnv(envp);
1447             String expected = "+=+\nBAZ=GORP\nFOO=BAR\nQUUX=\n_=_\n~=~\n";
1448             // On Windows, Java must keep the environment sorted.
1449             // Order is random on Unix, so this test does the sort.
1450             if (! Windows.is())
1451                 output = sortByLinesWindowsly(output);
1452             equal(output, expected);
1453         } catch (Throwable t) { unexpected(t); }
1454 















1455         //----------------------------------------------------------------
1456         // System.getenv() must be consistent with System.getenv(String)
1457         //----------------------------------------------------------------
1458         try {
1459             for (Map.Entry<String,String> e : getenv().entrySet())
1460                 equal(getenv(e.getKey()), e.getValue());
1461         } catch (Throwable t) { unexpected(t); }
1462 
1463         //----------------------------------------------------------------
1464         // Fiddle with working directory in child
1465         //----------------------------------------------------------------
1466         try {
1467             String canonicalUserDir =
1468                 new File(System.getProperty("user.dir")).getCanonicalPath();
1469             String[] sdirs = new String[]
1470                 {".", "..", "/", "/bin",
1471                  "C:", "c:", "C:/", "c:\\", "\\", "\\bin",
1472                  "c:\\windows  ", "c:\\Program Files", "c:\\Program Files\\" };
1473             for (String sdir : sdirs) {
1474                 File dir = new File(sdir);




   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  * @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
  27  *      5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
  28  *      6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
  29  *      4947220 7018606 7034570
  30  * @summary Basic tests for Process and Environment Variable code
  31  * @run main/othervm/timeout=300 Basic
  32  * @author Martin Buchholz
  33  */
  34 
  35 import java.lang.ProcessBuilder.Redirect;
  36 import static java.lang.ProcessBuilder.Redirect.*;
  37 
  38 import java.io.*;
  39 import java.util.*;
  40 import java.util.concurrent.CountDownLatch;
  41 import java.security.*;
  42 import java.util.regex.Pattern;
  43 import static java.lang.System.getenv;
  44 import static java.lang.System.out;
  45 import static java.lang.Boolean.TRUE;
  46 import static java.util.AbstractMap.SimpleImmutableEntry;
  47 
  48 public class Basic {
  49 


1423         try {
1424             Set<String> env1 = new HashSet<String>
1425                 (Arrays.asList(nativeEnv((String[])null).split("\n")));
1426 
1427             ProcessBuilder pb = new ProcessBuilder();
1428             pb.environment().put("QwErTyUiOp","AsDfGhJk");
1429 
1430             Set<String> env2 = new HashSet<String>
1431                 (Arrays.asList(nativeEnv(pb).split("\n")));
1432 
1433             check(env2.size() == env1.size() + 1);
1434             env1.add("QwErTyUiOp=AsDfGhJk");
1435             check(env1.equals(env2));
1436         } catch (Throwable t) { unexpected(t); }
1437 
1438         //----------------------------------------------------------------
1439         // Test Runtime.exec(...envp...)
1440         // Check for sort order of environment variables on Windows.
1441         //----------------------------------------------------------------
1442         try {
1443             String systemRoot = "SystemRoot=" + System.getenv("SystemRoot");
1444             // '+' < 'A' < 'Z' < '_' < 'a' < 'z' < '~'
1445             String[]envp = {"FOO=BAR","BAZ=GORP","QUUX=",
1446                             "+=+", "_=_", "~=~", systemRoot};
1447             String output = nativeEnv(envp);
1448             String expected = "+=+\nBAZ=GORP\nFOO=BAR\nQUUX=\n"+systemRoot+"\n_=_\n~=~\n";
1449             // On Windows, Java must keep the environment sorted.
1450             // Order is random on Unix, so this test does the sort.
1451             if (! Windows.is())
1452                 output = sortByLinesWindowsly(output);
1453             equal(output, expected);
1454         } catch (Throwable t) { unexpected(t); }
1455 
1456         //----------------------------------------------------------------
1457         // Test Runtime.exec(...envp...)
1458         // and check SystemRoot gets set automatically on Windows
1459         //----------------------------------------------------------------
1460         try {
1461             if (Windows.is()) {
1462                 String systemRoot = "SystemRoot=" + System.getenv("SystemRoot");
1463                 String[]envp = {"FOO=BAR","BAZ=GORP","QUUX=",
1464                                 "+=+", "_=_", "~=~"};
1465                 String output = nativeEnv(envp);
1466                 String expected = "+=+\nBAZ=GORP\nFOO=BAR\nQUUX=\n"+systemRoot+"\n_=_\n~=~\n";
1467                 equal(output, expected);
1468             }
1469         } catch (Throwable t) { unexpected(t); }
1470 
1471         //----------------------------------------------------------------
1472         // System.getenv() must be consistent with System.getenv(String)
1473         //----------------------------------------------------------------
1474         try {
1475             for (Map.Entry<String,String> e : getenv().entrySet())
1476                 equal(getenv(e.getKey()), e.getValue());
1477         } catch (Throwable t) { unexpected(t); }
1478 
1479         //----------------------------------------------------------------
1480         // Fiddle with working directory in child
1481         //----------------------------------------------------------------
1482         try {
1483             String canonicalUserDir =
1484                 new File(System.getProperty("user.dir")).getCanonicalPath();
1485             String[] sdirs = new String[]
1486                 {".", "..", "/", "/bin",
1487                  "C:", "c:", "C:/", "c:\\", "\\", "\\bin",
1488                  "c:\\windows  ", "c:\\Program Files", "c:\\Program Files\\" };
1489             for (String sdir : sdirs) {
1490                 File dir = new File(sdir);