PhysicalIndexMetaDataLoaderTest.java 2.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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.
 */

18
package org.apache.shardingsphere.infra.metadata.schema.loader.physical;
19

20
import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData;
21 22 23 24
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

25 26 27 28 29
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;

30 31
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
L
Liang Zhang 已提交
32 33
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
34 35 36
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
L
Liang Zhang 已提交
37
public final class PhysicalIndexMetaDataLoaderTest {
38 39 40
    
    @Test
    public void assertLoad() throws SQLException {
L
Liang Zhang 已提交
41
        Collection<IndexMetaData> actual = PhysicalIndexMetaDataLoader.load(mockConnection(), "tbl");
42
        assertThat(actual.size(), is(1));
43
        IndexMetaData indexMetaData = actual.iterator().next();
44 45
        assertThat(indexMetaData.getName(), is("my_index"));
    }
L
Liang Zhang 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    
    private Connection mockConnection() throws SQLException {
        Connection result = mock(Connection.class, RETURNS_DEEP_STUBS);
        ResultSet resultSet = mockResultSet();
        when(result.getMetaData().getIndexInfo("catalog", null, "tbl", false, false)).thenReturn(resultSet);
        when(result.getCatalog()).thenReturn("catalog");
        return result;
    }
    
    private ResultSet mockResultSet() throws SQLException {
        ResultSet result = mock(ResultSet.class);
        when(result.next()).thenReturn(true, true, false);
        when(result.getString("INDEX_NAME")).thenReturn("my_index");
        return result;
    }
61
}