Skip to content

Commit

Permalink
Springboot test - Getting "Could not open JPA EntityManager" because …
Browse files Browse the repository at this point in the history
…of "Connection is not available, request timed out after 30000ms" with HikariCP Datasource in Spring eclipse-ee4j#2219

Signed-off-by: Radek Felcman <[email protected]>
  • Loading branch information
rfelcman committed Sep 18, 2024
1 parent 4a7149f commit a6011b2
Show file tree
Hide file tree
Showing 9 changed files with 545 additions and 1 deletion.
257 changes: 257 additions & 0 deletions jpa/eclipselink.jpa.springboot.test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 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
-->

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>

<name>EclipseLink JPA Spring Test</name>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.springboot.test</artifactId>
<packaging>jar</packaging>

<parent>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.parent</artifactId>
<version>5.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<properties>
<comp.xlint>-Xlint:all</comp.xlint>
<comp.xdoclint>-Xdoclint:all,-missing</comp.xdoclint>
<comp.test.xlint>-Xlint:all</comp.test.xlint>

<!--Properties used for test resources filtering/replacement-->
<!--DB connection properties-->
<dbPlatform>${db.platform}</dbPlatform>
<dbUser>${db.user}</dbUser>
<dbPassword>${db.pwd}</dbPassword>
<driverClass>${db.driver}</driverClass>
<dbURL>${db.url}</dbURL>
<!--Log level-->
<loglevel>${logging.level}</loglevel>

<test-skip-jpa-spring>${skipTests}</test-skip-jpa-spring> <!-- Spring 4 is not supported with jakarta.persistence -->
</properties>

<dependencies>
<!--
&lt;!&ndash;Test dependencies&ndash;&gt;
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
-->
<!--
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<scope>test</scope>
</dependency>
<!--Other modules-->
<!--EclipseLink JPA-->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa</artifactId>
<scope>test</scope>
</dependency>
<!--EclipseLink JPA test framework-->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.test.framework</artifactId>
<scope>test</scope>
</dependency>
<!--API dependencies-->
<dependency>
<groupId>jakarta.transaction</groupId>
<artifactId>jakarta.transaction-api</artifactId>
<scope>test</scope>
</dependency>
<!--Other libraries dependencies-->
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>test</scope>
<!--TODO RFELCMAN move later to parent pom-->
<version>${springboot.version}</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</exclusion>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!--TODO RFELCMAN move later to parent pom-->
<version>${springboot.version}</version>
</dependency>

<!--JDBC driver (test dependency)-->
<dependency>
<groupId>${db.driver.groupId}</groupId>
<artifactId>${db.driver.artifactId}</artifactId>
<version>${db.driver.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<!--Filtering enables generate/substitute test properties from Maven into *.xml files.-->
<testResources>
<testResource>
<directory>${integration.test.resources.directory}</directory>
<filtering>true</filtering>
</testResource>
</testResources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<!--TODO Update project/module files to use standard pattern @*@ -->
<delimiter>%%*%%</delimiter>
</delimiters>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
<!--Required for EL Weaving (-javaagent:${org.eclipse.persistence:org.eclipse.persistence.jpa:jar})-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<!--Resolve dependencies into Maven properties like ${org.eclipse.persistence:org.eclipse.persistence.jpa:jar} for JPA module-->
<execution>
<id>get-test-classpath-to-properties</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<!--
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.3.3</version>
</plugin>
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<workingDirectory>${project.build.directory}/test-run</workingDirectory>
<!--Set system properties required for tests-->
<systemPropertiesFile>${test.properties.file}</systemPropertiesFile>
</configuration>
<executions>
<execution>
<id>test-jpa-springboot</id>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<!--EclipseLink Weaving is enabled.
When it will be disabled by -Dignore it leads into test errors
...SpringJunitTestCase.testAddressVH:182 Exception when Address value holder retrieved-->
<!--<argLine>-Dignore</argLine>-->
<argLine>-javaagent:${org.eclipse.persistence:org.eclipse.persistence.jpa:jar}</argLine>
<skipTests>${test-skip-jpa-spring}</skipTests>
<reportNameSuffix>test-jpa-spring</reportNameSuffix>
<includes>
<include>org.eclipse.persistence.testing.tests.jpa.springboot.TestPersonRepository</include>
</includes>
</configuration>
</execution>
<execution>
<id>verify-integration-tests</id>
<goals>
<goal>verify</goal>
</goals>
<configuration>
<skip>${integration.test.skip.verify}</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.carlspring.maven</groupId>
<artifactId>derby-maven-plugin</artifactId>
<executions>
<execution>
<id>start-derby</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-derby</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>oracle</id>
<dependencies>
<!--db.platform=org.eclipse.persistence.platform.database.oracle.Oracle12Platform comes from there-->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.oracle</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>test-jpa-spring</id>
<properties>
<test-skip-jpa-spring>false</test-skip-jpa-spring>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 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.jpa.springboot;

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class EclipseLinkJpaConfiguration extends JpaBaseConfiguration {

protected EclipseLinkJpaConfiguration(DataSource dataSource, JpaProperties properties, ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
super(dataSource, properties, jtaTransactionManager);
}

@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
return new EclipseLinkJpaVendorAdapter();
}

@Override
protected Map<String, Object> getVendorProperties() {
HashMap<String, Object> map = new HashMap<>();
map.put(PersistenceUnitProperties.WEAVING, detectWeavingMode());
map.put(PersistenceUnitProperties.DDL_GENERATION, "drop-and-create-tables");
return map;
}

private String detectWeavingMode() {
return InstrumentationLoadTimeWeaver.isInstrumentationAvailable() ? "true" : "static";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 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.jpa.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories("test.org.eclipse.persistence.testing.models.jpa.springboot")
public class PersonApplication {

public static void main(String[] args) {
SpringApplication.run(PersonApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 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.jpa.springboot;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ContextConfiguration;
import test.org.eclipse.persistence.testing.models.jpa.springboot.Person;
import test.org.eclipse.persistence.testing.models.jpa.springboot.PersonRepository;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ContextConfiguration(classes = { PersonApplication.class, EclipseLinkJpaConfiguration.class })
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class TestPersonRepository {

private final Person INITIAL_PERSON = new Person(1, "John", "Doe");

@Autowired
private PersonRepository personRepository;

@Test
public void testPerson() {
Person person = personRepository.findPersonById(1);
assertEquals(INITIAL_PERSON, person);
}

@BeforeEach
public void setUp() {
personRepository.deleteAll();
personRepository.save(INITIAL_PERSON);
}

@AfterEach
public void tearDown() {
personRepository.deleteAll();
}
}
Loading

0 comments on commit a6011b2

Please sign in to comment.