|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 software.amazon.jdbc.exceptions; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | + |
| 21 | +import com.mysql.cj.exceptions.CJException; |
| 22 | +import java.sql.SQLNonTransientConnectionException; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import software.amazon.jdbc.targetdriverdialect.MysqlConnectorJTargetDriverDialect; |
| 25 | + |
| 26 | +public class MySQLExceptionHandlerTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testIsLoginException_NestedAuthenticationException() { |
| 30 | + |
| 31 | + MySQLExceptionHandler handler = new MySQLExceptionHandler(); |
| 32 | + |
| 33 | + // Create the nested exception structure as described in requirements |
| 34 | + // Inner cause: Authentication exception with SQL state "28000" |
| 35 | + CJException authException = new CJException( |
| 36 | + "Access denied for user 'db_user'@'172.18.0.1' (using password: YES)" |
| 37 | + ); |
| 38 | + authException.setSQLState("28000"); |
| 39 | + |
| 40 | + // Outer exception: Connection exception with SQL state "08001" |
| 41 | + SQLNonTransientConnectionException connectionException = new SQLNonTransientConnectionException( |
| 42 | + "Could not create connection to database server. Attempted reconnect 3 times. Giving up.", |
| 43 | + "08001", |
| 44 | + authException |
| 45 | + ); |
| 46 | + |
| 47 | + assertTrue(handler.isLoginException(connectionException, new MysqlConnectorJTargetDriverDialect()), |
| 48 | + "Should detect nested authentication exception with dialect"); |
| 49 | + } |
| 50 | +} |
0 commit comments