Skip to content

Commit 195b596

Browse files
authored
Merge pull request #30 from ligangty/master
Refine junit5 extension code
2 parents 43506be + a76646c commit 195b596

File tree

5 files changed

+138
-108
lines changed

5 files changed

+138
-108
lines changed

junit5/src/main/java/org/commonjava/test/http/junit5/annotations/Expected.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.lang.annotation.Target;
2222

2323
/**
24-
* This annotation is used to help inject the
24+
* This annotation is used to help inject an http test server into a test instance field by the {@link org.commonjava.test.http.junit5.expect.ExpectationServerExtension}
2525
*/
2626
@Retention( RetentionPolicy.RUNTIME )
2727
@Target( ElementType.FIELD )

junit5/src/main/java/org/commonjava/test/http/junit5/expect/ExpectationServerExtension.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,18 @@ public void postProcessTestInstance( Object testInstance, ExtensionContext conte
6767
{
6868
for ( Field field : testInstance.getClass().getDeclaredFields() )
6969
{
70-
if ( field.isAnnotationPresent( Expected.class ) )
70+
if ( field.isAnnotationPresent( Expected.class ) && field.getType().equals( ExpectationServer.class ) )
7171
{
72+
7273
Expected expected = field.getAnnotation( Expected.class );
7374
String base = expected.base();
7475
int port = expected.port();
7576
logger.debug( "Found field with @Expected annotation, base resource is {}, port is {}", base, port );
7677
this.server = new ExpectationServer( base, port );
7778
field.setAccessible( true );
78-
if ( field.getType().equals( ExpectationServer.class ) )
79-
{
80-
logger.debug( "Injecting the field {} with server instance", field.getName() );
81-
field.set( testInstance, this.server );
82-
return;
83-
}
79+
logger.debug( "Injecting the field {} with server instance", field.getName() );
80+
field.set( testInstance, this.server );
81+
return;
8482
}
8583
}
8684
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* Copyright (C) 2011-2022 Red Hat, Inc. (https://github.com/Commonjava/http-testserver)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.test.http.junit5;
17+
18+
import org.apache.commons.io.IOUtils;
19+
import org.apache.http.client.methods.CloseableHttpResponse;
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.apache.http.impl.client.CloseableHttpClient;
22+
import org.apache.http.impl.client.HttpClients;
23+
import org.apache.http.util.EntityUtils;
24+
import org.commonjava.test.http.common.CommonMethod;
25+
import org.commonjava.test.http.expect.ExpectationServer;
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.io.InputStream;
29+
30+
import static org.hamcrest.CoreMatchers.equalTo;
31+
import static org.hamcrest.CoreMatchers.is;
32+
import static org.hamcrest.CoreMatchers.notNullValue;
33+
import static org.hamcrest.MatcherAssert.assertThat;
34+
35+
public abstract class AbstractExtensionTest
36+
{
37+
protected abstract ExpectationServer getServer();
38+
39+
@Test
40+
public void simpleStatus()
41+
throws Exception
42+
{
43+
final String subPath = "/path/to/something.txt";
44+
final String url = getServer().formatUrl( subPath );
45+
getServer().expect( url, 200, "" );
46+
47+
final HttpGet request = new HttpGet( url );
48+
final CloseableHttpClient client = HttpClients.createDefault();
49+
CloseableHttpResponse response = null;
50+
51+
try
52+
{
53+
response = client.execute( request );
54+
assertThat( response.getStatusLine().getStatusCode(), is( 200 ) );
55+
}
56+
finally
57+
{
58+
if ( response != null && response.getEntity() != null )
59+
{
60+
EntityUtils.consumeQuietly( response.getEntity() );
61+
IOUtils.closeQuietly( response );
62+
}
63+
64+
request.reset();
65+
66+
if ( client != null )
67+
{
68+
IOUtils.closeQuietly( client );
69+
}
70+
}
71+
72+
}
73+
74+
@Test
75+
public void simpleDownload()
76+
throws Exception
77+
{
78+
79+
final String subPath = "/path/to/something.txt";
80+
final String content = "this is the content";
81+
final String url = getServer().formatUrl( subPath );
82+
final String path = getServer().formatPath( subPath );
83+
getServer().expect( url, 200, content );
84+
85+
final HttpGet request = new HttpGet( url );
86+
final CloseableHttpClient client = HttpClients.createDefault();
87+
CloseableHttpResponse response = null;
88+
89+
InputStream stream = null;
90+
try
91+
{
92+
response = client.execute( request );
93+
stream = response.getEntity().getContent();
94+
final String result = IOUtils.toString( stream );
95+
96+
assertThat( result, notNullValue() );
97+
assertThat( result, equalTo( content ) );
98+
}
99+
finally
100+
{
101+
IOUtils.closeQuietly( stream );
102+
if ( response != null && response.getEntity() != null )
103+
{
104+
EntityUtils.consumeQuietly( response.getEntity() );
105+
IOUtils.closeQuietly( response );
106+
}
107+
108+
request.reset();
109+
110+
if ( client != null )
111+
{
112+
IOUtils.closeQuietly( client );
113+
}
114+
}
115+
116+
System.out.println( getServer().getAccessesByPathKey() );
117+
118+
final String key = getServer().getAccessKey( CommonMethod.GET.name(), path );
119+
System.out.println( "Getting accesses for: '" + key + "'" );
120+
assertThat( getServer().getAccessesByPathKey().get( key ), equalTo( 1 ) );
121+
}
122+
}

junit5/src/test/java/org/commonjava/test/http/junit5/TestHttpServerExtendWithTest.java

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,65 +31,21 @@
3131
import java.io.InputStream;
3232

3333
import static org.hamcrest.CoreMatchers.equalTo;
34+
import static org.hamcrest.CoreMatchers.is;
3435
import static org.hamcrest.CoreMatchers.notNullValue;
3536
import static org.hamcrest.MatcherAssert.assertThat;
3637

3738
@ExtendWith( ExpectationServerExtension.class )
3839
public class TestHttpServerExtendWithTest
40+
extends AbstractExtensionTest
3941
{
4042
// @RegisterExtension
41-
@Expected( base = "repos", port = 9090)
43+
@Expected( base = "repos", port = 9090 )
4244
private ExpectationServer server;
4345

44-
@Test
45-
public void simpleDownload()
46-
throws Exception
46+
@Override
47+
protected ExpectationServer getServer()
4748
{
48-
final String subPath = "/path/to/something.txt";
49-
final String content = "this is the content";
50-
final String url = server.formatUrl( subPath );
51-
final String path = server.formatPath( subPath );
52-
server.expect( url, 200, content );
53-
54-
final HttpGet request = new HttpGet( url );
55-
final CloseableHttpClient client = HttpClients.createDefault();
56-
CloseableHttpResponse response = null;
57-
58-
InputStream stream = null;
59-
try
60-
{
61-
response = client.execute( request );
62-
stream = response.getEntity().getContent();
63-
final String result = IOUtils.toString( stream );
64-
65-
assertThat( result, notNullValue() );
66-
assertThat( result, equalTo( content ) );
67-
}
68-
finally
69-
{
70-
IOUtils.closeQuietly( stream );
71-
if ( response != null && response.getEntity() != null )
72-
{
73-
EntityUtils.consumeQuietly( response.getEntity() );
74-
IOUtils.closeQuietly( response );
75-
}
76-
77-
if ( request != null )
78-
{
79-
request.reset();
80-
}
81-
82-
if ( client != null )
83-
{
84-
IOUtils.closeQuietly( client );
85-
}
86-
}
87-
88-
System.out.println( server.getAccessesByPathKey() );
89-
90-
final String key = server.getAccessKey( CommonMethod.GET.name(), path );
91-
System.out.println( "Getting accesses for: '" + key + "'" );
92-
assertThat( server.getAccessesByPathKey().get( key ), equalTo( 1 ) );
49+
return this.server;
9350
}
94-
9551
}

junit5/src/test/java/org/commonjava/test/http/junit5/TestHttpServerRegisterTest.java

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,60 +36,14 @@
3636
import static org.hamcrest.MatcherAssert.assertThat;
3737

3838
public class TestHttpServerRegisterTest
39+
extends AbstractExtensionTest
3940
{
4041
@RegisterExtension
4142
private final ExpectationServerExtension expected = new ExpectationServerExtension( "repos" );
4243

43-
@Test
44-
public void simpleDownload()
45-
throws Exception
44+
@Override
45+
protected ExpectationServer getServer()
4646
{
47-
final ExpectationServer server = expected.getServer();
48-
final String subPath = "/path/to/something.txt";
49-
final String content = "this is the content";
50-
final String url = server.formatUrl( subPath );
51-
final String path = server.formatPath( subPath );
52-
server.expect( url, 200, content );
53-
54-
final HttpGet request = new HttpGet( url );
55-
final CloseableHttpClient client = HttpClients.createDefault();
56-
CloseableHttpResponse response = null;
57-
58-
InputStream stream = null;
59-
try
60-
{
61-
response = client.execute( request );
62-
stream = response.getEntity().getContent();
63-
final String result = IOUtils.toString( stream );
64-
65-
assertThat( result, notNullValue() );
66-
assertThat( result, equalTo( content ) );
67-
}
68-
finally
69-
{
70-
IOUtils.closeQuietly( stream );
71-
if ( response != null && response.getEntity() != null )
72-
{
73-
EntityUtils.consumeQuietly( response.getEntity() );
74-
IOUtils.closeQuietly( response );
75-
}
76-
77-
if ( request != null )
78-
{
79-
request.reset();
80-
}
81-
82-
if ( client != null )
83-
{
84-
IOUtils.closeQuietly( client );
85-
}
86-
}
87-
88-
System.out.println( server.getAccessesByPathKey() );
89-
90-
final String key = server.getAccessKey( CommonMethod.GET.name(), path );
91-
System.out.println( "Getting accesses for: '" + key + "'" );
92-
assertThat( server.getAccessesByPathKey().get( key ), equalTo( 1 ) );
47+
return this.expected.getServer();
9348
}
94-
9549
}

0 commit comments

Comments
 (0)