diff --git a/thread/framework/java/android/net/thread/ThreadNetworkException.java b/thread/framework/java/android/net/thread/ThreadNetworkException.java index 23ed53e1cc..66f13ce312 100644 --- a/thread/framework/java/android/net/thread/ThreadNetworkException.java +++ b/thread/framework/java/android/net/thread/ThreadNetworkException.java @@ -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; } diff --git a/thread/tests/cts/src/android/net/thread/cts/ThreadNetworkExceptionTest.java b/thread/tests/cts/src/android/net/thread/cts/ThreadNetworkExceptionTest.java new file mode 100644 index 0000000000..6770175d08 --- /dev/null +++ b/thread/tests/cts/src/android/net/thread/cts/ThreadNetworkExceptionTest.java @@ -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")); + } +}