Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,44 @@
package com.baeldung.jpa.criteria;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import java.util.List;
import org.junit.Test;
import com.baeldung.jpa.criteria.Item;
import com.baeldung.jpa.criteria.CustomItemRepository;
public class CustomItemRepositoryIntegrationTest {
CustomItemRepository customItemRepository = new CustomItemRepositoryImpl();
@Test
public void givenItems_whenFindItemsByColorAndGrade_thenReturnItems() {
List<Item> items = customItemRepository.findItemsByColorAndGrade();
assertFalse("No items found", items.isEmpty());
assertEquals("There should be only one item", 1, items.size());
Item item = items.get(0);
assertEquals("this item do not have blue color", "blue", item.getColor());
assertEquals("this item does not belong to A grade", "A", item.getGrade());
}
@Test
public void givenItems_whenFindItemByColorOrGrade_thenReturnItems() {
List<Item> items = customItemRepository.findItemByColorOrGrade();
assertFalse("No items found", items.isEmpty());
assertEquals("There should be only one item", 1, items.size());
Item item = items.get(0);
assertEquals("this item do not have red color", "red", item.getColor());
assertEquals("this item does not belong to D grade", "D", item.getGrade());
}
}
@@ -0,0 +1,63 @@
package com.baeldung.jpa.defaultvalues;
import com.baeldung.jpa.defaultvalues.User;
import com.baeldung.jpa.defaultvalues.UserRepository;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import static org.junit.Assert.*;
public class UserDefaultValuesUnitTest {
private static UserRepository userRepository = null;
@BeforeClass
public static void once() {
userRepository = new UserRepository();
}
@Test
@Ignore // SQL default values are also defined
public void saveUser_shouldSaveWithDefaultFieldValues() {
User user = new User();
userRepository.save(user, 1L);
user = userRepository.find(1L);
assertEquals(user.getName(), "John Snow");
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
@Test
@Ignore // SQL default values are also defined
public void saveUser_shouldSaveWithNullName() {
User user = new User();
user.setName(null);
userRepository.save(user, 2L);
user = userRepository.find(2L);
assertNull(user.getName());
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
@Test
public void saveUser_shouldSaveWithDefaultSqlValues() {
User user = new User();
userRepository.save(user, 3L);
user = userRepository.find(3L);
assertEquals(user.getName(), "John Snow");
assertEquals(25, (int) user.getAge());
assertFalse(user.getLocked());
}
@AfterClass
public static void destroy() {
userRepository.clean();
}
}
@@ -0,0 +1,79 @@
package com.baeldung.jpa.multipletables;
import static org.assertj.core.api.Assertions.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.baeldung.jpa.multipletables.multipleentities.MealWithMultipleEntities;
import com.baeldung.jpa.multipletables.secondarytable.MealAsSingleEntity;
import com.baeldung.jpa.multipletables.secondarytable.embeddable.MealWithEmbeddedAllergens;
public class MultipleTablesIntegrationTest {
private static EntityManagerFactory emf;
private static EntityManager em;
@BeforeClass
public static void setup() {
emf = Persistence.createEntityManagerFactory("jpa-h2-multipltables");
em = emf.createEntityManager();
}
@Test
public void entityManager_shouldLoadMealAsSingleEntity() {
// given
// when
MealAsSingleEntity meal = em.find(MealAsSingleEntity.class, 1L);
// then
assertThat(meal).isNotNull();
assertThat(meal.getId()).isEqualTo(1L);
assertThat(meal.isPeanuts()).isFalse();
assertThat(meal.isCelery()).isTrue();
}
@Test
public void entityManager_shouldLoadMealWithEmbeddedAllergens() {
// given
// when
MealWithEmbeddedAllergens meal = em.find(MealWithEmbeddedAllergens.class, 1L);
// then
assertThat(meal).isNotNull();
assertThat(meal.getId()).isEqualTo(1L);
assertThat(meal.getAllergens()).isNotNull();
assertThat(meal.getAllergens().isPeanuts()).isFalse();
assertThat(meal.getAllergens().isCelery()).isTrue();
}
@Test
public void entityManager_shouldLoadMealWithAllergensEntity() {
// given
// when
MealWithMultipleEntities meal = em.find(MealWithMultipleEntities.class, 1L);
// then
assertThat(meal).isNotNull();
assertThat(meal.getId()).isEqualTo(1L);
assertThat(meal.getAllergens()).isNotNull();
assertThat(meal.getAllergens().isPeanuts()).isFalse();
assertThat(meal.getAllergens().isCelery()).isTrue();
}
@AfterClass
public static void teardown() {
if (emf != null) {
emf.close();
}
}
}
@@ -0,0 +1,133 @@
package com.baeldung.jpa.projections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class HibernateProjectionsIntegrationTest {
private static Session session;
private static SessionFactory sessionFactory;
private Transaction transaction;
@BeforeClass
public static void init() {
Configuration configuration = getConfiguration();
configuration.addAnnotatedClass(Product.class);
sessionFactory = configuration.buildSessionFactory();
}
@Before
public void before() {
session = sessionFactory.getCurrentSession();
transaction = session.beginTransaction();
}
@After
public void after() {
if(transaction.isActive()) {
transaction.rollback();
}
}
private static Configuration getConfiguration() {
Configuration cfg = new Configuration();
cfg.setProperty(AvailableSettings.DIALECT,
"org.hibernate.dialect.H2Dialect");
cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "none");
cfg.setProperty(AvailableSettings.DRIVER, "org.h2.Driver");
cfg.setProperty(AvailableSettings.URL,
"jdbc:h2:mem:myexceptiondb2;DB_CLOSE_DELAY=-1;;INIT=RUNSCRIPT FROM 'src/test/resources/products.sql'");
cfg.setProperty(AvailableSettings.USER, "sa");
cfg.setProperty(AvailableSettings.PASS, "");
cfg.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");
return cfg;
}
@SuppressWarnings("deprecation")
@Test
public void givenProductData_whenIdAndNameProjectionUsingCriteria_thenListOfObjectArrayReturned() {
Criteria criteria = session.createCriteria(Product.class);
criteria = criteria.setProjection(Projections.projectionList()
.add(Projections.id())
.add(Projections.property("name")));
List<Object[]> resultList = criteria.list();
assertNotNull(resultList);
assertEquals(4, resultList.size());
assertEquals(1L, resultList.get(0)[0]);
assertEquals("Product Name 1", resultList.get(0)[1]);
assertEquals(2L, resultList.get(1)[0]);
assertEquals("Product Name 2", resultList.get(1)[1]);
assertEquals(3L, resultList.get(2)[0]);
assertEquals("Product Name 3", resultList.get(2)[1]);
assertEquals(4L, resultList.get(3)[0]);
assertEquals("Product Name 4", resultList.get(3)[1]);
}
@Test
public void givenProductData_whenNameProjectionUsingCriteria_thenListOfStringReturned() {
Criteria criteria = session.createCriteria(Product.class);
criteria = criteria.setProjection(Projections.property("name"));
List resultList = criteria.list();
assertNotNull(resultList);
assertEquals(4, resultList.size());
assertEquals("Product Name 1", resultList.get(0));
assertEquals("Product Name 2", resultList.get(1));
assertEquals("Product Name 3", resultList.get(2));
assertEquals("Product Name 4", resultList.get(3));
}
@Test
public void givenProductData_whenCountByCategoryUsingCriteria_thenOK() {
Criteria criteria = session.createCriteria(Product.class);
criteria = criteria.setProjection(Projections.projectionList()
.add(Projections.groupProperty("category"))
.add(Projections.rowCount()));
List<Object[]> resultList = criteria.list();
assertNotNull(resultList);
assertEquals(3, resultList.size());
assertEquals("category1", resultList.get(0)[0]);
assertEquals(2L, resultList.get(0)[1]);
assertEquals("category2", resultList.get(1)[0]);
assertEquals(1L, resultList.get(1)[1]);
assertEquals("category3", resultList.get(2)[0]);
assertEquals(1L, resultList.get(2)[1]);
}
@Test
public void givenProductData_whenCountByCategoryWithAliasUsingCriteria_thenOK() {
Criteria criteria = session.createCriteria(Product.class);
criteria = criteria.setProjection(Projections.projectionList()
.add(Projections.groupProperty("category"))
.add(Projections.alias(Projections.rowCount(), "count")));
criteria.addOrder(Order.asc("count"));
List<Object[]> resultList = criteria.list();
assertNotNull(resultList);
assertEquals(3, resultList.size());
assertEquals("category2", resultList.get(0)[0]);
assertEquals(1L, resultList.get(0)[1]);
assertEquals("category3", resultList.get(1)[0]);
assertEquals(1L, resultList.get(1)[1]);
assertEquals("category1", resultList.get(2)[0]);
assertEquals(2L, resultList.get(2)[1]);
}
}
@@ -0,0 +1,105 @@
package com.baeldung.jpa.projections;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
public class ProductRepositoryIntegrationTest {
private static ProductRepository productRepository;
@BeforeClass
public static void once() throws IOException {
productRepository = new ProductRepository();
}
@Test
public void givenProductData_whenIdAndNameProjectionUsingJPQL_thenListOfObjectArrayReturned() {
List<Object[]> resultList = productRepository.findAllIdAndNamesUsingJPQL();
assertNotNull(resultList);
assertEquals(4, resultList.size());
assertEquals(1L, resultList.get(0)[0]);
assertEquals("Product Name 1", resultList.get(0)[1]);
assertEquals(2L, resultList.get(1)[0]);
assertEquals("Product Name 2", resultList.get(1)[1]);
assertEquals(3L, resultList.get(2)[0]);
assertEquals("Product Name 3", resultList.get(2)[1]);
assertEquals(4L, resultList.get(3)[0]);
assertEquals("Product Name 4", resultList.get(3)[1]);
}
@Test
public void givenProductData_whenIdAndNameProjectionUsingCriteriaBuilder_thenListOfObjectArrayReturned() {
List<Object[]> resultList = productRepository.findAllIdAndNamesUsingCriteriaBuilderArray();
assertNotNull(resultList);
assertEquals(4, resultList.size());
assertEquals(1L, resultList.get(0)[0]);
assertEquals("Product Name 1", resultList.get(0)[1]);
assertEquals(2L, resultList.get(1)[0]);
assertEquals("Product Name 2", resultList.get(1)[1]);
assertEquals(3L, resultList.get(2)[0]);
assertEquals("Product Name 3", resultList.get(2)[1]);
assertEquals(4L, resultList.get(3)[0]);
assertEquals("Product Name 4", resultList.get(3)[1]);
}
@SuppressWarnings("rawtypes")
@Test
public void givenProductData_whenNameProjectionUsingJPQL_thenListOfStringReturned() {
List resultList = productRepository.findAllNamesUsingJPQL();
assertNotNull(resultList);
assertEquals(4, resultList.size());
assertEquals("Product Name 1", resultList.get(0));
assertEquals("Product Name 2", resultList.get(1));
assertEquals("Product Name 3", resultList.get(2));
assertEquals("Product Name 4", resultList.get(3));
}
@Test
public void givenProductData_whenNameProjectionUsingCriteriaBuilder_thenListOfStringReturned() {
List<String> resultList = productRepository.findAllNamesUsingCriteriaBuilder();
assertNotNull(resultList);
assertEquals(4, resultList.size());
assertEquals("Product Name 1", resultList.get(0));
assertEquals("Product Name 2", resultList.get(1));
assertEquals("Product Name 3", resultList.get(2));
assertEquals("Product Name 4", resultList.get(3));
}
@Test
public void givenProductData_whenCountByCategoryUsingJPQL_thenOK() {
List<Object[]> resultList = productRepository.findCountByCategoryUsingJPQL();
assertNotNull(resultList);
assertEquals(3, resultList.size());
assertEquals("category1", resultList.get(0)[0]);
assertEquals(2L, resultList.get(0)[1]);
assertEquals("category2", resultList.get(1)[0]);
assertEquals(1L, resultList.get(1)[1]);
assertEquals("category3", resultList.get(2)[0]);
assertEquals(1L, resultList.get(2)[1]);
}
@Test
public void givenProductData_whenCountByCategoryUsingCriteriaBuider_thenOK() {
List<Object[]> resultList = productRepository.findCountByCategoryUsingCriteriaBuilder();
assertNotNull(resultList);
assertEquals(3, resultList.size());
assertEquals("category1", resultList.get(0)[0]);
assertEquals(2L, resultList.get(0)[1]);
assertEquals("category2", resultList.get(1)[0]);
assertEquals(1L, resultList.get(1)[1]);
assertEquals("category3", resultList.get(2)[0]);
assertEquals(1L, resultList.get(2)[1]);
}
}
@@ -0,0 +1,109 @@
package com.baeldung.jpa.queryparams;
import java.util.Arrays;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* JPAQueryParamsTest class tests.
*
* @author gmlopez.mackinnon@gmail.com
*/
public class JPAQueryParamsUnitTest {
private static EntityManager entityManager;
@BeforeClass
public static void setup() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2-queryparams");
entityManager = factory.createEntityManager();
}
@Test
public void givenEmpNumber_whenUsingPositionalParameter_thenReturnExpectedEmployee() {
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber = ?1", Employee.class);
String empNumber = "A123";
Employee employee = query.setParameter(1, empNumber)
.getSingleResult();
Assert.assertNotNull("Employee not found", employee);
}
@Test
public void givenEmpNumberList_whenUsingPositionalParameter_thenReturnExpectedEmployee() {
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber IN (?1)", Employee.class);
List<String> empNumbers = Arrays.asList("A123", "A124");
List<Employee> employees = query.setParameter(1, empNumbers)
.getResultList();
Assert.assertNotNull("Employees not found", employees);
Assert.assertFalse("Employees not found", employees.isEmpty());
}
@Test
public void givenEmpNumber_whenUsingNamedParameter_thenReturnExpectedEmployee() {
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber = :number", Employee.class);
String empNumber = "A123";
Employee employee = query.setParameter("number", empNumber)
.getSingleResult();
Assert.assertNotNull("Employee not found", employee);
}
@Test
public void givenEmpNumberList_whenUsingNamedParameter_thenReturnExpectedEmployee() {
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber IN (:numbers)", Employee.class);
List<String> empNumbers = Arrays.asList("A123", "A124");
List<Employee> employees = query.setParameter("numbers", empNumbers)
.getResultList();
Assert.assertNotNull("Employees not found", employees);
Assert.assertFalse("Employees not found", employees.isEmpty());
}
@Test
public void givenEmpNameAndEmpAge_whenUsingTwoNamedParameters_thenReturnExpectedEmployees() {
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.name = :name AND e.age = :empAge", Employee.class);
String empName = "John Doe";
int empAge = 55;
List<Employee> employees = query.setParameter("name", empName)
.setParameter("empAge", empAge)
.getResultList();
Assert.assertNotNull("Employees not found!", employees);
Assert.assertTrue("Employees not found!", !employees.isEmpty());
}
@Test
public void givenEmpNumber_whenUsingCriteriaQuery_thenReturnExpectedEmployee() {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Employee> cQuery = cb.createQuery(Employee.class);
Root<Employee> c = cQuery.from(Employee.class);
ParameterExpression<String> paramEmpNumber = cb.parameter(String.class);
cQuery.select(c)
.where(cb.equal(c.get(Employee_.empNumber), paramEmpNumber));
TypedQuery<Employee> query = entityManager.createQuery(cQuery);
String empNumber = "A123";
query.setParameter(paramEmpNumber, empNumber);
Employee employee = query.getSingleResult();
Assert.assertNotNull("Employee not found!", employee);
}
@Test
public void givenEmpNumber_whenUsingLiteral_thenReturnExpectedEmployee() {
String empNumber = "A123";
TypedQuery<Employee> query = entityManager.createQuery("SELECT e FROM Employee e WHERE e.empNumber = '" + empNumber + "'", Employee.class);
Employee employee = query.getSingleResult();
Assert.assertNotNull("Employee not found!", employee);
}
}
@@ -0,0 +1,65 @@
package com.baeldung.jpa.querytypes;
import org.junit.Assert;
import org.junit.Test;
/**
* QueryTypesExamples class integration tests.
*
* @author Rodolfo Felipe
*/
public class QueryTypesExamplesIntegrationTest {
QueryTypesExamples userDao = new QueryTypesExamples();
@Test
public void givenUserId_whenCallingPlaingQueryMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithPlainQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithPlainQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
@Test
public void givenUserId_whenCallingTypedQueryMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithTypedQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithTypedQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
@Test
public void givenUserId_whenCallingNamedQueryMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithNamedQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithNamedQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
@Test
public void givenUserId_whenCallingNativeQueryMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithNativeQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithNativeQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
@Test
public void givenUserId_whenCallingCriteriaApiMethod_thenReturnExpectedUser() {
UserEntity firstUser = userDao.getUserByIdWithCriteriaQuery(1L);
Assert.assertNotNull("User not found", firstUser);
Assert.assertEquals("User should be baeldung", "baeldung", firstUser.getName());
UserEntity lastUser = userDao.getUserByIdWithCriteriaQuery(4L);
Assert.assertNotNull("User not found", lastUser);
Assert.assertEquals("User should be baeldung", "batman", lastUser.getName());
}
}
@@ -0,0 +1,41 @@
package com.baeldung.jpa.text;
import static org.junit.Assert.assertEquals;
import javax.persistence.PersistenceException;
import org.junit.BeforeClass;
import org.junit.Test;
public class JPATextUnitTest {
private static ExamRepository examRepository = null;
@BeforeClass
public static void once() {
examRepository = new ExamRepository();
}
@Test
public void givenExam_whenSaveExam_thenReturnExpectedExam() {
Exam exam = new Exam();
exam.setDescription("This is a description. Sometimes the description can be very very long! ");
exam.setText("This is a text. Sometimes the text can be very very long!");
exam.setShortText("A short text");
exam = examRepository.save(exam);
assertEquals(examRepository.find(exam.getId()), exam);
}
@Test(expected = PersistenceException.class)
public void givenExamWithVeryLongShortText_whenSaveExam_thenThrowPersistenceException() {
Exam exam = new Exam();
exam.setDescription("This is a very long text");
exam.setText("This is a long text");
exam.setShortText("This is a very long long short text. Maybe this can cause problems!!");
examRepository.save(exam);
}
}
@@ -0,0 +1,8 @@
drop table if exists allergens;
drop table if exists meal;
create table meal (id bigint auto_increment, name varchar(255) not null, description varchar(255) not null, price decimal(19, 2) not null, primary key (id));
create table allergens (meal_id bigint auto_increment, peanuts number(1) not null, celery number(1) not null, sesame_seeds number(1) not null, primary key (meal_id));
insert into meal (id, name, description, price) values (1, 'Pizza', 'Delicious', 5);
insert into allergens (meal_id, peanuts, celery, sesame_seeds) values (1, 0, 1, 0);
@@ -0,0 +1,5 @@
create table Product (id bigint not null, category varchar(255), description varchar(255), name varchar(255), unitPrice decimal(19,2), primary key (id));
insert into product(id, name, description, category) values (1,'Product Name 1','This is Product 1', 'category1');
insert into product(id, name, description, category) values (2,'Product Name 2','This is Product 2', 'category1');
insert into product(id, name, description, category) values (3,'Product Name 3','This is Product 3', 'category2');
insert into product(id, name, description, category) values (4,'Product Name 4','This is Product 4', 'category3');
@@ -0,0 +1,2 @@
INSERT INTO employees (employee_number, employee_name, employee_age) VALUES ('111', 'John Doe', 55);
INSERT INTO employees (employee_number, employee_name, employee_age) VALUES ('A123', 'John Doe Junior', 25);
@@ -0,0 +1,4 @@
INSERT INTO users(id,name) VALUES(1,'baeldung');
INSERT INTO users(id,name) VALUES(2,'john doe');
INSERT INTO users(id,name) VALUES(3,'jane doe');
INSERT INTO users(id,name) VALUES(4,'batman');