/*
* Copyright 2004-2015 the Seasar Foundation and the Others.
*
* 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.seasar.extension.jdbc.gen.internal.generator;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.seasar.extension.jdbc.gen.desc.SequenceDesc;
import org.seasar.extension.jdbc.gen.desc.TableDesc;
import org.seasar.extension.jdbc.gen.generator.GenerationContext;
import org.seasar.extension.jdbc.gen.internal.dialect.HsqlGenDialect;
import org.seasar.extension.jdbc.gen.internal.model.TableModelFactoryImpl;
import org.seasar.extension.jdbc.gen.model.SqlIdentifierCaseType;
import org.seasar.extension.jdbc.gen.model.SqlKeywordCaseType;
import org.seasar.extension.jdbc.gen.model.TableModel;
import org.seasar.framework.mock.sql.MockConnection;
import org.seasar.framework.mock.sql.MockDataSource;
import org.seasar.framework.mock.sql.MockPreparedStatement;
import org.seasar.framework.mock.sql.MockResultSet;
import org.seasar.framework.util.ArrayMap;
import org.seasar.framework.util.TextUtil;
import static org.junit.Assert.*;
/**
* @author taedium
*
*/
public class GenerateSequenceTest {
private GeneratorImplStub generator;
private DataSource dataSource;
private TableModel model;
/**
*
* @throws Exception
*/
@Before
public void setUp() throws Exception {
generator = new GeneratorImplStub();
dataSource = new MockDataSource() {
@Override
public Connection getConnection() throws SQLException {
return new MockConnection() {
@Override
public MockPreparedStatement prepareMockStatement(String sql) {
return new MockPreparedStatement(this, sql) {
@Override
public ResultSet executeQuery() throws SQLException {
MockResultSet resultSet = new MockResultSet();
ArrayMap map = new ArrayMap();
map.put(null, 200);
resultSet.addRowData(map);
return resultSet;
}
};
}
};
}
};
SequenceDesc sequenceDesc = new SequenceDesc();
sequenceDesc.setSequenceName("HOGE");
sequenceDesc.setInitialValue(1);
sequenceDesc.setAllocationSize(50);
sequenceDesc.setDataType("integer");
TableDesc tableDesc = new TableDesc();
tableDesc.setCanonicalName("aaa");
tableDesc.addSequenceDesc(sequenceDesc);
TableModelFactoryImpl factory = new TableModelFactoryImpl(
new HsqlGenDialect(), dataSource,
SqlIdentifierCaseType.ORIGINALCASE,
SqlKeywordCaseType.ORIGINALCASE, ';', null, false) {
@Override
protected Long getNextValue(String sequenceName, int allocationSize) {
return 200L;
}
};
model = factory.getTableModel(tableDesc);
}
/**
*
* @throws Exception
*/
@Test
public void testCreate() throws Exception {
GenerationContext context = new GenerationContextImpl(model, new File(
"file"), "sql/create-sequence.ftl", "UTF-8", false);
generator.generate(context);
String path = getClass().getName().replace(".", "/") + "_Create.txt";
assertEquals(TextUtil.readUTF8(path), generator.getResult());
}
/**
*
* @throws Exception
*/
@Test
public void testDrop() throws Exception {
GenerationContext context = new GenerationContextImpl(model, new File(
"file"), "sql/drop-sequence.ftl", "UTF-8", false);
generator.generate(context);
String path = getClass().getName().replace(".", "/") + "_Drop.txt";
assertEquals(TextUtil.readUTF8(path), generator.getResult());
}
}