Skip to content

Commit

Permalink
Small update
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael1993 committed Jan 11, 2024
1 parent 5962b7e commit 8e1c2c0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 17 deletions.
12 changes: 3 additions & 9 deletions docs/free-port.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
: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

Expand All @@ -17,13 +19,5 @@ To get the `FreePort` as an argument on a test just add the annotation to your t

[source,java]
----
@ExtendWith(FreePortExtension.class)
class ServerTest {
@Test
void test(FreePort port) {
// use port.number() or check before using port.isFreeNow()
}
}
include::{demo}[tag=basic_free_port_example]
----
29 changes: 29 additions & 0 deletions src/demo/java/org/junitpioneer/jupiter/resource/FreePortDemo.java
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
Expand Up @@ -8,15 +8,12 @@
* http://www.eclipse.org/legal/epl-v20.html
*/

package org.junitpioneer.jupiter;
package org.junitpioneer.jupiter.resource;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.List;

import org.junitpioneer.jupiter.resource.Resource;
import org.junitpioneer.jupiter.resource.ResourceFactory;

public final class FreePort implements ResourceFactory<ServerSocket> {

public FreePort() {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/junitpioneer/jupiter/resource/NewPort.java
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)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface NewPort {
}
3 changes: 2 additions & 1 deletion src/test/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
exports org.junitpioneer.jupiter.params;
exports org.junitpioneer.jupiter.json;
exports org.junitpioneer.jupiter.converter;
exports org.junitpioneer.jupiter.resource;

opens org.junitpioneer.vintage to org.junit.platform.commons;
opens org.junitpioneer.jupiter to org.junit.platform.commons, nl.jqno.equalsverifier;
opens org.junitpioneer.jupiter.cartesian to org.junit.platform.commons;
opens org.junitpioneer.jupiter.issue to org.junit.platform.commons;
opens org.junitpioneer.jupiter.params to org.junit.platform.commons;
opens org.junitpioneer.jupiter.resource to org.junit.platform.commons;
opens org.junitpioneer.jupiter.resource to nl.jqno.equalsverifier, org.junit.platform.commons;
opens org.junitpioneer.jupiter.json to org.junit.platform.commons, com.fasterxml.jackson.databind;
opens org.junitpioneer.jupiter.converter to org.junit.platform.commons;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* http://www.eclipse.org/legal/epl-v20.html
*/

package org.junitpioneer.jupiter;
package org.junitpioneer.jupiter.resource;

import static org.junitpioneer.testkit.PioneerTestKit.executeTestClass;
import static org.junitpioneer.testkit.assertion.PioneerAssert.assertThat;
Expand All @@ -18,7 +18,6 @@
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.resource.New;
import org.junitpioneer.testkit.ExecutionResults;

@DisplayName("Free port extension")
Expand All @@ -34,7 +33,7 @@ void testFreePortParameterResolution() {
static class FreePortTestCase {

@Test
void testFreePortParameterResolution(@New(FreePort.class) ServerSocket port) {
void testFreePortParameterResolution(@NewPort ServerSocket port) {
Assertions.assertThat(port).isNotNull();
}

Expand Down

0 comments on commit 8e1c2c0

Please sign in to comment.