Merge "[Thread] add CTS tests for ThreadNetworkException" into main am: 2fe1db6e49

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2949385

Change-Id: I940c62c71eaec045b8f2e6d07c655966cf6316a0
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Kangping Dong 2024-02-04 10:23:12 +00:00 • committed by Automerger Merge Worker
commit e36a2e2b16
2 changed files with 85 additions and 3 deletions

View file

@ -137,11 +137,29 @@ public class ThreadNetworkException extends Exception {
*/
public static final int ERROR_THREAD_DISABLED = 12;
private static final int ERROR_MIN = ERROR_INTERNAL_ERROR;
private static final int ERROR_MAX = ERROR_THREAD_DISABLED;
private final int mErrorCode;
/** Creates a new {@link ThreadNetworkException} object with given error code and message. */
public ThreadNetworkException(@ErrorCode int errorCode, @NonNull String errorMessage) {
super(requireNonNull(errorMessage, "errorMessage cannot be null"));
/**
* Creates a new {@link ThreadNetworkException} object with given error code and message.
*
* @throws IllegalArgumentException if {@code errorCode} is not a value in {@link #ERROR_}
* @throws NullPointerException if {@code message} is {@code null}
*/
public ThreadNetworkException(@ErrorCode int errorCode, @NonNull String message) {
super(requireNonNull(message, "message cannot be null"));
if (errorCode < ERROR_MIN || errorCode > ERROR_MAX) {
throw new IllegalArgumentException(
"errorCode cannot be "
+ errorCode
+ " (allowedRange = ["
+ ERROR_MIN
+ ", "
+ ERROR_MAX
+ "])");
}
this.mErrorCode = errorCode;
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (C) 2023 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.thread.cts;
import static android.net.thread.ThreadNetworkException.ERROR_INTERNAL_ERROR;
import static android.net.thread.ThreadNetworkException.ERROR_THREAD_DISABLED;
import static android.net.thread.ThreadNetworkException.ERROR_UNKNOWN;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import android.net.thread.ThreadNetworkException;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Tests for {@link ThreadNetworkException}. */
@SmallTest
@RunWith(AndroidJUnit4.class)
public final class ThreadNetworkExceptionTest {
@Test
public void constructor_validValues_valuesAreConnectlySet() throws Exception {
ThreadNetworkException errorThreadDisabled =
new ThreadNetworkException(ERROR_THREAD_DISABLED, "Thread disabled error!");
ThreadNetworkException errorInternalError =
new ThreadNetworkException(ERROR_INTERNAL_ERROR, "internal error!");
assertThat(errorThreadDisabled.getErrorCode()).isEqualTo(ERROR_THREAD_DISABLED);
assertThat(errorThreadDisabled.getMessage()).isEqualTo("Thread disabled error!");
assertThat(errorInternalError.getErrorCode()).isEqualTo(ERROR_INTERNAL_ERROR);
assertThat(errorInternalError.getMessage()).isEqualTo("internal error!");
}
@Test
public void constructor_nullMessage_throwsNullPointerException() throws Exception {
assertThrows(
NullPointerException.class,
() -> new ThreadNetworkException(ERROR_UNKNOWN, null /* message */));
}
@Test
public void constructor_invalidErrorCode_throwsIllegalArgumentException() throws Exception {
assertThrows(IllegalArgumentException.class, () -> new ThreadNetworkException(0, "0"));
assertThrows(IllegalArgumentException.class, () -> new ThreadNetworkException(13, "13"));
}
}