Skip to content

Commit d09906a

Browse files
committed
CAUSEWAY-3899: adds guard for DomainChangeRecord
1 parent 638c48c commit d09906a

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

api/applib/src/main/java/org/apache/causeway/applib/mixins/system/DomainChangeRecord.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.ElementType;
2222
import java.lang.annotation.Retention;
2323
import java.lang.annotation.RetentionPolicy;
24+
import java.util.Optional;
2425
import java.util.UUID;
2526

2627
import org.apache.causeway.applib.annotation.DomainObject;
@@ -151,16 +152,18 @@ public String toString() {
151152
@Retention(RetentionPolicy.RUNTIME)
152153
@interface TargetLogicalTypeName {
153154
int MAX_LENGTH = 255;
154-
boolean NULLABLE = false;
155-
String ALLOWS_NULL = "false";
155+
boolean NULLABLE = HasTarget.Target.NULLABLE;
156+
String ALLOWS_NULL = HasTarget.Target.ALLOWS_NULL;
156157
}
157158

158159
/**
159160
* The logical type name of the domain object being changed.
160161
*/
161162
@TargetLogicalTypeName
162163
default String getTargetLogicalTypeName() {
163-
return getTarget().logicalTypeName();
164+
return Optional.ofNullable(getTarget())
165+
.map(Bookmark::logicalTypeName)
166+
.orElse(null);
164167
}
165168

166169
@Property(
@@ -312,4 +315,16 @@ default boolean hidePostValue() {
312315
return getType() != ChangeType.AUDIT_ENTRY;
313316
}
314317

318+
/**
319+
* A no-op implementation of {@link DomainChangeRecord} that can be used for testing.
320+
*/
321+
class Empty implements DomainChangeRecord {
322+
@Override public ChangeType getType() {return null;}
323+
@Override public UUID getInteractionId() {return null;}
324+
@Override public String getUsername() {return "";}
325+
@Override public java.sql.Timestamp getTimestamp() {return null;}
326+
@Override public Bookmark getTarget() {return null;}
327+
@Override public String getLogicalMemberIdentifier() {return "";}
328+
}
329+
315330
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.apache.causeway.applib.mixins.system;
2+
3+
import java.util.UUID;
4+
5+
import org.apache.causeway.applib.services.bookmark.Bookmark;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
class DomainChangeRecord_Test {
12+
13+
@Test
14+
void when_populated() {
15+
final var dcr = new DomainChangeRecord.Empty() {
16+
@Override
17+
public Bookmark getTarget() {
18+
return Bookmark.forLogicalTypeNameAndIdentifier("Customer", "12345");
19+
}
20+
};
21+
22+
assertThat(dcr.getTargetLogicalTypeName()).isEqualTo("Customer");
23+
}
24+
25+
@Test
26+
void when_not_populated() {
27+
final var dcr = new DomainChangeRecord.Empty() {
28+
@Override
29+
public Bookmark getTarget() {
30+
return null;
31+
}
32+
};
33+
34+
assertThat(dcr.getTargetLogicalTypeName()).isNull();
35+
}
36+
37+
}

0 commit comments

Comments
 (0)