org.gaixie.jibu.JibuTestSupport.java Source code

Java tutorial

Introduction

Here is the source code for org.gaixie.jibu.JibuTestSupport.java

Source

/**
 * Copyright (C) 2014 Gaixie.ORG.
 *
 * 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.
 */

package org.gaixie.jibu;

import com.google.inject.Guice;
import com.google.inject.Injector;

import org.gaixie.jibu.JibuConfig;
import org.gaixie.jibu.dao.DAOModule;
import org.gaixie.jibu.service.ServiceModule;
import org.gaixie.jibu.utils.ConnectionUtils;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

/**
 * ?Guice?injector???injector?
 * ??jibu.propertiesdatabaseTypeDAO?
 * ??
 */
public class JibuTestSupport {
    private static Injector injector;

    public synchronized Injector getInjector() {
        if (injector != null) {
            return injector;
        }
        String databaseType = JibuConfig.getProperty("databaseType");
        injector = Guice.createInjector(new ServiceModule(), new DAOModule(databaseType));
        return injector;
    }

    /**
     * ????
     * ??
     */
    protected void clearTable() {
        Connection conn = null;
        try {
            conn = ConnectionUtils.getConnection();
            QueryRunner run = new QueryRunner();
            run.update(conn, "DELETE from schema_changes");
            DbUtils.commitAndClose(conn);
        } catch (SQLException e) {
            DbUtils.rollbackAndCloseQuietly(conn);
            System.out.println(e.getMessage());
        }
    }
}