Skip to content

Commit cf3cb18

Browse files
committed
HADOOP-19414. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-auth.
1 parent db81ea6 commit cf3cb18

18 files changed

+641
-583
lines changed

hadoop-common-project/hadoop-auth/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@
205205
<artifactId>guava</artifactId>
206206
<scope>test</scope>
207207
</dependency>
208+
<dependency>
209+
<groupId>org.assertj</groupId>
210+
<artifactId>assertj-core</artifactId>
211+
<scope>test</scope>
212+
</dependency>
208213
</dependencies>
209214

210215
<build>

hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/client/AuthenticatorTestCase.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
*/
1414
package org.apache.hadoop.security.authentication.client;
1515

16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertFalse;
18+
import static org.junit.jupiter.api.Assertions.assertNull;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
20+
1621
import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
1722
import org.apache.http.HttpResponse;
1823
import org.apache.http.auth.AuthScope;
@@ -55,8 +60,6 @@
5560
import java.util.EnumSet;
5661
import java.util.Properties;
5762

58-
import org.junit.Assert;
59-
6063
public class AuthenticatorTestCase {
6164
private Server server;
6265
private String host = null;
@@ -170,11 +173,11 @@ protected void _testAuthentication(Authenticator authenticator, boolean doPost)
170173
try {
171174
URL url = new URL(getBaseURL());
172175
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
173-
Assert.assertFalse(token.isSet());
176+
assertFalse(token.isSet());
174177
TestConnectionConfigurator connConf = new TestConnectionConfigurator();
175178
AuthenticatedURL aUrl = new AuthenticatedURL(authenticator, connConf);
176179
HttpURLConnection conn = aUrl.openConnection(url, token);
177-
Assert.assertTrue(connConf.invoked);
180+
assertTrue(connConf.invoked);
178181
String tokenStr = token.toString();
179182
if (doPost) {
180183
conn.setRequestMethod("POST");
@@ -186,18 +189,18 @@ protected void _testAuthentication(Authenticator authenticator, boolean doPost)
186189
writer.write(POST);
187190
writer.close();
188191
}
189-
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
192+
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
190193
if (doPost) {
191194
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
192195
String echo = reader.readLine();
193-
Assert.assertEquals(POST, echo);
194-
Assert.assertNull(reader.readLine());
196+
assertEquals(POST, echo);
197+
assertNull(reader.readLine());
195198
}
196199
aUrl = new AuthenticatedURL();
197200
conn = aUrl.openConnection(url, token);
198201
conn.connect();
199-
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
200-
Assert.assertEquals(tokenStr, token.toString());
202+
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
203+
assertEquals(tokenStr, token.toString());
201204
} finally {
202205
stop();
203206
}
@@ -233,7 +236,7 @@ private void doHttpClientRequest(HttpClient httpClient, HttpUriRequest request)
233236
try {
234237
response = httpClient.execute(request);
235238
final int httpStatus = response.getStatusLine().getStatusCode();
236-
Assert.assertEquals(HttpURLConnection.HTTP_OK, httpStatus);
239+
assertEquals(HttpURLConnection.HTTP_OK, httpStatus);
237240
} finally {
238241
if (response != null) EntityUtils.consumeQuietly(response.getEntity());
239242
}
@@ -255,7 +258,7 @@ protected void _testAuthenticationHttpClient(Authenticator authenticator, boolea
255258
// Important that the entity is not repeatable -- this means if
256259
// we have to renegotiate (e.g. b/c the cookie wasn't handled properly)
257260
// the test will fail.
258-
Assert.assertFalse(entity.isRepeatable());
261+
assertFalse(entity.isRepeatable());
259262
post.setEntity(entity);
260263
doHttpClientRequest(httpClient, post);
261264
}

hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/client/TestAuthenticatedURL.java

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,17 @@
1313
*/
1414
package org.apache.hadoop.security.authentication.client;
1515

16-
import org.junit.Assert;
17-
import org.junit.Test;
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertFalse;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.fail;
20+
import static org.mockito.Mockito.anyString;
21+
import static org.mockito.Mockito.eq;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.verify;
24+
import static org.mockito.Mockito.when;
25+
26+
import org.junit.jupiter.api.Test;
1827
import org.mockito.Mockito;
1928

2029
import java.net.HttpURLConnection;
@@ -29,128 +38,128 @@ public class TestAuthenticatedURL {
2938
@Test
3039
public void testToken() throws Exception {
3140
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
32-
Assert.assertFalse(token.isSet());
41+
assertFalse(token.isSet());
3342
token = new AuthenticatedURL.Token("foo");
34-
Assert.assertTrue(token.isSet());
35-
Assert.assertEquals("foo", token.toString());
43+
assertTrue(token.isSet());
44+
assertEquals("foo", token.toString());
3645
}
3746

3847
@Test
3948
public void testInjectToken() throws Exception {
40-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
49+
HttpURLConnection conn = mock(HttpURLConnection.class);
4150
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
4251
token.set("foo");
4352
AuthenticatedURL.injectToken(conn, token);
44-
Mockito.verify(conn).addRequestProperty(Mockito.eq("Cookie"), Mockito.anyString());
53+
verify(conn).addRequestProperty(eq("Cookie"), anyString());
4554
}
4655

4756
@Test
4857
public void testExtractTokenOK() throws Exception {
49-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
58+
HttpURLConnection conn = mock(HttpURLConnection.class);
5059

51-
Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
60+
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
5261

5362
String tokenStr = "foo";
5463
Map<String, List<String>> headers = new HashMap<String, List<String>>();
5564
List<String> cookies = new ArrayList<String>();
5665
cookies.add(AuthenticatedURL.AUTH_COOKIE + "=" + tokenStr);
5766
headers.put("Set-Cookie", cookies);
58-
Mockito.when(conn.getHeaderFields()).thenReturn(headers);
67+
when(conn.getHeaderFields()).thenReturn(headers);
5968

6069
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
6170
AuthenticatedURL.extractToken(conn, token);
6271

63-
Assert.assertEquals(tokenStr, token.toString());
72+
assertEquals(tokenStr, token.toString());
6473
}
6574

6675
@Test
6776
public void testExtractTokenFail() throws Exception {
68-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
77+
HttpURLConnection conn = mock(HttpURLConnection.class);
6978

70-
Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
79+
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
7180

7281
String tokenStr = "foo";
7382
Map<String, List<String>> headers = new HashMap<String, List<String>>();
7483
List<String> cookies = new ArrayList<String>();
7584
cookies.add(AuthenticatedURL.AUTH_COOKIE + "=" + tokenStr);
7685
headers.put("Set-Cookie", cookies);
77-
Mockito.when(conn.getHeaderFields()).thenReturn(headers);
86+
when(conn.getHeaderFields()).thenReturn(headers);
7887

7988
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
8089
token.set("bar");
8190
try {
8291
AuthenticatedURL.extractToken(conn, token);
83-
Assert.fail();
92+
fail();
8493
} catch (AuthenticationException ex) {
8594
// Expected
86-
Assert.assertFalse(token.isSet());
95+
assertFalse(token.isSet());
8796
} catch (Exception ex) {
88-
Assert.fail();
97+
fail();
8998
}
9099
}
91100

92101
@Test
93102
public void testExtractTokenCookieHeader() throws Exception {
94-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
103+
HttpURLConnection conn = mock(HttpURLConnection.class);
95104

96-
Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
105+
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
97106

98107
String tokenStr = "foo";
99108
Map<String, List<String>> headers = new HashMap<>();
100109
List<String> cookies = new ArrayList<>();
101110
cookies.add(AuthenticatedURL.AUTH_COOKIE + "=" + tokenStr);
102111
headers.put("Set-Cookie", cookies);
103-
Mockito.when(conn.getHeaderFields()).thenReturn(headers);
112+
when(conn.getHeaderFields()).thenReturn(headers);
104113

105114
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
106115
AuthenticatedURL.extractToken(conn, token);
107116

108-
Assert.assertTrue(token.isSet());
117+
assertTrue(token.isSet());
109118
}
110119

111120
@Test
112121
public void testExtractTokenLowerCaseCookieHeader() throws Exception {
113-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
122+
HttpURLConnection conn = mock(HttpURLConnection.class);
114123

115-
Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
124+
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
116125

117126
String tokenStr = "foo";
118127
Map<String, List<String>> headers = new HashMap<>();
119128
List<String> cookies = new ArrayList<>();
120129
cookies.add(AuthenticatedURL.AUTH_COOKIE + "=" + tokenStr);
121130
headers.put("set-cookie", cookies);
122-
Mockito.when(conn.getHeaderFields()).thenReturn(headers);
131+
when(conn.getHeaderFields()).thenReturn(headers);
123132

124133
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
125134
AuthenticatedURL.extractToken(conn, token);
126135

127-
Assert.assertTrue(token.isSet());
136+
assertTrue(token.isSet());
128137
}
129138

130139
@Test
131140
public void testConnectionConfigurator() throws Exception {
132-
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
133-
Mockito.when(conn.getResponseCode()).
141+
HttpURLConnection conn = mock(HttpURLConnection.class);
142+
when(conn.getResponseCode()).
134143
thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
135144

136145
ConnectionConfigurator connConf =
137-
Mockito.mock(ConnectionConfigurator.class);
146+
mock(ConnectionConfigurator.class);
138147
Mockito.when(connConf.configure(Mockito.<HttpURLConnection>any())).
139148
thenReturn(conn);
140149

141150
Authenticator authenticator = Mockito.mock(Authenticator.class);
142151

143152
AuthenticatedURL aURL = new AuthenticatedURL(authenticator, connConf);
144153
aURL.openConnection(new URL("http://foo"), new AuthenticatedURL.Token());
145-
Mockito.verify(connConf).configure(Mockito.<HttpURLConnection>any());
154+
verify(connConf).configure(Mockito.<HttpURLConnection>any());
146155
}
147156

148157
@Test
149158
public void testGetAuthenticator() throws Exception {
150-
Authenticator authenticator = Mockito.mock(Authenticator.class);
159+
Authenticator authenticator = mock(Authenticator.class);
151160

152161
AuthenticatedURL aURL = new AuthenticatedURL(authenticator);
153-
Assert.assertEquals(authenticator, aURL.getAuthenticator());
162+
assertEquals(authenticator, aURL.getAuthenticator());
154163
}
155164

156165
}

hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/client/TestPseudoAuthenticator.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
*/
1414
package org.apache.hadoop.security.authentication.client;
1515

16+
import static org.junit.jupiter.api.Assertions.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
18+
1619
import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
1720
import org.apache.hadoop.security.authentication.server.PseudoAuthenticationHandler;
18-
import org.junit.Assert;
19-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
2022

2123
import java.net.HttpURLConnection;
2224
import java.net.URL;
@@ -34,7 +36,7 @@ private Properties getAuthenticationHandlerConfiguration(boolean anonymousAllowe
3436
@Test
3537
public void testGetUserName() throws Exception {
3638
PseudoAuthenticator authenticator = new PseudoAuthenticator();
37-
Assert.assertEquals(System.getProperty("user.name"), authenticator.getUserName());
39+
assertEquals(System.getProperty("user.name"), authenticator.getUserName());
3840
}
3941

4042
@Test
@@ -47,7 +49,7 @@ public void testAnonymousAllowed() throws Exception {
4749
URL url = new URL(auth.getBaseURL());
4850
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
4951
conn.connect();
50-
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
52+
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
5153
} finally {
5254
auth.stop();
5355
}
@@ -63,9 +65,9 @@ public void testAnonymousDisallowed() throws Exception {
6365
URL url = new URL(auth.getBaseURL());
6466
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
6567
conn.connect();
66-
Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
67-
Assert.assertTrue(conn.getHeaderFields().containsKey("WWW-Authenticate"));
68-
Assert.assertEquals("Authentication required", conn.getResponseMessage());
68+
assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
69+
assertTrue(conn.getHeaderFields().containsKey("WWW-Authenticate"));
70+
assertEquals("Authentication required", conn.getResponseMessage());
6971
} finally {
7072
auth.stop();
7173
}

0 commit comments

Comments
 (0)