View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library;
17  
18  import java.math.BigDecimal;
19  
20  import org.jdiagnose.DiagnosticUnit;
21  import org.jdiagnose.DiagnosticException;
22  
23  /***
24   * User: jamie
25   * Date: May 31, 2004
26   * Time: 8:12:36 PM
27   */
28  public class JavaVersionDiagnostic extends DiagnosticUnit {
29  
30      private static final BigDecimal javaClassVersion = new BigDecimal(System.getProperty("java.class.version", "44.0"));
31      
32      private static final BigDecimal fourtyFivePointThree = new BigDecimal("45.3");
33      private static final BigDecimal fourtySix = new BigDecimal(46);
34      private static final BigDecimal fourtySeven = new BigDecimal(47);
35      private static final BigDecimal fourtyEight = new BigDecimal(48);
36      private static final BigDecimal fourtyNine = new BigDecimal(49);
37      
38      private String javaVersion = null;
39  
40      public JavaVersionDiagnostic() {}
41  
42      /***
43       * Constructor for JavaVersionDiagnostic.
44       * @param javaVersion
45       */
46      public JavaVersionDiagnostic(String javaVersion) {
47          this.javaVersion = javaVersion;
48      }
49  
50      public void diagnoseJavaVersion() throws DiagnosticException {
51          assertNotNull("javaVersion property should not be null", javaVersion);
52          if (javaVersion.endsWith("+")) {
53              double javaVersionDouble = Double.parseDouble(javaVersion.substring(0, javaVersion.length() - 1));
54              if (javaVersionDouble == 1.1) {
55                  assertTrue(javaClassVersion.compareTo(fourtyFivePointThree) >= 0);
56              } else if (javaVersionDouble == 1.2) {
57                  assertTrue(javaClassVersion.compareTo(fourtySix) >= 0);
58              } else if (javaVersionDouble == 1.3) {
59                  assertTrue(javaClassVersion.compareTo(fourtySeven) >= 0);
60              } else if (javaVersionDouble == 1.4) {
61                  assertTrue(javaClassVersion.compareTo(fourtyEight) >= 0);
62              } else if (javaVersionDouble == 1.5) {
63                  assertTrue(javaClassVersion.compareTo(fourtyNine) >= 0);
64              } else {
65                  throw new DiagnosticException("javaVersion to test can only be one of 1.1, 1.2, 1.3, 1.4 or 1.5");
66              }
67          } else {
68              double javaVersionDouble = Double.parseDouble(javaVersion);
69              if (javaVersionDouble == 1.1) {
70                  assertTrue(isJDK11only());
71              } else if (javaVersionDouble == 1.2) {
72                  assertTrue(isJDK12only());
73              } else if (javaVersionDouble == 1.3) {
74                  assertTrue(isJDK13only());
75              } else if (javaVersionDouble == 1.4) {
76                  assertTrue(isJDK14only());
77              } else if (javaVersionDouble == 1.5) {
78                  assertTrue(isJDK15andAbove());
79              } else {
80                  throw new DiagnosticException("javaVersion to test can only be one of 1.1, 1.2, 1.3, 1.4 or 1.5");
81              }
82          }
83      }
84  
85      public boolean isJDK11only() {
86          return (fourtySix.compareTo(javaClassVersion) > 0) && (fourtyFivePointThree.compareTo(javaClassVersion) <= 0);
87      }
88  
89      public boolean isJDK12only() {
90          return (fourtySeven.compareTo(javaClassVersion) > 0) && (fourtySix.compareTo(javaClassVersion) <= 0);
91      }
92  
93      public boolean isJDK13only() {
94          return (fourtyEight.compareTo(javaClassVersion) > 0) && (fourtySeven.compareTo(javaClassVersion) <= 0);
95      }
96  
97      public boolean isJDK14only() {
98          return (fourtyNine.compareTo(javaClassVersion) > 0) && (fourtyEight.compareTo(javaClassVersion) <= 0);
99      }
100 
101     public boolean isJDK15andAbove() {
102         return (fourtyNine.compareTo(javaClassVersion) <= 0);
103     }
104 
105     /***
106      * @return
107      */
108     public String getJavaVersion() {
109         return javaVersion;
110     }
111 
112     /***
113      * @param string
114      */
115     public void setJavaVersion(String string) {
116         javaVersion = string;
117     }
118 
119 }