Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bdb0bfba18eabe3d3a070f76ae0e1de5b7afe906 (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
/*
 * Copyright (c) 2009-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:
 *    Stefan Winkler - initial API and implementation
 */
package org.eclipse.emf.cdo.tests;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDExternal;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.internal.common.id.CDOIDNullImpl;
import org.eclipse.emf.cdo.internal.common.id.CDOIDObjectLongImpl;
import org.eclipse.emf.cdo.internal.common.id.CDOIDTempObjectExternalImpl;
import org.eclipse.emf.cdo.internal.common.id.CDOIDTempObjectImpl;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.config.IRepositoryConfig;
import org.eclipse.emf.cdo.tests.model1.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.tests.model1.Supplier;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.emf.internal.cdo.transaction.CDOTransactionImpl;

/**
 * @author Stefan Winkler
 */
public class CDOIDTest extends AbstractCDOTest
{
  public void testGetLong_Null()
  {
    assertEquals(0L, CDOIDUtil.getLong(null));
  }

  public void testGetLong_NullId()
  {
    CDOIDNullImpl id = CDOIDNullImpl.INSTANCE;
    assertEquals(0L, CDOIDUtil.getLong(id));
  }

  public void testGetLong_LongId()
  {
    CDOIDObjectLongImpl id = CDOIDObjectLongImpl.create(123L);
    assertEquals(123L, CDOIDUtil.getLong(id));
  }

  public void testGetLong_TempId()
  {
    CDOIDTempObjectImpl id = CDOIDTempObjectImpl.create(456);
    assertIllegalArgument(id);
  }

  @Requires(IRepositoryConfig.CAPABILITY_EXTERNAL_REFS)
  public void testGetLong_ExtTempId()
  {
    CDOIDTempObjectExternalImpl id = CDOIDTempObjectExternalImpl.create("cdo://repo123/resource456");
    assertIllegalArgument(id);
  }

  @Requires(IRepositoryConfig.CAPABILITY_EXTERNAL_REFS)
  public void testGetLong_ExtId()
  {
    CDOIDExternal id = CDOIDUtil.createExternal("cdo://repo123/resource456");
    assertIllegalArgument(id);
  }

  private void assertIllegalArgument(CDOID id)
  {
    boolean thrown = false;

    try
    {
      CDOIDUtil.getLong(id);
    }
    catch (IllegalArgumentException e)
    {
      thrown = true;
    }

    if (!thrown)
    {
      fail("Expected IllegalArgumentException!");
    }
  }

  public void testURIFragment() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));

    Supplier supplier = getModel1Factory().createSupplier();
    supplier.setName("Stepper");

    resource.getContents().add(supplier);
    transaction.commit();

    StringBuilder builder = new StringBuilder();
    CDOIDUtil.write(builder, CDOUtil.getCDOObject(supplier).cdoID());

    String uriFragment = builder.toString();
    System.out.println(uriFragment);

    CDOID id = CDOIDUtil.read(uriFragment);
    System.out.println(id);
  }

  @Requires(IRepositoryConfig.CAPABILITY_BRANCHING)
  public void testSetID() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath("/test1"));

    Company company = getModel1Factory().createCompany();
    resource.getContents().add(company);
    transaction.commit();

    CDOID idToForce = CDOUtil.getCDOObject(company).cdoID();
    CDOBranch newBranch = transaction.getBranch().createBranch("new");
    transaction.setBranch(newBranch);

    resource.getContents().clear();
    transaction.commit();

    Company company2 = getModel1Factory().createCompany();
    resource.getContents().add(company2);

    CDOTransactionImpl.resurrectObject(CDOUtil.getCDOObject(company2), idToForce);
    transaction.commit();

    CDOSession sessionB = openSession();
    CDOTransaction transactionB = sessionB.openTransaction(newBranch);
    CDOResource resourceB = transactionB.getResource(getResourcePath("/test1"));
    Company companyB = (Company)resourceB.getContents().get(0);
    assertEquals(idToForce, CDOUtil.getCDOObject(companyB).cdoID());
  }

  @CleanRepositoriesBefore(reason = "New transaction seems to need a clean repo be restarted")
  @Requires({ IRepositoryConfig.CAPABILITY_BRANCHING, IRepositoryConfig.CAPABILITY_RESTARTABLE })
  public void testSetIDWithReferences() throws Exception
  {
    CDOID idToForce1;
    CDOID idToForce2;

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(getResourcePath("/test1"));

      Company company = getModel1Factory().createCompany();
      Category category = getModel1Factory().createCategory();
      company.getCategories().add(category);
      resource.getContents().add(company);
      transaction.commit();

      idToForce1 = CDOUtil.getCDOObject(company).cdoID();
      idToForce2 = CDOUtil.getCDOObject(category).cdoID();
      CDOBranch newBranch = transaction.getBranch().createBranch("new");
      transaction.setBranch(newBranch);

      resource.getContents().clear();
      transaction.commit();

      Company company2 = getModel1Factory().createCompany();
      Category category2 = getModel1Factory().createCategory();
      company2.getCategories().add(category2);
      resource.getContents().add(company2);

      CDOTransactionImpl.resurrectObject(CDOUtil.getCDOObject(company2), idToForce1);
      CDOTransactionImpl.resurrectObject(CDOUtil.getCDOObject(category2), idToForce2);
      transaction.commit();
      session.close();
    }

    restartRepository();

    CDOSession sessionB = openSession();
    CDOBranch newBranch = sessionB.getBranchManager().getBranch("MAIN/new");

    CDOTransaction transactionB = sessionB.openTransaction(newBranch);
    CDOResource resourceB = transactionB.getResource(getResourcePath("/test1"));
    Company companyB = (Company)resourceB.getContents().get(0);
    Category categoryB = companyB.getCategories().get(0);

    CDOID companyID = CDOUtil.getCDOObject(companyB).cdoID();
    assertEquals(idToForce1, companyID);

    CDOID categoryID = CDOUtil.getCDOObject(categoryB).cdoID();
    assertEquals(idToForce2, categoryID);

    Object containerID = CDOUtil.getCDOObject(categoryB).cdoRevision().data().getContainerID();
    assertEquals(companyID, containerID);
  }
}

Back to the top