Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 25a9c2181c54e65ea7dcb28a663670d711d1b0e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.emf.cdo.tests.store;

import org.eclipse.emf.cdo.internal.server.Transaction;
import org.eclipse.emf.cdo.server.IStore;
import org.eclipse.emf.cdo.server.internal.db.CDODBSchema;
import org.eclipse.emf.cdo.server.internal.db.DBStore;
import org.eclipse.emf.cdo.server.internal.db.HorizontalMappingStrategy;
import org.eclipse.emf.cdo.server.internal.db.MappingStrategy;
import org.eclipse.emf.cdo.server.internal.db.ToMany;
import org.eclipse.emf.cdo.server.internal.db.ToOne;

import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBConnectionProvider;
import org.eclipse.net4j.db.hsqldb.HSQLDBDataSource;
import org.eclipse.net4j.db.internal.hsqldb.HSQLDBAdapter;
import org.eclipse.net4j.internal.db.DataSourceConnectionProvider;
import org.eclipse.net4j.util.WrappedException;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public class DBStoreHorizontalTest extends TestLogic
{
  private HorizontalMappingStrategy mappingStrategy;

  private HSQLDBAdapter dbAdapter;

  private IDBConnectionProvider dbConnectionProvider;

  private DBStore store;

  public DBStoreHorizontalTest()
  {
  }

  @Override
  protected void doTearDown() throws Exception
  {
    store.getDBSchema().drop(dbAdapter, dbConnectionProvider);
    super.doTearDown();
    CDODBSchema.INSTANCE.drop(dbAdapter, dbConnectionProvider);
  }

  @Override
  protected IStore createStore()
  {
    mappingStrategy = createMappingStrategy();
    dbAdapter = createDBAdapter();
    dbConnectionProvider = createDBConnectionProvider();

    store = new DBStore();
    store.setMappingStrategy(mappingStrategy);
    store.setDbAdapter(dbAdapter);
    store.setDbConnectionProvider(dbConnectionProvider);
    return store;
  }

  protected IDBConnectionProvider createDBConnectionProvider()
  {
    HSQLDBDataSource dataSource = new HSQLDBDataSource();
    dataSource.setDatabase("jdbc:hsqldb:mem:storetest");
    dataSource.setUser("sa");

    return new DataSourceConnectionProvider(dataSource);
  }

  protected HSQLDBAdapter createDBAdapter()
  {
    return new HSQLDBAdapter();
  }

  protected HorizontalMappingStrategy createMappingStrategy()
  {
    Map<String, String> props = new HashMap<String, String>();
    props.put(MappingStrategy.PROP_TO_MANY_REFERENCE_MAPPING, ToMany.PER_CLASS.toString());
    props.put(MappingStrategy.PROP_TO_ONE_REFERENCE_MAPPING, ToOne.LIKE_ATTRIBUTES.toString());

    HorizontalMappingStrategy mappingStrategy = new HorizontalMappingStrategy();
    mappingStrategy.setProperties(props);
    return mappingStrategy;
  }

  private void verifyRowCount(int expectedRows, String tableName)
  {
    int actuaRowsl = query("select count(*) from " + tableName);
    assertEquals("Rows in " + tableName.toUpperCase(), expectedRows, actuaRowsl);
  }

  private int query(String sql)
  {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try
    {
      connection = store.getConnection();
      statement = connection.createStatement();
      resultSet = statement.executeQuery(sql);
      if (!resultSet.next())
      {
        return 0;
      }

      return resultSet.getInt(1);
    }
    catch (Exception ex)
    {
      throw WrappedException.wrap(ex);
    }
    finally
    {
      DBUtil.close(resultSet);
      DBUtil.close(statement);
      DBUtil.close(connection);
    }
  }

  @Override
  protected void verifyCreateModel1(Transaction transaction)
  {
    verifyRowCount(1, "cdo_repository");
    verifyRowCount(1, "cdo_packages");
    verifyRowCount(11, "cdo_classes");
    verifyRowCount(8, "cdo_supertypes");
    verifyRowCount(26, "cdo_features");
  }

  @Override
  protected void verifyCreateModel2(Transaction transaction)
  {
    verifyRowCount(1, "cdo_repository");
    verifyRowCount(2, "cdo_packages");
    verifyRowCount(12, "cdo_classes");
    verifyRowCount(9, "cdo_supertypes");
    verifyRowCount(28, "cdo_features");
  }

  @Override
  protected void verifyCreateModel3(Transaction transaction)
  {
    verifyRowCount(1, "cdo_repository");
    verifyRowCount(1, "cdo_packages");
    verifyRowCount(1, "cdo_classes");
    verifyRowCount(0, "cdo_supertypes");
    verifyRowCount(1, "cdo_features");
  }

  @Override
  protected void verifyCreateMango(Transaction transaction)
  {
    verifyRowCount(1, "cdo_repository");
    verifyRowCount(1, "cdo_packages");
    verifyRowCount(2, "cdo_classes");
    verifyRowCount(0, "cdo_supertypes");
    verifyRowCount(3, "cdo_features");
  }
}

Back to the top