|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * This program and the accompanying materials are made available under the |
| 4 | + * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
| 5 | + * which accompanies this distribution. |
| 6 | + * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * and the Eclipse Distribution License is available at |
| 8 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Oracle - Initial implementation |
| 12 | + ******************************************************************************/ |
| 13 | +package dbws.testing.plsqlrecord; |
| 14 | + |
| 15 | +import static org.junit.Assert.assertNotNull; |
| 16 | +import static org.junit.Assert.assertTrue; |
| 17 | + |
| 18 | +import java.io.StringReader; |
| 19 | +import java.sql.SQLException; |
| 20 | +import java.sql.Statement; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +//java eXtension imports |
| 25 | +import javax.wsdl.WSDLException; |
| 26 | + |
| 27 | +//EclipseLink imports |
| 28 | +import org.eclipse.persistence.internal.xr.Invocation; |
| 29 | +import org.eclipse.persistence.internal.xr.Operation; |
| 30 | +import org.eclipse.persistence.oxm.XMLMarshaller; |
| 31 | +//JUnit4 imports |
| 32 | +import org.junit.AfterClass; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.Test; |
| 35 | +import org.w3c.dom.Document; |
| 36 | + |
| 37 | +//test imports |
| 38 | +import dbws.testing.DBWSTestSuite; |
| 39 | + |
| 40 | +public class PLSQLTypeReturnTestSuite extends DBWSTestSuite { |
| 41 | + static final String EMPREC_TYPE = "TYPE EMP_RECORD_PACKAGE_EMPREC"; |
| 42 | + |
| 43 | + static final String CREATE_EMPTYPE_TABLE = "CREATE TABLE EMPTYPEX (" + "\nEMPNO NUMERIC(4) NOT NULL," |
| 44 | + + "\nENAME VARCHAR(25)," + "\nPRIMARY KEY (EMPNO)" + "\n)"; |
| 45 | + static final String[] POPULATE_EMPTYPE_TABLE = new String[] { |
| 46 | + "INSERT INTO EMPTYPEX (EMPNO, ENAME) VALUES (69, 'Holly')", |
| 47 | + "INSERT INTO EMPTYPEX (EMPNO, ENAME) VALUES (70, 'Brooke')", |
| 48 | + "INSERT INTO EMPTYPEX (EMPNO, ENAME) VALUES (71, 'Patty')" }; |
| 49 | + static final String DROP_EMPTYPE_TABLE = "DROP TABLE EMPTYPEX"; |
| 50 | + |
| 51 | + static final String CREATE_EMP_RECORD_PACKAGE = "create or replace PACKAGE EMP_RECORD_PACKAGE IS \r\n" |
| 52 | + + "function get_emp_record (l_empno EMPTYPEX.EMPNO%TYPE) return EMPTYPEX.ENAME%TYPE;\r\n" |
| 53 | + + "END EMP_RECORD_PACKAGE;"; |
| 54 | + static final String DROP_EMP_RECORD_PACKAGE = "DROP PACKAGE EMP_RECORD_PACKAGE"; |
| 55 | + |
| 56 | + static final String DROP_EMP_RECORD_PACKAGE_BODY = "DROP PACKAGE BODY EMP_RECORD_PACKAGE"; |
| 57 | + |
| 58 | + static final String CREATE_EMP_RECORD_PACKAGE_BODY = "create or replace PACKAGE BODY EMP_RECORD_PACKAGE IS\r\n" |
| 59 | + + "function get_emp_record (l_empno EMPTYPEX.EMPNO%TYPE) return EMPTYPEX.ENAME%TYPE\r\n" + "is\r\n" |
| 60 | + + "ename_result EMPTYPEX.ENAME%TYPE;\r\n" + "BEGIN\r\n" + "SELECT ENAME into ename_result\r\n" |
| 61 | + + "FROM EMPTYPEX\r\n" + "WHERE \r\n" + "EMPNO = l_empno;\r\n" + "return ename_result;\r\n" + "END;\r\n" |
| 62 | + + "END EMP_RECORD_PACKAGE;"; |
| 63 | + |
| 64 | + static boolean ddlCreate = false; |
| 65 | + static boolean ddlDrop = false; |
| 66 | + static boolean ddlDebug = false; |
| 67 | + |
| 68 | + @BeforeClass |
| 69 | + public static void setUp() throws WSDLException { |
| 70 | + if (conn == null) { |
| 71 | + try { |
| 72 | + conn = buildConnection(); |
| 73 | + } catch (Exception e) { |
| 74 | + e.printStackTrace(); |
| 75 | + } |
| 76 | + } |
| 77 | + String ddlCreateProp = System.getProperty(DATABASE_DDL_CREATE_KEY, DEFAULT_DATABASE_DDL_CREATE); |
| 78 | + if ("true".equalsIgnoreCase(ddlCreateProp)) { |
| 79 | + ddlCreate = true; |
| 80 | + } |
| 81 | + String ddlDropProp = System.getProperty(DATABASE_DDL_DROP_KEY, DEFAULT_DATABASE_DDL_DROP); |
| 82 | + if ("true".equalsIgnoreCase(ddlDropProp)) { |
| 83 | + ddlDrop = true; |
| 84 | + } |
| 85 | + String ddlDebugProp = System.getProperty(DATABASE_DDL_DEBUG_KEY, DEFAULT_DATABASE_DDL_DEBUG); |
| 86 | + if ("true".equalsIgnoreCase(ddlDebugProp)) { |
| 87 | + ddlDebug = true; |
| 88 | + } |
| 89 | + if (ddlCreate) { |
| 90 | + runDdl(conn, CREATE_EMPTYPE_TABLE, ddlDebug); |
| 91 | + try { |
| 92 | + Statement stmt = conn.createStatement(); |
| 93 | + for (int i = 0; i < POPULATE_EMPTYPE_TABLE.length; i++) { |
| 94 | + stmt.addBatch(POPULATE_EMPTYPE_TABLE[i]); |
| 95 | + } |
| 96 | + stmt.executeBatch(); |
| 97 | + } catch (SQLException e) { |
| 98 | + if (ddlDebug) { |
| 99 | + e.printStackTrace(); |
| 100 | + } |
| 101 | + } |
| 102 | + runDdl(conn, CREATE_EMP_RECORD_PACKAGE, ddlDebug); |
| 103 | + runDdl(conn, CREATE_EMP_RECORD_PACKAGE_BODY, ddlDebug); |
| 104 | + } |
| 105 | + DBWS_BUILDER_XML_USERNAME = |
| 106 | + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + |
| 107 | + "<dbws-builder xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" + |
| 108 | + "<properties>" + |
| 109 | + "<property name=\"projectName\">PLSQLRecord</property>" + |
| 110 | + "<property name=\"logLevel\">off</property>" + |
| 111 | + "<property name=\"username\">"; |
| 112 | + DBWS_BUILDER_XML_PASSWORD = |
| 113 | + "</property><property name=\"password\">"; |
| 114 | + DBWS_BUILDER_XML_URL = |
| 115 | + "</property><property name=\"url\">"; |
| 116 | + DBWS_BUILDER_XML_DRIVER = |
| 117 | + "</property><property name=\"driver\">"; |
| 118 | + DBWS_BUILDER_XML_PLATFORM = |
| 119 | + "</property><property name=\"platformClassname\">"; |
| 120 | + DBWS_BUILDER_XML_MAIN = |
| 121 | + "</property>" + |
| 122 | + "</properties>" + |
| 123 | + "<plsql-procedure " + |
| 124 | + "name=\"TestRecWithPercentTypeField\" " + |
| 125 | + "catalogPattern=\"EMP_RECORD_PACKAGE\" " + |
| 126 | + "procedurePattern=\"get_emp_record\" " + |
| 127 | + "/>" + |
| 128 | + "</dbws-builder>"; |
| 129 | + |
| 130 | + builder = null; |
| 131 | + DBWSTestSuite.setUp("."); |
| 132 | + |
| 133 | + // execute shadow type ddl to generate JDBC equivalents of PL/SQL types |
| 134 | + ArrayList<String> ddls = new ArrayList<String>(); |
| 135 | + for (String ddl : builder.getTypeDDL()) { |
| 136 | + ddls.add(ddl); |
| 137 | + } |
| 138 | + // execute the DDLs in order to avoid dependency issues |
| 139 | + executeDDLForString(ddls, EMPREC_TYPE); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Execute the DDL in the provided list containing the given DDL string. |
| 144 | + * |
| 145 | + */ |
| 146 | + protected static void executeDDLForString(List<String> ddls, String ddlString) { |
| 147 | + for (int i = 0; i < ddls.size(); i++) { |
| 148 | + String ddl = ddls.get(i); |
| 149 | + if (ddl.contains(ddlString)) { |
| 150 | + runDdl(conn, ddl, ddlDebug); |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @AfterClass |
| 157 | + public static void tearDown() { |
| 158 | + if (ddlDrop) { |
| 159 | + runDdl(conn, DROP_EMP_RECORD_PACKAGE_BODY, ddlDebug); |
| 160 | + runDdl(conn, DROP_EMP_RECORD_PACKAGE, ddlDebug); |
| 161 | + runDdl(conn, DROP_EMPTYPE_TABLE, ddlDebug); |
| 162 | + |
| 163 | + // drop shadow type ddl |
| 164 | + for (String ddl : builder.getTypeDropDDL()) { |
| 165 | + // may need to strip off trailing ';' |
| 166 | + try { |
| 167 | + int lastIdx = ddl.lastIndexOf(";"); |
| 168 | + if (lastIdx == (ddl.length() - 1)) { |
| 169 | + ddl = ddl.substring(0, ddl.length() - 1); |
| 170 | + } |
| 171 | + } catch (Exception xxx) { |
| 172 | + } |
| 173 | + runDdl(conn, ddl, ddlDebug); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testRecordWithPercentTypeField() { |
| 180 | + Invocation invocation = new Invocation("TestRecWithPercentTypeField"); |
| 181 | + invocation.setParameter("EMPNO", 69); |
| 182 | + Operation op = xrService.getOperation(invocation.getName()); |
| 183 | + Object result = op.invoke(xrService, invocation); |
| 184 | + assertNotNull("result is null", result); |
| 185 | + Document doc = xmlPlatform.createDocument(); |
| 186 | + XMLMarshaller marshaller = xrService.getXMLContext().createMarshaller(); |
| 187 | + marshaller.marshal(result, doc); |
| 188 | + Document controlDoc = xmlParser.parse(new StringReader(EMPREC_XML)); |
| 189 | + assertTrue("Expected:\n" + documentToString(controlDoc) + "\nActual:\n" + documentToString(doc), |
| 190 | + comparer.isNodeEqual(controlDoc, doc)); |
| 191 | + } |
| 192 | + |
| 193 | + public static final String EMPREC_XML = "<emp_record_package_emprecType xmlns=\"urn:PLSQLRecord\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" |
| 194 | + + "<emp_id>69</emp_id>" + "<emp_name>Holly</emp_name>" + "</emp_record_package_emprecType>"; |
| 195 | + |
| 196 | +} |
0 commit comments