Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openrewrite.xml.tree.Xml;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -124,7 +125,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
// There is no shared-cache-mode, so process javax if present.
// javax property is deleted below if shared-cache-mode is set.
if (sdh.sharedCacheModeProperty != null &&
sdh.sharedCacheModePropertyUnspecified) {
sdh.sharedCacheModePropertyUnspecified) {

String scmValue = "NONE";
if (sdh.openJPACacheProperty != null) {
Expand Down Expand Up @@ -154,10 +155,11 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) {
// if we could determine an appropriate value, create the element.
if (scmValue != null) {
if (!v1) {
Xml.Tag newNode = Xml.Tag.build("<shared-cache-mode>" + scmValue + "</shared-cache-mode>");
// Ideally we would insert <shared-cache-mode> before the <validation-mode> and <properties> nodes
Cursor parent = getCursor().getParentOrThrow();
t = autoFormat(addOrUpdateChild(t, newNode, parent), ctx, parent);
Content sharedCacheTag = Xml.Tag.build("<shared-cache-mode>" + scmValue + "</shared-cache-mode>");
t = t.withContent(ListUtils.insertInOrder((List<Content>) t.getContent(), sharedCacheTag, Comparator.comparing(c1 ->
c1 instanceof Xml.Tag && ("validation-mode".equals(((Xml.Tag) c1).getName()) || "properties".equals(((Xml.Tag) c1).getName())) ? 1 : 0)));
t = autoFormat(t, ctx, getCursor().getParentTreeCursor());
} else {
// version="1.0"
// add a property for eclipselink
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,12 +838,12 @@ void notset_notset_set1() {
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="notset_notset_set1">
<!-- flag -->
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<!-- Connection properties -->
<!-- remove and insert shared-cache-mode NONE -->
</properties>
<shared-cache-mode>NONE</shared-cache-mode>
</persistence-unit>
</persistence>
""",
Expand Down Expand Up @@ -874,12 +874,12 @@ void notset_notset_set2() {
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="notset_notset_set2">
<!-- flag -->
<shared-cache-mode>ALL</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<!-- Connection properties -->
<!-- remove and insert shared-cache-mode ALL -->
</properties>
<shared-cache-mode>ALL</shared-cache-mode>
</persistence-unit>
</persistence>
""",
Expand Down Expand Up @@ -911,13 +911,13 @@ void notset_notset_set3() {
<persistence-unit name="notset_notset_set3">
<!-- flag -->
<!-- add shared-cache-mode ENABLE_SELECTIVE -->
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<!-- Connection properties -->
<property name="openjpa.DataCache" value="truE(Types=foo.bar.Person;foo.bar.Employee)"/>
<!-- leave - manual fix-->
</properties>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
</persistence-unit>
</persistence>
""",
Expand Down Expand Up @@ -949,13 +949,13 @@ void notset_notset_set4() {
<persistence-unit name="notset_notset_set4">
<!-- flag -->
<!-- add shared-cache-mode DISABLE_SELECTIVE -->
<shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<!-- Connection properties -->
<property name="openjpa.DataCache" value="TRUE(ExcludedTypes=foo.bar.Person;foo.bar.Employee)"/>
<!-- leave - manual fix -->
</properties>
<shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>
</persistence-unit>
</persistence>
""",
Expand Down Expand Up @@ -986,12 +986,12 @@ void notset_notset_notset1() {
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="notset_notset_notset1">
<!-- flag, insert shared-cache-mode NONE -->
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<!-- Connection properties -->
<property name="somethingelese" value="junk"></property>
</properties>
<shared-cache-mode>NONE</shared-cache-mode>
</persistence-unit>
</persistence>
""",
Expand Down Expand Up @@ -1141,11 +1141,11 @@ void openjpa_cache3_flagged() {
<persistence-unit name="openjpa_cache3_flagged">
<!-- flag -->
<!-- create shared-cache-mode NONE -->
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" />
</properties>
<shared-cache-mode>NONE</shared-cache-mode>
</persistence-unit>
</persistence>
""",
Expand Down Expand Up @@ -1353,11 +1353,11 @@ void openjpa_cache10_flagged() {
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="openjpa_cache10_flagged">
<!-- flag and insert shared-cache-mode NONE -->
<shared-cache-mode>NONE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<!-- delete -->
</properties>
<shared-cache-mode>NONE</shared-cache-mode>
</persistence-unit>
</persistence>
""",
Expand Down Expand Up @@ -1386,15 +1386,53 @@ void openjpa_cache11_flagged() {
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="openjpa_cache11_flagged">
<!-- flag and insert shared-cache-mode ALL-->
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<!-- delete -->
</properties>
</persistence-unit>
</persistence>
""",
sourceSpecs -> sourceSpecs.path(PERSISTENCE_FILENAME)
)
);
}

@Test
void openjpa_shared_cache11_flagged() {
rewriteRun(
//language=xml
xml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="openjpa_cache11_flagged">
<!-- flag and insert shared-cache-mode ALL-->
<properties>
<property name="openjpa.DataCache" value="tRue"/><!-- delete -->
</properties>
</persistence-unit>
</persistence>
""",
"""
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="openjpa_cache11_flagged">
<!-- flag and insert shared-cache-mode ALL-->
<shared-cache-mode>ALL</shared-cache-mode>
<properties>
<!-- delete -->
</properties>
</persistence-unit>
</persistence>
""",
sourceSpecs -> sourceSpecs.path(PERSISTENCE_FILENAME)
)
);
}

}