Merge changes from topic "QosCallbackException"

* changes:
  CTS test for QosCallbackException
  Expose constructor of Exceptions.
This commit is contained in:
Sewook Seo
2022-03-19 00:35:28 +00:00
committed by Gerrit Code Review
6 changed files with 120 additions and 9 deletions

View File

@@ -361,6 +361,7 @@ package android.net {
}
public class NetworkReleasedException extends java.lang.Exception {
ctor public NetworkReleasedException();
}
public class NetworkRequest implements android.os.Parcelable {
@@ -425,6 +426,8 @@ package android.net {
}
public final class QosCallbackException extends java.lang.Exception {
ctor public QosCallbackException(@NonNull String);
ctor public QosCallbackException(@NonNull Throwable);
}
public abstract class QosFilter {
@@ -470,9 +473,11 @@ package android.net {
}
public class SocketLocalAddressChangedException extends java.lang.Exception {
ctor public SocketLocalAddressChangedException();
}
public class SocketNotBoundException extends java.lang.Exception {
ctor public SocketNotBoundException();
}
public final class StaticIpConfiguration implements android.os.Parcelable {

View File

@@ -18,6 +18,8 @@ package android.net;
import android.annotation.SystemApi;
import com.android.internal.annotations.VisibleForTesting;
/**
* Indicates that the {@link Network} was released and is no longer available.
*
@@ -25,7 +27,7 @@ import android.annotation.SystemApi;
*/
@SystemApi
public class NetworkReleasedException extends Exception {
/** @hide */
@VisibleForTesting
public NetworkReleasedException() {
super("The network was released and is no longer available");
}

View File

@@ -21,6 +21,8 @@ import android.annotation.NonNull;
import android.annotation.SystemApi;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -94,16 +96,12 @@ public final class QosCallbackException extends Exception {
}
}
/**
* @hide
*/
@VisibleForTesting
public QosCallbackException(@NonNull final String message) {
super(message);
}
/**
* @hide
*/
@VisibleForTesting
public QosCallbackException(@NonNull final Throwable cause) {
super(cause);
}

View File

@@ -18,6 +18,8 @@ package android.net;
import android.annotation.SystemApi;
import com.android.internal.annotations.VisibleForTesting;
/**
* Thrown when the local address of the socket has changed.
*
@@ -25,7 +27,7 @@ import android.annotation.SystemApi;
*/
@SystemApi
public class SocketLocalAddressChangedException extends Exception {
/** @hide */
@VisibleForTesting
public SocketLocalAddressChangedException() {
super("The local address of the socket changed");
}

View File

@@ -18,6 +18,8 @@ package android.net;
import android.annotation.SystemApi;
import com.android.internal.annotations.VisibleForTesting;
/**
* Thrown when a previously bound socket becomes unbound.
*
@@ -25,7 +27,7 @@ import android.annotation.SystemApi;
*/
@SystemApi
public class SocketNotBoundException extends Exception {
/** @hide */
@VisibleForTesting
public SocketNotBoundException() {
super("The socket is unbound");
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net.cts;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import android.net.NetworkReleasedException;
import android.net.QosCallbackException;
import android.net.SocketLocalAddressChangedException;
import android.net.SocketNotBoundException;
import android.os.Build;
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo;
import com.android.testutils.DevSdkIgnoreRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(DevSdkIgnoreRunner.class)
@IgnoreUpTo(Build.VERSION_CODES.R)
public class QosCallbackExceptionTest {
private static final String ERROR_MESSAGE = "Test Error Message";
private static final String ERROR_MSG_SOCK_NOT_BOUND = "The socket is unbound";
private static final String ERROR_MSG_NET_RELEASED =
"The network was released and is no longer available";
private static final String ERROR_MSG_SOCK_ADDR_CHANGED =
"The local address of the socket changed";
@Test
public void testQosCallbackException() throws Exception {
final Throwable testcause = new Throwable(ERROR_MESSAGE);
final QosCallbackException exception = new QosCallbackException(testcause);
assertEquals(testcause, exception.getCause());
final QosCallbackException exceptionMsg = new QosCallbackException(ERROR_MESSAGE);
assertEquals(ERROR_MESSAGE, exceptionMsg.getMessage());
}
@Test
public void testNetworkReleasedExceptions() throws Exception {
final Throwable netReleasedException = new NetworkReleasedException();
final QosCallbackException exception = new QosCallbackException(netReleasedException);
assertTrue(exception.getCause() instanceof NetworkReleasedException);
assertEquals(netReleasedException, exception.getCause());
assertTrue(exception.getMessage().contains(ERROR_MSG_NET_RELEASED));
assertThrowableMessageContains(exception, ERROR_MSG_NET_RELEASED);
}
@Test
public void testSocketNotBoundExceptions() throws Exception {
final Throwable sockNotBoundException = new SocketNotBoundException();
final QosCallbackException exception = new QosCallbackException(sockNotBoundException);
assertTrue(exception.getCause() instanceof SocketNotBoundException);
assertEquals(sockNotBoundException, exception.getCause());
assertTrue(exception.getMessage().contains(ERROR_MSG_SOCK_NOT_BOUND));
assertThrowableMessageContains(exception, ERROR_MSG_SOCK_NOT_BOUND);
}
@Test
public void testSocketLocalAddressChangedExceptions() throws Exception {
final Throwable localAddrChangedException = new SocketLocalAddressChangedException();
final QosCallbackException exception = new QosCallbackException(localAddrChangedException);
assertTrue(exception.getCause() instanceof SocketLocalAddressChangedException);
assertEquals(localAddrChangedException, exception.getCause());
assertTrue(exception.getMessage().contains(ERROR_MSG_SOCK_ADDR_CHANGED));
assertThrowableMessageContains(exception, ERROR_MSG_SOCK_ADDR_CHANGED);
}
private void assertThrowableMessageContains(QosCallbackException exception, String errorMsg)
throws Exception {
try {
triggerException(exception);
fail("Expect exception");
} catch (QosCallbackException e) {
assertTrue(e.getMessage().contains(errorMsg));
}
}
private void triggerException(QosCallbackException exception) throws Exception {
throw new QosCallbackException(exception.getCause());
}
}