JAVA-29238 | fixing tests (#15434)

This commit is contained in:
Gaetano Piazzolla 2023-12-18 22:11:10 +01:00 committed by GitHub
parent 94fdc2f4bc
commit 357494e56f
24 changed files with 226 additions and 192 deletions

View File

@ -20,6 +20,6 @@ After importing the project into Eclipse, you may see the following error:
"No persistence xml file found in project"
This can be ignored:
- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
- Project -> Properties -> Java Persistence -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
Or:
- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator

View File

@ -0,0 +1,21 @@
package com.baeldung.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.baeldung.jpa.JpaApplication;
import com.baeldung.boot.daos.impl.ExtendedRepositoryImpl;
@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class, basePackages = "com.baeldung.boot.daos")
@EntityScan({"com.baeldung.boot.domain"})
@ComponentScan("com.baeldung.boot.daos")
public class BootApplication {
public static void main(String[] args) {
SpringApplication.run(JpaApplication.class, args);
}
}

View File

@ -1,17 +1,17 @@
package com.baeldung;
package com.baeldung.jpa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import com.baeldung.boot.daos.impl.ExtendedRepositoryImpl;
@SpringBootApplication
@EnableJpaRepositories(repositoryBaseClass = ExtendedRepositoryImpl.class)
public class Application {
@ComponentScan("com.baeldung.jpa")
@EnableJpaRepositories("com.baeldung.jpa.repository")
public class JpaApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(JpaApplication.class, args);
}
}

View File

