Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 67bc9b269687925c3372b6112aa44fe1ef06c109 (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
/***************************************************************************
 * Copyright (c) 2004, 2005, 2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, 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.topology;


import org.eclipse.emf.cdo.client.CDOResource;
import org.eclipse.emf.cdo.client.ResourceManager;
import org.eclipse.emf.cdo.client.impl.CDOResourceFactoryImpl;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import org.springframework.jdbc.core.JdbcTemplate;

import java.io.IOException;

import javax.sql.DataSource;

import junit.framework.TestCase;


public abstract class AbstractTopologyTest extends TestCase
{
  public static final String CDO_TEST_MODE_KEY = "cdo.test.mode";

  public static final String CLIENT_SEPARATED_SERVER_MODE = "client-separated-server";

  public static final String CLIENT_SERVER_MODE = "client-server";

  public static final String CLIENT_MODE = "client";

  public static final String EMBEDDED_MODE = "embedded";

  private ITopology topology;

  @Override
  protected void setUp() throws Exception
  {
    System.out.println("=========================================================================");
    System.out.println("TC_START " + getName());
    System.out.println("=========================================================================");

    super.setUp();
    topology = createTopology();
    topology.start();
  }

  @Override
  protected void tearDown() throws Exception
  {
    JdbcTemplate jdbc = getJdbcTemplate();
    wipeDatabase(jdbc);

    topology.stop();
    super.tearDown();

    System.out.println("=========================================================================");
    System.out.println("TC_END " + getName());
    System.out.println("=========================================================================");
    System.out.println();
  }

  protected void wipeDatabase(JdbcTemplate jdbc)
  {
    jdbc.execute("DROP TABLE CDO_ATTRIBUTE");
    jdbc.execute("DROP TABLE CDO_CLASS");
    jdbc.execute("DROP TABLE CDO_CONTENT");
    jdbc.execute("DROP TABLE CDO_OBJECT");
    jdbc.execute("DROP TABLE CDO_PACKAGE");
    jdbc.execute("DROP TABLE CDO_REFERENCE");
    jdbc.execute("DROP TABLE CDO_RESOURCE");
  }

  protected ITopology getTopology()
  {
    return topology;
  }

  protected DataSource getDataSource()
  {
    return topology.getDataSource();
  }

  protected JdbcTemplate getJdbcTemplate()
  {
    return new JdbcTemplate(getDataSource());
  }

  protected ResourceManager createResourceManager(ResourceSet resourceSet)
  {
    return topology.createResourceManager(resourceSet);
  }

  protected ResourceManager createResourceManager()
  {
    ResourceSet resourceSet = new ResourceSetImpl();
    return topology.createResourceManager(resourceSet);
  }

  protected CDOResource createResource(String path)
  {
    ResourceManager resourceManager = createResourceManager();
    URI uri = CDOResourceFactoryImpl.formatURI(path);
    return (CDOResource) resourceManager.createResource(uri);
  }

  protected CDOResource getResource(String path)
  {
    ResourceManager resourceManager = createResourceManager();
    URI uri = CDOResourceFactoryImpl.formatURI(path);
    return (CDOResource) resourceManager.getResource(uri, true);
  }

  protected EObject loadRoot(String path) throws IOException
  {
    CDOResource resource = getResource(path);
    return (EObject) resource.getContents().get(0);
  }

  protected CDOResource saveRoot(EObject root, String path) throws IOException
  {
    CDOResource resource = createResource(path);
    resource.getContents().add(root);
    resource.save(null);
    return resource;
  }

  protected ITopology createTopology()
  {
    String mode = getMode();
    if (EMBEDDED_MODE.equals(mode))
    {
      return new EmbeddedTopology();
    }

    if (CLIENT_MODE.equals(mode))
    {
      return new ClientTopology();
    }

    if (CLIENT_SERVER_MODE.equals(mode))
    {
      return new ClientServerTopology();
    }

    if (CLIENT_SEPARATED_SERVER_MODE.equals(mode))
    {
      return new ClientSeparatedServerTopology();
    }

    return null;
  }

  protected String getMode()
  {
    return System.getProperty(CDO_TEST_MODE_KEY, EMBEDDED_MODE).toLowerCase();
  }
}

Back to the top