KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > JavaVersionHolder


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.JavaVersionHolder
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.harness;
23
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27   To break down the java version into major and minor
28   Used by the test harness for special cases
29   */

30 public class JavaVersionHolder
31 {
32  
33     private String JavaDoc majorVersion;
34     private String JavaDoc minorVersion;
35     private int major;
36     private int minor;
37     
38     public JavaVersionHolder(String JavaDoc javaVersion)
39         throws java.lang.NumberFormatException JavaDoc
40     {
41         // check for jdk12 or higher
42
int i = javaVersion.indexOf('.');
43         int j = javaVersion.indexOf('.', i+1);
44         majorVersion = javaVersion.substring(0, i);
45         try
46         {
47             Integer JavaDoc imajor = new Integer JavaDoc(majorVersion);
48             major = imajor.intValue();
49             if (j != -1)
50             {
51                 minorVersion = javaVersion.substring(i+1, j);
52                 Integer JavaDoc iminor = new Integer JavaDoc(minorVersion);
53                 minor = iminor.intValue();
54             }
55             else
56             {
57                 minorVersion = javaVersion.substring(i+1);
58                 Integer JavaDoc iminor = new Integer JavaDoc(minorVersion);
59                 minor = iminor.intValue();
60             }
61         }
62         catch (NumberFormatException JavaDoc nfe)
63         {
64             // Cannot parse the version as an Integer
65
// such as on HP: hack for this special case
66
if (javaVersion.startsWith("HP"))
67             {
68                 // attempt to get the version
69
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(javaVersion,".");
70                 String JavaDoc tmp = st.nextToken();
71                 majorVersion = st.nextToken();
72                 if (majorVersion.equals("01"))
73                     majorVersion = "1";
74                 else if (majorVersion.equals("02"))
75                     majorVersion = "2";
76                 minorVersion = st.nextToken();
77                 if (minorVersion.startsWith("1"))
78                     minorVersion = "1";
79                 else if (minorVersion.startsWith("2"))
80                     minorVersion = "2";
81                 //System.out.println("majorVersion: " + majorVersion);
82
//System.out.println("minorVersion: " + minorVersion);
83
try
84                 {
85                     Integer JavaDoc imajor = new Integer JavaDoc(majorVersion);
86                     major = imajor.intValue();
87                     Integer JavaDoc iminor = new Integer JavaDoc(minorVersion);
88                     minor = iminor.intValue();
89                 }
90                 catch (NumberFormatException JavaDoc nfe2)
91                 {
92                     System.out.println("Could not parse version: " + nfe2);
93                     // Still couldn't parse the vesion
94
// have to give up
95
}
96             }
97             else
98             {
99                 System.out.println("NumberFormatException thrown trying to parse the version. " + javaVersion);
100                 System.out.println("The test harness only handles the HP special case.");
101             }
102                 
103         }
104     }
105
106     public String JavaDoc getMajorVersion()
107     {
108         return majorVersion;
109     }
110     
111     public String JavaDoc getMinorVersion()
112     {
113         return minorVersion;
114     }
115     
116     public int getMajorNumber()
117     {
118         return major;
119     }
120     
121     public int getMinorNumber()
122     {
123         return minor;
124     }
125
126     /**
127      * <p>
128      * Return true if we are at least at the passed in version.
129      * </p>
130      */

131     public boolean atLeast( int baseMajor, int baseMinor )
132     {
133         if ( major < baseMajor ) { return false; }
134         if ( major > baseMajor ) { return true; }
135
136         // same major number
137

138         return ( minor >= baseMinor );
139     }
140
141 }
142
Popular Tags