Skip to content

Latest commit

 

History

History
79 lines (51 loc) · 5.42 KB

File metadata and controls

79 lines (51 loc) · 5.42 KB

Little Code Gems

Most software projects require countless inquiries, considerations and ultimately decisions. Seemingly simple questions can often turn into surprisingly elaborate and deep rabbit holes. These "side quests" may appear insignificant, but overtime add up to a considerable amount of knowledge, expertise and intuition. The lessons learned can become little gems that stay with you long after the project ships.

Questions & Answers

How to connect to H2 with a database browser?

It's not as straightforward as one would hope. H2 memory database is only visible to the JVM process that opens it. Connecting to the seemingly-same database through a browser (example: IntelliJ Database Tool) would not show the expected tables and data. One solution is to configure H2 to store its data to a file and enable auto server mode. Update application.properties as follows:

# IMPORTANT:
# - Don't forget to include ";AUTO_SERVER=true" at the end of url
# - Make sure all directories exist in {PATH_OF_YOUR_CHOICE}. Create them if necessary   
spring.datasource.url=jdbc:h2:file:{PATH_OF_YOUR_CHOICE}/tictactoe;AUTO_SERVER=true
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create-drop

# Optional properties to help monitor SQL queries
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true

More Info: YouTube - Spring Boot Java H2 Database Setup in IntelliJ

Why does Spring JPA warn about "spring.jpa.open-in-view is enabled by default" at startup?

In a small project, the warning and its implications can likely be ignored (also you might still be inviting more trouble than it is worth down the road). However, the enabled-by-default setting can have very adverse performance implications in a production environment with high volume of traffic. It is recommended to start new projects with spring.jpa.open-in-view=false property in application.properties file.

For a proper and in-depth explanation, see: The Open Session In View Anti-Pattern. If you are feeling brave, check out the spirited discussion in Spring Boot Issue #7107.

More Info: SO - Disable the Hypersistence banner in Spring Boot

Why use AssertJ library in unit tests?

AssertJ provides a fluent assertion syntax along with lots of built-in conveniences. It tends to result in slightly easier to read code, stronger assertion checks and more user-friendly test failures.

Examples:

String veryLongMessage = "many words but only one potato ...";
assertThat(veryLongMessage).containsOnlyOnce("potato");

List<String> list = List.of("b", "c", "a");
assertThat(list).hasSize(3).containsExactlyInAnyOrder("a", "b", "c");

Map<String, String> map = Map.of("potato", "tasty");
assertThat(map).hasSize(1).containsEntry("potato", "tasty");

assertThatThrownBy(() -> someMethodThatThrows())
        .isInstanceOf(IllegalArgumentException.class)
        .hasMessageContaining("out of potatoes");

What is the project's naming convention for unit test methods?

Unit test methods follow the Roy Osherove's naming strategy: Naming Standards for Unit Tests. However, your millage may vary and a more recent trend seems to favor the naming strategy outlined by Vladimir Khorikov. 🙃

More Info: SO - Unit Test Naming Best Practices.

How to compare enum values in Thymeleaf templates?

In short, it's somewhat clumsy and painful. A couple approaches:

  1. Use T() operator. Example: th:if="${state == T(tictactoe.game.dao.model.Game.GameState).IN_PROGRESS}". Rather ugly and verbose due to needing the package path. May cause problems during refactoring.
  2. Convert to string state.name() before each comparison, but that mostly negates making the enum available to Thymeleaf in the first place. Complicates refactoring as well.
  3. Add extra methods to enum itself. Example: state.isInProgress(). May not be practical if there are a lot enum values and introduces extra code strictly for sake of Thymeleaf.

These lackluster options may prompt some to avoid passing enums to the view layer at all if template logic has a lot of comparisons. Rather convert enums to a string in a single place before passing it to the view layer. Hopefully one day Thymeleaf will have a more developer friendly solution.

More Info: SO - Comparing Enum Constants in Thymeleaf

What screenshot tool was used to capture screenshots shown on the README page?

Shottr app for macOS → https://shottr.cc/

Author's note: I've goldilock-ed my way through a handful of screenshot apps over the years and Shottr has emerged as "just right" in terms of features, productivity and intuitive use. Well worth the reasonable and single time purchase cost.