Skip to content

Commit

Permalink
Add unit test for DataSourcesCloserTest (#31060)
Browse files Browse the repository at this point in the history
* Add unit test for DataSourcesCloserTest

* Rename test to assertClose

* Add QuietlyCloser Unit Test
  • Loading branch information
sroopsai committed Apr 30, 2024
1 parent 24c7c89 commit abd3fb6
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.infra.util.close;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import javax.sql.DataSource;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

class DataSourcesCloserTest {

@Test
void assertClose() {
DataSource mockDataSource = Mockito.mock(DataSource.class, Mockito.withSettings().extraInterfaces(AutoCloseable.class));
DataSourcesCloser.close(Collections.singletonList(mockDataSource));
assertDoesNotThrow(() -> verify((AutoCloseable) mockDataSource, times(1)).close());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.shardingsphere.infra.util.close;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

class QuietlyCloserTest {

@Test
void testClose() {
AutoCloseable mockAutoCloseable = Mockito.mock(AutoCloseable.class);
QuietlyCloser.close(mockAutoCloseable);
assertDoesNotThrow(() -> verify(mockAutoCloseable, times(1)).close());
}
}

0 comments on commit abd3fb6

Please sign in to comment.