File: DataHandlerTest.java

package info (click to toggle)
jython 2.7.3%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 62,820 kB
  • sloc: python: 641,384; java: 306,981; xml: 2,066; sh: 514; ansic: 126; makefile: 77
file content (108 lines) | stat: -rw-r--r-- 4,046 bytes parent folder | download | duplicates (3)
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
package com.ziclix.python.sql;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.List;

import org.python.core.PyObject;
import org.python.core.PySystemState;

import junit.framework.TestCase;

public class DataHandlerTest extends TestCase {

    private DataHandler _handler;

    @Override
    protected void setUp() throws Exception {
        PySystemState.initialize();
        _handler = new DataHandler();
    }

    /**
     * make sure we handle every {@link java.sql.Types} somehow
     * 
     * @throws Exception
     */
    public void testGetPyObjectResultSetIntInt() throws Exception {
        ResultSet rs = (ResultSet)Proxy.newProxyInstance(getClass().getClassLoader(),
                                                         new Class<?>[] {ResultSet.class},
                                                         new DefaultReturnHandler());
        List<String> unsupportedTypes = Arrays.asList(
                "ARRAY",
                "DATALINK",
                "DISTINCT",
                "REF",
                "REF_CURSOR",
                "ROWID",
                "STRUCT",
                "TIME_WITH_TIMEZONE",
                "TIMESTAMP_WITH_TIMEZONE"
        );
        for (Field field : Types.class.getDeclaredFields()) {
            String typeName = field.getName();
            int type = field.getInt(null);
            if (unsupportedTypes.contains(typeName)) {
                try {
                    _handler.getPyObject(rs, 1, type);
                    fail("SQLException expected: " + typeName);
                } catch (SQLException sqle) {
                    // expected
                }
            } else {
                try {
                    PyObject pyobj = _handler.getPyObject(rs, 1, type);
                    assertNotNull(typeName + " should return None", pyobj);
                } catch (SQLException sqle) {
                    // unexpected! but useful for future proofing changes in SQL support
                    fail("unexpected SQLException: " + typeName);
                }
            }
        }
    }

    /**
     * This is a poor man's mock - i cannot introduce a mock framework at this point in time
     */
    static class DefaultReturnHandler implements InvocationHandler {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            Class<?> returnType = method.getReturnType();
            if (returnType.equals(Boolean.class) || returnType.equals(Boolean.TYPE)) {
                return Boolean.FALSE;
            } else if (Character.TYPE.equals(returnType)) {
                return Character.valueOf('0');
            } else if (Byte.TYPE.equals(returnType)) {
                return Byte.valueOf((byte)0);
            } else if (Short.TYPE.equals(returnType)) {
                return Short.valueOf((short)0);
            } else if (Integer.TYPE.equals(returnType)) {
                return Integer.valueOf(0);
            } else if (Long.TYPE.equals(returnType)) {
                return Long.valueOf(0L);
            } else if (Float.TYPE.equals(returnType)) {
                return Float.valueOf(0);
            } else if (Double.TYPE.equals(returnType)) {
                return Double.valueOf(0);
            } else if (returnType.isPrimitive()) {
                throw new RuntimeException("unhandled primitve type " + returnType);
            } else if (returnType.isAssignableFrom(BigInteger.class)) {
                return BigInteger.ZERO;
            } else if (returnType.isAssignableFrom(BigDecimal.class)) {
                return BigDecimal.ZERO;
            } else if (returnType.isAssignableFrom(Number.class)) {
                return 0;
            } else {
                return null;
            }
        }
    }
}