diff --git a/java-cucumber-maven/README.md b/java-cucumber-maven/README.md index 2f4d0ff..68bd1cc 100644 --- a/java-cucumber-maven/README.md +++ b/java-cucumber-maven/README.md @@ -24,7 +24,7 @@ This simple demo shows how Testomat.io Java reporter works in your project. │ │ ├── ProductSteps │ │ ├── ReportSteps │ │ └── UserSteps - │ └── RunnerTest + │ └── TestRunner └── resources/ └── features/ ├── notification.feature @@ -43,13 +43,9 @@ This simple demo shows how Testomat.io Java reporter works in your project. cd java-cucumber-maven ``` -2. Install dependencies +2. Install dependencies (run this commands in Bash) ```sh - mvn install:install-file\ - -Dfile=lib/java-reporter-0.1.0.jar\ - -Dversion=0.1.0 -Dpackaging=jar\ - -Dgroup=com.testomatio.reporter\ - -DartifactId=java-reporter + mvn install:install-file -Dfile=lib/java-reporter-0.1.0.jar -Dversion=0.1.0 -Dpackaging=jar -Dgroup=com.testomatio.reporter -DartifactId=java-reporter ``` ```sh mvn clean diff --git a/java-cucumber-maven/pom.xml b/java-cucumber-maven/pom.xml index 11fdea8..cc41996 100644 --- a/java-cucumber-maven/pom.xml +++ b/java-cucumber-maven/pom.xml @@ -18,6 +18,11 @@ + + org.opentest4j + opentest4j + 1.2.0 + io.cucumber cucumber-java @@ -52,7 +57,7 @@ 3.0.0-M7 - **/RunnerTest.java + **/TestRunner.java true diff --git a/java-cucumber-maven/src/main/resources/testomatio.properties b/java-cucumber-maven/src/main/resources/testomatio.properties index 98c637a..ff0fa7f 100644 --- a/java-cucumber-maven/src/main/resources/testomatio.properties +++ b/java-cucumber-maven/src/main/resources/testomatio.properties @@ -2,7 +2,7 @@ testomatio.batch.size=5 testomatio.batch.flush.interval=20 testomatio.url=https://app.testomat.io/ testomatio.run.title= -testomatio.api.key=${TESTOMATIO_API_KEY} +testomatio.api.key= ## (OFF, SEVERE, WARNING, INFO, FINE, FINER, FINEST, ALL) testomatio.log.level=INFO diff --git a/java-cucumber-maven/src/test/java/RunnerTest.java b/java-cucumber-maven/src/test/java/TestRunner.java similarity index 83% rename from java-cucumber-maven/src/test/java/RunnerTest.java rename to java-cucumber-maven/src/test/java/TestRunner.java index 759f5eb..22821fa 100644 --- a/java-cucumber-maven/src/test/java/RunnerTest.java +++ b/java-cucumber-maven/src/test/java/TestRunner.java @@ -5,14 +5,13 @@ @RunWith(Cucumber.class) @CucumberOptions( features = "src/test/resources/features", - glue = "steps", + glue = {"steps"}, plugin = { "pretty", "json:target/cucumber-reports/cucumber.json", "html:target/cucumber-reports/report", "com.testomatio.reporter.core.framework_integration.CucumberListener" - }, - tags = "not @ignore" + } ) -public class RunnerTest { +public class TestRunner { } \ No newline at end of file diff --git a/java-junit-maven-0.1.0/.gitignore b/java-junit-maven-0.1.0/.gitignore new file mode 100644 index 0000000..2e0882f --- /dev/null +++ b/java-junit-maven-0.1.0/.gitignore @@ -0,0 +1,2 @@ +**/logs/ +**/target/ \ No newline at end of file diff --git a/java-junit-maven-0.1.0/README.md b/java-junit-maven-0.1.0/README.md new file mode 100644 index 0000000..7bdfb79 --- /dev/null +++ b/java-junit-maven-0.1.0/README.md @@ -0,0 +1,109 @@ +# Java reporter integration with JUnit5 + +## Overview + +This simple demo shows how Testomat.io Java reporter works in your project. + +- Includes a pack of 90 tests. Some will fail on purpose and other will be disabled for demo. + +## Structure + +``` +src/ +├── main/ +│ ├── java/ +│ │ └── com/library/ +│ │ ├── util/ +│ │ │ └── BookUtils.java +│ │ ├── Author.java +│ │ ├── Book.java +│ │ ├── Genre.java +│ │ ├── Library.java +│ │ ├── LibraryCard.java +│ │ ├── Loan.java +│ │ ├── Publisher.java +│ │ └── Reader.java +│ └── resources/ +│ └── junit-platform.properties +└── test/ + └── java/ + └── library/ + ├── AuthorTest.java + ├── BookTest.java + ├── LibraryTest.java + ├── LoanTest.java + └── ReaderTest.java +``` + +## Installation + +1. Clone the repository + +```sh + git clone + cd java-junit-maven-0.1.0 +``` + +2. Install dependencies + +```sh + mvn install:install-file\ + -Dfile=lib/java-reporter-0.1.0.jar\ + -Dversion=0.1.0 -Dpackaging=jar\ + -Dgroup=com.testomatio.reporter\ + -DartifactId=java-reporter +``` + +```sh + mvn clean +``` + +```sh + mvn install -DskipTests +``` + +## Configurations + +**By default, the library runs with properties default values except `testomatio.api.key`** + +You can pass your custom properties as JVM properties, OS env variables or in the `testomatio.properties` file. + +The file, if you want to use this approach, must be created int the `main/resources` folder. + +To let JUnit run extension automatically - you will need to add this line into your **junit-platform.properties** file: + +```properties +junit.jupiter.extensions.autodetection.enabled=true +``` + +- Optionally, in the `testomatio.properties` file can configure run parameters: + +```properties +testomatio.batch.size=5 +testomatio.url=https://app.testomat.io/ +testomatio.run.title= +testomatio.api.key= +## (OFF, SEVERE, WARNING, INFO, FINE, FINER, FINEST, ALL) +testomatio.log.level=INFO +testomatio.log.file=logs/testomatio.log +testomatio.log.console=true +``` + +And the default is "Default run title", so you might want to change it. + +## Run + +Run tests with + +```sh + mvn test -D=tstmt_key +``` + +where `tstmt_key` is your Testomat.io key from a particular project. + +As a result, you will see a run report in your Project tab -> Runs on Testomat.io. + +
+ demo report result png +
+ diff --git a/java-junit-maven-0.1.0/img/runReport.png b/java-junit-maven-0.1.0/img/runReport.png new file mode 100644 index 0000000..ab2a27d Binary files /dev/null and b/java-junit-maven-0.1.0/img/runReport.png differ diff --git a/java-junit-maven-0.1.0/lib/java-reporter-0.1.0.jar b/java-junit-maven-0.1.0/lib/java-reporter-0.1.0.jar new file mode 100644 index 0000000..76d3bd4 Binary files /dev/null and b/java-junit-maven-0.1.0/lib/java-reporter-0.1.0.jar differ diff --git a/java-junit-maven-0.1.0/pom.xml b/java-junit-maven-0.1.0/pom.xml new file mode 100644 index 0000000..029ef21 --- /dev/null +++ b/java-junit-maven-0.1.0/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + com.testomatio.reporter + java-junit-maven-0.1.0 + 1.0-SNAPSHOT + + + 11 + 11 + UTF-8 + 5.9.2 + + + + + + com.testomatio.reporter + java-reporter + 0.1.0 + test + + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + test + + + + \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/java/com/library/Author.java b/java-junit-maven-0.1.0/src/main/java/com/library/Author.java new file mode 100644 index 0000000..1a15faa --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/java/com/library/Author.java @@ -0,0 +1,112 @@ +package com.library; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class Author { + private String id; + private String firstName; + private String lastName; + private LocalDate birthDate; + private String nationality; + private List books; + + public Author(String id, String firstName, String lastName, LocalDate birthDate, String nationality) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.birthDate = birthDate; + this.nationality = nationality; + this.books = new ArrayList<>(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getFullName() { + return firstName + " " + lastName; + } + + public LocalDate getBirthDate() { + return birthDate; + } + + public void setBirthDate(LocalDate birthDate) { + this.birthDate = birthDate; + } + + public String getNationality() { + return nationality; + } + + public void setNationality(String nationality) { + this.nationality = nationality; + } + + public List getBooks() { + return new ArrayList<>(books); + } + + public void addBook(Book book) { + if (!books.contains(book)) { + books.add(book); + } + } + + public void removeBook(Book book) { + books.remove(book); + } + + public int getBookCount() { + return books.size(); + } + + public int getAge() { + return LocalDate.now().getYear() - birthDate.getYear(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Author author = (Author) o; + return Objects.equals(id, author.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Author{" + + "id='" + id + '\'' + + ", fullName='" + getFullName() + '\'' + + ", nationality='" + nationality + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/java/com/library/Book.java b/java-junit-maven-0.1.0/src/main/java/com/library/Book.java new file mode 100644 index 0000000..3831892 --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/java/com/library/Book.java @@ -0,0 +1,118 @@ +package com.library; + +import java.time.LocalDate; +import java.util.Objects; + +public class Book { + private String isbn; + private String title; + private Author author; + private Publisher publisher; + private Genre genre; + private LocalDate publicationDate; + private int pages; + private boolean available; + + public Book(String isbn, String title, Author author, Publisher publisher, + Genre genre, LocalDate publicationDate, int pages) { + this.isbn = isbn; + this.title = title; + this.author = author; + this.publisher = publisher; + this.genre = genre; + this.publicationDate = publicationDate; + this.pages = pages; + this.available = true; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Author getAuthor() { + return author; + } + + public void setAuthor(Author author) { + this.author = author; + } + + public Publisher getPublisher() { + return publisher; + } + + public void setPublisher(Publisher publisher) { + this.publisher = publisher; + } + + public Genre getGenre() { + return genre; + } + + public void setGenre(Genre genre) { + this.genre = genre; + } + + public LocalDate getPublicationDate() { + return publicationDate; + } + + public void setPublicationDate(LocalDate publicationDate) { + this.publicationDate = publicationDate; + } + + public int getPages() { + return pages; + } + + public void setPages(int pages) { + this.pages = pages; + } + + public boolean isAvailable() { + return available; + } + + public void setAvailable(boolean available) { + this.available = available; + } + + public int getAge() { + return LocalDate.now().getYear() - publicationDate.getYear(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Book book = (Book) o; + return Objects.equals(isbn, book.isbn); + } + + @Override + public int hashCode() { + return Objects.hash(isbn); + } + + @Override + public String toString() { + return "Book{" + + "isbn='" + isbn + '\'' + + ", title='" + title + '\'' + + ", author=" + author + + ", available=" + available + + '}'; + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/java/com/library/Genre.java b/java-junit-maven-0.1.0/src/main/java/com/library/Genre.java new file mode 100644 index 0000000..8f07b89 --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/java/com/library/Genre.java @@ -0,0 +1,103 @@ +package com.library; + +import java.util.Objects; + +public class Genre { + private String id; + private String name; + private String description; + private Genre parentGenre; + + public Genre(String id, String name, String description) { + this.id = id; + this.name = name; + this.description = description; + } + + public Genre(String id, String name, String description, Genre parentGenre) { + this(id, name, description); + this.parentGenre = parentGenre; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Genre getParentGenre() { + return parentGenre; + } + + public void setParentGenre(Genre parentGenre) { + this.parentGenre = parentGenre; + } + + public boolean hasParentGenre() { + return parentGenre != null; + } + + public String getFullPath() { + if (parentGenre == null) { + return name; + } + return parentGenre.getFullPath() + " > " + name; + } + + public boolean isSubGenreOf(Genre other) { + if (parentGenre == null) { + return false; + } + if (parentGenre.equals(other)) { + return true; + } + return parentGenre.isSubGenreOf(other); + } + + public int getDepthLevel() { + if (parentGenre == null) { + return 0; + } + return parentGenre.getDepthLevel() + 1; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Genre genre = (Genre) o; + return Objects.equals(id, genre.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Genre{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", path='" + getFullPath() + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/java/com/library/Library.java b/java-junit-maven-0.1.0/src/main/java/com/library/Library.java new file mode 100644 index 0000000..5b8289c --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/java/com/library/Library.java @@ -0,0 +1,159 @@ +package com.library; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class Library { + private String name; + private String address; + private Map books; + private Map readers; + private List loans; + + public Library(String name, String address) { + this.name = name; + this.address = address; + this.books = new HashMap<>(); + this.readers = new HashMap<>(); + this.loans = new ArrayList<>(); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public Collection getBooks() { + return new ArrayList<>(books.values()); + } + + public Book getBook(String isbn) { + return books.get(isbn); + } + + public void addBook(Book book) { + books.put(book.getIsbn(), book); + } + + public void removeBook(String isbn) { + books.remove(isbn); + } + + public Collection getReaders() { + return new ArrayList<>(readers.values()); + } + + public Reader getReader(String id) { + return readers.get(id); + } + + public void registerReader(Reader reader) { + readers.put(reader.getId(), reader); + } + + public void removeReader(String id) { + readers.remove(id); + } + + public List getLoans() { + return new ArrayList<>(loans); + } + + public Loan loanBook(String readerId, String isbn) { + Reader reader = readers.get(readerId); + Book book = books.get(isbn); + + if (reader == null || book == null) { + throw new IllegalArgumentException("Reader or book not found"); + } + + if (!reader.canBorrow()) { + throw new IllegalStateException("Reader cannot borrow more books"); + } + + if (!book.isAvailable()) { + throw new IllegalStateException("Book is not available"); + } + + Loan loan = new Loan(generateLoanId(), book, reader, LocalDate.now()); + loans.add(loan); + reader.addLoan(loan); + book.setAvailable(false); + + return loan; + } + + public void returnBook(String loanId) { + Loan loan = loans.stream() + .filter(l -> l.getId().equals(loanId)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Loan not found")); + + loan.setReturnDate(LocalDate.now()); + loan.getBook().setAvailable(true); + } + + public List getAvailableBooks() { + return books.values().stream() + .filter(Book::isAvailable) + .collect(Collectors.toList()); + } + + public List getBooksByGenre(Genre genre) { + return books.values().stream() + .filter(book -> book.getGenre().equals(genre)) + .collect(Collectors.toList()); + } + + public List getBooksByAuthor(Author author) { + return books.values().stream() + .filter(book -> book.getAuthor().equals(author)) + .collect(Collectors.toList()); + } + + public List getOverdueLoans() { + LocalDate today = LocalDate.now(); + return loans.stream() + .filter(loan -> loan.getReturnDate() == null && loan.isOverdue()) + .collect(Collectors.toList()); + } + + public int getTotalBookCount() { + return books.size(); + } + + public int getActiveReaderCount() { + return (int) readers.values().stream() + .filter(Reader::isActive) + .count(); + } + + private String generateLoanId() { + return "LOAN-" + System.currentTimeMillis(); + } + + @Override + public String toString() { + return "Library{" + + "name='" + name + '\'' + + ", totalBooks=" + books.size() + + ", totalReaders=" + readers.size() + + '}'; + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/java/com/library/LibraryCard.java b/java-junit-maven-0.1.0/src/main/java/com/library/LibraryCard.java new file mode 100644 index 0000000..d585bca --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/java/com/library/LibraryCard.java @@ -0,0 +1,126 @@ +package com.library; + +import java.time.LocalDate; +import java.util.Objects; + +public class LibraryCard { + private String cardNumber; + private Reader owner; + private LocalDate issueDate; + private LocalDate expiryDate; + private boolean blocked; + private String blockReason; + + public LibraryCard(String cardNumber, Reader owner) { + this.cardNumber = cardNumber; + this.owner = owner; + this.issueDate = LocalDate.now(); + this.expiryDate = issueDate.plusYears(2); + this.blocked = false; + } + + public String getCardNumber() { + return cardNumber; + } + + public void setCardNumber(String cardNumber) { + this.cardNumber = cardNumber; + } + + public Reader getOwner() { + return owner; + } + + public void setOwner(Reader owner) { + this.owner = owner; + } + + public LocalDate getIssueDate() { + return issueDate; + } + + public void setIssueDate(LocalDate issueDate) { + this.issueDate = issueDate; + } + + public LocalDate getExpiryDate() { + return expiryDate; + } + + public void setExpiryDate(LocalDate expiryDate) { + this.expiryDate = expiryDate; + } + + public boolean isBlocked() { + return blocked; + } + + public void block(String reason) { + this.blocked = true; + this.blockReason = reason; + } + + public void unblock() { + this.blocked = false; + this.blockReason = null; + } + + public String getBlockReason() { + return blockReason; + } + + public boolean isExpired() { + return LocalDate.now().isAfter(expiryDate); + } + + public boolean isValid() { + return !isExpired() && !isBlocked(); + } + + public void renew() { + if (isExpired()) { + expiryDate = LocalDate.now().plusYears(2); + } else { + expiryDate = expiryDate.plusYears(2); + } + } + + public int getDaysUntilExpiry() { + if (isExpired()) { + return 0; + } + return (int) LocalDate.now().until(expiryDate).toTotalMonths() * 30; + } + + public String getFormattedCardNumber() { + if (cardNumber.length() == 16) { + return cardNumber.substring(0, 4) + "-" + + cardNumber.substring(4, 8) + "-" + + cardNumber.substring(8, 12) + "-" + + cardNumber.substring(12); + } + return cardNumber; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + LibraryCard that = (LibraryCard) o; + return Objects.equals(cardNumber, that.cardNumber); + } + + @Override + public int hashCode() { + return Objects.hash(cardNumber); + } + + @Override + public String toString() { + return "LibraryCard{" + + "cardNumber='" + getFormattedCardNumber() + '\'' + + ", owner=" + owner.getFullName() + + ", valid=" + isValid() + + '}'; + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/java/com/library/Loan.java b/java-junit-maven-0.1.0/src/main/java/com/library/Loan.java new file mode 100644 index 0000000..69ef0dc --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/java/com/library/Loan.java @@ -0,0 +1,135 @@ +package com.library; + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import java.util.Objects; + +public class Loan { + private String id; + private Book book; + private Reader reader; + private LocalDate loanDate; + private LocalDate dueDate; + private LocalDate returnDate; + private static final int LOAN_PERIOD_DAYS = 14; + private static final double FINE_PER_DAY = 0.50; + + public Loan(String id, Book book, Reader reader, LocalDate loanDate) { + this.id = id; + this.book = book; + this.reader = reader; + this.loanDate = loanDate; + this.dueDate = loanDate.plusDays(LOAN_PERIOD_DAYS); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Book getBook() { + return book; + } + + public void setBook(Book book) { + this.book = book; + } + + public Reader getReader() { + return reader; + } + + public void setReader(Reader reader) { + this.reader = reader; + } + + public LocalDate getLoanDate() { + return loanDate; + } + + public void setLoanDate(LocalDate loanDate) { + this.loanDate = loanDate; + } + + public LocalDate getDueDate() { + return dueDate; + } + + public void setDueDate(LocalDate dueDate) { + this.dueDate = dueDate; + } + + public LocalDate getReturnDate() { + return returnDate; + } + + public void setReturnDate(LocalDate returnDate) { + this.returnDate = returnDate; + } + + public boolean isReturned() { + return returnDate != null; + } + + public boolean isOverdue() { + if (isReturned()) { + return returnDate.isAfter(dueDate); + } + return LocalDate.now().isAfter(dueDate); + } + + public long getDaysOverdue() { + if (!isOverdue()) { + return 0; + } + + LocalDate endDate = isReturned() ? returnDate : LocalDate.now(); + return ChronoUnit.DAYS.between(dueDate, endDate); + } + + public double calculateFine() { + return getDaysOverdue() * FINE_PER_DAY; + } + + public int getLoanDuration() { + LocalDate endDate = isReturned() ? returnDate : LocalDate.now(); + return (int) ChronoUnit.DAYS.between(loanDate, endDate); + } + + public void extendLoan(int days) { + if (isReturned()) { + throw new IllegalStateException("Cannot extend returned loan"); + } + if (isOverdue()) { + throw new IllegalStateException("Cannot extend overdue loan"); + } + dueDate = dueDate.plusDays(days); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Loan loan = (Loan) o; + return Objects.equals(id, loan.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Loan{" + + "id='" + id + '\'' + + ", book=" + book.getTitle() + + ", reader=" + reader.getFullName() + + ", dueDate=" + dueDate + + ", returned=" + isReturned() + + '}'; + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/java/com/library/Publisher.java b/java-junit-maven-0.1.0/src/main/java/com/library/Publisher.java new file mode 100644 index 0000000..8ae3e3e --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/java/com/library/Publisher.java @@ -0,0 +1,120 @@ +package com.library; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +public class Publisher { + private String id; + private String name; + private String address; + private String country; + private LocalDate foundedDate; + private List publishedBooks; + + public Publisher(String id, String name, String address, String country, LocalDate foundedDate) { + this.id = id; + this.name = name; + this.address = address; + this.country = country; + this.foundedDate = foundedDate; + this.publishedBooks = new ArrayList<>(); + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public LocalDate getFoundedDate() { + return foundedDate; + } + + public void setFoundedDate(LocalDate foundedDate) { + this.foundedDate = foundedDate; + } + + public List getPublishedBooks() { + return new ArrayList<>(publishedBooks); + } + + public void addPublishedBook(Book book) { + if (!publishedBooks.contains(book)) { + publishedBooks.add(book); + } + } + + public void removePublishedBook(Book book) { + publishedBooks.remove(book); + } + + public int getPublishedBooksCount() { + return publishedBooks.size(); + } + + public int getYearsInBusiness() { + return LocalDate.now().getYear() - foundedDate.getYear(); + } + + public List getBooksByYear(int year) { + return publishedBooks.stream() + .filter(book -> book.getPublicationDate().getYear() == year) + .collect(Collectors.toList()); + } + + public boolean isEstablished() { + return getYearsInBusiness() >= 10; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Publisher publisher = (Publisher) o; + return Objects.equals(id, publisher.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Publisher{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + ", country='" + country + '\'' + + ", yearsInBusiness=" + getYearsInBusiness() + + '}'; + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/java/com/library/Reader.java b/java-junit-maven-0.1.0/src/main/java/com/library/Reader.java new file mode 100644 index 0000000..3b9c65e --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/java/com/library/Reader.java @@ -0,0 +1,142 @@ +package com.library; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +public class Reader { + private String id; + private String firstName; + private String lastName; + private String email; + private String phone; + private LocalDate registrationDate; + private LibraryCard libraryCard; + private List loanHistory; + private boolean active; + + public Reader(String id, String firstName, String lastName, String email, String phone) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + this.phone = phone; + this.registrationDate = LocalDate.now(); + this.loanHistory = new ArrayList<>(); + this.active = true; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getFullName() { + return firstName + " " + lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public LocalDate getRegistrationDate() { + return registrationDate; + } + + public void setRegistrationDate(LocalDate registrationDate) { + this.registrationDate = registrationDate; + } + + public LibraryCard getLibraryCard() { + return libraryCard; + } + + public void setLibraryCard(LibraryCard libraryCard) { + this.libraryCard = libraryCard; + } + + public List getLoanHistory() { + return new ArrayList<>(loanHistory); + } + + public void addLoan(Loan loan) { + loanHistory.add(loan); + } + + public List getActiveLoans() { + return loanHistory.stream() + .filter(loan -> loan.getReturnDate() == null) + .collect(Collectors.toList()); + } + + public boolean isActive() { + return active; + } + + public void setActive(boolean active) { + this.active = active; + } + + public int getMembershipDays() { + return (int) registrationDate.until(LocalDate.now()).toTotalMonths() * 30; + } + + public boolean canBorrow() { + return active && getActiveLoans().size() < 5; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Reader reader = (Reader) o; + return Objects.equals(id, reader.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } + + @Override + public String toString() { + return "Reader{" + + "id='" + id + '\'' + + ", fullName='" + getFullName() + '\'' + + ", email='" + email + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/java/com/library/util/BookUtils.java b/java-junit-maven-0.1.0/src/main/java/com/library/util/BookUtils.java new file mode 100644 index 0000000..b891645 --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/java/com/library/util/BookUtils.java @@ -0,0 +1,168 @@ +package com.library.util; + +import com.library.Author; +import com.library.Book; +import com.library.Genre; +import java.time.LocalDate; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.stream.Collectors; + +public class BookUtils { + + private BookUtils() { + // Private constructor to prevent instantiation + } + + public static boolean isValidIsbn(String isbn) { + if (isbn == null || isbn.isEmpty()) { + return false; + } + + String cleanIsbn = isbn.replaceAll("[^0-9X]", ""); + + if (cleanIsbn.length() == 10) { + return isValidIsbn10(cleanIsbn); + } else if (cleanIsbn.length() == 13) { + return isValidIsbn13(cleanIsbn); + } + + return false; + } + + private static boolean isValidIsbn10(String isbn) { + int sum = 0; + for (int i = 0; i < 9; i++) { + sum += (isbn.charAt(i) - '0') * (10 - i); + } + + char lastChar = isbn.charAt(9); + if (lastChar == 'X') { + sum += 10; + } else { + sum += lastChar - '0'; + } + + return sum % 11 == 0; + } + + private static boolean isValidIsbn13(String isbn) { + int sum = 0; + for (int i = 0; i < 13; i++) { + int digit = isbn.charAt(i) - '0'; + sum += (i % 2 == 0) ? digit : digit * 3; + } + return sum % 10 == 0; + } + + public static List sortByTitle(List books) { + return books.stream() + .sorted(Comparator.comparing(Book::getTitle)) + .collect(Collectors.toList()); + } + + public static List sortByPublicationDate(List books) { + return books.stream() + .sorted(Comparator.comparing(Book::getPublicationDate)) + .collect(Collectors.toList()); + } + + public static List filterByGenre(List books, Genre genre) { + return books.stream() + .filter(book -> book.getGenre().equals(genre)) + .collect(Collectors.toList()); + } + + public static List filterByAuthor(List books, Author author) { + return books.stream() + .filter(book -> book.getAuthor().equals(author)) + .collect(Collectors.toList()); + } + + public static List filterByYearRange(List books, int startYear, int endYear) { + return books.stream() + .filter(book -> { + int year = book.getPublicationDate().getYear(); + return year >= startYear && year <= endYear; + }) + .collect(Collectors.toList()); + } + + public static Map> groupByGenre(List books) { + return books.stream() + .collect(Collectors.groupingBy(Book::getGenre)); + } + + public static Map> groupByAuthor(List books) { + return books.stream() + .collect(Collectors.groupingBy(Book::getAuthor)); + } + + public static double calculateAveragePages(List books) { + if (books.isEmpty()) { + return 0; + } + return books.stream() + .mapToInt(Book::getPages) + .average() + .orElse(0); + } + + public static Book findOldestBook(List books) { + return books.stream() + .min(Comparator.comparing(Book::getPublicationDate)) + .orElse(null); + } + + public static Book findNewestBook(List books) { + return books.stream() + .max(Comparator.comparing(Book::getPublicationDate)) + .orElse(null); + } + + public static List searchByTitle(List books, String keyword) { + String lowerKeyword = keyword.toLowerCase(); + return books.stream() + .filter(book -> book.getTitle().toLowerCase().contains(lowerKeyword)) + .collect(Collectors.toList()); + } + + public static int countAvailableBooks(List books) { + return (int) books.stream() + .filter(Book::isAvailable) + .count(); + } + + public static List getRecentBooks(List books, int years) { + LocalDate cutoffDate = LocalDate.now().minusYears(years); + return books.stream() + .filter(book -> book.getPublicationDate().isAfter(cutoffDate)) + .collect(Collectors.toList()); + } + + public static String generateIsbn13() { + Random random = new Random(); + StringBuilder isbn = new StringBuilder("978"); + + for (int i = 0; i < 9; i++) { + isbn.append(random.nextInt(10)); + } + + int checksum = calculateIsbn13Checksum(isbn.toString()); + isbn.append(checksum); + + return isbn.toString(); + } + + private static int calculateIsbn13Checksum(String isbn12) { + int sum = 0; + for (int i = 0; i < 12; i++) { + int digit = isbn12.charAt(i) - '0'; + sum += (i % 2 == 0) ? digit : digit * 3; + } + int remainder = sum % 10; + return (remainder == 0) ? 0 : 10 - remainder; + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/main/resources/junit-platform.properties b/java-junit-maven-0.1.0/src/main/resources/junit-platform.properties new file mode 100644 index 0000000..8249188 --- /dev/null +++ b/java-junit-maven-0.1.0/src/main/resources/junit-platform.properties @@ -0,0 +1,7 @@ +junit.jupiter.extensions.autodetection.enabled = true + +junit.jupiter.execution.parallel.enabled=true +junit.jupiter.execution.parallel.mode.default=concurrent +junit.jupiter.execution.parallel.mode.classes.default=concurrent +junit.jupiter.execution.parallel.config.strategy=dynamic +junit.jupiter.execution.parallel.config.dynamic.factor=1.0 \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/test/java/library/AuthorTest.java b/java-junit-maven-0.1.0/src/test/java/library/AuthorTest.java new file mode 100644 index 0000000..4269075 --- /dev/null +++ b/java-junit-maven-0.1.0/src/test/java/library/AuthorTest.java @@ -0,0 +1,218 @@ +package library; + + +import com.library.Author; +import com.library.Book; +import com.library.Genre; +import com.library.Publisher; +import com.testomatio.reporter.annotation.TestId; +import com.testomatio.reporter.annotation.Title; +import java.time.LocalDate; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Execution(ExecutionMode.CONCURRENT) +public class AuthorTest { + + private Author author; + private Book book1; + private Book book2; + + @BeforeEach + void setUp() { + author = new Author("A001", "John", "Doe", LocalDate.of(1970, 1, 1), "USA"); + Publisher publisher = new Publisher("P001", "Test Publisher", "123 Main St", "USA", LocalDate.of(2000, 1, 1)); + Genre genre = new Genre("G001", "Fiction", "Fictional works"); + book1 = new Book("978-1-11111-11", "Book 1", author, publisher, genre, LocalDate.of(2020, 1, 1), 300); + book2 = new Book("978-2-22222-22-2", "Book 2", author, publisher, genre, LocalDate.of(2021, 1, 1), 400); + } + + @Test + @TestId("TJunit19") + @Title("testAuthorCreation JUnit") + @Disabled + @Execution(ExecutionMode.CONCURRENT) + void testAuthorCreation() { + assertNotNull(author); + assertEquals("A001", author.getId()); + assertEquals("John", author.getFirstName()); + assertEquals("Doe", author.getLastName()); + } + + @Test + @TestId("TJunit20") + @Title("testGetFullName JUnit") + @Execution(ExecutionMode.CONCURRENT) + @Disabled + void testGetFullName() { + assertEquals("Jon Doe", author.getFullName()); + } + + @Test + @TestId("TJunit21") + @Title("testAddBook JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testAddBook() { + author.addBook(book1); + assertEquals(1, author.getBookCount()); + assertTrue(author.getBooks().contains(book1)); + } + + @Test + @TestId("TJunit22") + @Title("testAddDuplicateBook JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testAddDuplicateBook() { + author.addBook(book1); + author.addBook(book1); + assertEquals(1, author.getBookCount()); + } + + @Test + @TestId("TJunit23") + @Title("testRemoveBook JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testRemoveBook() { + author.addBook(book1); + author.addBook(book2); + author.removeBook(book1); + assertEquals(1, author.getBookCount()); + assertFalse(author.getBooks().contains(book1)); + } + + @Test + @TestId("TJunit24") + @Title("testGetAge JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetAge() { + int expectedAge = LocalDate.now().getYear() - 1972; + assertEquals(expectedAge, author.getAge()); + } + + @Test + @TestId("TJunit25") + @Title("testGetBooksReturnsCopy JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetBooksReturnsCopy() { + author.addBook(book1); + var books = author.getBooks(); + books.clear(); + assertEquals(1, author.getBookCount()); + } + + @Test + @TestId("TJunit26") + @Title("testEqualsWithSameId JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithSameId() { + Author author2 = new Author("A001", "Jane", "Smith", LocalDate.of(1980, 1, 1), "UK"); + assertEquals(author, author2); + } + + @Test + @TestId("TJunit27") + @Title("testEqualsWithDifferentId JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithDifferentId() { + Author author2 = new Author("A002", "John", "Doe", LocalDate.of(1970, 1, 1), "USA"); + assertNotEquals(author, author2); + } + + @Test + @TestId("TJunit28") + @Title("testHashCode JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testHashCode() { + Author author2 = new Author("A001", "Jane", "Smith", LocalDate.of(1980, 1, 1), "UK"); + assertEquals(author.hashCode(), author2.hashCode()); + } + + @Test + @TestId("TJunit29") + @Title("testToString JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testToString() { + String result = author.toString(); + assertTrue(result.contains("A001")); + assertTrue(result.contains("John Doe")); + assertTrue(result.contains("USA")); + } + + @Test + @TestId("TJunit30") + @Title("testSetters JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetters() { + author.setId("A002"); + author.setFirstName("Jane"); + author.setLastName("Smith"); + author.setNationality("UK"); + + assertEquals("A002", author.getId()); + assertEquals("Jane", author.getFirstName()); + assertEquals("Smith", author.getLastName()); + assertEquals("UK", author.getNationality()); + } + + @Test + @TestId("TJunit31") + @Title("testSetBirthDate JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetBirthDate() { + LocalDate newDate = LocalDate.of(1985, 5, 15); + author.setBirthDate(newDate); + assertEquals(newDate, author.getBirthDate()); + } + + @Test + @TestId("TJunit32") + @Title("testEmptyBooksList JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEmptyBooksList() { + assertEquals(0, author.getBookCount()); + assertTrue(author.getBooks().isEmpty()); + } + + @Test + @TestId("TJunit33") + @Title("testMultipleBooks JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testMultipleBooks() { + author.addBook(book1); + author.addBook(book2); + assertEquals(2, author.getBookCount()); + } + + @Test + @TestId("TJunit34") + @Title("testEqualsWithNull JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithNull() { + assertNotEquals(null, author); + } + + @Test + @TestId("TJunit35") + @Title("testEqualsWithSameObject JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithSameObject() { + assertEquals(author, author); + } + + @Test + @TestId("TJunit36") + @Title("testEqualsWithDifferentClass JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithDifferentClass() { + assertNotEquals("Not an author", author); + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/test/java/library/BookTest.java b/java-junit-maven-0.1.0/src/test/java/library/BookTest.java new file mode 100644 index 0000000..dde23fb --- /dev/null +++ b/java-junit-maven-0.1.0/src/test/java/library/BookTest.java @@ -0,0 +1,203 @@ +package library; + +import com.library.Author; +import com.library.Book; +import com.library.Genre; +import com.library.Publisher; +import com.testomatio.reporter.annotation.TestId; +import com.testomatio.reporter.annotation.Title; +import java.time.LocalDate; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Execution(ExecutionMode.CONCURRENT) +public class BookTest { + + private Book book; + private Author author; + private Publisher publisher; + private Genre genre; + + @BeforeEach + void setUp() { + author = new Author("A001", "John", "Doe", LocalDate.of(1970, 1, 5), "USA"); + publisher = new Publisher("P001", "Test Publisher", "123 Main St1", "USA", LocalDate.of(2000, 1, 1)); + genre = new Genre("G001", "Fiction", "Fictional works1"); + book = new Book("978-3-16-148410-0", "Test Book1", author, publisher, genre, LocalDate.of(2020, 1, 1), 300); + } + + @Test + @TestId("TJunit1") + @Title("testBookCreation JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testBookCreation() { + assertNotNull(book); + assertEquals("978-3-16-148410", book.getIsbn()); + assertEquals("Test Book", book.getTitle()); + } + + @Test + @TestId("TJunit2") + @Title("testDefaultAvailability JUnit") + @Disabled + @Execution(ExecutionMode.CONCURRENT) + void testDefaultAvailability() { + assertTrue(book.isAvailable()); + } + + @Test + @TestId("TJunit3") + @Title("testSetAvailability JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetAvailability() { + assertTrue(false); + } + + @Test + @TestId("TJunit4") + @Title("testGetAge JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetAge() { + assertTrue(false); + } + + @Test + @TestId("TJunit5") + @Title("testGettersAndSetters JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGettersAndSetters() { + book.setTitle("New Title"); + assertEquals("New Title", book.getTitle()); + + book.setPages(400); + assertEquals(400, book.getPages()); + } + + @Test + @TestId("TJunit6") + @Title("testEqualsWithSameIsbn JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithSameIsbn() { + Book book2 = new Book("978-3-16-148410-0", "Different Title", author, publisher, genre, LocalDate.of(2021, 1, 1), 200); + assertEquals(book, book2); + } + + @Test + @TestId("TJunit7") + @Title("testEqualsWithDifferentIsbn JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithDifferentIsbn() { + Book book2 = new Book("978-3-16-148410-1", "Test Book", author, publisher, genre, LocalDate.of(2020, 1, 1), 300); + assertNotEquals(book, book2); + } + + @Test + @TestId("TJunit8") + @Title("testHashCode JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testHashCode() { + Book book2 = new Book("978-3-16-148410-0", "Different Title", author, publisher, genre, LocalDate.of(2021, 1, 1), 200); + assertEquals(book.hashCode(), book2.hashCode()); + } + + @Test + @TestId("TJunit9") + @Title("testToString JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testToString() { + String result = book.toString(); + assertTrue(result.contains("978-3-16-148410-0")); + assertTrue(result.contains("Test Book")); + } + + @Test + @TestId("TJunit10") + @Title("testSetAuthor JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetAuthor() { + Author newAuthor = new Author("A002", "Jane", "Smith", LocalDate.of(1980, 1, 1), "UK"); + book.setAuthor(newAuthor); + assertEquals(newAuthor, book.getAuthor()); + } + + @Test + @TestId("TJunit11") + @Title("testSetPublisher JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetPublisher() { + Publisher newPublisher = new Publisher("P002", "New Publisher", "456 Oak St", "UK", LocalDate.of(2010, 1, 1)); + book.setPublisher(newPublisher); + assertEquals(newPublisher, book.getPublisher()); + } + + @Test + @TestId("TJunit12") + @Title("testSetGenre JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetGenre() { + Genre newGenre = new Genre("G002", "Non-Fiction", "Non-fictional works"); + book.setGenre(newGenre); + assertEquals(newGenre, book.getGenre()); + } + + @Test + @TestId("TJunit13") + @Title("testSetPublicationDate JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetPublicationDate() { + LocalDate newDate = LocalDate.of(2022, 6, 15); + book.setPublicationDate(newDate); + assertEquals(newDate, book.getPublicationDate()); + } + + @Test + @TestId("TJunit14") + @Title("testEqualsWithNull JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithNull() { + assertNotEquals(null, book); + } + + @Test + @TestId("TJunit15") + @Title("testEqualsWithSameObject JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithSameObject() { + assertEquals(book, book); + } + + @Test + @TestId("TJunit16") + @Title("testEqualsWithDifferentClass JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithDifferentClass() { + assertNotEquals("Not a book", book); + } + + @Test + @TestId("TJunit17") + @Title("testSetIsbn JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetIsbn() { + book.setIsbn("978-0-123456-78-9"); + assertEquals("978-0-123456-78-9", book.getIsbn()); + } + + @Test + @TestId("TJunit18") + @Title("testBookWithZeroPages JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testBookWithZeroPages() { + Book zeroPageBook = new Book("978-0-000000-00-0", "Zero Pages", author, publisher, genre, LocalDate.of(2020, 1, 1), 0); + assertEquals(0, zeroPageBook.getPages()); + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/test/java/library/LibraryTest.java b/java-junit-maven-0.1.0/src/test/java/library/LibraryTest.java new file mode 100644 index 0000000..7d10a56 --- /dev/null +++ b/java-junit-maven-0.1.0/src/test/java/library/LibraryTest.java @@ -0,0 +1,261 @@ +package library; + +import com.library.Author; +import com.library.Book; +import com.library.Genre; +import com.library.Library; +import com.library.Loan; +import com.library.Publisher; +import com.library.Reader; +import com.testomatio.reporter.annotation.TestId; +import com.testomatio.reporter.annotation.Title; +import java.time.LocalDate; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +import static org.junit.jupiter.api.Assertions.*; + +@Execution(ExecutionMode.CONCURRENT) +public class LibraryTest { + + private Library library; + private Book book; + private Reader reader; + private Author author; + private Publisher publisher; + private Genre genre; + + @BeforeEach + void setUp() { + library = new Library("Central Library", "123 Main St"); + + author = new Author("A001", "n", "Doe", LocalDate.of(1970, 1, 1), "USA"); + publisher = new Publisher("P001", "Test Publisher", "123 Main St", "USA", LocalDate.of(2000, 1, 1)); + genre = new Genre("G001", "Fiction", "Fictional"); + book = new Book("978-1-11111-11-1", "Test Book", author, publisher, genre, LocalDate.of(2020, 1, 1), 300); + + reader = new Reader("R001", "Alice", "Johnson", "alice@example.com", "123-456-7890"); + } + + @Test + @TestId("TJunit55") + @Title("testLibraryCreation JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testLibraryCreation() { + assertNotNull(library); + assertEquals("Central Library", library.getName()); + assertEquals("123 Main St", library.getAddress()); + } + + @Test + @TestId("TJunit56") + @Title("testAddBook JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testAddBook() { + library.addBook(book); + assertEquals(7, library.getTotalBookCount()); + assertEquals(book, library.getBook(book.getIsbn())); + } + + @Test + @TestId("TJunit57") + @Title("testRemoveBook JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testRemoveBook() { + library.addBook(book); + library.removeBook(book.getIsbn()); + assertEquals(0, library.getTotalBookCount()); + assertNull(library.getBook(book.getIsbn())); + } + + @Test + @TestId("TJunit58") + @Title("testRegisterReader JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testRegisterReader() { + library.registerReader(reader); + assertEquals(reader, library.getReader(reader.getId())); + assertEquals(1, library.getActiveReaderCount()); + } + + @Test + @TestId("TJunit59") + @Title("testRemoveReader JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testRemoveReader() { + library.registerReader(reader); + library.removeReader(reader.getId()); + assertNull(library.getReader(reader.getId())); + } + + @Test + @TestId("TJunit60") + @Title("testLoanBook JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testLoanBook() { + library.addBook(book); + library.registerReader(reader); + + Loan loan = library.loanBook(reader.getId(), book.getIsbn()); + assertNotNull(loan); + assertFalse(book.isAvailable()); + assertEquals(1, library.getLoans().size()); + } + + @Test + @TestId("TJunit61") + @Title("testLoanBookWithInvalidReader JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testLoanBookWithInvalidReader() { + library.addBook(book); + assertThrows(IllegalArgumentException.class, () -> + library.loanBook("INVALID", book.getIsbn()) + ); + } + + @Test + @TestId("TJunit62") + @Title("testLoanBookWithInvalidBook JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testLoanBookWithInvalidBook() { + library.registerReader(reader); + assertThrows(IllegalArgumentException.class, () -> + library.loanBook(reader.getId(), "INVALID") + ); + } + + @Test + @TestId("TJunit63") + @Title("testLoanUnavailableBook JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testLoanUnavailableBook() { + library.addBook(book); + library.registerReader(reader); + book.setAvailable(false); + + assertThrows(IllegalStateException.class, () -> + library.loanBook(reader.getId(), book.getIsbn()) + ); + } + + @Test + @TestId("TJunit64") + @Title("testReturnBook JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testReturnBook() { + library.addBook(book); + library.registerReader(reader); + Loan loan = library.loanBook(reader.getId(), book.getIsbn()); + + library.returnBook(loan.getId()); + assertTrue(book.isAvailable()); + assertNotNull(loan.getReturnDate()); + } + + @Test + @TestId("TJunit65") + @Title("testGetAvailableBooks JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetAvailableBooks() { + Book book2 = new Book("978-2-22222-22-2", "Book 2", author, publisher, genre, LocalDate.of(2021, 1, 1), 400); + library.addBook(book); + library.addBook(book2); + library.registerReader(reader); + library.loanBook(reader.getId(), book.getIsbn()); + + var available = library.getAvailableBooks(); + assertEquals(1, available.size()); + assertTrue(available.contains(book2)); + } + + @Test + @TestId("TJunit66") + @Title("testGetBooksByGenre JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetBooksByGenre() { + Genre genre2 = new Genre("G002", "Non-Fiction", "Non-fictional works"); + Book book2 = new Book("978-2-22222-22-2", "Book 2", author, publisher, genre2, LocalDate.of(2021, 1, 1), 400); + + library.addBook(book); + library.addBook(book2); + + var fictionBooks = library.getBooksByGenre(genre); + assertEquals(1, fictionBooks.size()); + assertTrue(fictionBooks.contains(book)); + } + + @Test + @TestId("TJunit67") + @Title("testGetBooksByAuthor JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetBooksByAuthor() { + Author author2 = new Author("A002", "Jane", "Smith", LocalDate.of(1980, 1, 1), "UK"); + Book book2 = new Book("978-2-22222-22-2", "Book 2", author2, publisher, genre, LocalDate.of(2021, 1, 1), 400); + + library.addBook(book); + library.addBook(book2); + + var johnDoeBooks = library.getBooksByAuthor(author); + assertEquals(1, johnDoeBooks.size()); + assertTrue(johnDoeBooks.contains(book)); + } + + @Test + @TestId("TJunit68") + @Title("testGetOverdueLoans JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetOverdueLoans() { + library.addBook(book); + library.registerReader(reader); + Loan loan = library.loanBook(reader.getId(), book.getIsbn()); + loan.setDueDate(LocalDate.now().minusDays(5)); + + var overdueLoans = library.getOverdueLoans(); + assertEquals(1, overdueLoans.size()); + assertTrue(overdueLoans.contains(loan)); + } + + @Test + @TestId("TJunit69") + @Title("testSetName JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetName() { + library.setName("New Library Name"); + assertEquals("New Library Name", library.getName()); + } + + @Test + @TestId("TJunit70") + @Title("testSetAddress JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetAddress() { + library.setAddress("456 Oak St"); + assertEquals("456 Oak St", library.getAddress()); + } + + @Test + @TestId("TJunit71") + @Title("testToString JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testToString() { + library.addBook(book); + library.registerReader(reader); + + String result = library.toString(); + assertTrue(result.contains("Central Library")); + assertTrue(result.contains("totalBooks=1")); + assertTrue(result.contains("totalReaders=1")); + } + + @Test + @TestId("TJunit72") + @Title("testReturnInvalidLoan JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testReturnInvalidLoan() { + assertThrows(IllegalArgumentException.class, () -> + library.returnBook("INVALID-LOAN-ID") + ); + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/test/java/library/LoanTest.java b/java-junit-maven-0.1.0/src/test/java/library/LoanTest.java new file mode 100644 index 0000000..f8cc523 --- /dev/null +++ b/java-junit-maven-0.1.0/src/test/java/library/LoanTest.java @@ -0,0 +1,213 @@ +package library; + +import com.library.Author; +import com.library.Book; +import com.library.Genre; +import com.library.Loan; +import com.library.Publisher; +import com.library.Reader; +import com.testomatio.reporter.annotation.TestId; +import com.testomatio.reporter.annotation.Title; +import java.time.LocalDate; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Execution(ExecutionMode.CONCURRENT) +public class LoanTest { + + private Loan loan; + private Book book; + private Reader reader; + + @BeforeEach + void setUp() { + Author author = new Author("A001", "John", "Doe", LocalDate.of(1970, 1, 1), "USA"); + Publisher publisher = new Publisher("P001", "Test Publisher", "123 Main St", "USA", LocalDate.of(2000, 1, 1)); + Genre genre = new Genre("G001", "Fiction", "Fictional works"); + book = new Book("978-1-11111-11-1", "Test Book", author, publisher, genre, LocalDate.of(2020, 1, 1), 300); + + reader = new Reader("R001", "Alice", "Johnson", "alice@example.com", "123-456-7890"); + loan = new Loan("L001", book, reader, LocalDate.now()); + } + + @Test + @TestId("TJunit73") + @Title("testLoanCreation JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testLoanCreation() { + assertNotNull(loan); + assertEquals("L001", loan.getId()); + assertEquals(book, loan.getBook()); + assertEquals(reader, loan.getReader()); + assertEquals(LocalDate.now(), loan.getLoanDate()); + assertEquals(LocalDate.now().plusDays(14), loan.getDueDate()); + assertNull(loan.getReturnDate()); + } + + @Test + @TestId("TJunit74") + @Title("testIsReturned JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testIsReturned() { + assertFalse(loan.isReturned()); + loan.setReturnDate(LocalDate.now()); + assertTrue(!loan.isReturned()); + } + + @Test + @TestId("TJunit75") + @Title("testIsOverdueNotReturned JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testIsOverdueNotReturned() { + loan.setDueDate(LocalDate.now().minusDays(1)); + assertTrue(!loan.isOverdue()); + } + + @Test + @TestId("TJunit76") + @Title("testIsOverdueReturned JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testIsOverdueReturned() { + loan.setDueDate(LocalDate.now().minusDays(5)); + loan.setReturnDate(LocalDate.now()); + assertTrue(loan.isOverdue()); + } + + @Test + @TestId("TJunit77") + @Title("testIsNotOverdue JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testIsNotOverdue() { + assertFalse(!loan.isOverdue()); + } + + @Test + @TestId("TJunit78") + @Title("testGetDaysOverdue JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetDaysOverdue() { + loan.setDueDate(LocalDate.now().minusDays(3)); + assertEquals(3, loan.getDaysOverdue()); + } + + @Test + @TestId("TJunit79") + @Title("testGetDaysOverdueNotOverdue JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetDaysOverdueNotOverdue() { + assertEquals(0, loan.getDaysOverdue()); + } + + @Test + @TestId("TJunit80") + @Title("testCalculateFine JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testCalculateFine() { + loan.setDueDate(LocalDate.now().minusDays(5)); + assertEquals(2.50, loan.calculateFine() + 1, 0.01); + } + + @Test + @TestId("TJunit81") + @Title("testCalculateFineNoOverdue JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testCalculateFineNoOverdue() { + assertEquals(0.0, loan.calculateFine(), 0.01); + } + + @Test + @TestId("TJunit82") + @Title("testGetLoanDuration JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetLoanDuration() { + loan.setLoanDate(LocalDate.now().minusDays(10)); + assertEquals(10, loan.getLoanDuration()); + } + + @Test + @TestId("TJunit83") + @Title("testGetLoanDurationReturned JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetLoanDurationReturned() { + loan.setLoanDate(LocalDate.now().minusDays(10)); + loan.setReturnDate(LocalDate.now().minusDays(5)); + assertEquals(5, loan.getLoanDuration()); + } + + @Test + @TestId("TJunit84") + @Title("testExtendLoan JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testExtendLoan() { + LocalDate originalDueDate = loan.getDueDate(); + loan.extendLoan(7); + assertEquals(originalDueDate.plusDays(8), loan.getDueDate()); + } + + @Test + @TestId("TJunit85") + @Title("testExtendReturnedLoan JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testExtendReturnedLoan() { + loan.setReturnDate(LocalDate.now()); + assertThrows(IllegalStateException.class, () -> loan.extendLoan(7)); + } + + @Test + @TestId("TJunit86") + @Title("testExtendOverdueLoan JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testExtendOverdueLoan() { + loan.setDueDate(LocalDate.now().minusDays(1)); + assertThrows(IllegalStateException.class, () -> loan.extendLoan(7)); + } + + @Test + @TestId("TJunit87") + @Title("testEqualsWithSameId JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithSameId() { + Loan loan2 = new Loan("L001", book, reader, LocalDate.now().minusDays(5)); + assertEquals(loan, loan2); + } + + @Test + @TestId("TJunit88") + @Title("testEqualsWithDifferentId JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithDifferentId() { + Loan loan2 = new Loan("L002", book, reader, LocalDate.now()); + assertNotEquals(loan, loan2); + } + + @Test + @TestId("TJunit89") + @Title("testHashCode JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testHashCode() { + Loan loan2 = new Loan("L001", book, reader, LocalDate.now().minusDays(5)); + assertEquals(loan.hashCode(), loan2.hashCode()); + } + + @Test + @TestId("TJunit90") + @Title("testToString JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testToString() { + String result = loan.toString(); + assertTrue(result.contains("L001")); + assertTrue(result.contains("Test Book")); + assertTrue(result.contains("Alice Johnson")); + assertTrue(result.contains("returned=false")); + } +} \ No newline at end of file diff --git a/java-junit-maven-0.1.0/src/test/java/library/ReaderTest.java b/java-junit-maven-0.1.0/src/test/java/library/ReaderTest.java new file mode 100644 index 0000000..beb8ca8 --- /dev/null +++ b/java-junit-maven-0.1.0/src/test/java/library/ReaderTest.java @@ -0,0 +1,222 @@ +package library; + +import com.library.Author; +import com.library.Book; +import com.library.Genre; +import com.library.LibraryCard; +import com.library.Loan; +import com.library.Publisher; +import com.library.Reader; +import com.testomatio.reporter.annotation.TestId; +import com.testomatio.reporter.annotation.Title; +import java.time.LocalDate; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; + +import static org.junit.jupiter.api.Assertions.*; + +@Execution(ExecutionMode.CONCURRENT) +public class ReaderTest { + + private Reader reader; + private Loan loan1; + private Loan loan2; + + @BeforeEach + void setUp() { + reader = new Reader("R001", "Alice", "Johnson", "alice@example.com", "123-456-7890"); + + Author author = new Author("A001", "John", "Doe", LocalDate.of(1970, 1, 1), "USA"); + Publisher publisher = new Publisher("P001", "Test Publisher", "123 Main St", "USA", LocalDate.of(2000, 1, 1)); + Genre genre = new Genre("G001", "Fiction", "Fictional works"); + Book book1 = new Book("978-1-11111-11-1", "Book 1", author, publisher, genre, LocalDate.of(2020, 1, 1), 300); + Book book2 = new Book("978-2-22222-22-3", "Book 2", author, publisher, genre, LocalDate.of(2021, 1, 1), 400); + + loan1 = new Loan("L001", book1, reader, LocalDate.now()); + loan2 = new Loan("L002", book2, reader, LocalDate.now().minusDays(20)); + } + + @Test + @TestId("TJunit37") + @Title("testReaderCreation JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testReaderCreation() { + assertNotNull(reader); + assertEquals("R001", reader.getId()); + assertEquals("Alice", reader.getFirstName()); + assertEquals("Johnson", reader.getLastName()); + assertTrue(reader.isActive()); + } + + @Test + @TestId("TJunit38") + @Title("testGetFullName JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetFullName() { + assertEquals("Alice Johnson", reader.getFullName()); + } + + @Test + @TestId("TJunit39") + @Title("testRegistrationDate JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testRegistrationDate() { + assertEquals(LocalDate.now(), reader.getRegistrationDate()); + } + + @Test + @TestId("TJunit40") + @Title("testAddLoan JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testAddLoan() { + reader.addLoan(loan1); + assertEquals(1, reader.getLoanHistory().size()); + assertTrue(reader.getLoanHistory().contains(loan1)); + } + + @Test + @TestId("TJunit41") + @Title("testGetActiveLoans JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetActiveLoans() { + reader.addLoan(loan1); + reader.addLoan(loan2); + loan2.setReturnDate(LocalDate.now()); + + var activeLoans = reader.getActiveLoans(); + assertEquals(1, activeLoans.size()); + assertTrue(activeLoans.contains(loan1)); + } + + @Test + @TestId("TJunit42") + @Title("testCanBorrowWhenActive JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testCanBorrowWhenActive() { + assertTrue(reader.canBorrow()); + } + + @Test + @TestId("TJunit43") + @Title("testCanBorrowWhenInactive JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testCanBorrowWhenInactive() { + reader.setActive(false); + assertFalse(reader.canBorrow()); + } + + @Test + @TestId("TJunit44") + @Title("testCanBorrowWithMaxLoans JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testCanBorrowWithMaxLoans() { + for (int i = 0; i < 5; i++) { + reader.addLoan(new Loan("L00" + i, null, reader, LocalDate.now())); + } + assertFalse(reader.canBorrow()); + } + + @Test + @TestId("TJunit45") + @Title("testSetLibraryCard JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetLibraryCard() { + LibraryCard card = new LibraryCard("1234567890123456", reader); + reader.setLibraryCard(card); + assertEquals(card, reader.getLibraryCard()); + } + + @Test + @TestId("TJunit46") + @Title("testGetMembershipDays JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testGetMembershipDays() { + reader.setRegistrationDate(LocalDate.now().minusMonths(3)); + assertTrue(reader.getMembershipDays() >= 90); + } + + @Test + @TestId("TJunit47") + @Title("testEqualsWithSameId JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithSameId() { + Reader reader2 = new Reader("R001", "Bob", "Smith", "bob@example.com", "987-654-3210"); + assertEquals(reader, reader2); + } + + @Test + @TestId("TJunit48") + @Title("testEqualsWithDifferentId JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithDifferentId() { + Reader reader2 = new Reader("R002", "Alice", "Johnson", "alice@example.com", "123-456-7890"); + assertNotEquals(reader, reader2); + } + + @Test + @TestId("TJunit49") + @Title("testHashCode JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testHashCode() { + Reader reader2 = new Reader("R001", "Bob", "Smith", "bob@example.com", "987-654-3210"); + assertEquals(reader.hashCode(), reader2.hashCode()); + } + + @Test + @TestId("TJunit50") + @Title("testToString JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testToString() { + String result = reader.toString(); + assertTrue(result.contains("R001")); + assertTrue(result.contains("Alice Johnson")); + assertTrue(result.contains("alice@example.com")); + } + + @Test + @TestId("TJunit51") + @Title("testSetters JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testSetters() { + reader.setId("R002"); + reader.setFirstName("Bob"); + reader.setLastName("Smith"); + reader.setEmail("bob@example.com"); + reader.setPhone("987-654-3210"); + + assertEquals("R002", reader.getId()); + assertEquals("Bob", reader.getFirstName()); + assertEquals("Smith", reader.getLastName()); + assertEquals("bob@example.com", reader.getEmail()); + assertEquals("987-654-3210", reader.getPhone()); + } + + @Test + @TestId("TJunit52") + @Title("testLoanHistoryReturnsCopy JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testLoanHistoryReturnsCopy() { + reader.addLoan(loan1); + var history = reader.getLoanHistory(); + history.clear(); + assertEquals(1, reader.getLoanHistory().size()); + } + + @Test + @TestId("TJunit53") + @Title("testEqualsWithNull JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithNull() { + assertNotEquals(null, reader); + } + + @Test + @TestId("TJunit54") + @Title("testEqualsWithSameObject JUnit") + @Execution(ExecutionMode.CONCURRENT) + void testEqualsWithSameObject() { + assertEquals(reader, reader); + } +} \ No newline at end of file