8000 HADOOP-19414. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-auth. by slfan1989 · Pull Request #7638 · apache/hadoop · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

HADOOP-19414. [JDK17] Upgrade JUnit from 4 to 5 in hadoop-auth. #7638

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

Merged
merged 2 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions hadoop-common-project/hadoop-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@
<artifactId>guava</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
&l 10000 t;artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
< 10000 tr data-hunk="82914f7b44e7faf62cbd7e5c7ef6087c6af4c1da70118c5618d7268cd57cb710" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
package org.apache.hadoop.security.authentication.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
Expand Down Expand Up @@ -55,8 +60,6 @@
import java.util.EnumSet;
import java.util.Properties;

import org.junit.Assert;

public class AuthenticatorTestCase {
private Server server;
private String host = null;
Expand Down Expand Up @@ -170,11 +173,11 @@ protected void _testAuthentication(Authenticator authenticator, boolean doPost)
try {
URL url = new URL(getBaseURL());
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
Assert.assertFalse(token.isSet());
assertFalse(token.isSet());
TestConnectionConfigurator connConf = new TestConnectionConfigurator();
AuthenticatedURL aUrl = new AuthenticatedURL(authenticator, connConf);
HttpURLConnection conn = aUrl.openConnection(url, token);
Assert.assertTrue(connConf.invoked);
assertTrue(connConf.invoked);
String tokenStr = token.toString();
if (doPost) {
conn.setRequestMethod("POST");
Expand All @@ -186,18 +189,18 @@ protected void _testAuthentication(Authenticator authenticator, boolean doPost)
writer.write(POST);
writer.close();
}
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
if (doPost) {
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String echo = reader.readLine();
Assert.assertEquals(POST, echo);
Assert.assertNull(reader.readLine());
assertEquals(POST, echo);
assertNull(reader.readLine());
}
aUrl = new AuthenticatedURL();
conn = aUrl.openConnection(url, token);
conn.connect();
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
Assert.assertEquals(tokenStr, token.toString());
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
assertEquals(tokenStr, token.toString());
} finally {
stop();
}
Expand Down Expand Up @@ -233,7 +236,7 @@ private void doHttpClientRequest(HttpClient httpClient, HttpUriRequest request)
try {
response = httpClient.execute(request);
final int httpStatus = response.getStatusLine().getStatusCode();
Assert.assertEquals(HttpURLConnection.HTTP_OK, httpStatus);
assertEquals(HttpURLConnection.HTTP_OK, httpStatus);
} finally {
if (response != null) EntityUtils.consumeQuietly(response.getEntity());
}
Expand All @@ -255,7 +258,7 @@ protected void _testAuthenticationHttpClient(Authenticator authenticator, boolea
// Important that the entity is not repeatable -- this means if
// we have to renegotiate (e.g. b/c the cookie wasn't handled properly)
// the test will fail.
Assert.assertFalse(entity.isRepeatable());
assertFalse(entity.isRepeatable());
post.setEntity(entity);
doHttpClientRequest(httpClient, post);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,17 @@
*/
package org.apache.hadoop.security.authentication.client;

import org.junit.Assert;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.net.HttpURLConnection;
Expand All @@ -29,128 +38,128 @@ public class TestAuthenticatedURL {
@Test
public void testToken() throws Exception {
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
Assert.assertFalse(token.isSet());
assertFalse(token.isSet());
token = new AuthenticatedURL.Token("foo");
Assert.assertTrue(token.isSet());
Assert.assertEquals("foo", token.toString());
assertTrue(token.isSet());
assertEquals("foo", token.toString());
}

@Test
public void testInjectToken() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
HttpURLConnection conn = mock(HttpURLConnection.class);
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
token.set("foo");
AuthenticatedURL.injectToken(conn, token);
Mockito.verify(conn).addRequestProperty(Mockito.eq("Cookie"), Mockito.anyString());
verify(conn).addRequestProperty(eq("Cookie"), anyString());
}

@Test
public void testExtractTokenOK() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
HttpURLConnection conn = mock(HttpURLConnection.class);

Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);

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

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

Assert.assertEquals(tokenStr, token.toString());
assertEquals(tokenStr, token.toString());
}

