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

hazendaz / jmockit1 / 422

30 Oct 2025 09:23PM UTC coverage: 72.22% (+0.02%) from 72.198%
422

push

github

hazendaz
Combine catches

5674 of 8356 branches covered (67.9%)

Branch coverage included in aggregate %.

0 of 1 new or added line in 1 file covered. (0.0%)

52 existing lines in 8 files now uncovered.

11929 of 16018 relevant lines covered (74.47%)

0.74 hits per line

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

53.38
/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 | InstantiationException | IllegalArgumentException
×
128
                | InvocationTargetException | NoSuchMethodException | SecurityException e) {
129
            throw new RuntimeException(e instanceof InstantiationException ? e.getCause() : e);
×
130
        }
1✔
131
    }
1✔
132

133
    private void setDataSourcePropertiesFromConfiguredValuesJakarta(
134
            @NonNull jakarta.annotation.sql.DataSourceDefinition dsDef) {
135
        try {
136
            BeanInfo beanInfo = Introspector.getBeanInfo(dsClass, Object.class);
1✔
137
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
1✔
138

139
            setProperty(properties, "url", dsDef.url());
1✔
140
            setProperty(properties, "user", dsDef.user());
1✔
141
            setProperty(properties, "password", dsDef.password());
1✔
UNCOV
142
        } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
×
UNCOV
143
            throw new RuntimeException(e);
×
144
        }
1✔
145
    }
1✔
146

147
    private void createDataSourceJavax(@NonNull javax.annotation.sql.DataSourceDefinitions dsDefs) {
UNCOV
148
        for (javax.annotation.sql.DataSourceDefinition dsDef : dsDefs.value()) {
×
UNCOV
149
            createDataSourceJavax(dsDef);
×
150

151
            if (ds != null) {
×
152
                return;
×
153
            }
154
        }
UNCOV
155
    }
×
156

157
    private void createDataSourceJavax(@NonNull javax.annotation.sql.DataSourceDefinition dsDef) {
158
        String configuredDataSourceName = InjectionPoint.getNameFromJNDILookup(dsDef.name());
×
159

160
        if (configuredDataSourceName.equals(dsName)) {
×
161
            instantiateConfiguredDataSourceClassJavax(dsDef);
×
UNCOV
162
            setDataSourcePropertiesFromConfiguredValuesJavax(dsDef);
×
163
        }
164
    }
×
165

166
    private void instantiateConfiguredDataSourceClassJavax(@NonNull javax.annotation.sql.DataSourceDefinition dsDef) {
167
        String className = dsDef.className();
×
168

169
        try {
170
            // noinspection unchecked
171
            dsClass = (Class<? extends CommonDataSource>) Class.forName(className);
×
172
            // noinspection ClassNewInstance
173
            ds = dsClass.getDeclaredConstructor().newInstance();
×
UNCOV
174
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | IllegalArgumentException
×
175
                | InvocationTargetException | NoSuchMethodException | SecurityException e) {
176
            throw new RuntimeException(e instanceof InstantiationException ? e.getCause() : e);
×
UNCOV
177
        }
×
UNCOV
178
    }
×
179

180
    private void setDataSourcePropertiesFromConfiguredValuesJavax(
181
            @NonNull javax.annotation.sql.DataSourceDefinition dsDef) {
182
        try {
183
            BeanInfo beanInfo = Introspector.getBeanInfo(dsClass, Object.class);
×
184
            PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
×
185

186
            setProperty(properties, "url", dsDef.url());
×
187
            setProperty(properties, "user", dsDef.user());
×
188
            setProperty(properties, "password", dsDef.password());
×
189
        } catch (IntrospectionException | IllegalAccessException | InvocationTargetException e) {
×
190
            throw new RuntimeException(e);
×
191
        }
×
192
    }
×
193

194
    private void setProperty(@NonNull PropertyDescriptor[] properties, @NonNull String name, @NonNull String value)
195
            throws InvocationTargetException, IllegalAccessException {
196
        for (PropertyDescriptor property : properties) {
1!
197
            if (property.getName().equals(name)) {
1✔
198
                Method writeMethod = property.getWriteMethod();
1✔
199

200
                if (writeMethod != null) {
1!
201
                    writeMethod.invoke(ds, value);
1✔
202
                }
203

204
                return;
1✔
205
            }
206
        }
207
    }
×
208
}
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