/******************************************************************************* * Copyright (c) 2011, 2015 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 v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution. * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: * David McCann - July 2013 - Initial Implementation ******************************************************************************/ package org.eclipse.persistence.tools.metadata.generation.test.functiontype; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.DATABASE_DDL_CREATE_KEY; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.DATABASE_DDL_DEBUG_KEY; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.DATABASE_DDL_DROP_KEY; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.DEFAULT_DATABASE_DDL_CREATE; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.DEFAULT_DATABASE_DDL_DEBUG; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.DEFAULT_DATABASE_DDL_DROP; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.DEFAULT_PACKAGE_NAME; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.comparer; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.conn; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.databasePlatform; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.documentToString; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.removeEmptyTextNodes; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.runDdl; import static org.eclipse.persistence.tools.metadata.generation.test.AllTests.xmlParser; //javase imports import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.StringReader; import java.sql.SQLException; import java.util.List; import org.eclipse.persistence.internal.jpa.metadata.xml.XMLEntityMappings; import org.eclipse.persistence.internal.jpa.metadata.xml.XMLEntityMappingsWriter; import org.eclipse.persistence.tools.metadata.generation.JPAMetadataGenerator; import org.eclipse.persistence.tools.metadata.generation.test.AllTests; import org.eclipse.persistence.tools.oracleddl.parser.ParseException; import org.eclipse.persistence.tools.oracleddl.util.DatabaseTypeBuilder; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.w3c.dom.Document; /** * Tests metadata generation from a FunctionType. * */ public class FunctionTypeTestSuite { static final String CREATE_FINDMAXSALFORDEPT_FUNC = "CREATE OR REPLACE FUNCTION FINDMAXSALFORDEPT(DEPT IN DECIMAL) RETURN DECIMAL AS" + "\nMAXSAL DECIMAL(7,2);" + "\nBEGIN" + "\nMAXSAL := 123456.66;" + "\nRETURN(MAXSAL);" + "\nEND FINDMAXSALFORDEPT;"; static final String DROP_FINDMAXSALFORDEPT_FUNC = "DROP FUNCTION FINDMAXSALFORDEPT"; static boolean ddlCreate = false; static boolean ddlDrop = false; static boolean ddlDebug = false; @SuppressWarnings("rawtypes") static List dbProcedures; static DatabaseTypeBuilder dbTypeBuilder; @BeforeClass public static void setUp() throws ClassNotFoundException, SQLException { AllTests.setUp(); String ddlCreateProp = System.getProperty(DATABASE_DDL_CREATE_KEY, DEFAULT_DATABASE_DDL_CREATE); if ("true".equalsIgnoreCase(ddlCreateProp)) { ddlCreate = true; } String ddlDropProp = System.getProperty(DATABASE_DDL_DROP_KEY, DEFAULT_DATABASE_DDL_DROP); if ("true".equalsIgnoreCase(ddlDropProp)) { ddlDrop = true; } String ddlDebugProp = System.getProperty(DATABASE_DDL_DEBUG_KEY, DEFAULT_DATABASE_DDL_DEBUG); if ("true".equalsIgnoreCase(ddlDebugProp)) { ddlDebug = true; } if (ddlCreate) { runDdl(conn, CREATE_FINDMAXSALFORDEPT_FUNC, ddlDebug); } // use DatabaseTypeBuilder to generate a list of ProcedureTypes dbTypeBuilder = new DatabaseTypeBuilder(); try { dbProcedures = dbTypeBuilder.buildFunctions(conn, "TOPLEVEL", "FINDMAXSALFORDEPT"); } catch (ParseException e) { e.printStackTrace(); } } @AfterClass public static void tearDown() { if (ddlDrop) { runDdl(conn, DROP_FINDMAXSALFORDEPT_FUNC, ddlDebug); } } @Test @SuppressWarnings({ "unchecked" }) public void testJPAFunctionMetadata() { if (dbProcedures == null || dbProcedures.isEmpty()) { fail("No types were generated."); } XMLEntityMappings mappings = null; try { JPAMetadataGenerator gen = new JPAMetadataGenerator(DEFAULT_PACKAGE_NAME, databasePlatform); mappings = gen.generateXmlEntityMappings(dbProcedures); } catch (Exception x) { fail("An unexpected exception occurred: " + x.getMessage()); } if (mappings == null) { fail("No JPA metadata was generated"); } ByteArrayOutputStream metadata = new ByteArrayOutputStream(); XMLEntityMappingsWriter.write(mappings, metadata); Document testDoc = xmlParser.parse(new StringReader(metadata.toString())); removeEmptyTextNodes(testDoc); Document controlDoc = xmlParser.parse(new StringReader(functionMetadata)); removeEmptyTextNodes(controlDoc); assertTrue("Metadata comparison failed. Expected:\n" + documentToString(controlDoc) + "\nActual\n" + documentToString(testDoc), comparer.isNodeEqual(controlDoc, testDoc)); } static final String functionMetadata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<orm:entity-mappings xsi:schemaLocation=\"http://www.eclipse.org/eclipselink/xsds/persistence/orm org/eclipse/persistence/jpa/eclipselink_orm_2_5.xsd\"" + " xmlns:orm=\"http://www.eclipse.org/eclipselink/xsds/persistence/orm\" " + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" + " <orm:named-stored-function-query name=\"FINDMAXSALFORDEPT\" function-name=\"FINDMAXSALFORDEPT\">\n" + " <orm:parameter mode=\"IN\" name=\"DEPT\" type=\"java.math.BigDecimal\" class=\"java.math.BigDecimal\" jdbc-type=\"3\" jdbc-type-name=\"DECIMAL\"/>\n" + " <orm:return-parameter type=\"java.math.BigDecimal\" class=\"java.math.BigDecimal\" jdbc-type=\"3\" jdbc-type-name=\"DECIMAL\"/>\n" + " </orm:named-stored-function-query>\n" + "</orm:entity-mappings>"; }