Skip to content

Commit

Permalink
[RESTEASY-3388] In some cases it's likely more efficient to use the s…
Browse files Browse the repository at this point in the history
…uppler messages for failures. This is likely not all cases where it could be used.

https://issues.redhat.com/browse/RESTEASY-3388
Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Feb 16, 2024
1 parent c63becd commit 21daa10
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -18,6 +21,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -239,19 +243,44 @@ public static String readString(final InputStream in) throws IOException {
return builder.toString();
}

public static String getErrorMessageForKnownIssue(String jira, String message) {
StringBuilder s = new StringBuilder();
s.append("https://issues.jboss.org/browse/");
s.append(jira);
s.append(" - ");
s.append(message);
return s.toString();
public static Supplier<String> getErrorMessageForKnownIssue(String jira, String message) {
return getErrorMessageForKnownIssue(jira, message, null);
}

public static String getErrorMessageForKnownIssue(String jira) {
public static Supplier<String> getErrorMessageForKnownIssue(String jira) {
return getErrorMessageForKnownIssue(jira, "known issue");
}

public static Supplier<String> getErrorMessageForKnownIssue(String jira, Throwable throwable) {
return getErrorMessageForKnownIssue(jira, null, throwable);
}

public static Supplier<String> getErrorMessageForKnownIssue(final String jira, final String message,
final Throwable throwable) {
return () -> {
final String url = "https://issues.redhat.com/browse/";
if (throwable == null) {
return url + jira + " - " + (message == null ? "" : message);
}
try (
StringWriter writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer)) {
writer.write(url);
writer.write(jira);
if (message != null) {
writer.write(" - ");
writer.write(message);
}
writer.write(System.lineSeparator());
throwable.printStackTrace(pw);
return writer.toString();
} catch (IOException e) {
// This should never happen, but we need to appease the compiler
throw new UncheckedIOException(e);
}
};
}

public static String getJbossHome() {
return System.getProperty("jboss.home");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void doTestGetFormData() throws Exception {
}
}
} catch (Exception e) {
throw new RuntimeException(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119"), e);
Assertions.fail(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119", e));
}
}

Expand Down Expand Up @@ -167,7 +167,7 @@ void doTestGetMixed() throws Exception {
}
}
} catch (Exception e) {
throw new RuntimeException(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119"), e);
Assertions.fail(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119", e));
}
}

Expand Down Expand Up @@ -196,7 +196,7 @@ void doTestGetList() throws Exception {
MatcherAssert.assertThat("Received customers list do not contain all items", customers, hasItems("Bill"));
MatcherAssert.assertThat("Received customers list do not contain all items", customers, hasItems("Bob"));
} catch (Exception e) {
throw new RuntimeException(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119"), e);
Assertions.fail(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119", e));
}
}

Expand Down Expand Up @@ -231,7 +231,7 @@ public void doTestGetMap() throws Exception {
MatcherAssert.assertThat("Received customers list do not contain all items", customers, hasItems("Bill"));
MatcherAssert.assertThat("Received customers list do not contain all items", customers, hasItems("Bob"));
} catch (Exception e) {
throw new RuntimeException(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119"), e);
Assertions.fail(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119", e));
}
}

Expand Down Expand Up @@ -265,7 +265,7 @@ void doTestGetRelated() throws Exception {
MatcherAssert.assertThat("Received customers list do not contain all items", parts, hasItems("Bill"));
MatcherAssert.assertThat("Received customers list do not contain all items", parts, hasItems("Bob"));
} catch (Exception e) {
throw new RuntimeException(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119"), e);
Assertions.fail(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119", e));
}
}

Expand Down Expand Up @@ -506,8 +506,10 @@ <T> T get(String path, Class<T> clazz, Annotation[] annotations) throws Exceptio
client.close();
return entity;
} catch (Exception e) {
throw new RuntimeException(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119"), e);
Assertions.fail(TestUtil.getErrorMessageForKnownIssue("RESTEASY-1119", e));
}
// Shouldn't happen with the failure assertion above
return null;
}

@SuppressWarnings({ "unchecked" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;
Expand Down Expand Up @@ -108,9 +107,9 @@ public void testAddMessage() throws Exception {
messageClient.close();
}
Assertions.assertFalse(msgEventSource.isOpen(), "SseEventSource is not closed");
Assertions.assertTrue(results.size() == 5, "5 messages are expected, but is : " + results.size());
Assertions.assertTrue(results.size() == 5, () -> "5 messages are expected, but is : " + results.size());
for (String s : sent) {
Assertions.assertTrue(results.contains(s), "Sent message \"" + s + "\" not found as result.");
Assertions.assertTrue(results.contains(s), () -> "Sent message \"" + s + "\" not found as result.");
}
client.close();
}
Expand All @@ -132,10 +131,9 @@ public void testLastEventId() throws Exception {
});
lastEventSource.open("1");
Assertions.assertTrue(missedEventLatch.await(30, TimeUnit.SECONDS),
"Waiting for missed events to be delivered has timed our, received events :"
+ Arrays.toString(missedEvents.toArray(new String[] {})));
Assertions.assertTrue(missedEvents.size() == 3,
"3 messages are expected, but is : " + missedEvents.toArray(new String[] {}));
() -> "Waiting for missed events to be delivered has timed our, received events :"
+ missedEvents);
Assertions.assertEquals(3, missedEvents.size(), () -> "3 messages are expected, but is : " + missedEvents);
lastEventTarget.request().delete();
lastEventSource.close();
c.close();
Expand Down Expand Up @@ -165,9 +163,9 @@ public void testSseEvent() throws Exception {
boolean waitResult = latch.await(30, TimeUnit.SECONDS);
Assertions.assertEquals(0, errors.get());
Assertions.assertTrue(waitResult, "Waiting for event to be delivered has timed out.");
Assertions.assertTrue(results.size() == 6, "6 SseInboundEvent expected");
Assertions.assertTrue(results.toArray(new String[] {})[5].indexOf("Done") > -1,
"Expect the last event is Done event, but it is :" + results.toArray(new String[] {})[5]);
Assertions.assertEquals(6, results.size(), "6 SseInboundEvent expected");
Assertions.assertTrue(results.get(5).contains("Done"),
() -> "Expect the last event is Done event, but it is :" + results);
eventSource.close();
client.close();
}
Expand All @@ -181,7 +179,7 @@ public void testBroadcast() throws Exception {
final String textMessage = "This is broadcast message";
Consumer<InboundSseEvent> checkConsumer = insse -> {
if (latch.getCount() > 0) {
Assertions.assertTrue(textMessage.equals(insse.readData()), "Unexpected sever sent event data");
Assertions.assertEquals(textMessage, insse.readData(), "Unexpected sever sent event data");
}
latch.countDown();
};
Expand Down Expand Up @@ -232,7 +230,7 @@ public void testBroadcast() throws Exception {
latch6.countDown();
});

Assertions.assertTrue(latch5.getCount() == 5, "Eventsource should not receive event after close");
Assertions.assertEquals(5, latch5.getCount(), "Eventsource should not receive event after close");
Assertions.assertTrue(latch6.await(20, TimeUnit.SECONDS),
"Waiting for eventsource2 receive broadcast events to be delivered has timed out.");

Expand Down Expand Up @@ -304,7 +302,7 @@ public void run() {
boolean waitResult = latch.await(30, TimeUnit.SECONDS);
Assertions.assertEquals(0, errors.get());
Assertions.assertTrue(waitResult, "Waiting for event to be delivered has timed out.");
Assertions.assertTrue(results.size() == 10, "10 events are expected, but is : " + results.size());
Assertions.assertEquals(10, results.size(), () -> "10 events are expected, but is : " + results.size());
target.request().delete();
proxy.stop();
}
Expand Down Expand Up @@ -378,10 +376,10 @@ public void testMultipleDataFields() throws Exception {
messageClient.close();
}
Assertions.assertFalse(msgEventSource.isOpen(), "SseEventSource is not closed");
Assertions.assertTrue(results.size() == 7, "5 messages are expected, but is : " + results.size());
Assertions.assertEquals(7, results.size(), () -> "5 messages are expected, but is : " + results.size());
String[] lines = results.toArray(new String[] {})[1].split("\n");
Assertions.assertTrue(lines.length == 3, "3 data fields are expected, but is : " + lines.length);
Assertions.assertEquals("data1b", lines[1], "expect second data field value is : " + lines[1]);
Assertions.assertEquals(3, lines.length, () -> "3 data fields are expected, but is : " + lines.length);
Assertions.assertEquals("data1b", lines[1], () -> "expect second data field value is : " + lines[1]);
client.close();
}

Expand Down Expand Up @@ -415,9 +413,9 @@ public void testEscapedMessage() throws Exception {
Assertions.assertTrue(waitResult, "Waiting for event to be delivered has timed out.");
}
Assertions.assertFalse(msgEventSource.isOpen(), "SseEventSource is not closed");
Assertions.assertTrue(results.size() == 3, "3 messages are expected, but is : " + results.size());
Assertions.assertEquals(3, results.size(), () -> "3 messages are expected, but is : " + results.size());
for (String s : sent) {
Assertions.assertTrue(results.contains(s), "Sent message \"" + s + "\" not found as result.");
Assertions.assertTrue(results.contains(s), () -> "Sent message \"" + s + "\" not found as result.");
}
client.close();
}
Expand Down Expand Up @@ -519,7 +517,7 @@ public void testNoContent() throws Exception {
} catch (InterruptedException e) {
logger.info("Thread sleep interruped", e);
}
Assertions.assertTrue(errors.get() == 0, "error is not expected");
Assertions.assertEquals(0, errors.get(), "error is not expected");
client.close();
}

