From ecb7049b42dac3659e602234ec5e9da517767a67 Mon Sep 17 00:00:00 2001 From: Ivan_Kishkan Date: Thu, 20 Feb 2020 17:09:30 +0400 Subject: [PATCH 1/5] remove com.sun.deploy --- .../izh/rd/online/SchoolbookServiceTest.java | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/test/java/com/epam/izh/rd/online/SchoolbookServiceTest.java b/src/test/java/com/epam/izh/rd/online/SchoolbookServiceTest.java index 560023af..80ce0c86 100644 --- a/src/test/java/com/epam/izh/rd/online/SchoolbookServiceTest.java +++ b/src/test/java/com/epam/izh/rd/online/SchoolbookServiceTest.java @@ -6,7 +6,6 @@ import com.epam.izh.rd.online.repository.BookRepository; import com.epam.izh.rd.online.service.AuthorService; import com.epam.izh.rd.online.service.BookService; -import com.sun.deploy.util.ReflectionUtil; import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -72,11 +71,10 @@ void testConstructors(Class clazz, Class[] params) { @MethodSource("com.epam.izh.rd.online.Providers#testServiceAndRepositories") @DisplayName("Проверка наличия классов репозиториев, сервисов и их интерфейсов") void testClasses(String canonicalClassName, String interfaceName) { - boolean isClassAvailable = ReflectionUtil - .isClassAvailable(canonicalClassName, Thread.currentThread().getContextClassLoader()); + boolean isClassAvailable = isClassAvailable(canonicalClassName, Thread.currentThread().getContextClassLoader()); assertTrue(isClassAvailable, "Не найден класс " + canonicalClassName); - Class clazz = ReflectionUtil.getClass(canonicalClassName, Thread.currentThread().getContextClassLoader()); + Class clazz = getClass(canonicalClassName, Thread.currentThread().getContextClassLoader()); List interfacesNames = Arrays.asList(clazz.getInterfaces()).stream().map(Class::getCanonicalName) .collect( Collectors.toList()); @@ -102,7 +100,7 @@ void testEqualsAndHash() { @Test @DisplayName("Проверка всей общей логики работы сервисов и репозиториев") void testLogic() { - Class clazz = ReflectionUtil.getClass("com.epam.izh.rd.online.repository.SimpleAuthorRepository", + Class clazz = getClass("com.epam.izh.rd.online.repository.SimpleAuthorRepository", Thread.currentThread().getContextClassLoader()); AuthorRepository authorRepository = null; try { @@ -113,7 +111,7 @@ void testLogic() { fail("Сначала полностью реализуйте всю стрктуру приложения."); } - clazz = ReflectionUtil.getClass("com.epam.izh.rd.online.repository.SimpleSchoolBookRepository", + clazz = getClass("com.epam.izh.rd.online.repository.SimpleSchoolBookRepository", Thread.currentThread().getContextClassLoader()); BookRepository bookRepository = null; try { @@ -124,7 +122,7 @@ void testLogic() { fail("Сначала полностью реализуйте всю стрктуру приложения."); } - clazz = ReflectionUtil.getClass("com.epam.izh.rd.online.service.SimpleAuthorService", + clazz = getClass("com.epam.izh.rd.online.service.SimpleAuthorService", Thread.currentThread().getContextClassLoader()); AuthorService authorService = null; try { @@ -136,7 +134,7 @@ void testLogic() { fail("Сначала полностью реализуйте всю стрктуру приложения."); } - clazz = ReflectionUtil.getClass("com.epam.izh.rd.online.service.SimpleSchoolBookService", + clazz = getClass("com.epam.izh.rd.online.service.SimpleSchoolBookService", Thread.currentThread().getContextClassLoader()); BookService bookService = null; try { @@ -266,8 +264,7 @@ void testLogic() { } private Author getNewInstanceOfAuthor(String name, String lastName, LocalDate birthdate, String country) { - Class clazz = ReflectionUtil - .getClass("com.epam.izh.rd.online.entity.Author", Thread.currentThread().getContextClassLoader()); + Class clazz = getClass("com.epam.izh.rd.online.entity.Author", Thread.currentThread().getContextClassLoader()); try { Author author = (Author) clazz.newInstance(); @@ -286,8 +283,8 @@ private Author getNewInstanceOfAuthor(String name, String lastName, LocalDate bi private SchoolBook getNewInstanceOfSchoolBook(int numberOfPages, String name, String authorName, String authorLastName, LocalDate publishDate) { - Class clazz = ReflectionUtil - .getClass("com.epam.izh.rd.online.entity.SchoolBook", Thread.currentThread().getContextClassLoader()); + Class clazz = getClass( + "com.epam.izh.rd.online.entity.SchoolBook", Thread.currentThread().getContextClassLoader()); try { SchoolBook schoolBook = (SchoolBook) clazz.newInstance(); @@ -312,4 +309,22 @@ private void setValueToField(Class clazz, String fieldName, Object obj, Object v ReflectionUtils.setField(Objects.requireNonNull(ReflectionUtils.findField(clazz, fieldName)), obj, value); } + + private static Class getClass(String name, ClassLoader classLoader) { + try { + return Class.forName(name, false, classLoader); + } catch (Throwable e) { + return null; + } + } + + private static boolean isClassAvailable(String name, ClassLoader classLoader) { + try { + Class clazz = Class.forName(name, false, classLoader); + return clazz != null; + } catch (Throwable e) { + return false; + } + } + } From a52183b7e400fcf61cb7bb8537ee543f31178860 Mon Sep 17 00:00:00 2001 From: Yuriy Titov Date: Tue, 4 Aug 2020 22:32:48 +0200 Subject: [PATCH 2/5] entity package classes done --- .../com/epam/izh/rd/online/entity/Author.java | 71 +++++++++++++++++++ .../com/epam/izh/rd/online/entity/Book.java | 47 ++++++++++++ .../epam/izh/rd/online/entity/SchoolBook.java | 67 +++++++++++++++++ 3 files changed, 185 insertions(+) diff --git a/src/main/java/com/epam/izh/rd/online/entity/Author.java b/src/main/java/com/epam/izh/rd/online/entity/Author.java index 166be587..68ac7f79 100644 --- a/src/main/java/com/epam/izh/rd/online/entity/Author.java +++ b/src/main/java/com/epam/izh/rd/online/entity/Author.java @@ -19,5 +19,76 @@ * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ public class Author { + private String name; + private String lastName; + private LocalDate birthdate; + private String country; + public Author() { + } + + public Author(String name, String lastName, LocalDate birthdate, String country) { + this.name = name; + this.lastName = lastName; + this.birthdate = birthdate; + this.country = country; + } + + @Override + public String toString() { + return "Author{" + + "name='" + name + '\'' + + ", lastName='" + lastName + '\'' + + ", birthdate=" + birthdate + + ", country='" + country + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Author author = (Author) o; + return name.equals(author.name) && + lastName.equals(author.lastName) && + birthdate.equals(author.birthdate) && + country.equals(author.country); + } + + @Override + public int hashCode() { + return Objects.hash(name, lastName, birthdate, country); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public LocalDate getBirthdate() { + return birthdate; + } + + public void setBirthdate(LocalDate birthdate) { + this.birthdate = birthdate; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } } diff --git a/src/main/java/com/epam/izh/rd/online/entity/Book.java b/src/main/java/com/epam/izh/rd/online/entity/Book.java index 08bdccb8..838696b6 100644 --- a/src/main/java/com/epam/izh/rd/online/entity/Book.java +++ b/src/main/java/com/epam/izh/rd/online/entity/Book.java @@ -16,5 +16,52 @@ * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ public abstract class Book { + private int numberOfPages; + private String name; + public Book() { + } + + public Book(int numberOfPages, String name) { + this.numberOfPages = numberOfPages; + this.name = name; + } + + @Override + public String toString() { + return "Book{" + + "numberOfPages=" + numberOfPages + + ", name='" + name + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Book book = (Book) o; + return numberOfPages == book.numberOfPages && + name.equals(book.name); + } + + @Override + public int hashCode() { + return Objects.hash(numberOfPages, name); + } + + public int getNumberOfPages() { + return numberOfPages; + } + + public void setNumberOfPages(int numberOfPages) { + this.numberOfPages = numberOfPages; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } } diff --git a/src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java b/src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java index a9834db4..0c9570ee 100644 --- a/src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java +++ b/src/main/java/com/epam/izh/rd/online/entity/SchoolBook.java @@ -20,5 +20,72 @@ * 6) Переопределить метод toString с выводом всех полей (не забывайте alt+inset) */ public class SchoolBook extends Book { + private String authorName; + private String authorLastName; + private LocalDate publishDate; + public SchoolBook() { + } + + public SchoolBook(String authorName, String authorLastName, LocalDate publishDate) { + this.authorName = authorName; + this.authorLastName = authorLastName; + this.publishDate = publishDate; + } + + public SchoolBook(int numberOfPages, String name, String authorName, String authorLastName, LocalDate publishDate) { + super(numberOfPages, name); + this.authorName = authorName; + this.authorLastName = authorLastName; + this.publishDate = publishDate; + } + + @Override + public String toString() { + return "SchoolBook{" + + "authorName='" + authorName + '\'' + + ", authorLastName='" + authorLastName + '\'' + + ", publishData=" + publishDate + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + SchoolBook that = (SchoolBook) o; + return Objects.equals(authorName, that.authorName) && + Objects.equals(authorLastName, that.authorLastName) && + Objects.equals(publishDate, that.publishDate); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), authorName, authorLastName, publishDate); + } + + public String getAuthorName() { + return authorName; + } + + public void setAuthorName(String authorName) { + this.authorName = authorName; + } + + public String getAuthorLastName() { + return authorLastName; + } + + public void setAuthorLastName(String authorLastName) { + this.authorLastName = authorLastName; + } + + public LocalDate getPublishDate() { + return publishDate; + } + + public void setPublishDate(LocalDate publishDate) { + this.publishDate = publishDate; + } } From 35c2997f7a84cdf3fbb92debe47a3ad9f73f2cb5 Mon Sep 17 00:00:00 2001 From: Yuriy Titov Date: Tue, 4 Aug 2020 23:39:09 +0200 Subject: [PATCH 3/5] repository done --- .../repository/SimpleAuthorRepository.java | 55 +++++++++++++++++++ .../SimpleSchoolBookRepository.java | 43 +++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java create mode 100644 src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java diff --git a/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java new file mode 100644 index 00000000..0e624611 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java @@ -0,0 +1,55 @@ +package com.epam.izh.rd.online.repository; + +import com.epam.izh.rd.online.entity.Author; + +import java.util.Arrays; + +public class SimpleAuthorRepository implements AuthorRepository { + private Author[] authors = {}; + + @Override + public boolean save(Author author) { + if (isAuthorExist(author)) { + return false; + } + + Author[] resultArr = Arrays.copyOf(authors, authors.length + 1); + resultArr[resultArr.length - 1] = author; + authors = resultArr; + + return true; + } + + @Override + public Author findByFullName(String name, String lastname) { + for (Author author : authors) { + if (author.getName().equals(name) && + author.getLastName().equals(lastname)) { + return author; + } + } + return null; + } + + @Override + public boolean remove(Author author) { + if (!isAuthorExist(author)) { + return false; + } + + Author[] tempArr = Arrays.stream(authors).filter(theAuthor -> + theAuthor.getName().equals(author.getName()) + && theAuthor.getLastName().equals(author.getLastName())).toArray(Author[]::new); + authors = tempArr; + return true; + } + + @Override + public int count() { + return authors.length; + } + + private boolean isAuthorExist(Author author) { + return findByFullName(author.getName(), author.getLastName()) != null; + } +} diff --git a/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java new file mode 100644 index 00000000..7df214bc --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java @@ -0,0 +1,43 @@ +package com.epam.izh.rd.online.repository; + +import com.epam.izh.rd.online.entity.SchoolBook; + +import java.util.Arrays; + +public class SimpleSchoolBookRepository implements BookRepository { + private SchoolBook[] schoolBooks = {}; + + @Override + public boolean save(SchoolBook book) { + SchoolBook[] resultArr = Arrays.copyOf(schoolBooks, schoolBooks.length + 1); + resultArr[resultArr.length - 1] = book; + schoolBooks = resultArr; + return true; + } + + @Override + public SchoolBook[] findByName(String name) { + return Arrays.stream(schoolBooks).filter(theSchoolBook -> + theSchoolBook.getAuthorName().equals(name)) + .toArray(SchoolBook[]::new); + } + + @Override + public boolean removeByName(String name) { + if (findByName(name).length == 0) { + return false; + } + + SchoolBook[] tempArr = Arrays.stream(schoolBooks) + .filter(book -> !book.getAuthorName().equals(name)) + .toArray(SchoolBook[]::new); + schoolBooks = tempArr; + + return true; + } + + @Override + public int count() { + return schoolBooks.length; + } +} From 3a15e3bb37d84a3102dd5cb31533eb70784332b2 Mon Sep 17 00:00:00 2001 From: Yuriy Titov Date: Tue, 4 Aug 2020 23:51:49 +0200 Subject: [PATCH 4/5] SimpleSchoolBookService implementation in progress --- .../online/service/SimpleAuthorService.java | 35 ++++++++++++++ .../service/SimpleSchoolBookService.java | 48 +++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java create mode 100644 src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java b/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java new file mode 100644 index 00000000..bd885470 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleAuthorService.java @@ -0,0 +1,35 @@ +package com.epam.izh.rd.online.service; + +import com.epam.izh.rd.online.entity.Author; +import com.epam.izh.rd.online.repository.AuthorRepository; + +public class SimpleAuthorService implements AuthorService { + private AuthorRepository authorRepository; + + public SimpleAuthorService() { + } + + public SimpleAuthorService(AuthorRepository authorRepository) { + this.authorRepository = authorRepository; + } + + @Override + public boolean save(Author author) { + return authorRepository.save(author); + } + + @Override + public Author findByFullName(String name, String lastname) { + return authorRepository.findByFullName(name, lastname); + } + + @Override + public boolean remove(Author author) { + return authorRepository.remove(author); + } + + @Override + public int count() { + return authorRepository.count(); + } +} diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java new file mode 100644 index 00000000..660be5d2 --- /dev/null +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java @@ -0,0 +1,48 @@ +package com.epam.izh.rd.online.service; + +import com.epam.izh.rd.online.entity.Author; +import com.epam.izh.rd.online.entity.SchoolBook; +import com.epam.izh.rd.online.repository.BookRepository; + +public class SimpleSchoolBookService implements BookService { + private BookRepository schoolBookBookRepository; + private AuthorService authorService; + + public SimpleSchoolBookService() { + } + + public SimpleSchoolBookService(BookRepository schoolBookBookRepository, AuthorService authorService) { + this.schoolBookBookRepository = schoolBookBookRepository; + this.authorService = authorService; + } + + @Override + public boolean save(SchoolBook book) { + return false; + } + + @Override + public SchoolBook[] findByName(String name) { + return new SchoolBook[0]; + } + + @Override + public int getNumberOfBooksByName(String name) { + return 0; + } + + @Override + public boolean removeByName(String name) { + return false; + } + + @Override + public int count() { + return 0; + } + + @Override + public Author findAuthorByBookName(String name) { + return null; + } +} From 4cdbae49f1d8403c93138754015b42dc7f020f06 Mon Sep 17 00:00:00 2001 From: Yuriy Titov Date: Wed, 5 Aug 2020 09:48:12 +0200 Subject: [PATCH 5/5] task completed, tests passed --- .../repository/SimpleAuthorRepository.java | 14 +++++------ .../SimpleSchoolBookRepository.java | 6 ++--- .../service/SimpleSchoolBookService.java | 25 ++++++++++++++----- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java index 0e624611..e77e84eb 100644 --- a/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleAuthorRepository.java @@ -5,11 +5,11 @@ import java.util.Arrays; public class SimpleAuthorRepository implements AuthorRepository { - private Author[] authors = {}; + private Author[] authors = new Author[0]; @Override public boolean save(Author author) { - if (isAuthorExist(author)) { + if (!isAuthorNotExist(author)) { return false; } @@ -33,13 +33,13 @@ public Author findByFullName(String name, String lastname) { @Override public boolean remove(Author author) { - if (!isAuthorExist(author)) { + if (isAuthorNotExist(author)) { return false; } Author[] tempArr = Arrays.stream(authors).filter(theAuthor -> - theAuthor.getName().equals(author.getName()) - && theAuthor.getLastName().equals(author.getLastName())).toArray(Author[]::new); + !theAuthor.getName().equals(author.getName()) + || !theAuthor.getLastName().equals(author.getLastName())).toArray(Author[]::new); authors = tempArr; return true; } @@ -49,7 +49,7 @@ public int count() { return authors.length; } - private boolean isAuthorExist(Author author) { - return findByFullName(author.getName(), author.getLastName()) != null; + private boolean isAuthorNotExist(Author author) { + return findByFullName(author.getName(), author.getLastName()) == null; } } diff --git a/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java index 7df214bc..713733e0 100644 --- a/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java +++ b/src/main/java/com/epam/izh/rd/online/repository/SimpleSchoolBookRepository.java @@ -5,7 +5,7 @@ import java.util.Arrays; public class SimpleSchoolBookRepository implements BookRepository { - private SchoolBook[] schoolBooks = {}; + private SchoolBook[] schoolBooks = new SchoolBook[0]; @Override public boolean save(SchoolBook book) { @@ -18,7 +18,7 @@ public boolean save(SchoolBook book) { @Override public SchoolBook[] findByName(String name) { return Arrays.stream(schoolBooks).filter(theSchoolBook -> - theSchoolBook.getAuthorName().equals(name)) + theSchoolBook.getName().equals(name)) .toArray(SchoolBook[]::new); } @@ -29,7 +29,7 @@ public boolean removeByName(String name) { } SchoolBook[] tempArr = Arrays.stream(schoolBooks) - .filter(book -> !book.getAuthorName().equals(name)) + .filter(book -> !book.getName().equals(name)) .toArray(SchoolBook[]::new); schoolBooks = tempArr; diff --git a/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java index 660be5d2..53d7b9ed 100644 --- a/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java +++ b/src/main/java/com/epam/izh/rd/online/service/SimpleSchoolBookService.java @@ -18,31 +18,44 @@ public SimpleSchoolBookService(BookRepository schoolBookBookReposito @Override public boolean save(SchoolBook book) { - return false; + if (authorService.findByFullName(book.getAuthorName(), book.getAuthorLastName()) == null) { + return false; + } + + schoolBookBookRepository.save(book); + return true; } @Override public SchoolBook[] findByName(String name) { - return new SchoolBook[0]; + return schoolBookBookRepository.findByName(name); } @Override public int getNumberOfBooksByName(String name) { - return 0; + return schoolBookBookRepository.findByName(name).length; } @Override public boolean removeByName(String name) { - return false; + return schoolBookBookRepository.removeByName(name); } @Override public int count() { - return 0; + return schoolBookBookRepository.count(); } @Override public Author findAuthorByBookName(String name) { - return null; + if (schoolBookBookRepository.findByName(name).length == 0) { + return null; + } + SchoolBook book = schoolBookBookRepository.findByName(name)[0]; + + String authorName = book.getAuthorName(); + String authorLastName = book.getAuthorLastName(); + + return authorService.findByFullName(authorName, authorLastName); } }