Skip to main content
summaryrefslogtreecommitdiffstats
blob: ee04d77f10686513c99991e209bce26044fda42e (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
/*
 * Copyright (c) 2008-2013 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:
 *    Simon McDuff - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.tests;

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.config.impl.ConfigTest.Requires;
import org.eclipse.emf.cdo.tests.model1.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOQuery;

import org.eclipse.emf.internal.cdo.query.CDOQueryResultIteratorImpl;

import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.collection.CloseableIterator;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Simon McDuff
 */
@Requires("MEM")
public class MEMStoreQueryTest extends AbstractCDOTest
{
  public void testBasicQuery() throws Exception
  {
    Set<Object> objects = new HashSet<Object>();
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();

    CDOResource resource1 = transaction.createResource(getResourcePath("/test1"));
    objects.add(resource1);
    Company company1 = getModel1Factory().createCompany();
    Category category1 = getModel1Factory().createCategory();

    resource1.getContents().add(company1);
    company1.getCategories().add(category1);

    objects.add(company1);
    objects.add(category1);
    objects.add(transaction.getRootResource());
    objects.add(transaction.getResourceNode(getResourcePath(null)));
    company1.setName("TEST");

    transaction.commit();

    CDOQuery query = transaction.createQuery("TEST", "QUERYSTRING");
    List<Object> result = query.getResult(Object.class);
    assertEquals(5, result.size());
    for (Object object : result)
    {
      assertEquals(true, objects.contains(object));
    }

    transaction.close();
    session.close();
  }

  public void testQueryException() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOQuery query = transaction.createQuery("TEST", "QUERYSTRING");
    query.setParameter("error", 5000);
    query.setParameter("integers", 10000);

    CloseableIterator<Object> it = query.getResultAsync(Object.class);
    int i = 0;

    for (;;)
    {
      ++i;

      try
      {
        if (!it.hasNext())
        {
          break;
        }
      }
      catch (Exception expected)
      {
        expected.printStackTrace();
        break;
      }

      it.next();
    }

    assertEquals("Query should have stopped at the 5000th result", 5000, i);
  }

  public void testBasicQuery_EClassParameter() throws Exception
  {
    Set<Object> objects = new HashSet<Object>();
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();

    CDOResource resource1 = transaction.createResource(getResourcePath("/test1"));
    objects.add(resource1);
    Company company1 = getModel1Factory().createCompany();
    Category category1 = getModel1Factory().createCategory();

    resource1.getContents().add(company1);
    company1.getCategories().add(category1);

    objects.add(company1);
    objects.add(category1);

    company1.setName("TEST");

    transaction.commit();
    System.out.println(category1.eClass().getEPackage().getNsURI());

    CDOQuery query = transaction.createQuery("TEST", "QUERYSTRING");
    query.setParameter("context", getModel1Package().getCategory());

    List<Category> result = query.getResult(Category.class);
    assertEquals(1, result.size());
    assertEquals(category1, result.get(0));

    transaction.close();
    session.close();
  }

  public void testQueryCancel_successful() throws Exception
  {
    CDOTransaction transaction = initialize(500);
    CDOQuery query = transaction.createQuery("TEST", "QUERYSTRING");
    query.setParameter("sleep", 1000L);
    final CloseableIterator<Object> result = query.getResultAsync(Object.class);
    result.close();

    new PollingTimeOuter()
    {
      @Override
      protected boolean successful()
      {
        return !getRepository().getQueryManager().isRunning(((CDOQueryResultIteratorImpl<?>)result).getQueryID());
      }
    }.assertNoTimeOut();

    CDOSession session = transaction.getSession();
    transaction.close();
    session.close();
  }

  public void testQueryCancel_ViewClose() throws Exception
  {
    CDOTransaction transaction = initialize(500);
    CDOQuery query = transaction.createQuery("TEST", "QUERYSTRING");
    query.setParameter("sleep", 1000L);
    final CloseableIterator<Object> result = query.getResultAsync(Object.class);
    CDOSession session = transaction.getSession();
    transaction.close();
    new PollingTimeOuter()
    {
      @Override
      protected boolean successful()
      {
        return !getRepository().getQueryManager().isRunning(((CDOQueryResultIteratorImpl<?>)result).getQueryID());
      }
    }.assertNoTimeOut();

    session.close();
  }

  public void testQueryCancel_SessionClose() throws Exception
  {
    CDOTransaction transaction = initialize(500);
    CDOQuery query = transaction.createQuery("TEST", "QUERYSTRING");
    query.setParameter("sleep", 1000L);
    final CloseableIterator<Object> result = query.getResultAsync(Object.class);
    transaction.getSession().close();

    new PollingTimeOuter()
    {
      @Override
      protected boolean successful()
      {
        return !getRepository().getQueryManager().isRunning(((CDOQueryResultIteratorImpl<?>)result).getQueryID());
      }
    }.assertNoTimeOut();
  }

  public void testQueryAsync_UnsupportedLanguage() throws Exception
  {
    CDOTransaction transaction = initialize(100);
    CDOQuery query = transaction.createQuery("TESTss", "QUERYSTRING");

    try
    {
      CloseableIterator<Object> result = query.getResultAsync(Object.class);
      result.hasNext();
      fail("Should throw an exception");
    }
    catch (Exception expected)
    {
    }
  }

  public void testQuerySync_UnsupportedLanguage() throws Exception
  {
    CDOTransaction transaction = initialize(100);
    CDOQuery query = transaction.createQuery("TESTss", "QUERYSTRING");

    try
    {
      query.getResult(Object.class);
      fail("Should throw an exception");
    }
    catch (Exception expected)
    {
    }
  }

  private CDOTransaction initialize(int number)
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource1 = transaction.createResource(getResourcePath("/test1"));

    for (int i = 0; i < number; i++)
    {
      Category category1 = getModel1Factory().createCategory();
      resource1.getContents().add(category1);
    }

    try
    {
      transaction.commit();
    }
    catch (CommitException ex)
    {
      throw WrappedException.wrap(ex);
    }

    return transaction;
  }
}

Back to the top