Expand Down Expand Up @@ -549,15 +547,15 @@ public void testBigMessage() throws Exception {
Assertions.assertTrue(waitResult, "Waiting for event to be delivered has timed out.");
}
Assertions.assertFalse(msgEventSource.isOpen(), "SseEventSource is not closed");
Assertions.assertTrue(results.size() == 1, "1 messages are expected, but is : " + results.size());
Assertions.assertEquals(1, results.size(), () -> "1 messages are expected, but is : " + results.size());
java.nio.file.Path filepath = Paths.get(SseTest.class.getResource("bigmsg.json").toURI());
String bigMsg = new String(Files.readAllBytes(filepath));
ObjectMapper om = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Object> m1 = (Map<String, Object>) (om.readValue(bigMsg, Map.class));
@SuppressWarnings("unchecked")
Map<String, Object> m2 = (Map<String, Object>) (om.readValue(results.get(0), Map.class));
Assertions.assertTrue(m1.equals(m2), "Unexpceted big size message");
Assertions.assertEquals(m1, m2, "Unexpceted big size message");
client.close();
}

Expand Down Expand Up @@ -587,13 +585,13 @@ public void testSetJsonType() throws Exception {
Assertions.assertTrue(waitResult, "Waiting for event to be delivered has timed out.");
}
Assertions.assertFalse(msgEventSource.isOpen(), "SseEventSource is not closed");
Assertions.assertTrue(results.size() == 1, "1 messages are expected, but is : " + results.size());
Assertions.assertEquals(1, results.size(), () -> "1 messages are expected, but is : " + results.size());
ObjectMapper om = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Object> m1 = (Map<String, Object>) (om.readValue(SseResource.jsonMessage, Map.class));
@SuppressWarnings("unchecked")
Map<String, Object> m2 = (Map<String, Object>) (om.readValue(results.get(0), Map.class));
Assertions.assertTrue(m1.equals(m2), "Unexpceted big size message");
Assertions.assertEquals(m1, m2, "Unexpceted big size message");
client.close();
}
// @Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void handleNotification(Notification notification, Object handback) {
coutDown.await(10, TimeUnit.SECONDS);

