|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery.exception; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | + |
| 21 | +import com.google.cloud.bigquery.BigQueryException; |
| 22 | +import java.util.stream.Stream; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.Arguments; |
| 26 | +import org.junit.jupiter.params.provider.MethodSource; |
| 27 | + |
| 28 | +public class BigQueryJdbcExceptionTest { |
| 29 | + |
| 30 | + @FunctionalInterface |
| 31 | + interface ExceptionCreator { |
| 32 | + Throwable create(String message, Throwable cause); |
| 33 | + } |
| 34 | + |
| 35 | + static Stream<Arguments> exceptionProvider() { |
| 36 | + return Stream.of( |
| 37 | + Arguments.of( |
| 38 | + (ExceptionCreator) |
| 39 | + (msg, cause) -> new BigQueryJdbcException(msg, (BigQueryException) cause)), |
| 40 | + Arguments.of((ExceptionCreator) BigQueryJdbcException::new), |
| 41 | + Arguments.of((ExceptionCreator) BigQueryJdbcRuntimeException::new), |
| 42 | + Arguments.of((ExceptionCreator) BigQueryConversionException::new), |
| 43 | + Arguments.of( |
| 44 | + (ExceptionCreator) |
| 45 | + (msg, cause) -> new BigQueryJdbcCoercionException((Exception) cause)), |
| 46 | + Arguments.of( |
| 47 | + (ExceptionCreator) |
| 48 | + (msg, cause) -> |
| 49 | + new BigQueryJdbcSqlSyntaxErrorException(msg, (BigQueryException) cause))); |
| 50 | + } |
| 51 | + |
| 52 | + @ParameterizedTest |
| 53 | + @MethodSource("exceptionProvider") |
| 54 | + public void testExceptionMessageFormatting(ExceptionCreator creator) { |
| 55 | + String message = "Custom error message"; |
| 56 | + Throwable cause = new BigQueryException(500, "Underlying error"); |
| 57 | + |
| 58 | + Throwable ex = creator.create(message, cause); |
| 59 | + |
| 60 | + String expectedPrefix = |
| 61 | + ex instanceof BigQueryJdbcCoercionException ? "Coercion error" : message; |
| 62 | + String expectedMessage = expectedPrefix + "\n" + cause.getMessage(); |
| 63 | + |
| 64 | + assertEquals(expectedMessage, ex.getMessage()); |
| 65 | + assertEquals(cause, ex.getCause()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testException_withCauseHavingNullMessage() { |
| 70 | + String message = "Custom error message"; |
| 71 | + Throwable cause = new RuntimeException(); // Null message |
| 72 | + |
| 73 | + BigQueryJdbcException ex = new BigQueryJdbcException(message, cause); |
| 74 | + |
| 75 | + String expectedMessage = message + "\n" + cause.toString(); |
| 76 | + assertEquals(expectedMessage, ex.getMessage()); |
| 77 | + } |
| 78 | +} |
0 commit comments