• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / jmockit1 / 398

29 Oct 2025 01:58PM UTC coverage: 72.206% (-0.02%) from 72.226%
398

push

github

web-flow
Merge pull request #395 from hazendaz/develop

Migrate jmockit tests away from javax to jakarta

5683 of 8360 branches covered (67.98%)

Branch coverage included in aggregate %.

11946 of 16055 relevant lines covered (74.41%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.17
/main/src/main/java/mockit/internal/injection/full/TestDataSource.java
1
/*
2
 * Copyright (c) 2006 JMockit developers
3
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
4
 */
5
package mockit.internal.injection.full;
6

7
import edu.umd.cs.findbugs.annotations.NonNull;
8
import edu.umd.cs.findbugs.annotations.Nullable;
9

10
import java.beans.BeanInfo;
11
import java.beans.IntrospectionException;
12
import java.beans.Introspector;
13
import java.beans.PropertyDescriptor;
14
import java.lang.annotation.Annotation;
15
import java.lang.reflect.InvocationTargetException;
16
import java.lang.reflect.Method;
17

18
import javax.sql.CommonDataSource;
19

20
import mockit.internal.injection.InjectionPoint;
21
import mockit.internal.injection.TestedClass;
22

23
final class TestDataSource {
24
    @Nullable
25
    private final String dsName;
26
    private Class<? extends CommonDataSource> dsClass;
27
    private CommonDataSource ds;
28

29
    TestDataSource(@NonNull InjectionPoint injectionPoint) {
1✔
30
        dsName = injectionPoint.name;
1✔
31
    }
1✔
32

33
    @Nullable
34
    CommonDataSource createIfDataSourceDefinitionAvailable(@NonNull TestedClass testedClass) {
35
        TestedClass testedClassWithDataSource = testedClass.parent;
1✔
36

37
        if (testedClassWithDataSource == null || dsName == null) {
1!
38
            return null;
×
39
        }
40

41
        Class<?> testClass = testedClassWithDataSource.testClass;
1✔
42

43
        if (testClass != null) {
1!
44
            createFromTestedClassOrASuperclass(testClass);
1✔
45
        }
46

47
        if (ds != null) {
1!
48
            return ds;
×
49
        }
50

51
        TestedClass testedClassToBeSearched = testedClassWithDataSource;
1✔
52

53
        do {
54
            createFromTestedClassOrASuperclass(testedClassToBeSearched.targetClass);
1✔
55

56
            if (ds != null) {
1!
57
                return ds;
1✔
58
            }
59

60
            testedClassToBeSearched = testedClassToBeSearched.parent;
×
61
        } while (testedClassToBeSearched != null);
×
62

63
        throw new IllegalStateException("Missing @DataSourceDefinition of name \"" + dsName + "\" on "
×
64
                + testedClassWithDataSource.nameOfTestedClass + " or on a super/parent class");
65
    }
66

67
    private void createFromTestedClassOrASuperclass(@NonNull Class<?> targetClass) {
68
        do {
69
            createDataSource(targetClass);
1✔
70

71
            if (ds != null) {
1✔
72
                return;
1✔
73
            }
74

75
            targetClass = targetClass.getSuperclass();
1✔
76
        } while (targetClass != null && targetClass != Object.class);
1!
77
    }
1✔
78

79
    private void createDataSource(@NonNull Class<?> targetClass) {
80
        for (Annotation annotation : targetClass.getDeclaredAnnotations()) {
1✔
81
            String annotationName = annotation.annotationType().getName();
1✔
82

83
            if ("jakarta.annotation.sql.DataSourceDefinitions".equals(annotationName)) {
1✔
84
                createDataSourceJakarta((jakarta.annotation.sql.DataSourceDefinitions) annotation);
1✔
85
            } else if ("jakarta.annotation.sql.DataSourceDefinition".equals(annotationName)) {
1!
86
                createDataSourceJakarta((jakarta.annotation.sql.DataSourceDefinition) annotation);
1✔
87
            } else if ("javax.annotation.sql.DataSourceDefinitions".equals(annotationName)) {
×
88
                createDataSourceJavax((javax.annotation.sql.DataSourceDefinitions) annotation);
×
89
            } else if ("javax.annotation.sql.DataSourceDefinition".equals(annotationName)) {
×
90
                createDataSourceJavax((javax.annotation.sql.DataSourceDefinition) annotation);
×
91
            }
92

93
            if (ds != null) {
1!
94
                return;
1✔
95
            }
96
        }
97
    }
1✔
98

99
    private void createDataSourceJakarta(@NonNull jakarta.annotation.sql.DataSourceDefinitions dsDefs) {
100
        for (jakarta.annotation.sql.DataSourceDefinition dsDef : dsDefs.value()) {
1!
101
            createDataSourceJakarta(dsDef);
1✔
102

103
            if (ds != null) {
1✔
104
                return;
1✔
105
            }
106
        }
107
    }
×
108

109
    private void createDataSourceJakarta(@NonNull jakarta.annotation.sql.DataSourceDefinition dsDef) {
110
        String configuredDataSourceName = InjectionPoint.getNameFromJNDILookup(dsDef.name());
1✔
111

112
        if (configuredDataSourceName.equals(dsName)) {
1✔
113
            instantiateConfiguredDataSourceClassJakarta(dsDef);
1✔
114
            setDataSourcePropertiesFromConfiguredValuesJakarta(dsDef);
1✔
115
        }
116
    }
1✔
117

118
    private void instantiateConfiguredDataSourceClassJakarta(
119
            @NonNull jakarta.annotation.sql.DataSourceDefinition dsDef) {
120
        String className = dsDef.className();
1✔
121

122
        try {
123
            // noinspection unchecked
124
            dsClass = (Class<? extends CommonDataSource>) Class.forName(className);
1✔
125
            // noinspection ClassNewInstance
126
            ds = dsClass.getDeclaredConstructor().newInstance();
1✔
127
        } catch (ClassNotFoundException | IllegalAccessException e) {
×
128
            throw new RuntimeException(e);
×
129
        } catch (InstantiationException e) {
×
130
            throw new RuntimeException(e.getCause());
×
131
        } catch (IllegalArgumentException e) {
×
132
            throw new RuntimeException(e);
×
133
        } catch (InvocationTargetException e) {
×
134
            throw new RuntimeException(e);
×
135
        } catch (NoSuchMethodException e) {
×
136
            throw new RuntimeException(e);
×
137
        } catch (SecurityException e) {
×
138
            throw new RuntimeException(e);
×
139
        }
1✔
140
    }
1✔
141

142
    private void setDataSourcePropertiesFromConfiguredValuesJakarta(
143
            @NonNull jakarta.annotation.sql.DataSourceDefinition dsDef) {
144
        try {
145
            BeanInfo beanInfo = Introspector.getBeanInfo(dsClass, Object.class);
1✔
146
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
1✔
147

148
            setProperty(properties, "url", dsDef.url());
1✔
149
            setProperty(properties, "user", dsDef.user());
1✔
150
            setProperty(properties, "password", dsDef.password());
1✔
151
        } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
×
152
            throw new RuntimeException(e);
×
153
        }
1✔
154
    }
1✔
155

156
    private void createDataSourceJavax(@NonNull javax.annotation.sql.DataSourceDefinitions dsDefs) {
157
        for (javax.annotation.sql.DataSourceDefinition dsDef : dsDefs.value()) {
×
158
            createDataSourceJavax(dsDef);
×
159

160
            if (ds != null) {
×
161
                return;
×
162
            }
163
        }
164
    }
×
165

166
    private void createDataSourceJavax(@NonNull javax.annotation.sql.DataSourceDefinition dsDef) {
167
        String configuredDataSourceName = InjectionPoint.getNameFromJNDILookup(dsDef.name());
×
168

169
        if (configuredDataSourceName.equals(dsName)) {
×
170
            instantiateConfiguredDataSourceClassJavax(dsDef);
×
171
            setDataSourcePropertiesFromConfiguredValuesJavax(dsDef);
×
172
        }
173
    }
×
174

175
    private void instantiateConfiguredDataSourceClassJavax(@NonNull javax.annotation.sql.DataSourceDefinition dsDef) {
176
        String className = dsDef.className();
×
177

178
        try {
179
            // noinspection unchecked
180
            dsClass = (Class<? extends CommonDataSource>) Class.forName(className);
×
181
            // noinspection ClassNewInstance
182
            ds = dsClass.getDeclaredConstructor().newInstance();
×
183
        } catch (ClassNotFoundException | IllegalAccessException e) {
×
184
            throw new RuntimeException(e);
×
185
        } catch (InstantiationException e) {
×
186
            throw new RuntimeException(e.getCause());
×
187
        } catch (IllegalArgumentException e) {
×
188
            throw new RuntimeException(e);
×
189
        } catch (InvocationTargetException e) {
×
190
            throw new RuntimeException(e);
×
191
        } catch (NoSuchMethodException e) {
×
192
            throw new RuntimeException(e);
×
193
        } catch (SecurityException e) {
×
194
            throw new RuntimeException(e);
×
195
        }
×
196
    }
×
197

198
    private void setDataSourcePropertiesFromConfiguredValuesJavax(
199
            @NonNull javax.annotation.sql.DataSourceDefinition dsDef) {
200
        try {
201
            BeanInfo beanInfo = Introspector.getBeanInfo(dsClass, Object.class);
×
202
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
×
203

204
            setProperty(properties, "url", dsDef.url());
×
205
            setProperty(properties, "user", dsDef.user());
×
206
            setProperty(properties, "password", dsDef.password());
×
207
        } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
×
208
            throw new RuntimeException(e);
×
209
        }
×
210
    }
×
211

212
    private void setProperty(@NonNull PropertyDescriptor[] properties, @NonNull String name, @NonNull String value)
213
            throws InvocationTargetException, IllegalAccessException {
214
        for (PropertyDescriptor property : properties) {
1!
215
            if (property.getName().equals(name)) {
1✔
216
                Method writeMethod = property.getWriteMethod();
1✔
217

218
                if (writeMethod != null) {
1!
219
                    writeMethod.invoke(ds, value);
1✔
220
                }
221

222
                return;
1✔
223
            }
224
        }
225
    }
×
226
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc