All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.MetaModelTestCase Maven / Gradle / Ivy

The newest version!
/**
 *  This file is part of MetaModel.
 *
 *  MetaModel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MetaModel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MetaModel.  If not, see .
 */
package dk.eobjects.metamodel;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.apache.commons.lang.ArrayUtils;
import org.easymock.EasyMock;

import dk.eobjects.metamodel.data.DataSet;
import dk.eobjects.metamodel.data.Row;
import dk.eobjects.metamodel.schema.Column;
import dk.eobjects.metamodel.schema.ColumnType;
import dk.eobjects.metamodel.schema.Relationship;
import dk.eobjects.metamodel.schema.Schema;
import dk.eobjects.metamodel.schema.Table;
import dk.eobjects.metamodel.schema.TableType;

/**
 * Convenient super-class to use for unittesting
 */
public abstract class MetaModelTestCase extends TestCase {

	public static final String COLUMN_CONTRIBUTOR_COUNTRY = "country";
	public static final String COLUMN_CONTRIBUTOR_NAME = "name";
	public static final String COLUMN_CONTRIBUTOR_CONTRIBUTOR_ID = "contributor_id";

	public static final String COLUMN_PROJECT_PROJECT_ID = "project_id";
	public static final String COLUMN_PROJECT_NAME = "name";
	public static final String COLUMN_PROJECT_LINES_OF_CODE = "lines_of_code";
	public static final String COLUMN_PROJECT_PARENT_PROJECT_ID = "parent_project_id";

	public static final String COLUMN_ROLE_PROJECT_ID = "project_id";
	public static final String COLUMN_ROLE_CONTRIBUTOR_ID = "contributor_id";
	public static final String COLUMN_ROLE_ROLE_NAME = "name";

	public static final String COLUMN_PROJECT_CONTRIBUTOR_CONTRIBUTOR = "contributor";
	public static final String COLUMN_PROJECT_CONTRIBUTOR_ROLE = "role";
	public static final String COLUMN_PROJECT_CONTRIBUTOR_PROJECT = "project";

	public static final String TABLE_PROJECT_CONTRIBUTOR = "project_contributor";
	public static final String TABLE_ROLE = "role";
	public static final String TABLE_PROJECT = "project";
	public static final String TABLE_CONTRIBUTOR = "contributor";

	/**
	 * Creates an example schema with three tables and a view:
	 * 
    *
  • contributor[contributor_id,name,country] (TABLE)
  • *
  • project[project_id,name,lines_of_code,parent_project_id] (TABLE)
  • *
  • role[contributor_id,project_id,role_name] (TABLE)
  • *
  • project_contributor[contributor,project,role] (VIEW)
  • *
* The example schema is good for testing purposes and possess various * features of the schema model: *
    *
  • Relations between tables: one-Contributor-to-many-Role's and * many-Role's-to-one-Project
  • *
  • Recursive relations: A project can have a parent project
  • *
  • Views: The ProjectContributor view
  • *
*/ protected Schema getExampleSchema() { Schema schema = new Schema("MetaModelSchema"); Table table1 = new Table(TABLE_CONTRIBUTOR, TableType.TABLE, schema); Column column1 = new Column(COLUMN_CONTRIBUTOR_CONTRIBUTOR_ID, ColumnType.INTEGER, table1, 0, false).setIndexed(true); Column column2 = new Column(COLUMN_CONTRIBUTOR_NAME, ColumnType.VARCHAR, table1, 1, false); Column column3 = new Column(COLUMN_CONTRIBUTOR_COUNTRY, ColumnType.VARCHAR, table1, 2, true); table1.setColumns(column1, column2, column3); Table table2 = new Table(TABLE_PROJECT, TableType.TABLE, schema); Column column4 = new Column(COLUMN_PROJECT_PROJECT_ID, ColumnType.INTEGER, table2, 0, false); Column column5 = new Column(COLUMN_PROJECT_NAME, ColumnType.VARCHAR, table2, 1, false); Column column6 = new Column(COLUMN_PROJECT_LINES_OF_CODE, ColumnType.BIGINT, table2, 2, true); Column column7 = new Column(COLUMN_PROJECT_PARENT_PROJECT_ID, ColumnType.INTEGER, table2, 3, true); table2.setColumns(column4, column5, column6, column7); Table table3 = new Table(TABLE_ROLE, TableType.TABLE, schema); Column column8 = new Column(COLUMN_ROLE_CONTRIBUTOR_ID, ColumnType.INTEGER, table3, 0, false); Column column9 = new Column(COLUMN_ROLE_PROJECT_ID, ColumnType.INTEGER, table3, 1, false); Column column10 = new Column(COLUMN_ROLE_ROLE_NAME, ColumnType.VARCHAR, table3, 2, false); table3.setColumns(column8, column9, column10); Table table4 = new Table(TABLE_PROJECT_CONTRIBUTOR, TableType.VIEW, schema); Column column11 = new Column(COLUMN_PROJECT_CONTRIBUTOR_CONTRIBUTOR, ColumnType.VARCHAR, table4, 0, false); Column column12 = new Column(COLUMN_PROJECT_CONTRIBUTOR_PROJECT, ColumnType.VARCHAR, table4, 1, false); Column column13 = new Column(COLUMN_PROJECT_CONTRIBUTOR_ROLE, ColumnType.VARCHAR, table4, 2, false); ArrayList columnList = new ArrayList(); columnList.add(column11); columnList.add(column12); columnList.add(column13); table4.setColumns(columnList); // one-Contributor-to-many-Role's Relationship.createRelationship(new Column[] { column1 }, new Column[] { column8 }); // one-Project-to-many-Role's Relationship.createRelationship(new Column[] { column4 }, new Column[] { column9 }); // view relation [contributor -> contributor_name] Relationship.createRelationship(new Column[] { column2 }, new Column[] { column11 }); // view relation [project -> project_name] Relationship.createRelationship(new Column[] { column5 }, new Column[] { column12 }); // view relation [role -> role_name] Relationship.createRelationship(new Column[] { column10 }, new Column[] { column13 }); schema.setTables(table1, table2, table3, table4); return schema; } private List _mocks = new ArrayList(); private Connection _connection; @Override protected void setUp() throws Exception { super.setUp(); _mocks.clear(); } @Override protected void tearDown() throws Exception { super.tearDown(); if (_connection != null) { if (!_connection.isClosed()) { _connection.close(); } _connection = null; } } public T createMock(Class clazz) { T mock = EasyMock.createMock(clazz); _mocks.add(mock); return mock; } public void verifyMocks() { EasyMock.verify(_mocks.toArray()); } public void replayMocks() { EasyMock.replay(_mocks.toArray()); } public Connection getTestDbConnection() throws Exception { if (_connection == null || _connection.isClosed()) { Class.forName("org.hsqldb.jdbcDriver"); _connection = DriverManager .getConnection("jdbc:hsqldb:res:metamodel"); _connection.setReadOnly(true); } return _connection; } public void assertEquals(DataSet ds1, DataSet ds2) { assertEquals(ArrayUtils.toString(ds1.getSelectItems()), ArrayUtils .toString(ds2.getSelectItems())); boolean ds1next = true; while (ds1next) { ds1next = ds1.next(); boolean ds2next = ds2.next(); assertEquals("DataSet 1 next=" + ds1next, ds1next, ds2next); if (ds1next) { Row row1 = ds1.getRow(); Row row2 = ds2.getRow(); assertEquals(row1, row2); } } } protected File getTestResourceAsFile(String filename) { return new File("src/test/resources/" + filename); } }