org.activiti5.engine.test.db.MetaDataTest.java Source code

Java tutorial

Introduction

Here is the source code for org.activiti5.engine.test.db.MetaDataTest.java

Source

/* 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.activiti5.engine.test.db;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import org.activiti5.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti5.engine.impl.db.DbSqlSession;
import org.activiti5.engine.impl.interceptor.Command;
import org.activiti5.engine.impl.interceptor.CommandContext;
import org.activiti5.engine.impl.test.PluggableActivitiTestCase;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Tom Baeyens
 */
public class MetaDataTest extends PluggableActivitiTestCase {

    private static Logger log = LoggerFactory.getLogger(MetaDataTest.class);

    public void testMetaData() {
        ProcessEngineConfigurationImpl activiti5ProcessEngineConfig = (ProcessEngineConfigurationImpl) processEngineConfiguration
                .getActiviti5CompatibilityHandler().getRawProcessConfiguration();
        activiti5ProcessEngineConfig.getCommandExecutor().execute(new Command<Object>() {
            public Object execute(CommandContext commandContext) {
                // PRINT THE TABLE NAMES TO CHECK IF WE CAN USE METADATA INSTEAD
                // THIS IS INTENDED FOR TEST THAT SHOULD RUN ON OUR QA INFRASTRUCTURE TO SEE IF METADATA 
                // CAN BE USED INSTEAD OF PERFORMING A QUERY THAT MIGHT FAIL
                try {
                    SqlSession sqlSession = commandContext.getSession(DbSqlSession.class).getSqlSession();
                    ResultSet tables = sqlSession.getConnection().getMetaData().getTables(null, null, null, null);
                    while (tables.next()) {
                        ResultSetMetaData resultSetMetaData = tables.getMetaData();
                        int columnCount = resultSetMetaData.getColumnCount();
                        for (int i = 1; i <= columnCount; i++) {
                            log.info("result set column {}|{}|{}|{}", i, resultSetMetaData.getColumnName(i),
                                    resultSetMetaData.getColumnLabel(i), tables.getString(i));
                        }
                        log.info("-------------------------------------------------------");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }
}