Skip to content

Commit e2a7413

Browse files
authored
Merge branch 'main' into test/logging-coverage
2 parents b9c7341 + f78b023 commit e2a7413

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

src/main/java/de/rub/nds/modifiablevariable/logging/ExtendedPatternLayout.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ public static ExtendedPatternLayout.SerializerBuilder newSerializerBuilder() {
147147
return new ExtendedPatternLayout.SerializerBuilder();
148148
}
149149

150+
/**
151+
* Determines if this layout requires location information (i.e., class name, method name, line
152+
* number).
153+
*
154+
* <p>Location information can be expensive to generate, so this method allows the logging
155+
* framework to determine whether it needs to capture stack traces for log events processed by
156+
* this layout.
157+
*
158+
* @return {@code true} if the configured event serializer requires location information, {@code
159+
* false} otherwise
160+
*/
150161
@Override
151162
public boolean requiresLocation() {
152163
return eventSerializer instanceof LocationAware
@@ -530,6 +541,14 @@ public ExtendedPatternLayout.Builder withFooter(String footer) {
530541
return this;
531542
}
532543

544+
/**
545+
* Builds a new ExtendedPatternLayout instance with the configured settings.
546+
*
547+
* <p>This method creates a new layout using all the settings configured on this builder. If
548+
* no configuration has been explicitly set, a default configuration will be used.
549+
*
550+
* @return A new ExtendedPatternLayout instance configured with the specified settings
551+
*/
533552
@Override
534553
public ExtendedPatternLayout build() {
535554
if (configuration == null) {
@@ -637,6 +656,20 @@ public SerializerBuilder() {
637656
private boolean disableAnsi;
638657
private boolean noConsoleNoAnsi;
639658

659+
/**
660+
* Builds a serializer for formatting log events according to the configured settings.
661+
*
662+
* <p>This method creates an appropriate serializer based on the configured options:
663+
*
664+
* <ul>
665+
* <li>If both pattern and defaultPattern are empty, returns null
666+
* <li>If a patternSelector is configured, creates a PatternSelectorSerializer
667+
* <li>Otherwise, creates an ExtendedPatternLayoutSerializer based on the parsed pattern
668+
* </ul>
669+
*
670+
* @return A configured serializer instance, or null if no pattern is available
671+
* @throws IllegalArgumentException if the pattern cannot be parsed
672+
*/
640673
@Override
641674
public AbstractStringLayout.Serializer build() {
642675
if (Strings.isEmpty(pattern) && Strings.isEmpty(defaultPattern)) {

src/test/java/de/rub/nds/modifiablevariable/ModifiableVariableTest.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,89 @@ public void testCopyConstructorWithModifications() {
8484
assertEquals(150, original.getValue());
8585
assertEquals(300, copy.getValue());
8686
}
87+
88+
/** Test getModifications method. */
89+
@Test
90+
public void testGetModifications() {
91+
ModifiableInteger integer = new ModifiableInteger();
92+
integer.setOriginalValue(100);
93+
94+
// Initially no modifications
95+
assertNull(integer.getModifications());
96+
97+
// Add modifications
98+
IntegerAddModification addMod = new IntegerAddModification(50);
99+
IntegerMultiplyModification multiplyMod = new IntegerMultiplyModification(2);
100+
integer.addModification(addMod);
101+
integer.addModification(multiplyMod);
102+
103+
// Get modifications
104+
List<VariableModification<Integer>> mods = integer.getModifications();
105+
assertNotNull(mods);
106+
assertEquals(2, mods.size());
107+
assertEquals(addMod, mods.get(0));
108+
assertEquals(multiplyMod, mods.get(1));
109+
110+
// Clear modifications
111+
integer.clearModifications();
112+
assertNull(integer.getModifications());
113+
}
114+
115+
/** Test containsAssertion method. */
116+
@Test
117+
public void testContainsAssertion() {
118+
ModifiableInteger integer = new ModifiableInteger();
119+
120+
// Initially no assertion
121+
assertFalse(integer.containsAssertion());
122+
123+
// Set assertEquals value
124+
integer.setAssertEquals(150);
125+
assertTrue(integer.containsAssertion());
126+
127+
// Create new instance without assertion
128+
ModifiableInteger noAssertion = new ModifiableInteger();
129+
assertFalse(noAssertion.containsAssertion());
130+
}
131+
132+
/** Test innerToString method through subclass toString. */
133+
@Test
134+
public void testInnerToString() {
135+
// Test with no modifications or assertions
136+
ModifiableInteger integer1 = new ModifiableInteger();
137+
integer1.setOriginalValue(100);
138+
String str1 = integer1.toString();
139+
assertTrue(str1.contains("originalValue=100"));
140+
assertFalse(str1.contains("modifications="));
141+
assertFalse(str1.contains("assertEquals="));
142+
143+
// Test with modifications
144+
ModifiableInteger integer2 = new ModifiableInteger();
145+
integer2.setOriginalValue(100);
146+
integer2.addModification(new IntegerAddModification(50));
147+
integer2.addModification(new IntegerMultiplyModification(2));
148+
String str2 = integer2.toString();
149+
assertTrue(str2.contains("originalValue=100"));
150+
assertTrue(str2.contains("modifications=["));
151+
assertTrue(str2.contains("IntegerAddModification"));
152+
assertTrue(str2.contains("IntegerMultiplyModification"));
153+
154+
// Test with assertions
155+
ModifiableInteger integer3 = new ModifiableInteger();
156+
integer3.setOriginalValue(100);
157+
integer3.setAssertEquals(150);
158+
String str3 = integer3.toString();
159+
assertTrue(str3.contains("originalValue=100"));
160+
assertTrue(str3.contains("assertEquals=150"));
161+
162+
// Test with both modifications and assertions
163+
ModifiableInteger integer4 = new ModifiableInteger();
164+
integer4.setOriginalValue(100);
165+
integer4.addModification(new IntegerAddModification(50));
166+
integer4.setAssertEquals(150);
167+
String str4 = integer4.toString();
168+
assertTrue(str4.contains("originalValue=100"));
169+
assertTrue(str4.contains("modifications=["));
170+
assertTrue(str4.contains("assertEquals=150"));
171+
}
87172
}

0 commit comments

Comments
 (0)