@ -1,35 +1,35 @@
package com.baeldung.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import com.baeldung.entity.Fruit;
@Configuration
public class JpaPopulators {
@Bean
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception {
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") });
return factory;
}
@Bean
public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
unmarshaller.setClassesToBeBound(Fruit.class);
UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
factory.setUnmarshaller(unmarshaller);
factory.setResources(new Resource[] { new ClassPathResource("apple-fruit-data.xml"), new ClassPathResource("guava-fruit-data.xml") });
return factory;
}
}
package com.baeldung.jpa.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
import com.baeldung.jpa.domain.Fruit;
@Configuration
public class JpaPopulators {
@Bean
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception {
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") });
return factory;
}
@Bean
public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
unmarshaller.setClassesToBeBound(Fruit.class);
UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
factory.setUnmarshaller(unmarshaller);
factory.setResources(new Resource[] { new ClassPathResource("apple-fruit-data.xml"), new ClassPathResource("guava-fruit-data.xml") });
return factory;
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.repository;
package com.baeldung.jpa.config;
import java.util.Properties;
@ -6,12 +6,10 @@ import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -23,24 +21,18 @@ import com.google.common.base.Preconditions;
@Configuration
@PropertySource("classpath:persistence.properties")
@ComponentScan("com.baeldung.repository")
//@ImportResource("classpath*:*springDataConfig.xml")
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.baeldung.repository")
//@ImportResource("classpath*:*springDataConfig.xml")
public class PersistenceConfig {
@Autowired
private Environment env;
public PersistenceConfig() {
super();
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("com.baeldung.spring.data.persistence.repository");
em.setPackagesToScan("com.baeldung.jpa.domain");
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);

View File

@ -1,4 +1,4 @@
package com.baeldung.repository;
package com.baeldung.jpa.domain;
import java.io.Serializable;

View File

@ -1,40 +1,40 @@
package com.baeldung.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@Entity
public class Fruit {
@Id
private long id;
private String name;
private String color;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
package com.baeldung.jpa.domain;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@Entity
public class Fruit {
@Id
private long id;
private String name;
private String color;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.entity;
package com.baeldung.jpa.domain;
import javax.persistence.Basic;
import javax.persistence.Column;

View File

@ -1,4 +1,4 @@
package com.baeldung.entity;
package com.baeldung.jpa.domain;
import javax.persistence.Column;
import javax.persistence.Entity;

View File

@ -1,27 +1,27 @@
package com.baeldung.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.baeldung.entity.Fruit;
@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
Long deleteByName(String name);
List<Fruit> deleteByColor(String color);
Long removeByName(String name);
List<Fruit> removeByColor(String color);
@Modifying
@Query("delete from Fruit f where f.name=:name or f.color=:color")
int deleteFruits(@Param("name") String name, @Param("color") String color);
}
package com.baeldung.jpa.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.baeldung.jpa.domain.Fruit;
@Repository
public interface FruitRepository extends JpaRepository<Fruit, Long> {
Long deleteByName(String name);
List<Fruit> deleteByColor(String color);
Long removeByName(String name);
List<Fruit> removeByColor(String color);
@Modifying
@Query("delete from Fruit f where f.name=:name or f.color=:color")
int deleteFruits(@Param("name") String name, @Param("color") String color);
}

View File

@ -1,9 +1,10 @@
package com.baeldung.repository;
package com.baeldung.jpa.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.baeldung.jpa.domain.Foo;
public interface IFooDAO extends JpaRepository<Foo, Long> {

View File

@ -1,13 +1,13 @@
package com.baeldung.repository;
package com.baeldung.jpa.repository;
import com.baeldung.entity.Passenger;
import com.baeldung.jpa.domain.Passenger;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
interface PassengerRepository extends JpaRepository<Passenger, Long> {
public interface PassengerRepository extends JpaRepository<Passenger, Long> {
List<Passenger> findByFirstNameIgnoreCase(String firstName);

View File

@ -1,11 +1,11 @@
package com.baeldung.repository;
package com.baeldung.jpa.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.baeldung.entity.Song;
import com.baeldung.jpa.domain.Song;
@Repository
public interface SongRepository extends JpaRepository<Song, Long> {

View File

@ -1,10 +1,14 @@
package com.baeldung.repository;
package com.baeldung.jpa.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baeldung.jpa.domain.Foo;
import com.baeldung.jpa.repository.IFooDAO;
@Service
public class FooService implements IFooService {
@Autowired
private IFooDAO dao;

View File

@ -0,0 +1,7 @@
package com.baeldung.jpa.service;
import com.baeldung.jpa.domain.Foo;
public interface IFooService {
Foo create(Foo foo);
}

View File

@ -1,5 +0,0 @@
package com.baeldung.repository;
public interface IFooService {
Foo create(Foo foo);
}

View File

@ -1,12 +1,12 @@
[
{
"_class": "com.baeldung.entity.Fruit",
"_class": "com.baeldung.jpa.domain.Fruit",
"name": "apple",
"color": "red",
"id": 1
},
{
"_class": "com.baeldung.entity.Fruit",
"_class": "com.baeldung.jpa.domain.Fruit",
"name": "guava",
"color": "green",
"id": 2

View File

@ -7,5 +7,5 @@
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
>
<jpa:repositories base-package="com.baeldung.repository"/>
<jpa:repositories base-package="com.baeldung.jpa.repository"/>
</beans>

View File

@ -13,11 +13,11 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.Application;
import com.baeldung.boot.BootApplication;
import com.baeldung.boot.domain.Student;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {Application.class})
@ContextConfiguration(classes = { BootApplication.class})
@DirtiesContext
public class ExtendedStudentRepositoryIntegrationTest {
@Resource

View File

@ -15,11 +15,11 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import com.baeldung.Application;
import com.baeldung.boot.BootApplication;
import com.baeldung.boot.domain.MerchandiseEntity;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@SpringBootTest(classes = BootApplication.class)
public class InventoryRepositoryIntegrationTest {
private static final String ORIGINAL_TITLE = "Pair of Pants";

View File

@ -1,4 +1,4 @@
package com.baeldung.repository;
package com.baeldung.jpa;
import javax.sql.DataSource;
@ -6,11 +6,16 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.jpa.domain.Foo;
import com.baeldung.jpa.service.IFooService;
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = PersistenceConfig.class)
@ContextConfiguration(classes = { JpaApplication.class})
@DirtiesContext
public class FooServiceIntegrationTest {
@Autowired

View File

@ -1,38 +1,39 @@
package com.baeldung.repository;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.entity.Fruit;
@RunWith(SpringRunner.class)
@SpringBootTest
public class FruitPopulatorIntegrationTest {
@Autowired
private FruitRepository fruitRepository;
@Test
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() {
List<Fruit> fruits = fruitRepository.findAll();
assertEquals("record count is not matching", 2, fruits.size());
fruits.forEach(fruit -> {
if (1 == fruit.getId()) {
assertEquals("apple", fruit.getName());
assertEquals("red", fruit.getColor());
} else if (2 == fruit.getId()) {
assertEquals("guava", fruit.getName());
assertEquals("green", fruit.getColor());
}
});
}
}
package com.baeldung.jpa;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.jpa.domain.Fruit;
import com.baeldung.jpa.repository.FruitRepository;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = JpaApplication.class)
public class FruitPopulatorIntegrationTest {
@Autowired
private FruitRepository fruitRepository;
@Test
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() {
List<Fruit> fruits = fruitRepository.findAll();
assertEquals("record count is not matching", 2, fruits.size());
fruits.forEach(fruit -> {
if (1 == fruit.getId()) {
assertEquals("apple", fruit.getName());
assertEquals("red", fruit.getColor());
} else if (2 == fruit.getId()) {
assertEquals("guava", fruit.getName());
assertEquals("green", fruit.getColor());
}
});
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.repository;
package com.baeldung.jpa;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
@ -8,18 +8,22 @@ import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.entity.Passenger;
import com.baeldung.jpa.domain.Passenger;
import com.baeldung.jpa.repository.PassengerRepository;
@DataJpaTest(showSql = false)
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { JpaApplication.class})
@DirtiesContext
public class PassengerRepositoryIntegrationTest {
@PersistenceContext
@ -28,6 +32,7 @@ public class PassengerRepositoryIntegrationTest {
private PassengerRepository repository;
@Before
@Transactional
public void before() {
entityManager.persist(Passenger.from("Jill", "Smith"));
entityManager.persist(Passenger.from("Eve", "Jackson"));
@ -36,6 +41,7 @@ public class PassengerRepositoryIntegrationTest {
entityManager.persist(Passenger.from("Siya", "Kolisi"));
}
@Transactional
@Test
public void givenPassengers_whenMatchingIgnoreCase_thenExpectedReturned() {
Passenger jill = Passenger.from("Jill", "Smith");

View File

@ -1,4 +1,4 @@
package com.baeldung.repository;
package com.baeldung.jpa;
import static org.junit.Assert.assertEquals;
@ -7,17 +7,19 @@ import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import com.baeldung.entity.Song;
import com.baeldung.repository.SongRepository;
import com.baeldung.jpa.domain.Song;
import com.baeldung.jpa.repository.SongRepository;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = { JpaApplication.class })
@Sql(scripts = { "/test-song-data.sql" })
@DirtiesContext
public class SongRepositoryIntegrationTest {
@Autowired private SongRepository songRepository;