Skip to content

Commit

Permalink
junit5
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeCqupt committed Jul 23, 2024
1 parent e2aea26 commit 3e0e375
Show file tree
Hide file tree
Showing 17 changed files with 224 additions and 218 deletions.
52 changes: 29 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<netty.version>4.1.75.Final</netty.version>
<hession.version>4.0.66</hession.version>
<common-lang3.version>3.13.0</common-lang3.version>
<lombok.version>1.18.28</lombok.version>
<slf4j.version>2.0.7</slf4j.version>
<junit.version>4.13</junit.version>

<logback.version>1.3.11</logback.version>
<mockito.version>4.11.0</mockito.version>
<junit5.version>5.9.1</junit5.version>
</properties>

<dependencies>
Expand All @@ -30,13 +34,13 @@
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.66</version>
<version>${hession.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
<version>${common-lang3.version}</version>
</dependency>

<dependency>
Expand All @@ -46,7 +50,7 @@
<scope>provided</scope>
</dependency>

<!-- log -->
<!-- log slf4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -55,16 +59,16 @@

<!-- test scope start -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
<version>${junit.version}</version>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>

Expand All @@ -78,18 +82,20 @@

</dependencies>

<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-surefire-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <argLine>-->
<!-- &#45;&#45;add-opens java.base/java.lang=ALL-UNNAMED-->
<!-- </argLine>-->
<!-- </configuration>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>

</project>
33 changes: 16 additions & 17 deletions src/test/java/io/github/xinfra/lab/remoting/EndpointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import io.github.xinfra.lab.remoting.protocol.ProtocolType;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Assert;
import org.junit.Test;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


public class EndpointTest {
Expand All @@ -16,38 +15,38 @@ public void testNewEndpointNormally() {
String host = "localhost";
int port = 1234;
Endpoint endpoint = new Endpoint(test, host, port);
Assert.assertNotNull(endpoint);
Assert.assertEquals(test, endpoint.getProtocolType());
Assert.assertEquals(host, endpoint.getIp());
Assert.assertEquals(port, endpoint.getPort());

Assert.assertNotNull(new Endpoint(test, host, 0xFFFF));
Assert.assertNotNull(new Endpoint(test, host, 0));
Assert.assertNotNull(new Endpoint(test, host, RandomUtils.nextInt(0, 0xFFFF)));
Assertions.assertNotNull(endpoint);
Assertions.assertEquals(test, endpoint.getProtocolType());
Assertions.assertEquals(host, endpoint.getIp());
Assertions.assertEquals(port, endpoint.getPort());

Assertions.assertNotNull(new Endpoint(test, host, 0xFFFF));
Assertions.assertNotNull(new Endpoint(test, host, 0));
Assertions.assertNotNull(new Endpoint(test, host, RandomUtils.nextInt(0, 0xFFFF)));
}

@Test
public void testNewEndpointError() {
Assert.assertThrows(NullPointerException.class, () -> {
Assertions.assertThrows(NullPointerException.class, () -> {
new Endpoint(null, "", -1);
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Endpoint(test, "", -1);
});
Assert.assertThrows(NullPointerException.class, () -> {
Assertions.assertThrows(NullPointerException.class, () -> {
new Endpoint(test, null, -1);
});
Assert.assertThrows(IllegalArgumentException.class, () -> {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Endpoint(test, " ", -1);
});


Assert.assertThrows(IllegalArgumentException.class, () -> {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Endpoint(test, "localhost", -1);
});

Assert.assertThrows(IllegalArgumentException.class, () -> {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Endpoint(test, "localhost", 0xFFFF + 1);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import io.github.xinfra.lab.remoting.protocol.TestProtocol;
import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.URL;
import java.net.URLClassLoader;
Expand All @@ -35,28 +35,28 @@ private InvokeFuture newInvokeFuture() {
}


@Before
@BeforeEach
public void before() {
invokeFuture = newInvokeFuture();
}

@Test
public void testTimeout() {
Assert.assertNull(invokeFuture.getTimeout());
Assert.assertFalse(invokeFuture.cancelTimeout());
Assertions.assertNull(invokeFuture.getTimeout());
Assertions.assertFalse(invokeFuture.cancelTimeout());

HashedWheelTimer timer = new HashedWheelTimer();

Timeout timeout = timer.newTimeout(t -> {
}, 3, TimeUnit.SECONDS);
invokeFuture.addTimeout(timeout);
Assert.assertEquals(invokeFuture.getTimeout(), timeout);
Assertions.assertEquals(invokeFuture.getTimeout(), timeout);

Assert.assertTrue(invokeFuture.cancelTimeout());
Assert.assertFalse(invokeFuture.cancelTimeout());
Assert.assertTrue(invokeFuture.getTimeout().isCancelled());
Assertions.assertTrue(invokeFuture.cancelTimeout());
Assertions.assertFalse(invokeFuture.cancelTimeout());
Assertions.assertTrue(invokeFuture.getTimeout().isCancelled());

Assert.assertThrows(IllegalArgumentException.class, () -> {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
invokeFuture.addTimeout(timeout);
});
}
Expand All @@ -74,29 +74,29 @@ public void testAwait() throws InterruptedException {
invokeFuture.finish(message);
});

Assert.assertNull(invokeFuture.await(1, TimeUnit.SECONDS));
Assert.assertFalse(invokeFuture.isDone());
Assertions.assertNull(invokeFuture.await(1, TimeUnit.SECONDS));
Assertions.assertFalse(invokeFuture.isDone());

Message result = invokeFuture.await();
Assert.assertSame(result, message);
Assert.assertTrue(invokeFuture.isDone());
Assertions.assertSame(result, message);
Assertions.assertTrue(invokeFuture.isDone());

result = invokeFuture.await();
Assert.assertSame(result, message);
Assert.assertTrue(invokeFuture.isDone());
Assertions.assertSame(result, message);
Assertions.assertTrue(invokeFuture.isDone());
}

@Test
public void testAppClassLoader() {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
Assert.assertSame(contextClassLoader, invokeFuture.getAppClassLoader());
Assertions.assertSame(contextClassLoader, invokeFuture.getAppClassLoader());

try {
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{}, contextClassLoader);
Thread.currentThread().setContextClassLoader(urlClassLoader);

InvokeFuture future = newInvokeFuture();
Assert.assertSame(future.getAppClassLoader(), urlClassLoader);
Assertions.assertSame(future.getAppClassLoader(), urlClassLoader);
} finally {
// recover current thread context classLoader
Thread.currentThread().setContextClassLoader(contextClassLoader);
Expand All @@ -114,7 +114,7 @@ public void testCallBackSync() {
};
invokeFuture.addCallBack(callBack);

Assert.assertThrows(IllegalArgumentException.class, () -> {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
invokeFuture.addCallBack(callBack);
});

Expand All @@ -123,12 +123,12 @@ public void testCallBackSync() {
invokeFuture.finish(message);

invokeFuture.executeCallBack();
Assert.assertTrue(callbackExecuted.get());
Assert.assertEquals(1, callBackExecuteTimes.get());
Assertions.assertTrue(callbackExecuted.get());
Assertions.assertEquals(1, callBackExecuteTimes.get());

invokeFuture.executeCallBack();
Assert.assertTrue(callbackExecuted.get());
Assert.assertEquals(1, callBackExecuteTimes.get());
Assertions.assertTrue(callbackExecuted.get());
Assertions.assertEquals(1, callBackExecuteTimes.get());
}

@Test
Expand Down Expand Up @@ -161,7 +161,7 @@ public void handleMessage(RemotingContext remotingContext, Object msg) {
};
invokeFuture.addCallBack(callBack);

Assert.assertThrows(IllegalArgumentException.class, () -> {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
invokeFuture.addCallBack(callBack);
});

Expand All @@ -172,12 +172,12 @@ public void handleMessage(RemotingContext remotingContext, Object msg) {

invokeFuture.asyncExecuteCallBack();
countDownLatch.await();
Assert.assertTrue(callbackExecuted.get());
Assert.assertEquals(1, callBackExecuteTimes.get());
Assertions.assertTrue(callbackExecuted.get());
Assertions.assertEquals(1, callBackExecuteTimes.get());

invokeFuture.asyncExecuteCallBack();
countDownLatch.await();
Assert.assertTrue(callbackExecuted.get());
Assert.assertEquals(1, callBackExecuteTimes.get());
Assertions.assertTrue(callbackExecuted.get());
Assertions.assertEquals(1, callBackExecuteTimes.get());
}
}
Loading

0 comments on commit 3e0e375

Please sign in to comment.