Skip to content

Commit 51579f4

Browse files
committed
Split the tests to catch and rethrow the exception
1 parent 0554b2d commit 51579f4

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/test/java/org/openrewrite/java/migrate/IllegalArgumentExceptionToAlreadyConnectedExceptionTest.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void defaults(RecipeSpec spec) {
3131

3232
@DocumentExample
3333
@Test
34-
void replaceCaughtException() {
34+
void catchException() {
3535
rewriteRun(
3636
//language=java
3737
java(
@@ -41,14 +41,12 @@ void replaceCaughtException() {
4141
import java.nio.channels.DatagramChannel;
4242
4343
public class Test {
44-
public void sendData() {
44+
public void sendDataCatch() {
4545
try {
4646
DatagramChannel channel = DatagramChannel.open();
4747
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
4848
} catch (IllegalArgumentException e) {
4949
System.out.println("Caught Exception");
50-
} catch (IllegalArgumentException e) {
51-
throw new IllegalArgumentException("DatagramChannel already connected to a different address");
5250
}
5351
}
5452
}
@@ -59,12 +57,51 @@ public void sendData() {
5957
import java.nio.channels.DatagramChannel;
6058
6159
public class Test {
62-
public void sendData() {
60+
public void sendDataCatch() {
6361
try {
6462
DatagramChannel channel = DatagramChannel.open();
6563
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
6664
} catch (AlreadyConnectedException e) {
6765
System.out.println("Caught Exception");
66+
}
67+
}
68+
}
69+
"""
70+
)
71+
);
72+
}
73+
74+
@Test
75+
void rethrowException() {
76+
rewriteRun(
77+
//language=java
78+
java(
79+
"""
80+
import java.nio.ByteBuffer;
81+
import java.net.SocketAddress;
82+
import java.nio.channels.DatagramChannel;
83+
84+
public class Test {
85+
public void sendDataRethrow() {
86+
try {
87+
DatagramChannel channel = DatagramChannel.open();
88+
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
89+
} catch (IllegalArgumentException e) {
90+
throw new IllegalArgumentException("DatagramChannel already connected to a different address");
91+
}
92+
}
93+
}
94+
""",
95+
"""
96+
import java.nio.ByteBuffer;
97+
import java.net.SocketAddress;
98+
import java.nio.channels.DatagramChannel;
99+
100+
public class Test {
101+
public void sendDataRethrow() {
102+
try {
103+
DatagramChannel channel = DatagramChannel.open();
104+
channel.send(ByteBuffer.allocate(1024), new java.net.InetSocketAddress("localhost", 8080));
68105
} catch (AlreadyConnectedException e) {
69106
throw new AlreadyConnectedException("DatagramChannel already connected to a different address");
70107
}
@@ -101,7 +138,7 @@ public void sendData() {
101138
}
102139

103140
@Test
104-
void retainIllegalArgumentExceptionWithoutChannelSendAnnotation() {
141+
void retainIllegalArgumentExceptionWithoutChannelSend() {
105142
rewriteRun(
106143
//language=java
107144
java(

0 commit comments

Comments
 (0)