@Test
public void testExtractTokenFail() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
HttpURLConnection conn = mock(HttpURLConnection.class);

Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);

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

AuthenticatedURL.Token token = new AuthenticatedURL.Token();
token.set("bar");
try {
AuthenticatedURL.extractToken(conn, token);
Assert.fail();
fail();
} catch (AuthenticationException ex) {
// Expected
Assert.assertFalse(token.isSet());
assertFalse(token.isSet());
} catch (Exception ex) {
Assert.fail();
fail();
}
}

@Test
public void testExtractTokenCookieHeader() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
HttpURLConnection conn = mock(HttpURLConnection.class);

Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);

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

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

Assert.assertTrue(token.isSet());
assertTrue(token.isSet());
}

@Test
public void testExtractTokenLowerCaseCookieHeader() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
HttpURLConnection conn = mock(HttpURLConnection.class);

Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);

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

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

Assert.assertTrue(token.isSet());
assertTrue(token.isSet());
}

@Test
public void testConnectionConfigurator() throws Exception {
HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
Mockito.when(conn.getResponseCode()).
HttpURLConnection conn = mock(HttpURLConnection.class);
when(conn.getResponseCode()).
thenReturn(HttpURLConnection.HTTP_UNAUTHORIZED);

ConnectionConfigurator connConf =
Mockito.mock(ConnectionConfigurator.class);
mock(ConnectionConfigurator.class);
Mockito.when(connConf.configure(Mockito.<HttpURLConnection>any())).
thenReturn(conn);

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

AuthenticatedURL aURL = new AuthenticatedURL(authenticator, connConf);
aURL.openConnection(new URL("http://foo"), new AuthenticatedURL.Token());
Mockito.verify(connConf).configure(Mockito.<HttpURLConnection>any());
verify(connConf).configure(Mockito.<HttpURLConnection>any());
}

@Test
public void testGetAuthenticator() throws Exception {
Authenticator authenticator = Mockito.mock(Authenticator.class);
Authenticator authenticator = mock(Authenticator.class);

AuthenticatedURL aURL = new AuthenticatedURL(authenticator);
Assert.assertEquals(authenticator, aURL.getAuthenticator());
assertEquals(authenticator, aURL.getAuthenticator());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
*/
package org.apache.hadoop.security.authentication.client;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.hadoop.security.authentication.server.AuthenticationFilter;
import org.apache.hadoop.security.authentication.server.PseudoAuthenticationHandler;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Test;

import java.net.HttpURLConnection;
import java.net.URL;
Expand All @@ -34,7 +36,7 @@ private Properties getAuthenticationHandlerConfiguration(boolean anonymousAllowe
@Test
public void testGetUserName() throws Exception {
PseudoAuthenticator authenticator = new PseudoAuthenticator();
Assert.assertEquals(System.getProperty("user.name"), authenticator.getUserName());
assertEquals(System.getProperty("user.name"), authenticator.getUserName());
}

@Test
Expand All @@ -47,7 +49,7 @@ public void testAnonymousAllowed() throws Exception {
URL url = new URL(auth.getBaseURL());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
} finally {
auth.stop();
}
Expand All @@ -63,9 +65,9 @@ public void testAnonymousDisallowed() throws Exception {
URL url = new URL(auth.getBaseURL());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
Assert.assertTrue(conn.getHeaderFields().containsKey("WWW-Authenticate"));
Assert.assertEquals("Authentication required", conn.getResponseMessage());
assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, conn.getResponseCode());
assertTrue(conn.getHeaderFields().containsKey("WWW-Authenticate"));
assertEquals("Authentication required", conn.getResponseMessage());
} finally {
auth.stop();
}
Expand Down
Loading
0