diff --git a/.sonarlint/connectedMode.json b/.sonarlint/connectedMode.json new file mode 100644 index 000000000000..51e27a33f8a0 --- /dev/null +++ b/.sonarlint/connectedMode.json @@ -0,0 +1,4 @@ +{ + "sonarCloudOrganization": "iluwatar", + "projectKey": "iluwatar_java-design-patterns" +} \ No newline at end of file diff --git a/view-helper/README.md b/view-helper/README.md new file mode 100644 index 000000000000..e9e9e4cb01d8 --- /dev/null +++ b/view-helper/README.md @@ -0,0 +1,214 @@ + +--- +title: "View Helper Pattern in Java: Optimizing Data Rendering" +shortTitle: View Helper +description: "Explore the View Helper Design Pattern in Java to optimize the process of data rendering. Learn how this pattern separates the logic of data processing and presentation, making the application more maintainable and scalable." +category: Structural +language: en +tag: + - Separation of concerns + - Data formatting + - MVC pattern +--- + +## Also known as + +* Data Formatter +* Data Presenter + +## Intent of View Helper Design Pattern + +The View Helper Design Pattern is a structural pattern used to manage the data rendering process by separating concerns. This pattern allows developers to delegate complex data processing tasks to specialized classes (view helpers), ensuring that the controller only handles user input and coordination between the view and the model. This leads to a cleaner, more maintainable application architecture. + +## Detailed Explanation of View Helper Pattern with Real-World Examples + +Real-world example + +> Think of a large e-commerce website where the product data needs to be processed differently depending on the context (e.g., displayed on the homepage, in a product detail page, or in search results). Instead of embedding complex logic within the view, which may cause redundancy and maintenance issues, a view helper class can format the data appropriately based on where it’s displayed. +> +> In this analogy, the view helper acts as the intermediary between the model and the view, ensuring that data is presented in the correct format without cluttering the view code with business logic. + +In plain words + +> The view helper pattern separates the logic of data formatting and processing from the actual display or view layer. It ensures that the view focuses solely on rendering the data while delegating complex logic to a separate class. + +Wikipedia says + +> A view helper is a design pattern used in web applications that assists in formatting or processing data before it is displayed in the view. + +## Programmatic Example of View Helper Pattern in Java + +The View Helper design pattern helps manage the separation of concerns in Java applications by delegating data processing and formatting tasks to specialized helper classes. + +Consider an online system that needs to display user and product information in different formats. A `UserViewHelper` is created to process the `User` object, and a `ProductViewHelper` is created to process the `Product` object. + +Here is an example implementation: + +### User.java + +```java +public class User { + private final String name; + private final int age; + private final String email; + + public User(String name, int age, String email) { + this.name = name; + this.age = age; + this.email = email; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + public String getEmail() { + return email; + } +} +``` + +### Product.java + +```java +public class Product { + private final int id; + private final String name; + private final double price; + + public Product(int id, String name, double price) { + this.id = id; + this.name = name; + this.price = price; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public double getPrice() { + return price; + } +} +``` + +### ViewHelper.java + +```java +public interface ViewHelper { + String handle(T object); +} + +``` + +### UserViewHelper.java + +```java +public class UserViewHelper implements ViewHelper { + @Override + public String handle(User user) { + return String.format("User: %s, Age: %d, Email: %s", user.getName(), user.getAge(), user.getEmail()); + } +} +``` + +### ProductViewHelper.java + +```java +public class ProductViewHelper implements ViewHelper { + @Override + public String handle(Product product) { + return String.format("Product ID: %d, Name: %s, Price: %.2f", product.getId(), product.getName(), product.getPrice()); + } +} +``` + +### ViewHelperApp.java + +```java +import java.util.logging.Logger; + +public final class ViewHelperApp { + + private static final Logger LOGGER = Logger.getLogger(ViewHelperApp.class.getName()); + + // Define constants for magic numbers + private static final int DEFAULT_USER_AGE = 30; + private static final double DEFAULT_PRODUCT_PRICE = 999.99; + private static final String DEFAULT_USER_NAME = "John Doe"; + private static final String DEFAULT_USER_EMAIL = "john.doe@example.com"; + private static final int DEFAULT_PRODUCT_ID = 1; + private static final String DEFAULT_PRODUCT_NAME = "Laptop"; + + private ViewHelperApp() { + // Prevent instantiation + } + + public static void main(String... args) { + // Creating a User instance using constants + User user = new User(DEFAULT_USER_NAME, DEFAULT_USER_AGE, DEFAULT_USER_EMAIL); + + // Creating a Product instance using constants + Product product = new Product(DEFAULT_PRODUCT_ID, DEFAULT_PRODUCT_NAME, DEFAULT_PRODUCT_PRICE); + + // Creating ViewHelper instances for User and Product + ViewHelper userViewHelper = new UserViewHelper(); + ViewHelper productViewHelper = new ProductViewHelper(); + + // Displaying the formatted user and product information using the logger + LOGGER.info(userViewHelper.handle(user)); + LOGGER.info(productViewHelper.handle(product)); + } +} + +``` + +### When to Use the View Helper Pattern in Java + +Use the View Helper pattern when: + +* There is a need to format or process data in a way that is independent of the view layer. +* You want to keep the controller focused on managing user interactions rather than handling data formatting. +* Your application requires different types of views with different data formats. + +### View Helper Pattern Java Tutorials + +* [The View Helper Pattern (Baeldung)](https://www.baeldung.com/java/view-helper-pattern) + +### Real-World Applications of View Helper Pattern in Java + +* Formatting user data before display (e.g., converting date formats, currency formatting). +* Managing product data to display in various parts of an e-commerce website. +* Separating the business logic from the view layer in MVC (Model-View-Controller) applications. + +### Benefits and Trade-offs of View Helper Pattern + +Benefits: + +* Promotes separation of concerns, improving code maintainability. +* Reduces duplication by centralizing data processing and formatting logic. + +Trade-offs: + +* Adds complexity by introducing additional classes. +* May result in slight overhead for smaller applications where such separation is unnecessary. + +### Related Java Design Patterns + +* [MVC (Model-View-Controller)](https://java-design-patterns.com/patterns/mvc/) +* [Strategy](https://java-design-patterns.com/patterns/strategy/) +* [Decorator](https://java-design-patterns.com/patterns/decorator/) + +### References and Credits + +* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI) +* [Effective Java](https://amzn.to/4cGk2Jz) +* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR) diff --git a/view-helper/etc/view-helper.urm.png b/view-helper/etc/view-helper.urm.png new file mode 100644 index 000000000000..aaedce1c47ef Binary files /dev/null and b/view-helper/etc/view-helper.urm.png differ diff --git a/view-helper/etc/view-helper.urm.puml b/view-helper/etc/view-helper.urm.puml new file mode 100644 index 000000000000..d85eeddd5533 --- /dev/null +++ b/view-helper/etc/view-helper.urm.puml @@ -0,0 +1,47 @@ +@startuml +package com.iluwatar.viewhelper { + class ViewHelper { + <> + + handle(object : T) : String + } + + class User { + - name : String + - age : int + - email : String + + User(name : String, age : int, email : String) + + getName() : String + + getAge() : int + + getEmail() : String + } + + class Product { + - id : int + - name : String + - price : double + + Product(id : int, name : String, price : double) + + getId() : int + + getName() : String + + getPrice() : double + } + + class UserViewHelper { + + handle(user : User) : String + } + + class ProductViewHelper { + + handle(product : Product) : String + } + + class ViewHelperApp { + - LOGGER : Logger {static} + + main(args : String[]) {static} + } + + ViewHelperApp ..> UserViewHelper : Uses + ViewHelperApp ..> ProductViewHelper : Uses + UserViewHelper ..|> ViewHelper : Implements + ProductViewHelper ..|> ViewHelper : Implements +} + +@enduml diff --git a/view-helper/pom.xml b/view-helper/pom.xml new file mode 100644 index 000000000000..cbdffeeebf2a --- /dev/null +++ b/view-helper/pom.xml @@ -0,0 +1,89 @@ + + + + 4.0.0 + + + com.iluwatar + java-design-patterns + 1.26.0-SNAPSHOT + + + view-helper + + + + + org.slf4j + slf4j-api + 1.7.32 + + + + + ch.qos.logback + logback-classic + 1.4.12 + + + + + org.junit.jupiter + junit-jupiter-engine + test + + + + + org.mockito + mockito-core + test + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + + + com.iluwatar.viewhelper.ViewHelper + + + + + + + + + + diff --git a/view-helper/src/main/java/com/iluwatar/viewhelper/Product.java b/view-helper/src/main/java/com/iluwatar/viewhelper/Product.java new file mode 100644 index 000000000000..78022a52670a --- /dev/null +++ b/view-helper/src/main/java/com/iluwatar/viewhelper/Product.java @@ -0,0 +1,63 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; + +/** + * Represents a product with an ID, name, and price. + */ +public class Product { + // Fields should have proper indentation and be marked private + private final int id; + private final String name; + private final double price; + + /** + * Constructs a Product with the specified ID, name, and price. + * + * @param id The product's ID + * @param name The product's name + * @param price The product's price + */ + public Product(int id, String name, double price) { + this.id = id; + this.name = name; + this.price = price; + } + + + public int getId() { + return id; + } + + + public String getName() { + return name; + } + + + public double getPrice() { + return price; + } +} diff --git a/view-helper/src/main/java/com/iluwatar/viewhelper/ProductViewHelper.java b/view-helper/src/main/java/com/iluwatar/viewhelper/ProductViewHelper.java new file mode 100644 index 000000000000..2a34b035c5a4 --- /dev/null +++ b/view-helper/src/main/java/com/iluwatar/viewhelper/ProductViewHelper.java @@ -0,0 +1,40 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; + +/** + * View Helper for formatting product information. + * + * @param the type of the object + */ +public class ProductViewHelper implements ViewHelper { + + @Override + public String handle(Product product) { + return String.format( + "Product ID: %d, Name: %s, Price: %.2f", + product.getId(), product.getName(), product.getPrice()); + } +} diff --git a/view-helper/src/main/java/com/iluwatar/viewhelper/User.java b/view-helper/src/main/java/com/iluwatar/viewhelper/User.java new file mode 100644 index 000000000000..859f5bb2485b --- /dev/null +++ b/view-helper/src/main/java/com/iluwatar/viewhelper/User.java @@ -0,0 +1,75 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; + +/** + * Represents a User with a name, age, and email address. + */ +public class User { + + private final String name; + private final int age; + private final String email; + + /** + * Constructs a User with the specified name, age, and email. + * + * @param name the user's name + * @param age the user's age + * @param email the user's email address + */ + public User(String name, int age, String email) { + this.name = name; + this.age = age; + this.email = email; + } + + /** + * Gets the user's name. + * + * @return the user's name + */ + public String getName() { + return name; + } + + /** + * Gets the user's age. + * + * @return the user's age + */ + public int getAge() { + return age; + } + + /** + * Gets the user's email address. + * + * @return the user's email address + */ + public String getEmail() { + return email; + } +} diff --git a/view-helper/src/main/java/com/iluwatar/viewhelper/UserViewHelper.java b/view-helper/src/main/java/com/iluwatar/viewhelper/UserViewHelper.java new file mode 100644 index 000000000000..9c97213d574f --- /dev/null +++ b/view-helper/src/main/java/com/iluwatar/viewhelper/UserViewHelper.java @@ -0,0 +1,40 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; + +/** + * View Helper for formatting user information. + * + * @param the type of the object + */ +public class UserViewHelper implements ViewHelper { + + @Override + public String handle(User user) { + return String.format( + "User: %s, Age: %d, Email: %s", + user.getName(), user.getAge(), user.getEmail()); + } +} diff --git a/view-helper/src/main/java/com/iluwatar/viewhelper/ViewHelper.java b/view-helper/src/main/java/com/iluwatar/viewhelper/ViewHelper.java new file mode 100644 index 000000000000..d7e7cc245758 --- /dev/null +++ b/view-helper/src/main/java/com/iluwatar/viewhelper/ViewHelper.java @@ -0,0 +1,41 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; + +/** + * Interface for defining a View Helper in the application. + * + * @param the type of object this View Helper processes + */ +public interface ViewHelper { + + /** + * Handles the processing of the given object and returns a formatted string representation. + * + * @param object the object to be processed + * @return a formatted string representation of the object + */ + String handle(T object); +} diff --git a/view-helper/src/main/java/com/iluwatar/viewhelper/ViewHelperApp.java b/view-helper/src/main/java/com/iluwatar/viewhelper/ViewHelperApp.java new file mode 100644 index 000000000000..91e532b2c080 --- /dev/null +++ b/view-helper/src/main/java/com/iluwatar/viewhelper/ViewHelperApp.java @@ -0,0 +1,63 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; + +import java.util.logging.Logger; + +/** + * The main application class for demonstrating the View Helper pattern. + */ +public final class ViewHelperApp { + + private static final Logger LOGGER = Logger.getLogger(ViewHelperApp.class.getName()); + private static final int AGE = 30; + private static final double PRICE = 999.99; + + private ViewHelperApp() { + // Prevent instantiation + } + + /** + * The entry point of the application. + * + * @param args command line arguments (not used) + */ + public static void main(String... args) { + User user = new User("John Doe", AGE, "john.doe@example.com"); + Product product = new Product(1, "Laptop", PRICE); + + // Conditionally log the user details with string formatting + if (LOGGER.isLoggable(java.util.logging.Level.INFO)) { + LOGGER.info(String.format("User details: Name = %s, Age = %d, Email = %s", + user.getName(), user.getAge(), user.getEmail())); + } + + // Conditionally log the product details with string formatting + if (LOGGER.isLoggable(java.util.logging.Level.INFO)) { + LOGGER.info(String.format("Product details: ID = %d, Name = %s, Price = %.2f", + product.getId(), product.getName(), product.getPrice())); + } + } +} diff --git a/view-helper/src/test/java/com/iluwatar/viewhelper/ProductViewHelperTest.java b/view-helper/src/test/java/com/iluwatar/viewhelper/ProductViewHelperTest.java new file mode 100644 index 000000000000..26879c350fbc --- /dev/null +++ b/view-helper/src/test/java/com/iluwatar/viewhelper/ProductViewHelperTest.java @@ -0,0 +1,46 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests for {@link ProductViewHelper} + */ +class ProductViewHelperTest { + + @Test + void testHandle() { + Product product = new Product(1, "Laptop", 999.99); + ViewHelper productViewHelper = new ProductViewHelper(); + + String result = productViewHelper.handle(product); + + // Assert that the output matches the expected format + assertEquals("Product ID: 1, Name: Laptop, Price: 999.99", result); + } +} diff --git a/view-helper/src/test/java/com/iluwatar/viewhelper/UserTest.java b/view-helper/src/test/java/com/iluwatar/viewhelper/UserTest.java new file mode 100644 index 000000000000..b2cdc652a257 --- /dev/null +++ b/view-helper/src/test/java/com/iluwatar/viewhelper/UserTest.java @@ -0,0 +1,84 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; +// Add package declaration here + + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class UserTest { + + private User user; + + @BeforeEach + void setUp() { + // Initializing the User object before each test + user = new User("John Doe", 30, "john.doe@example.com"); + } + + @Test + void testGetName() { + assertEquals("John Doe", user.getName(), "User's name should be 'John Doe'"); + } + + @Test + void testGetAge() { + assertEquals(30, user.getAge(), "User's age should be 30"); + } + + @Test + void testGetEmail() { + assertEquals("john.doe@example.com", user.getEmail(), "User's email should be 'john.doe@example.com'"); + } + + @Test + void testConstructorWithValidValues() { + User newUser = new User("Jane Doe", 25, "jane.doe@example.com"); + assertNotNull(newUser); + assertEquals("Jane Doe", newUser.getName()); + assertEquals(25, newUser.getAge()); + assertEquals("jane.doe@example.com", newUser.getEmail()); + } + + @Test + void testConstructorWithInvalidEmail() { + // Test with invalid email (edge case) + User invalidUser = new User("Invalid User", 40, "invalidemail"); + assertEquals("invalidemail", invalidUser.getEmail(), "Email should be 'invalidemail'"); + } + + @Test + void testGetNameNotNull() { + assertNotNull(user.getName(), "User's name should not be null"); + } + + @Test + void testAgePositive() { + assertTrue(user.getAge() > 0, "User's age should be a positive number"); + } +} diff --git a/view-helper/src/test/java/com/iluwatar/viewhelper/UserViewHelperTest.java b/view-helper/src/test/java/com/iluwatar/viewhelper/UserViewHelperTest.java new file mode 100644 index 000000000000..e3c707ea745e --- /dev/null +++ b/view-helper/src/test/java/com/iluwatar/viewhelper/UserViewHelperTest.java @@ -0,0 +1,46 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests for {@link UserViewHelper} + */ +class UserViewHelperTest { + + @Test + void testHandle() { + User user = new User("John Doe", 30, "john.doe@example.com"); + ViewHelper userViewHelper = new UserViewHelper(); + + String result = userViewHelper.handle(user); + + // Assert that the output matches the expected format + assertEquals("User: John Doe, Age: 30, Email: john.doe@example.com", result); + } +} diff --git a/view-helper/src/test/java/com/iluwatar/viewhelper/ViewHelperAppTest.java b/view-helper/src/test/java/com/iluwatar/viewhelper/ViewHelperAppTest.java new file mode 100644 index 000000000000..863315cc62ae --- /dev/null +++ b/view-helper/src/test/java/com/iluwatar/viewhelper/ViewHelperAppTest.java @@ -0,0 +1,56 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.viewhelper; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +/** + * Application test for ViewHelperApp + */ +class ViewHelperAppTest { + + /** + * Issue: Add at least one assertion to this test case. + * + * Solution: Inserted assertion to check whether the execution of the main method in {@link ViewHelperApp#main(String[])} + * throws an exception. + * + * This test case ensures that the main method of the ViewHelperApp class executes without throwing any exceptions. + * Since the main method does not return any result, we use assertDoesNotThrow to confirm it runs successfully. + */ + @Test + void shouldExecuteApplicationWithoutException() { + // Check that executing the main method does not throw any exceptions + assertDoesNotThrow(new Executable() { + @Override + public void execute() throws Throwable { + ViewHelperApp.main(); + } + }); + } +}