PageRenderTime 25ms CodeModel.GetById 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/tests/junit/org/apache/tools/ant/util/JavaEnvUtilsTest.java

https://bitbucket.org/kaendfinger/apache-ant
Java | 140 lines | 94 code | 22 blank | 24 comment | 9 complexity | fd3ed42ccf0ef4a1c5de9e170c402234 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.util;
  19. import java.io.File;
  20. import junit.framework.AssertionFailedError;
  21. import junit.framework.TestCase;
  22. import org.apache.tools.ant.taskdefs.condition.Os;
  23. /**
  24. * TestCase for JavaEnvUtils.
  25. *
  26. */
  27. public class JavaEnvUtilsTest extends TestCase {
  28. private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
  29. public JavaEnvUtilsTest(String s) {
  30. super(s);
  31. }
  32. public void testGetExecutableNetware() {
  33. if (Os.isName("netware")) {
  34. assertEquals("java", JavaEnvUtils.getJreExecutable("java"));
  35. assertEquals("javac", JavaEnvUtils.getJdkExecutable("javac"));
  36. assertEquals("foo", JavaEnvUtils.getJreExecutable("foo"));
  37. assertEquals("foo", JavaEnvUtils.getJdkExecutable("foo"));
  38. }
  39. }
  40. public void testGetExecutableWindows() {
  41. if (Os.isFamily("windows")) {
  42. String javaHome =
  43. FILE_UTILS.normalize(System.getProperty("java.home"))
  44. .getAbsolutePath();
  45. String j = JavaEnvUtils.getJreExecutable("java");
  46. assertTrue(j.endsWith(".exe"));
  47. assertTrue(j+" is absolute", (new File(j)).isAbsolute());
  48. try {
  49. assertTrue(j+" is normalized and in the JRE dir",
  50. j.startsWith(javaHome));
  51. } catch (AssertionFailedError e) {
  52. // java.home is bogus
  53. assertEquals("java.exe", j);
  54. }
  55. j = JavaEnvUtils.getJdkExecutable("javac");
  56. assertTrue(j.endsWith(".exe"));
  57. try {
  58. assertTrue(j+" is absolute", (new File(j)).isAbsolute());
  59. String javaHomeParent =
  60. FILE_UTILS.normalize(javaHome+"/..").getAbsolutePath();
  61. assertTrue(j+" is normalized and in the JDK dir",
  62. j.startsWith(javaHomeParent));
  63. assertTrue(j+" is normalized and not in the JRE dir",
  64. !j.startsWith(javaHome));
  65. } catch (AssertionFailedError e) {
  66. // java.home is bogus
  67. assertEquals("javac.exe", j);
  68. }
  69. assertEquals("foo.exe", JavaEnvUtils.getJreExecutable("foo"));
  70. assertEquals("foo.exe", JavaEnvUtils.getJdkExecutable("foo"));
  71. }
  72. }
  73. public void testGetExecutableMostPlatforms() {
  74. if (!Os.isName("netware") && !Os.isFamily("windows")) {
  75. String javaHome =
  76. FILE_UTILS.normalize(System.getProperty("java.home"))
  77. .getAbsolutePath();
  78. // could still be OS/2
  79. String extension = Os.isFamily("dos") ? ".exe" : "";
  80. String j = JavaEnvUtils.getJreExecutable("java");
  81. if (!extension.equals("")) {
  82. assertTrue(j.endsWith(extension));
  83. }
  84. assertTrue(j+" is absolute", (new File(j)).isAbsolute());
  85. assertTrue(j+" is normalized and in the JRE dir",
  86. j.startsWith(javaHome));
  87. j = JavaEnvUtils.getJdkExecutable("javac");
  88. if (!extension.equals("")) {
  89. assertTrue(j.endsWith(extension));
  90. }
  91. assertTrue(j+" is absolute", (new File(j)).isAbsolute());
  92. String javaHomeParent =
  93. FILE_UTILS.normalize(javaHome+"/..").getAbsolutePath();
  94. assertTrue(j+" is normalized and in the JDK dir",
  95. j.startsWith(javaHomeParent));
  96. if (Os.isFamily("mac") && JavaEnvUtils.getJavaVersionNumber() <= JavaEnvUtils.VERSION_1_6) {
  97. assertTrue(j+" is normalized and in the JRE dir",
  98. j.startsWith(javaHome));
  99. } else {
  100. assertTrue(j+" is normalized and not in the JRE dir",
  101. !j.startsWith(javaHome));
  102. }
  103. assertEquals("foo"+extension,
  104. JavaEnvUtils.getJreExecutable("foo"));
  105. assertEquals("foo"+extension,
  106. JavaEnvUtils.getJdkExecutable("foo"));
  107. }
  108. }
  109. public void testIsAtLeastJavaVersion()
  110. {
  111. assertTrue(
  112. "Current java version is not at least the current java version...",
  113. JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.getJavaVersion()));
  114. assertFalse(
  115. "In case the current java version is higher than 9.0 definitely a new algorithem will be needed",
  116. JavaEnvUtils.isAtLeastJavaVersion("9.0"));
  117. }
  118. }