Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 592522487164fc4e4de2c82c9b8741b3129df6ab (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
/*
 * Copyright (c) 2011, 2012, 2015, 2016 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.tests.mongodb;

import org.eclipse.emf.cdo.CDOState;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.server.internal.mongodb.MongoDBStore;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.model1.Supplier;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

import org.eclipse.net4j.util.concurrent.ConcurrencyUtil;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;

import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import java.io.PrintStream;

/**
 * @author Eike Stepper
 */
public class MongoDBInitialTest extends AbstractCDOTest
{
  public void testGetContentsClearedCache() throws Exception
  {
    {
      msg("Opening session");
      CDOSession session = openSession();

      msg("Opening transaction");
      CDOTransaction transaction = session.openTransaction();

      msg("Creating resource");
      CDOResource resource = transaction.createResource(getResourcePath("/test1"));

      msg("Creating supplier");
      Supplier supplier = getModel1Factory().createSupplier();

      msg("Setting name");
      supplier.setName("Stepper");

      msg("Adding supplier");
      resource.getContents().add(supplier);

      msg("Committing");
      transaction.commit();
      session.close();
    }

    clearCache(getRepository().getRevisionManager());

    msg("Opening session");
    CDOSession session = openSession();

    msg("Opening transaction");
    CDOTransaction transaction = session.openTransaction();

    msg("Getting resource");
    CDOResource resource = transaction.getResource(getResourcePath("/test1"));

    msg("Getting contents");
    EList<EObject> contents = resource.getContents();
    assertNotNull(contents);

    Supplier supplier = (Supplier)contents.get(0);
    String name = supplier.getName();
    assertEquals("Stepper", name);
  }

  public void testGetResourceClearedCache() throws Exception
  {
    Supplier supplier = getModel1Factory().createSupplier();
    supplier.setName("Stepper");

    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));
    resource.getContents().add(supplier);
    transaction.commit();

    clearCache(getRepository().getRevisionManager());

    // query(new BasicDBObject(Commits.REVISIONS, new BasicDBObject("$elemMatch", new BasicDBObject(
    // Commits.REVISIONS_RESOURCE, 1))));
    transaction = session.openTransaction();

    msg("Getting resource");
    resource = transaction.getResource(getResourcePath("/test1"), true);
    assertNotNull(resource);
    assertEquals(URI.createURI("cdo://" + session.getRepositoryInfo().getUUID() + getResourcePath("/test1")), resource.getURI());
    assertEquals(transaction.getResourceSet(), resource.getResourceSet());
    assertEquals(1, transaction.getResourceSet().getResources().size());
    assertEquals(CDOState.CLEAN, resource.cdoState());
    assertEquals(transaction, resource.cdoView());
    assertNotNull(resource.cdoRevision());
  }

  protected void query(DBObject query)
  {
    System.err.println();
    System.err.println("Query:");
    showDocument(query, "");
    System.err.println();
    System.err.println("Results:");

    MongoDBStore store = (MongoDBStore)getRepository().getStore();
    DBCollection collection = store.getCommits().getCollection();
    DBCursor cursor = collection.find(query);

    int i = 0;
    while (cursor.hasNext())
    {
      ++i;
      System.out.println("" + i + " = ");
      showDocument(cursor.next(), "    ");
    }

    ConcurrencyUtil.sleep(1000000L);
  }

  protected void showDocument(DBObject doc, String level)
  {
    PrintStream pout = System.out;
    for (String key : doc.keySet())
    {
      pout.print(level);
      pout.print(key);
      pout.print(" = ");

      Object value = doc.get(key);
      if (value instanceof DBObject)
      {
        DBObject child = (DBObject)value;
        pout.println();
        showDocument(child, level + "    ");
      }
      else
      {
        if (value instanceof String)
        {
          pout.print("\"");
        }

        pout.print(value);
        if (value instanceof String)
        {
          pout.print("\"");
        }

        pout.println();
      }
    }
  }
  //
  // @Override
  // protected IScenario getDefaultScenario()
  // {
  // Scenario scenario = new Scenario();
  // scenario.setContainerConfig(ContainerConfig.Combined.INSTANCE);
  // scenario.setRepositoryConfig(MongoDBStoreRepositoryConfig.INSTANCE);
  // scenario.setSessionConfig(Net4j.JVM.INSTANCE);
  // scenario.setModelConfig(ModelConfig.Native.INSTANCE);
  // return scenario;
  // }
}

Back to the top