Files
hibernate-demo/src/test/java/com/ankurm/hibernatedemo/cache/CacheApiNamespaceTest.java
T

64 lines
3.0 KiB
Java

package com.ankurm.hibernatedemo.cache;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.jupiter.api.Test;
/**
* The original article claimed: "Without the jakarta classifier, Ehcache 3 ships with the older
* javax.cache JCache API. Hibernate 7 requires the jakarta.cache namespace. Using the wrong
* artifact causes a ClassNotFoundException or NoSuchMethodError at startup." That claim is
* checked here directly against the classpath rather than repeated.
*
* <p>JSR-107 (JCache) was never migrated to the Jakarta namespace by its spec maintainers --
* unlike JPA, Bean Validation, or Servlet. {@code javax.cache.Caching} is the one and only API
* class, with or without Ehcache's own "jakarta" classifier. That classifier is Ehcache's own
* internal choice of JAXB runtime major version (used to parse its own {@code ehcache.xml}), not
* a JCache API namespace switch -- confirmed by comparing the two classifier jars' Gradle module
* metadata (one depends on {@code jaxb-runtime [2.2,3)}, the other on {@code [3,3.1)}) and by
* disassembling {@code ConfigurationParser.class} in both jars, which import
* {@code javax.xml.bind} and {@code jakarta.xml.bind} respectively -- never {@code javax.cache}
* or a {@code jakarta.cache} package, because the latter does not exist.
*
* <p>Docs: docs/18-ehcache-l2-configuration.md
*/
class CacheApiNamespaceTest {
@Test
void javaxCacheApi_isOnTheClasspath_regardlessOfEhcachesJakartaClassifier() throws ClassNotFoundException {
Class<?> caching = Class.forName("javax.cache.Caching");
Class<?> cacheManager = Class.forName("javax.cache.CacheManager");
System.out.println("RESULT[cache-api-namespace]: javax.cache.Caching loads fine from this classpath "
+ "(jar: " + caching.getProtectionDomain().getCodeSource().getLocation() + ")");
assertThat(caching).isNotNull();
assertThat(cacheManager).isNotNull();
}
@Test
void jakartaCacheNamespace_doesNotExist_onThisClasspathOrAnyOther() {
Throwable thrown = catchClassNotFound("jakarta.cache.Cache");
System.out.println("RESULT[cache-api-no-jakarta-namespace]: Class.forName(\"jakarta.cache.Cache\") -> "
+ thrown.getClass().getSimpleName()
+ " -- JSR-107 was never renamed to a jakarta.cache package, with or without Ehcache's "
+ "own \"jakarta\" classifier on org.ehcache:ehcache.");
assertThatThrownBy(() -> Class.forName("jakarta.cache.Cache"))
.as("no jakarta.cache package has ever existed -- JCache (JSR-107) kept the javax.cache "
+ "namespace even after Jakarta EE 9's javax->jakarta rename")
.isInstanceOf(ClassNotFoundException.class);
}
private static Throwable catchClassNotFound(String className) {
try {
Class.forName(className);
return null;
} catch (Throwable t) {
return t;
}
}
}