ins.transferTo(baos);
Assertions.assertEquals(10000000, baos.size(), "Received string: " + baos.toShortString());
Assertions.assertEquals(10000000, baos.size(), () -> "Received string: " + baos.toShortString());
} finally {
//remove the listener
for (GarbageCollectorMXBean gcbean : gcbeans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void envPropertyFailed() throws Exception {
.request().get();
final String value = checkFailedResponse(response);
Assertions.assertTrue(value.contains("\"java.lang.RuntimePermission\" \"getenv." + ENV_NAME + "\""),
"Expected the response to have failed with a property permission: " + value);
() -> "Expected the response to have failed with a property permission: " + value);
}
}

Expand All @@ -125,7 +125,7 @@ public void systemPropertyFailed() throws Exception {
.request().get();
final String value = checkFailedResponse(response);
Assertions.assertTrue(value.contains("\"java.util.PropertyPermission\" \"" + PROPERTY_NAME + "\""),
"Expected the response to have failed with a property permission: " + value);
() -> "Expected the response to have failed with a property permission: " + value);
}
}

Expand All @@ -139,7 +139,7 @@ public void configPropertyFailed() throws Exception {
Assertions.assertTrue(
value.contains(
"\"org.jboss.resteasy.spi.config.security.ConfigPropertyPermission\" \"" + PROPERTY_NAME + "\""),
"Expected the response to have failed with a property permission: " + value);
() -> "Expected the response to have failed with a property permission: " + value);
}
}

Expand All @@ -152,7 +152,7 @@ public void envConfigPropertyFailed() throws Exception {
final String value = checkFailedResponse(response);
Assertions.assertTrue(
value.contains("\"org.jboss.resteasy.spi.config.security.ConfigPropertyPermission\" \"" + ENV_NAME + "\""),
"Expected the response to have failed with a property permission: " + value);
() -> "Expected the response to have failed with a property permission: " + value);
}
}

Expand All @@ -166,7 +166,7 @@ public void optionFailed() throws Exception {
Assertions.assertTrue(
value.contains(
"\"org.jboss.resteasy.spi.config.security.ConfigPropertyPermission\" \"dev.resteasy.exception.mapper\""),
"Expected the response to have failed with a property permission: " + value);
() -> "Expected the response to have failed with a property permission: " + value);
}
}

Expand Down

0 comments on commit 21daa10

Please sign in to comment.