-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FreePort resource to create ServerSocket #733
base: main
Are you sure you want to change the base?
Changes from 10 commits
1fdf431
cabe821
9340843
078e478
8177bb3
5fcb444
a8761a0
e421dde
5962b7e
8e1c2c0
c3f59cd
22535df
16c8264
37bdd61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
:page-title: Free Port | ||
:page-description: Extends JUnit Jupiter with `@FreePort` to get a free port. | ||
:xp-demo-dir: ../src/demo/java | ||
:demo: {xp-demo-dir}/org/junitpioneer/jupiter/resource/FreePortDemo.java | ||
|
||
== Introduction | ||
|
||
Sometimes we want a free port in our tests so that we can either start a server on it or | ||
to test a failure scenario when there is no service at given port. | ||
|
||
It's a JUnit Jupiter extension which provides a free port for above-mentioned use cases. | ||
Also, there is no guarantee that the port will still be available by the time you use it. | ||
|
||
NOTE: You can retry the test in such cases based on `FreePort.isFreeNow` | ||
|
||
== Usage | ||
|
||
To get the `FreePort` as an argument on a test just add the annotation to your test suite as shown in the following example: | ||
|
||
[source,java] | ||
---- | ||
include::{demo}[tag=basic_free_port_example] | ||
---- |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2016-2023 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.resource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.ServerSocket; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class FreePortDemo { | ||
|
||
// tag::basic_free_port_example[] | ||
@Test | ||
void testFreePort(@NewPort ServerSocket port) { | ||
assertThat(port).isNotNull(); | ||
assertThat(port.isClosed()).isFalse(); | ||
} | ||
// end::basic_free_port_example[] | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2016-2022 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.resource; | ||
|
||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.util.List; | ||
|
||
public final class FreePort implements ResourceFactory<ServerSocket> { | ||
Michael1993 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public FreePort() { | ||
// recreate default constructor to prevent compiler warning | ||
} | ||
|
||
@Override | ||
public Resource<ServerSocket> create(List<String> arguments) throws Exception { | ||
return new FreePortResource(); | ||
} | ||
|
||
private static final class FreePortResource implements Resource<ServerSocket> { | ||
|
||
private final ServerSocket serverSocket; | ||
|
||
FreePortResource() throws IOException { | ||
this.serverSocket = new ServerSocket(0); | ||
} | ||
|
||
@Override | ||
public ServerSocket get() { | ||
return serverSocket; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
this.serverSocket.close(); | ||
} | ||
|
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2016-2023 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.resource; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@New(FreePort.class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it means that a new instance of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again - NewPort was removed. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for updating. |
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.PARAMETER) | ||
public @interface NewPort { | ||
Michael1993 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2016-2022 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.resource; | ||
|
||
import static org.junitpioneer.testkit.PioneerTestKit.executeTestClass; | ||
import static org.junitpioneer.testkit.assertion.PioneerAssert.assertThat; | ||
|
||
import java.net.ServerSocket; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junitpioneer.testkit.ExecutionResults; | ||
|
||
@DisplayName("Free port extension") | ||
public class FreePortExtensionTests { | ||
|
||
@Test | ||
@DisplayName("resolve FreePort parameter successfully") | ||
void testFreePortParameterResolution() { | ||
ExecutionResults results = executeTestClass(FreePortTestCase.class); | ||
assertThat(results).hasSingleSucceededTest(); | ||
} | ||
|
||
static class FreePortTestCase { | ||
|
||
@Test | ||
void testFreePortParameterResolution(@NewPort ServerSocket port) { | ||
Assertions.assertThat(port).isNotNull(); | ||
} | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe highlight this (port might be not free when used) as a note / warning?