-
Notifications
You must be signed in to change notification settings - Fork 159
/
JPQLExceptionTest.java
220 lines (189 loc) · 9.61 KB
/
JPQLExceptionTest.java
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
package org.eclipse.persistence.testing.tests.jpql;
import java.util.*;
import org.eclipse.persistence.exceptions.*;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.testing.framework.*;
import org.eclipse.persistence.queries.ReadAllQuery;
import org.eclipse.persistence.queries.ObjectLevelReadQuery;
public class JPQLExceptionTest extends JPQLTestCase {
public EclipseLinkException expectedException;
public EclipseLinkException caughtException;
public ClassDescriptor oldDescriptor;
public static JPQLExceptionTest recognitionExceptionTest() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.syntaxErrorAt(null, 0, 0, null, null);
theTest.setEjbqlString("SELECT OBJECT(emp) FROW Employee emp");
theTest.setName("Recognition Exception Test");
return theTest;
}
public static JPQLExceptionTest missingSelectExceptionTest() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.unexpectedToken(null, 0, 0, null, null);
theTest.setEjbqlString("OBJECT(emp) FROM Employee emp");
theTest.setName("Missing Select Exception Test");
return theTest;
}
public static JPQLExceptionTest malformedEjbqlExceptionTest1() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.unexpectedToken(null, 0, 0, null, null);
theTest.setEjbqlString("SELECT OBJECT(emp) FROM Employee emp WHERE emp.firstName == \"Fred\"");
theTest.setName("Malformed EJBQL Exception Test1");
return theTest;
}
public static JPQLExceptionTest malformedEjbqlExceptionTest2() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.unexpectedEOF(null, 0, 0, null);
theTest.setEjbqlString("SELECT OBJECT(emp) FROM Employee emp WHERE emp.firstName = \"Fred\" AND 1");
theTest.setName("Malformed EJBQL Exception Test2");
return theTest;
}
public static JPQLExceptionTest malformedEjbqlExceptionTest3() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.unexpectedEOF(null, 0, 0, null);
theTest.setEjbqlString("SELECT OBJECT(emp) FROM Employee emp WHERE emp.firstName = \"Fred\" OR \"Freda\"");
theTest.setName("Malformed EJBQL Exception Test3");
return theTest;
}
public static JPQLExceptionTest malformedEjbqlExceptionTest4() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.unexpectedToken(null, 0, 0, null, null);
theTest.setEjbqlString("SLEECT OBJECT(emp) FROM Employee emp WHERE emp.firstName = \"Fred\" OR \"Freda\"");
theTest.setName("Malformed EJBQL Exception Test4");
return theTest;
}
public static JPQLExceptionTest badAliasExceptionTest() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.aliasResolutionException(null, 0, 0, null);
theTest.setEjbqlString("SELECT OBJECT(emp) FROM Employee employee WHERE emp.firstName = \"Fred\"");
theTest.setName("Bad Alias Exception Test 2");
return theTest;
}
public static JPQLExceptionTest generalExceptionTest() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.unexpectedToken(null, 0, 0, null, null);
theTest.setEjbqlString("SELECT FROM EMPLOYEE emp");
theTest.setName("General Exception test");
return theTest;
}
public static JPQLExceptionTest classNotFoundExceptionTest() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.resolutionClassNotFoundException(null, null);
theTest.setReferenceClass(org.eclipse.persistence.testing.models.employee.domain.Address.class);
theTest.setEjbqlString("SELECT OBJECT(a) FROM AddressBean a WHERE a.city = \"Ottawa\"");
theTest.setName("Class Not Found Exception test");
return theTest;
}
public static JPQLExceptionTest aliasResolutionException() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.aliasResolutionException(null, 0, 0, null);
theTest.setName("Bad Alias Exception test 1");
theTest.setEjbqlString("SELECT OBJECT(nullRoot) FROM Employee emp WHERE emp.firstName = \"Fred\"");
return theTest;
}
public static JPQLExceptionTest missingDescriptorExceptionTest() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.entityTypeNotFound2(null, 0, 0, null);
theTest.setName("Missing Descriptor Exception test");
theTest.setEjbqlString("SELECT OBJECT(i) FROM Integer i WHERE i.city = \"Ottawa\"");
return theTest;
}
public static JPQLExceptionTest expressionNotSupportedTest() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.expressionNotSupported(null, null);
theTest.setName("Expression Not Supported Exception test");
theTest.setEjbqlString("SELECT OBJECT(emp) FROM Employee emp WHERE emp.phoneNumbers IS EMPTY");
return theTest;
}
public static JPQLExceptionTest memberOfNotSupportedTest() {
JPQLExceptionTest theTest = new JPQLExceptionTest();
theTest.expectedException = JPQLException.expressionNotSupported(null, null);
theTest.setName("MEMBER OF Not Supported Exception test");
theTest.setEjbqlString("SELECT OBJECT(proj) FROM Employee emp, Project proj " + " WHERE (proj.teamLeader MEMBER OF emp.manager.managedEmployees) " + "AND (emp.lastName = \"Chan\")");
return theTest;
}
public static void addTestsTo(TestSuite theSuite) {
//Descriptor exception will happen first--this makes the next test unnecessary
//theSuite.addTest(EJBQLExceptionTest.classNotFoundExceptionTest());
theSuite.addTest(JPQLExceptionTest.generalExceptionTest());
theSuite.addTest(JPQLExceptionTest.missingDescriptorExceptionTest());
theSuite.addTest(JPQLExceptionTest.aliasResolutionException());
theSuite.addTest(JPQLExceptionTest.recognitionExceptionTest());
theSuite.addTest(JPQLExceptionTest.malformedEjbqlExceptionTest1());
theSuite.addTest(JPQLExceptionTest.malformedEjbqlExceptionTest2());
theSuite.addTest(JPQLExceptionTest.malformedEjbqlExceptionTest3());
theSuite.addTest(JPQLExceptionTest.malformedEjbqlExceptionTest4());
theSuite.addTest(JPQLExceptionTest.missingSelectExceptionTest());
theSuite.addTest(JPQLExceptionTest.badAliasExceptionTest());
// Removed by JGL - IS [NOT] EMPTY is now supported - BUG 2775179
// theSuite.addTest(EJBQLExceptionTest.expressionNotSupportedTest());
// Removed by JED - Member of is now supported
// theSuite.addTest(EJBQLExceptionTest.memberOfNotSupportedTest());
}
@Override
public void setup() {
super.setup();
}
@Override
public void test() {
try {
getSession().logMessage("Running EJBQL -> " + getEjbqlString());
setReturnedObjects(getSession().executeQuery(getQuery()));
} catch (EclipseLinkException tle) {
caughtException = tle;
} catch (Exception e) {
caughtException = new TestErrorException(e.getMessage());
}
}
@Override
public void verify() {
if (caughtException == null) {
throw new TestErrorException("The proper exception was not thrown:" + System.lineSeparator() + "caught exception was null! \n\n[EXPECTING] " + expectedException);
}
if (caughtException instanceof JPQLException) {
return;
}
if (caughtException.getErrorCode() == expectedException.getErrorCode()) {
return;
}
if (caughtException.getClass() == JPQLException.class) {
Vector exceptions = (Vector)((JPQLException)caughtException).getInternalExceptions();
if (!exceptions.isEmpty()) {
JPQLException internalException = (JPQLException)exceptions.firstElement();
if (internalException.getErrorCode() == expectedException.getErrorCode()) {
return;
}
}
}
throw new TestErrorException("The proper exception was not thrown:" + System.lineSeparator() + "[CAUGHT] " + caughtException + "\n\n[EXPECTING] " + expectedException);
}
@Override
public void reset() {
super.reset();
}
@Override
public ObjectLevelReadQuery getQuery() {
if (theQuery == null) {
if (useReportQuery) {
setQuery(buildReportQuery());
} else {
setQuery(new ReadAllQuery());
}
getQuery().setEJBQLString(getEjbqlString());
getQuery().setReferenceClass(getReferenceClass());
}
return theQuery;
}
}