Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: df3be6f7ab5005903bbc6baabc7a7ddbb48f1b69 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/***************************************************************************
 * 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.ObjectUtil;
import org.eclipse.net4j.util.WrappedException;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
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 static final String DEFINITION_MODE = "";

  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()
    {
      @Override
      protected long getStartupTime()
      {
        return 1;
      }
    };

    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 defineOrCompare(String fileName) throws IOException
  {
    File file = new File(fileName + ".txt");
    if (fileName.equals(DEFINITION_MODE) || "*".equals(DEFINITION_MODE))
    {
      file.getParentFile().mkdirs();
      PrintStream out = new PrintStream(file);
      exportDatabase(out);
      out.close();
    }
    else
    {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      exportDatabase(new PrintStream(baos));

      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
      FileInputStream fis = new FileInputStream(file);
      compareLines(fileName, fis, bais);
    }
  }

  private void compareLines(String fileName, InputStream expectedStream, InputStream actualStream) throws IOException
  {
    BufferedReader expectedReader = new BufferedReader(new InputStreamReader(expectedStream));
    BufferedReader actualReader = new BufferedReader(new InputStreamReader(actualStream));

    int line = 1;
    while (true)
    {
      String expectedLine = expectedReader.readLine();
      String actualLine = actualReader.readLine();
      if (!ObjectUtil.equals(expectedLine, actualLine))
      {
        throw new IllegalStateException("Mismatch at (" + fileName + ":" + line + ")\n" + expectedLine + "\n"
            + actualLine);
      }

      if (expectedLine == null)
      {
        break;
      }

      ++line;
    }
  }

  private void exportDatabase(PrintStream out)
  {
    store.getDBSchema().export(dbConnectionProvider, out);
    CDODBSchema.INSTANCE.export(dbConnectionProvider, out);
  }

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

  private void assertFieldValue(Object expectedValue, String sql)
  {
    Object actualValue = query(sql);
    assertEquals("Field in " + sql, expectedValue, actualValue);
  }

  private Object 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())
      {
        throw new IllegalStateException("No row: " + sql.toUpperCase());
      }

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

  @Override
  protected void verifyCreateModel1(Transaction transaction) throws Exception
  {
    defineOrCompare("defs/horizontal/verifyCreateModel1");
    // assertRowCount(1, "cdo_repository");
    // assertRowCount(1, "cdo_packages");
    // assertRowCount(11, "cdo_classes");
    // assertRowCount(8, "cdo_supertypes");
    // assertRowCount(26, "cdo_features");
  }

  @Override
  protected void verifyCreateModel2(Transaction transaction) throws Exception
  {
    defineOrCompare("defs/horizontal/verifyCreateModel2");
    // assertRowCount(1, "cdo_repository");
    // assertRowCount(2, "cdo_packages");
    // assertRowCount(12, "cdo_classes");
    // assertRowCount(9, "cdo_supertypes");
    // assertRowCount(28, "cdo_features");
  }

  @Override
  protected void verifyCreateModel3(Transaction transaction) throws Exception
  {
    defineOrCompare("defs/horizontal/verifyCreateModel3");
    // assertRowCount(1, "cdo_repository");
    // assertRowCount(1, "cdo_packages");
    // assertRowCount(1, "cdo_classes");
    // assertRowCount(0, "cdo_supertypes");
    // assertRowCount(1, "cdo_features");
  }

  @Override
  protected void verifyCreateMango(Transaction transaction) throws Exception
  {
    defineOrCompare("defs/horizontal/verifyCreateMango");
    // assertRowCount(1, "cdo_repository");
    // assertRowCount(1, "cdo_packages");
    // assertRowCount(2, "cdo_classes");
    // assertRowCount(0, "cdo_supertypes");
    // assertRowCount(3, "cdo_features");
  }

  @Override
  protected void verifyCommitCompany(Transaction transaction) throws Exception
  {
    defineOrCompare("defs/horizontal/verifyCommitCompany");
    // assertRowCount(1, "CDOResource");
    // assertFieldValue("/res1", "select path_0 from CDOResource where cdo_id=1 and cdo_version=1");
    //
    // assertRowCount(1, "Company");
    // assertFieldValue("Sympedia", "select name from Company where cdo_id=1 and cdo_version=1");
    // assertFieldValue("Homestr. 17", "select street from Company where cdo_id=1 and cdo_version=1");
    // assertFieldValue("Berlin", "select city from Company where cdo_id=1 and cdo_version=1");
  }
}

Back to the top