SQLAssertJAXBHelper.java 4.2 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * 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.
 * </p>
 */

18
package io.shardingjdbc.core.integrate.jaxb.helper;
19

20
import io.shardingjdbc.core.constant.DatabaseType;
21
import io.shardingjdbc.core.constant.SQLType;
22 23
import io.shardingjdbc.core.integrate.jaxb.SQLAssert;
import io.shardingjdbc.core.integrate.jaxb.SQLAsserts;
24
import io.shardingjdbc.test.sql.jaxb.helper.SQLStatementHelper;
25 26 27 28 29 30 31 32

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
33
import java.util.List;
34

35
public final class SQLAssertJAXBHelper {
36
    
37 38 39 40 41 42 43
    /**
     * Get data parameters.
     * 
     * @param filePath file path
     * @param sqlType SQL type
     * @return data parameters
     */
44
    public static Collection<Object[]> getDataParameters(final String filePath, final SQLType sqlType) {
45
        Collection<Object[]> result = new ArrayList<>();
46
        URL url = SQLAssertJAXBHelper.class.getClassLoader().getResource(filePath);
47 48 49
        if (null == url) {
            return Collections.emptyList();
        }
50 51
        File assertFilePath = new File(url.getPath());
        if (!assertFilePath.exists()) {
52 53
            return Collections.emptyList();
        }
54 55 56 57 58 59 60 61 62 63
        
        if (assertFilePath.isDirectory()) {
            File[] files = assertFilePath.listFiles();
            if (null == files) {
                return Collections.emptyList();
            }
            for (File each : files) {
                if (each.isDirectory()) {
                    continue;
                }
64 65 66
                if (isTypeMatched(each.getName(), sqlType)) {
                    result.addAll(dataParameters(each));
                }
67 68
            }
        } else {
69 70 71
            if (isTypeMatched(assertFilePath.getName(), sqlType)) {
                result.addAll(dataParameters(assertFilePath));
            }
72 73 74 75 76
        }
        return result;
    }
    
    private static Collection<Object[]> dataParameters(final File file) {
T
terrymanu 已提交
77
        SQLAsserts asserts = loadSQLAsserts(file);
78
        List<Object[]> result = new ArrayList<>();
79
        for (int i = 0; i < asserts.getSqlAsserts().size(); i++) {
80
            SQLAssert assertObj = asserts.getSqlAsserts().get(i);
81 82
            for (io.shardingjdbc.test.sql.jaxb.DatabaseType each : SQLStatementHelper.getTypes(assertObj.getId())) {
                result.add(getDataParameter(assertObj, DatabaseType.valueOf(each.name())));
83
            }
84
        }
85
        return result;
86 87
    }
    
T
terrymanu 已提交
88
    private static SQLAsserts loadSQLAsserts(final File file) {
89
        try {
90
            return (SQLAsserts) JAXBContext.newInstance(SQLAsserts.class).createUnmarshaller().unmarshal(file);
91 92 93 94 95
        } catch (final JAXBException ex) {
            throw new RuntimeException(ex);
        }
    }
    
96
    private static Object[] getDataParameter(final SQLAssert sqlAssert, final DatabaseType dbType) {
97 98
        final Object[] result = new Object[4];
        result[0] = sqlAssert.getId();
99
        result[1] = SQLStatementHelper.getSql(sqlAssert.getId());
100
        result[2] = dbType;
101 102 103
        result[3] = sqlAssert.getSqlShardingRules();
        return result;
    }
104 105 106 107 108 109 110 111 112 113 114 115
    
    private static boolean isTypeMatched(final String fileName, final SQLType sqlType) {
        switch (sqlType) {
            case DDL:
                return fileName.startsWith("alter") || fileName.startsWith("create") || fileName.startsWith("drop") || fileName.startsWith("truncate");
            case DML:
                return fileName.startsWith("delete") || fileName.startsWith("insert") || fileName.startsWith("update");
            case DQL:
                return fileName.startsWith("select");
            default: return false;
        }
    }
116
}