/
TestProcErrors.java
169 lines (130 loc) · 7.31 KB
/
TestProcErrors.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
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.teiid.query.processor.proc;
import static org.junit.Assert.*;
import static org.teiid.query.processor.proc.TestProcedureProcessor.*;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.teiid.core.TeiidProcessingException;
import org.teiid.jdbc.TeiidSQLException;
import org.teiid.query.metadata.TransformationMetadata;
import org.teiid.query.processor.HardcodedDataManager;
import org.teiid.query.processor.ProcessorPlan;
import org.teiid.query.resolver.TestProcedureResolving;
@SuppressWarnings("nls")
public class TestProcErrors {
@Test public void testInvalidException() throws Exception {
String ddl =
"create virtual procedure vproc (x integer) returns integer as begin declare object e = sqlexception 'hello'; raise e; raise sqlexception 'hello world' sqlstate 'abc', 1 chain e; end;";
try {
TestProcedureResolving.createMetadata(ddl);
fail();
} catch (RuntimeException e) {
assertEquals("TEIID31080 View test.vproc validation error: QueryResolverException-TEIID31120 An exception may only be chained to another exception. e is not valid.", e.getMessage());
}
}
@Test public void testExceptionAndWarning() throws Exception {
String ddl =
"create virtual procedure vproc (x integer) returns integer as begin declare exception e = sqlexception 'hello'; raise sqlwarning e; raise sqlexception 'hello world' sqlstate 'abc', 1 chain e; end;";
TransformationMetadata tm = TestProcedureResolving.createMetadata(ddl);
String sql = "call vproc(1)"; //$NON-NLS-1$
ProcessorPlan plan = getProcedurePlan(sql, tm);
HardcodedDataManager dataManager = new HardcodedDataManager(tm);
try {
helpTestProcess(plan, null, dataManager, tm);
fail();
} catch (TeiidProcessingException e) {
TeiidSQLException tsw = (TeiidSQLException) plan.getContext().getAndClearWarnings().get(0);
assertEquals("hello", tsw.getMessage());
assertEquals(e.getCause().getCause(), tsw);
TeiidSQLException tse = (TeiidSQLException)e.getCause();
assertEquals("hello world", tse.getMessage());
assertEquals("abc", tse.getSQLState());
assertEquals(1, tse.getErrorCode());
}
}
@Test public void testExceptionGroup() throws Exception {
String ddl =
"create virtual procedure vproc () returns string as begin select 1/0; exception e \"return\" = e.state || ' ' || e.errorcode || ' ' || e.teiidcode || ' ' || cast(e.exception as string) || ' ' || cast(e.chain as string); end;";
TransformationMetadata tm = TestProcedureResolving.createMetadata(ddl);
String sql = "call vproc()"; //$NON-NLS-1$
ProcessorPlan plan = getProcedurePlan(sql, tm);
HardcodedDataManager dataManager = new HardcodedDataManager(tm);
helpTestProcess(plan, new List[] {Arrays.asList("50000 30328 TEIID30328 org.teiid.jdbc.TeiidSQLException: TEIID30328 Unable to evaluate (1 / 0): TEIID30384 Error while evaluating function / org.teiid.api.exception.query.ExpressionEvaluationException: TEIID30328 Unable to evaluate (1 / 0): TEIID30384 Error while evaluating function /")}, dataManager, tm);
}
@Test public void testExceptionHandling() throws Exception {
String ddl =
"create virtual procedure vproc (x integer) returns integer as begin " +
"raise sqlexception 'hello world' sqlstate 'abc', 1;" +
"exception e " +
"raise sqlwarning sqlexception 'caught' chain e.exception; " +
"\"return\" = 1;"+
"end;";
TransformationMetadata tm = TestProcedureResolving.createMetadata(ddl);
String sql = "call vproc(1)"; //$NON-NLS-1$
ProcessorPlan plan = getProcedurePlan(sql, tm);
HardcodedDataManager dataManager = new HardcodedDataManager(tm);
helpTestProcess(plan, new List[] {Arrays.asList(1)}, dataManager, tm);
TeiidSQLException tse = (TeiidSQLException)plan.getContext().getAndClearWarnings().get(0);
assertEquals("caught", tse.getMessage());
assertEquals("hello world", tse.getCause().getMessage());
}
/**
* ensures that a processing error is trappable
*/
@Test public void testExceptionHandlingWithResultSet() throws Exception {
String ddl =
"create virtual procedure proc2 (x integer) as begin atomic select 1; begin select 1/x; end exception e end;";
TransformationMetadata tm = TestProcedureResolving.createMetadata(ddl);
String sql = "call proc2(0)"; //$NON-NLS-1$
ProcessorPlan plan = getProcedurePlan(sql, tm);
HardcodedDataManager dataManager = new HardcodedDataManager(tm);
helpTestProcess(plan, new List[] {Arrays.asList(1)}, dataManager, tm);
}
/**
* ensures that the whole result is formed so that the error does not escape the handler
*/
@Test public void testExceptionHandlingWithResultSet1() throws Exception {
String ddl =
"create virtual procedure proc2 (x integer) as begin create local temporary table t (i integer); insert into t (i) values (1); declare integer y = 0; while (y < 16) begin insert into t (i) select 1 from t; y = y+1; end insert into t (i) values (0); select cast(1/i as string) from t; exception e end;";
TransformationMetadata tm = TestProcedureResolving.createMetadata(ddl);
String sql = "call proc2(0)"; //$NON-NLS-1$
ProcessorPlan plan = getProcedurePlan(sql, tm);
HardcodedDataManager dataManager = new HardcodedDataManager(tm);
helpTestProcess(plan, new List[0], dataManager, tm);
}
@Test public void testExceptionHandlingWithDynamic() throws Exception {
String ddl =
"create virtual procedure vproc (x integer) returns integer as begin " +
"raise sqlexception 'hello world' sqlstate 'abc', 5;" +
"exception e " +
"execute immediate 'select \"ERRORCODE\"' as x integer into #temp; " +
"\"return\" = (select x from #temp);"+
"end;";
TransformationMetadata tm = TestProcedureResolving.createMetadata(ddl);
String sql = "call vproc(1)"; //$NON-NLS-1$
ProcessorPlan plan = getProcedurePlan(sql, tm);
HardcodedDataManager dataManager = new HardcodedDataManager(tm);
helpTestProcess(plan, new List[] {Arrays.asList(5)}, dataManager, tm);
}
}