Skip to content

Commit

Permalink
Split the tests to catch and rethrow the exception
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Dec 7, 2024
1 parent 0554b2d commit 51579f4
Showing 1 changed file with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void defaults(RecipeSpec spec) {

@DocumentExample
@Test
void replaceCaughtException() {
void catchException() {
rewriteRun(
//language=java
java(
Expand All @@ -41,14 +41,12 @@ void replaceCaughtException() {
import java.nio.channels.DatagramChannel;
public class Test {
public void sendData() {
public void sendDataCatch() {
try {
DatagramChannel channel = DatagramChannel.open();
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
} catch (IllegalArgumentException e) {
System.out.println("Caught Exception");
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("DatagramChannel already connected to a different address");
}
}
}
Expand All @@ -59,12 +57,51 @@ public void sendData() {
import java.nio.channels.DatagramChannel;
public class Test {
public void sendData() {
public void sendDataCatch() {
try {
DatagramChannel channel = DatagramChannel.open();
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
} catch (AlreadyConnectedException e) {
System.out.println("Caught Exception");
}
}
}
"""
)
);
}

@Test
void rethrowException() {
rewriteRun(
//language=java
java(
"""
import java.nio.ByteBuffer;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;
public class Test {
public void sendDataRethrow() {
try {
DatagramChannel channel = DatagramChannel.open();
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("DatagramChannel already connected to a different address");
}
}
}
""",
"""
import java.nio.ByteBuffer;
import java.net.SocketAddress;
import java.nio.channels.DatagramChannel;
public class Test {
public void sendDataRethrow() {
try {
DatagramChannel channel = DatagramChannel.open();
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
} catch (AlreadyConnectedException e) {
throw new AlreadyConnectedException("DatagramChannel already connected to a different address");
}
Expand Down Expand Up @@ -101,7 +138,7 @@ public void sendData() {
}

@Test
void retainIllegalArgumentExceptionWithoutChannelSendAnnotation() {
void retainIllegalArgumentExceptionWithoutChannelSend() {
rewriteRun(
//language=java
java(
Expand Down

0 comments on commit 51579f4

Please sign in to comment.