Skip to content

Commit

Permalink
Ensure we properly truncate messages
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Sep 12, 2023
1 parent 511b2e5 commit dc35ad6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ private void send(Request.Type type, @Nullable String message) {
}
}

private static @Nullable String truncateMessage(@Nullable String message, int maxLength) {
static @Nullable String truncateMessage(@Nullable String message, int maxLength) {
if (message == null || message.length() <= maxLength) {
return message;
}
return "..." + message.substring(Math.max(message.length() - maxLength - 3, 0));
return "..." + message.substring(Math.max(message.length() - maxLength + 3, 0));
}

@Value
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/org/openrewrite/polyglot/RemoteProgressBarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
package org.openrewrite.polyglot;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openrewrite.internal.lang.Nullable;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -75,4 +79,32 @@ public ProgressBar setMax(int max) {
assertThat(latch.await(5, TimeUnit.SECONDS)).isTrue();
}
}

@ParameterizedTest
@MethodSource("longStrings")
void truncate(String input, String output) {
int maxLength = 255;
String actual = RemoteProgressBarSender.truncateMessage(input, maxLength);
assertThat(actual).isEqualTo(output);
if (maxLength <= input.length()) {
assertThat(actual).hasSize(maxLength);
} else {
assertThat(actual).hasSize(input.length());
}
}

private static Stream<Arguments> longStrings() {
String twohunderdfifty = "1234567890".repeat(25);
return Stream.of(
Arguments.of(twohunderdfifty + "123", twohunderdfifty + "123"),
Arguments.of(twohunderdfifty + "1234", twohunderdfifty + "1234"),
Arguments.of(twohunderdfifty + "12345", twohunderdfifty + "12345"),
Arguments.of(twohunderdfifty + "123456", "..." + twohunderdfifty.substring(4) + "123456"),
Arguments.of(twohunderdfifty + "1234567", "..." + twohunderdfifty.substring(5) + "1234567"),
Arguments.of(twohunderdfifty + "12345678", "..." + twohunderdfifty.substring(6) + "12345678"),
Arguments.of(twohunderdfifty + "123456789", "..." + twohunderdfifty.substring(7) + "123456789"),
Arguments.of(twohunderdfifty + "1234567890", "..." + twohunderdfifty.substring(8) + "1234567890")
);
}

}

0 comments on commit dc35ad6

Please sign in to comment.