File: SysinfoAPITest.java

package info (click to toggle)
derby 10.14.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 78,896 kB
  • sloc: java: 691,930; sql: 42,686; xml: 20,511; sh: 3,373; sed: 96; makefile: 60
file content (199 lines) | stat: -rw-r--r-- 7,818 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.tools.SysinfoAPITest

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.functionTests.tests.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.PrintWriter;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import junit.framework.Test;
import org.apache.derby.tools.sysinfo;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;

/**
 *  Test all the static public methods of the sysinfo class.
 *  
 *  Formerly named sysinfo_api. Currently disabled from running in
 *  nightly regression tests when run from jar files, only the first
 *  jar in the classpath properly reports its information through the 
 *  sysinfo API.
 */

public class SysinfoAPITest extends BaseJDBCTestCase {

    DatabaseMetaData dm;
    
    public SysinfoAPITest(String name) { 
        super(name); 
    }
    
    public static Test suite() {
        Test suite = new BaseTestSuite(SysinfoAPITest.class, "Sysinfo API");
    	
    	return suite;
    }

    /**
     *  Test various invocations of sysinfo.getMajorVersion()
     */
    public void testMajorVersion() throws Exception {
        int dmMajor = dm.getDriverMajorVersion();
        assertEquals(dmMajor, sysinfo.getMajorVersion());
        assertEquals(dmMajor, sysinfo.getMajorVersion(sysinfo.DBMS));
        assertEquals(dmMajor, sysinfo.getMajorVersion(sysinfo.TOOLS));
        assertEquals(dmMajor, sysinfo.getMajorVersion(sysinfo.NET));
        assertEquals(dmMajor, sysinfo.getMajorVersion(sysinfo.CLIENT));
        // bad usage
        assertEquals(-1, sysinfo.getMajorVersion("foo"));
        assertEquals(-1, sysinfo.getMajorVersion(null));
    }

    /**
     *  Test various invocations of sysinfo.getMinorVersion()
     */
    public void testMinorVersion() throws Exception {
        int dmMinor = dm.getDriverMinorVersion();
        assertEquals(dmMinor, sysinfo.getMinorVersion());
        assertEquals(dmMinor, sysinfo.getMinorVersion(sysinfo.DBMS));
        assertEquals(dmMinor, sysinfo.getMinorVersion(sysinfo.TOOLS));
        assertEquals(dmMinor, sysinfo.getMinorVersion(sysinfo.NET));
        assertEquals(dmMinor, sysinfo.getMinorVersion(sysinfo.CLIENT));
        // bad usage
        assertEquals(-1, sysinfo.getMinorVersion("foo"));
        assertEquals(-1, sysinfo.getMinorVersion(null));
    }

    /**
     *  Test various invocations of sysinfo.getProductName()
     */
    public void testProductName() {
        assertEquals("Apache Derby", sysinfo.getProductName());
        assertEquals("Apache Derby", sysinfo.getProductName(sysinfo.DBMS));
        assertEquals("Apache Derby", sysinfo.getProductName(sysinfo.TOOLS));
        assertEquals("Apache Derby", sysinfo.getProductName(sysinfo.NET));
        assertEquals("Apache Derby", sysinfo.getProductName(sysinfo.CLIENT));
        // bad usage
        assertEquals("<no name found>", sysinfo.getProductName("foo"));
        assertEquals("<no name found>", sysinfo.getProductName(null));
    }

    /**
     *  Test various invocations of sysinfo.getVersionString()
     * 
     *  NOTE: sysinfo.getVersionString() returns the short version string.
     *        We also chomp the version we get back from sysinfo to account
     *        for alpha/beta differences.
     */
    public void testVersionString() throws SQLException {
        String dmPv = dm.getDatabaseProductVersion().substring(0,sysinfo.getVersionString().indexOf(' '));
        assertEquals(dmPv, sysinfo.getVersionString().substring(0,sysinfo.getVersionString().indexOf(' ')));
        assertEquals(dmPv, sysinfo.getVersionString(sysinfo.DBMS).substring(0,sysinfo.getVersionString().indexOf(' ')));
        assertEquals(dmPv, sysinfo.getVersionString(sysinfo.TOOLS).substring(0,sysinfo.getVersionString().indexOf(' ')));
        assertEquals(dmPv, sysinfo.getVersionString(sysinfo.NET).substring(0,sysinfo.getVersionString().indexOf(' ')));
        assertEquals(dmPv, sysinfo.getVersionString(sysinfo.CLIENT).substring(0,sysinfo.getVersionString().indexOf(' ')));
        // bad usage
        assertEquals("<no name found>", sysinfo.getVersionString("foo"));
        assertEquals("<no name found>", sysinfo.getVersionString(null));
    }

    /**
     * Test various invocations of sysinfo.getBuildNumber()
     *
     * Extract the build number from the Database Product Version.
     * Compare with the result from sysinfo.getBuildNumber.
     */
    public void testBuildNumber() throws SQLException {
        String dmBn = dm.getDatabaseProductVersion();
        dmBn = dmBn.substring(dmBn.indexOf('(') + 1,dmBn.indexOf(')'));
        System.out.println(dmBn);
        assertEquals(dmBn, sysinfo.getBuildNumber());
        assertEquals(dmBn, sysinfo.getBuildNumber(sysinfo.DBMS));
        assertEquals(dmBn, sysinfo.getBuildNumber(sysinfo.TOOLS));
        assertEquals(dmBn, sysinfo.getBuildNumber(sysinfo.NET));
        assertEquals(dmBn, sysinfo.getBuildNumber(sysinfo.CLIENT));
        // bad usage
        assertEquals("????", sysinfo.getBuildNumber("foo"));
        assertEquals("????", sysinfo.getBuildNumber(null));
    }

    /**
     * Test sysinfo.getInfo()
     *
     * Currently only tests getInfo() by comparing the first line with the
     * expected first line in English. Because so much of sysinfo changes from
     * machine-to-machine, writing a better test may be difficult.
     *
     * Test spawns a separate thread in which to call sysinfo and feed the
     * PipedWriter. Using PipedWriter and PipedReader from the same thread
     * can cause a deadlock.
     */
    public void testGetInfo() throws IOException {
        sysinfo_api_helper sah = new sysinfo_api_helper();
        sah.start();
        PipedReader pipeR = new PipedReader(sah.getPipedWriter());
        BufferedReader br = new BufferedReader(pipeR);
        assertEquals("------------------ Java Information ------------------",
                     br.readLine());
        br.close();
        pipeR.close();
    }

    /**
     *  setUp - get a DatabaseMetadata object with which to compare
     *          database information with what is reported by sysinfo
     */
    public void setUp() throws SQLException {
        dm = getConnection().getMetaData();
    }


}

/**
 * sysinfo_api_helper - a helper class which calls sysinfo.getInfo() and
 *                      pushes the output into a PipedWriter so that we
 *                      can read it with a PipedReader in testGetInfo().
 *
 */
class sysinfo_api_helper extends Thread { 
    
    private static PipedWriter pipeW = new PipedWriter();

    public void run() {
        PrintWriter pw = new PrintWriter(pipeW, true);
        sysinfo.getInfo(pw);
        try {
            pw.close();
            pipeW.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   public PipedWriter getPipedWriter() {
       return pipeW;
   }
}