Java 11 migrate c-d (remaining) (#1111)

* Moves converter pattern to Java 11

* Moves cqrs pattern to Java 11

* Moves dao pattern to Java 11

* Moves data-bus pattern to Java 11

* Moves data-locality pattern to Java 11

* Moves data-mapper pattern to Java 11

* Moves data-transfer-object pattern to Java 11

* Moves decorator pattern to Java 11

* Moves delegation pattern to Java 11

* Moves dependency-injection to Java 11

* Moves dirty-flag to Java 11

* Moves double-buffer to Java 11

* Moves double-checked-locking to Java 11

* Moves double-dispatch to Java 11

* Corrects with changes thats breaking test cases
This commit is contained in:
Anurag Agarwal
2019-12-15 00:02:45 +05:30
committed by Ilkka Seppälä
parent 5681684157
commit ea57934db6
75 changed files with 576 additions and 713 deletions

View File

@ -24,15 +24,9 @@
package com.iluwatar.cqrs.app;
import com.iluwatar.cqrs.commandes.CommandServiceImpl;
import com.iluwatar.cqrs.commandes.ICommandService;
import com.iluwatar.cqrs.constants.AppConstants;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
import com.iluwatar.cqrs.queries.IQueryService;
import com.iluwatar.cqrs.queries.QueryServiceImpl;
import com.iluwatar.cqrs.util.HibernateUtil;
import java.math.BigInteger;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -56,7 +50,7 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
ICommandService commands = new CommandServiceImpl();
var commands = new CommandServiceImpl();
// Create Authors and Books using CommandService
commands.authorCreated(AppConstants.E_EVANS, "Eric Evans", "evans@email.com");
@ -72,15 +66,15 @@ public class App {
commands.bookAddedToAuthor("Domain Specific Languages", 48.89, AppConstants.M_FOWLER);
commands.authorNameUpdated(AppConstants.E_EVANS, "Eric J. Evans");
IQueryService queries = new QueryServiceImpl();
var queries = new QueryServiceImpl();
// Query the database using QueryService
Author nullAuthor = queries.getAuthorByUsername("username");
Author evans = queries.getAuthorByUsername(AppConstants.E_EVANS);
BigInteger blochBooksCount = queries.getAuthorBooksCount(AppConstants.J_BLOCH);
BigInteger authorsCount = queries.getAuthorsCount();
Book dddBook = queries.getBook("Domain-Driven Design");
List<Book> blochBooks = queries.getAuthorBooks(AppConstants.J_BLOCH);
var nullAuthor = queries.getAuthorByUsername("username");
var evans = queries.getAuthorByUsername(AppConstants.E_EVANS);
var blochBooksCount = queries.getAuthorBooksCount(AppConstants.J_BLOCH);
var authorsCount = queries.getAuthorsCount();
var dddBook = queries.getBook("Domain-Driven Design");
var blochBooks = queries.getAuthorBooks(AppConstants.J_BLOCH);
LOGGER.info("Author username : {}", nullAuthor);
LOGGER.info("Author evans : {}", evans);

View File

@ -26,8 +26,6 @@ package com.iluwatar.cqrs.commandes;
import com.iluwatar.cqrs.domain.model.Author;
import com.iluwatar.cqrs.domain.model.Book;
import com.iluwatar.cqrs.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
/**
@ -39,9 +37,9 @@ public class CommandServiceImpl implements ICommandService {
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
private Author getAuthorByUsername(String username) {
Author author = null;
try (Session session = sessionFactory.openSession()) {
Query query = session.createQuery("from Author where username=:username");
Author author;
try (var session = sessionFactory.openSession()) {
var query = session.createQuery("from Author where username=:username");
query.setParameter("username", username);
author = (Author) query.uniqueResult();
}
@ -53,9 +51,9 @@ public class CommandServiceImpl implements ICommandService {
}
private Book getBookByTitle(String title) {
Book book = null;
try (Session session = sessionFactory.openSession()) {
Query query = session.createQuery("from Book where title=:title");
Book book;
try (var session = sessionFactory.openSession()) {
var query = session.createQuery("from Book where title=:title");
query.setParameter("title", title);
book = (Book) query.uniqueResult();
}
@ -68,8 +66,8 @@ public class CommandServiceImpl implements ICommandService {
@Override
public void authorCreated(String username, String name, String email) {
Author author = new Author(username, name, email);
try (Session session = sessionFactory.openSession()) {
var author = new Author(username, name, email);
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.save(author);
session.getTransaction().commit();
@ -78,9 +76,9 @@ public class CommandServiceImpl implements ICommandService {
@Override
public void bookAddedToAuthor(String title, double price, String username) {
Author author = getAuthorByUsername(username);
Book book = new Book(title, price, author);
try (Session session = sessionFactory.openSession()) {
var author = getAuthorByUsername(username);
var book = new Book(title, price, author);
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.save(book);
session.getTransaction().commit();
@ -89,9 +87,9 @@ public class CommandServiceImpl implements ICommandService {
@Override
public void authorNameUpdated(String username, String name) {
Author author = getAuthorByUsername(username);
var author = getAuthorByUsername(username);
author.setName(name);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(author);
session.getTransaction().commit();
@ -100,9 +98,9 @@ public class CommandServiceImpl implements ICommandService {
@Override
public void authorUsernameUpdated(String oldUsername, String newUsername) {
Author author = getAuthorByUsername(oldUsername);
var author = getAuthorByUsername(oldUsername);
author.setUsername(newUsername);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(author);
session.getTransaction().commit();
@ -111,9 +109,9 @@ public class CommandServiceImpl implements ICommandService {
@Override
public void authorEmailUpdated(String username, String email) {
Author author = getAuthorByUsername(username);
var author = getAuthorByUsername(username);
author.setEmail(email);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(author);
session.getTransaction().commit();
@ -122,9 +120,9 @@ public class CommandServiceImpl implements ICommandService {
@Override
public void bookTitleUpdated(String oldTitle, String newTitle) {
Book book = getBookByTitle(oldTitle);
var book = getBookByTitle(oldTitle);
book.setTitle(newTitle);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(book);
session.getTransaction().commit();
@ -133,9 +131,9 @@ public class CommandServiceImpl implements ICommandService {
@Override
public void bookPriceUpdated(String title, double price) {
Book book = getBookByTitle(title);
var book = getBookByTitle(title);
book.setPrice(price);
try (Session session = sessionFactory.openSession()) {
try (var session = sessionFactory.openSession()) {
session.beginTransaction();
session.update(book);
session.getTransaction().commit();

View File

@ -80,7 +80,7 @@ public class Author {
if (!(obj instanceof Author)) {
return false;
}
Author other = (Author) obj;
var other = (Author) obj;
return username.equals(other.getUsername()) && email.equals(other.getEmail()) && name
.equals(other.getName());

View File

@ -73,7 +73,7 @@ public class Book {
if (!(obj instanceof Book)) {
return false;
}
Book book = (Book) obj;
var book = (Book) obj;
return title.equals(book.getTitle()) && price == book.getPrice();
}

View File

@ -29,8 +29,6 @@ import com.iluwatar.cqrs.dto.Book;
import com.iluwatar.cqrs.util.HibernateUtil;
import java.math.BigInteger;
import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
@ -44,9 +42,9 @@ public class QueryServiceImpl implements IQueryService {
@Override
public Author getAuthorByUsername(String username) {
Author authorDTo = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery("SELECT a.username as \"username\","
Author authorDTo;
try (var session = sessionFactory.openSession()) {
var sqlQuery = session.createSQLQuery("SELECT a.username as \"username\","
+ " a.name as \"name\", a.email as \"email\""
+ "FROM Author a where a.username=:username");
sqlQuery.setParameter(AppConstants.USER_NAME, username);
@ -58,9 +56,9 @@ public class QueryServiceImpl implements IQueryService {
@Override
public Book getBook(String title) {
Book bookDTo = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\","
Book bookDTo;
try (var session = sessionFactory.openSession()) {
var sqlQuery = session.createSQLQuery("SELECT b.title as \"title\","
+ " b.price as \"price\"" + " FROM Book b where b.title=:title");
sqlQuery.setParameter("title", title);
bookDTo =
@ -71,9 +69,9 @@ public class QueryServiceImpl implements IQueryService {
@Override
public List<Book> getAuthorBooks(String username) {
List<Book> bookDTos = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\""
List<Book> bookDTos;
try (var session = sessionFactory.openSession()) {
var sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\""
+ " FROM Author a , Book b where b.author_id = a.id and a.username=:username");
sqlQuery.setParameter(AppConstants.USER_NAME, username);
bookDTos = sqlQuery.setResultTransformer(Transformers.aliasToBean(Book.class)).list();
@ -83,9 +81,9 @@ public class QueryServiceImpl implements IQueryService {
@Override
public BigInteger getAuthorBooksCount(String username) {
BigInteger bookcount = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery(
BigInteger bookcount;
try (var session = sessionFactory.openSession()) {
var sqlQuery = session.createSQLQuery(
"SELECT count(b.title)" + " FROM Book b, Author a"
+ " where b.author_id = a.id and a.username=:username");
sqlQuery.setParameter(AppConstants.USER_NAME, username);
@ -96,9 +94,9 @@ public class QueryServiceImpl implements IQueryService {
@Override
public BigInteger getAuthorsCount() {
BigInteger authorcount = null;
try (Session session = sessionFactory.openSession()) {
SQLQuery sqlQuery = session.createSQLQuery("SELECT count(id) from Author");
BigInteger authorcount;
try (var session = sessionFactory.openSession()) {
var sqlQuery = session.createSQLQuery("SELECT count(id) from Author");
authorcount = (BigInteger) sqlQuery.uniqueResult();
}
return authorcount;

View File

@ -25,7 +25,6 @@ package com.iluwatar.cqrs.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -42,8 +41,7 @@ public class HibernateUtil {
private static SessionFactory buildSessionFactory() {
// configures settings from hibernate.cfg.xml
final StandardServiceRegistry registry =
new StandardServiceRegistryBuilder().configure().build();
final var registry = new StandardServiceRegistryBuilder().configure().build();
try {
return new MetadataSources(registry).buildMetadata().buildSessionFactory();
} catch (Exception ex) {

View File

@ -23,33 +23,28 @@
package com.iluwatar.cqrs;
import java.math.BigInteger;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.iluwatar.cqrs.commandes.CommandServiceImpl;
import com.iluwatar.cqrs.commandes.ICommandService;
import com.iluwatar.cqrs.dto.Author;
import com.iluwatar.cqrs.dto.Book;
import com.iluwatar.cqrs.queries.IQueryService;
import com.iluwatar.cqrs.queries.QueryServiceImpl;
import java.math.BigInteger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Integration test of IQueryService and ICommandService with h2 data
*
*/
public class IntegrationTest {
private static IQueryService queryService;
private static ICommandService commandService;
@BeforeAll
public static void initializeAndPopulateDatabase() {
commandService = new CommandServiceImpl();
var commandService = new CommandServiceImpl();
queryService = new QueryServiceImpl();
// create first author1
@ -73,7 +68,7 @@ public class IntegrationTest {
@Test
public void testGetAuthorByUsername() {
Author author = queryService.getAuthorByUsername("username1");
var author = queryService.getAuthorByUsername("username1");
assertEquals("username1", author.getUsername());
assertEquals("name1", author.getName());
assertEquals("email1", author.getEmail());
@ -81,22 +76,22 @@ public class IntegrationTest {
@Test
public void testGetUpdatedAuthorByUsername() {
Author author = queryService.getAuthorByUsername("new_username2");
Author expectedAuthor = new Author("new_name2", "new_email2", "new_username2");
var author = queryService.getAuthorByUsername("new_username2");
var expectedAuthor = new Author("new_name2", "new_email2", "new_username2");
assertEquals(expectedAuthor, author);
}
@Test
public void testGetBook() {
Book book = queryService.getBook("title1");
var book = queryService.getBook("title1");
assertEquals("title1", book.getTitle());
assertEquals(10, book.getPrice(), 0.01);
}
@Test
public void testGetAuthorBooks() {
List<Book> books = queryService.getAuthorBooks("username1");
var books = queryService.getAuthorBooks("username1");
assertEquals(2, books.size());
assertTrue(books.contains(new Book("title1", 10)));
assertTrue(books.contains(new Book("new_title2", 30)));
@ -104,13 +99,13 @@ public class IntegrationTest {
@Test
public void testGetAuthorBooksCount() {
BigInteger bookCount = queryService.getAuthorBooksCount("username1");
var bookCount = queryService.getAuthorBooksCount("username1");
assertEquals(new BigInteger("2"), bookCount);
}
@Test
public void testGetAuthorsCount() {
BigInteger authorCount = queryService.getAuthorsCount();
var authorCount = queryService.getAuthorsCount();
assertEquals(new BigInteger("2"), authorCount);
}