1

I'm trying to make unit tests work in H2 for the DB2 function TIMESTAMP(Date, int) which sets the precision for the timestamp.

A similar question led me to http://www.h2database.com/html/features.html#user_defined_functions but I'm not sure how the overloading would work in this scenario.

Can anyone give me an example?

( Also, please don't answer just to say that having different databases for testing and production is bad practice. It's not something I'll be able to change :/ )

  • Some examples are examined in [*Stored Procedure in H2 Database*](https://stackoverflow.com/q/11718865/230513). – trashgod Sep 04 '17 at 18:55

1 Answers1

0

hi i had a native query with VALUE and INT functions i did the following steps:

1) Add on before the custom functions

 @Before
public void addCustomFunctions() throws Exception{

    Context context = new InitialContext();
    DataSource dataSource =(DataSource) context.lookup("java:jboss/datasources/ejb3-junit-arguillian-exampleTestDS");

    Connection conn = dataSource.getConnection("sa","sa");
    Statement st = conn.createStatement();
    st.execute("CREATE ALIAS INT FOR \"com.ec.custom.IntFunction.transformInt\"");


}

2) Add the class with a static public method

public class IntFunction {

public static Integer transformInt(Object object) {
    Integer result = 0;
    result = Integer.parseInt(object.toString());
    return result;
}

}

3) The jndi for the datasource must exist on

java:jboss/datasources/ejb3-junit-arguillian-exampleTestDS

my persistence.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!--
    JBoss, Home of Professional Open Source
    Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
    contributors by the @authors tag. See the copyright.txt in the
    distribution for a full listing of individual contributors.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="primary">
        <!-- We use a different datasource for tests, so as to not overwrite
           production data. This is an unmanaged data source, backed by H2, an in memory
           database. Production applications should use a managed datasource. -->
        <!-- The datasource is deployed as WEB-INF/test-ds.xml,
           you can find it in the source at src/test/resources/test-ds.xml -->
        <jta-data-source>java:jboss/datasources/ejb3-junit-arguillian-exampleTestDS</jta-data-source>
        <properties>
            <!-- Properties for Hibernate -->
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.show_sql" value="false" />
        </properties>
    </persistence-unit>
</persistence>

You can follow the guide of arquillian its what i used.

https://github.com/monk8800/ejb3-junit-arguillian-example

cabaji99
  • 1,305
  • 11
  • 9