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
18package org.apache.harmony.sql.tests.java.sql;
19
20import java.lang.reflect.Field;
21import java.lang.reflect.Modifier;
22import java.util.HashMap;
23
24import junit.framework.TestCase;
25
26public class ConnectionTest extends TestCase {
27
28    /*
29     * Public statics test
30     */
31    public void testPublicStatics() {
32
33        HashMap<String, Integer> thePublicStatics = new HashMap<String, Integer>();
34        thePublicStatics.put("TRANSACTION_SERIALIZABLE", new Integer(8));
35        thePublicStatics.put("TRANSACTION_REPEATABLE_READ", new Integer(4));
36        thePublicStatics.put("TRANSACTION_READ_COMMITTED", new Integer(2));
37        thePublicStatics.put("TRANSACTION_READ_UNCOMMITTED", new Integer(1));
38        thePublicStatics.put("TRANSACTION_NONE", new Integer(0));
39
40        /*
41         * System.out.println( "TRANSACTION_SERIALIZABLE: " +
42         * Connection.TRANSACTION_SERIALIZABLE ); System.out.println(
43         * "TRANSACTION_REPEATABLE_READ: " +
44         * Connection.TRANSACTION_REPEATABLE_READ ); System.out.println(
45         * "TRANSACTION_READ_COMMITTED: " +
46         * Connection.TRANSACTION_READ_COMMITTED ); System.out.println(
47         * "TRANSACTION_READ_UNCOMMITTED: " +
48         * Connection.TRANSACTION_READ_UNCOMMITTED ); System.out.println(
49         * "TRANSACTION_NONE: " + Connection.TRANSACTION_NONE );
50         */
51
52        Class<?> connectionClass;
53        try {
54            connectionClass = Class.forName("java.sql.Connection");
55        } catch (ClassNotFoundException e) {
56            fail("java.sql.Connection class not found!");
57            return;
58        } // end try
59
60        Field[] theFields = connectionClass.getDeclaredFields();
61        int requiredModifier = Modifier.PUBLIC + Modifier.STATIC
62                + Modifier.FINAL;
63
64        int countPublicStatics = 0;
65        for (Field element : theFields) {
66            String fieldName = element.getName();
67            int theMods = element.getModifiers();
68            if (Modifier.isPublic(theMods) && Modifier.isStatic(theMods)) {
69                try {
70                    Object fieldValue = element.get(null);
71                    Object expectedValue = thePublicStatics.get(fieldName);
72                    if (expectedValue == null) {
73                        fail("Field " + fieldName + " missing!");
74                    } // end
75                    assertEquals("Field " + fieldName + " value mismatch: ",
76                            expectedValue, fieldValue);
77                    assertEquals("Field " + fieldName + " modifier mismatch: ",
78                            requiredModifier, theMods);
79                    countPublicStatics++;
80                } catch (IllegalAccessException e) {
81                    fail("Illegal access to Field " + fieldName);
82                } // end try
83            } // end if
84        } // end for
85
86    } // end method testPublicStatics
87
88} // end class ConnectionTest
89
90