Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 055c3ba2a619c9fee3fe75c9bb5e675d5b4965fd (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
/*
 * Copyright (c) 2010-2012, 2015 Eike Stepper (Berlin, Germany) and others.
 * 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.examples.hibernate.client;

import org.eclipse.emf.cdo.examples.company.CompanyPackage;
import org.eclipse.emf.cdo.net4j.CDONet4jSession;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.net4j.CDONet4jUtil;
import org.eclipse.emf.cdo.session.CDOSession;

import org.eclipse.net4j.Net4jUtil;
import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.tcp.TCPUtil;
import org.eclipse.net4j.util.container.ContainerUtil;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.om.OMPlatform;
import org.eclipse.net4j.util.om.log.PrintLogHandler;
import org.eclipse.net4j.util.om.trace.PrintTraceHandler;

import junit.framework.TestCase;

/**
 * This class contains code to connect to a CDO server in a standalone manner . So the junit test is not running inside
 * an OSGI container.
 *
 * @author Martin Taal
 */
public class BaseTest extends TestCase
{
  protected static final String REPO_NAME = "repo1"; //$NON-NLS-1$

  protected static final String CONNECTION_ADDRESS = "localhost:2036"; //$NON-NLS-1$

  private CDONet4jSessionConfiguration sessionConfiguration = null;

  /**
   * Opens a CDOSession, does not register an EPackage with the session. This should be done by the caller.
   */
  protected CDOSession openSession()
  {
    if (sessionConfiguration == null)
    {
      initialize();
    }

    final CDONet4jSession cdoSession = sessionConfiguration.openNet4jSession();
    cdoSession.getPackageRegistry().putEPackage(CompanyPackage.eINSTANCE);
    return cdoSession;
  }

  /**
   * Initializes the connection and creates a {@link CDONet4jSessionConfiguration} which is stored in a member of this
   * class.
   */
  protected void initialize()
  {
    OMPlatform.INSTANCE.setDebugging(true);
    OMPlatform.INSTANCE.addLogHandler(PrintLogHandler.CONSOLE);
    OMPlatform.INSTANCE.addTraceHandler(PrintTraceHandler.CONSOLE);

    // Prepare container
    final IManagedContainer container = ContainerUtil.createContainer();
    Net4jUtil.prepareContainer(container); // Register Net4j factories
    TCPUtil.prepareContainer(container); // Register TCP factories
    CDONet4jUtil.prepareContainer(container); // Register CDO factories
    // LifecycleUtil.activate(container);
    container.activate();

    // Create connector
    final IConnector connector = TCPUtil.getConnector(container, CONNECTION_ADDRESS);

    // Create configuration
    sessionConfiguration = CDONet4jUtil.createNet4jSessionConfiguration();
    sessionConfiguration.setConnector(connector);
    sessionConfiguration.setRepositoryName(REPO_NAME);
  }

  /**
   * Nullifies the session configuration so that a new test will start with a new one.
   */
  @Override
  protected void tearDown() throws Exception
  {
    sessionConfiguration = null;
  }
}

Back to the top