Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 64a67373f6cb44374a50d0e0388ef3bdc0696179 (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
/***************************************************************************
 * 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.logic;

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.MappingStrategy;

import org.eclipse.net4j.db.DBUtil;
import org.eclipse.net4j.db.IDBAdapter;
import org.eclipse.net4j.db.IDBConnectionProvider;
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;

/**
 * @author Eike Stepper
 */
public abstract class DBStoreTestLogic extends TestLogic
{
  public static final String DEFINITION_MODE = "";

  protected MappingStrategy mappingStrategy;

  protected IDBAdapter dbAdapter;

  protected IDBConnectionProvider dbConnectionProvider;

  protected DBStore store;

  public DBStoreTestLogic()
  {
  }

  @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 abstract IDBConnectionProvider createDBConnectionProvider();

  protected abstract IDBAdapter createDBAdapter();

  protected abstract MappingStrategy createMappingStrategy();

  protected 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);
  }

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

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

  protected 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);
    }
  }
}

Back to the top