Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3cebe5ad86ed0ad836cb3418ef2bd725fb06e39d (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
/***************************************************************************
 * 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:
 *    Simon McDuff - initial API and implementation
 **************************************************************************/
package org.eclipse.emf.cdo.tests;

import org.eclipse.emf.cdo.CDOSession;
import org.eclipse.emf.cdo.CDOTransaction;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.internal.server.Repository;
import org.eclipse.emf.cdo.query.CDOQuery;
import org.eclipse.emf.cdo.tests.model1.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.tests.model1.Model1Factory;
import org.eclipse.emf.cdo.tests.model1.Model1Package;

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

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

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

/**
 * @author Simon McDuff
 */
public class QueryTest extends AbstractCDOTest
{
  static String LANGUAGE_TEST = "TEST";

  public void testBasicQuery() throws Exception
  {

    Set<Object> objects = new HashSet<Object>();
    CDOSession session = openModel1Session();
    CDOTransaction transaction = session.openTransaction();

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

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

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

    company1.setName("TEST");

    transaction.commit();

    CDOQuery cdoQuery = transaction.createQuery(LANGUAGE_TEST, "QUERYSTRING");
    List<Object> queryResult = cdoQuery.getResult(Object.class);
    assertEquals(3, queryResult.size());
    for (Object object : queryResult)
    {
      assertEquals(true, objects.contains(object));
    }

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

  public void testBasicQuery_EClassParameter() throws Exception
  {

    Set<Object> objects = new HashSet<Object>();
    CDOSession session = openModel1Session();
    CDOTransaction transaction = session.openTransaction();

    CDOResource resource1 = transaction.createResource("/test1");
    objects.add(resource1);
    Company company1 = Model1Factory.eINSTANCE.createCompany();
    Category category1 = Model1Factory.eINSTANCE.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 cdoQuery = transaction.createQuery(LANGUAGE_TEST, "QUERYSTRING");

    cdoQuery.setParameter("context", Model1Package.eINSTANCE.getCategory());

    List<Category> queryResult = cdoQuery.getResult(Category.class);

    assertEquals(1, queryResult.size());
    assertEquals(category1, queryResult.get(0));

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

  public void testQueryCancel_successful() throws Exception
  {
    CDOTransaction transaction = initialize(1000);

    CDOQuery cdoQuery = transaction.createQuery(LANGUAGE_TEST, "QUERYSTRING");
    CloseableIterator<Object> queryResult = cdoQuery.getResultAsync(Object.class);

    queryResult.close();

    assertEquals(false, ((Repository)getRepository()).getQueryManager().isRunning(
        ((CDOQueryResultIteratorImpl<?>)queryResult).getQueryID()));

    transaction.close();
    transaction.getSession().close();
  }

  public void testQueryCancel_ViewClose() throws Exception
  {
    CDOTransaction transaction = initialize(100);

    CDOQuery cdoQuery = transaction.createQuery(LANGUAGE_TEST, "QUERYSTRING");
    CloseableIterator<Object> queryResult = cdoQuery.getResultAsync(Object.class);
    transaction.close();
    assertEquals(false, ((Repository)getRepository()).getQueryManager().isRunning(
        ((CDOQueryResultIteratorImpl<?>)queryResult).getQueryID()));
    transaction.getSession().close();
  }

  public void testQueryCancel_SessionClose() throws Exception
  {
    CDOTransaction transaction = initialize(100);

    CDOQuery cdoQuery = transaction.createQuery(LANGUAGE_TEST, "QUERYSTRING");
    CloseableIterator<Object> queryResult = cdoQuery.getResultAsync(Object.class);
    transaction.getSession().close();

    assertEquals(false, ((Repository)getRepository()).getQueryManager().isRunning(
        ((CDOQueryResultIteratorImpl<?>)queryResult).getQueryID()));

  }

  CDOTransaction initialize(int number)
  {
    CDOSession session = openModel1Session();
    CDOTransaction transaction = session.openTransaction();
    transaction.setUniqueResourceContents(false);
    CDOResource resource1 = transaction.createResource("/test1");

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

    transaction.commit();
    return transaction;
  }
}

Back to the top