Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2be708fd446d46304be3634e1d1394140e2ac468 (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
/*
 * Copyright (c) 2004 - 2011 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.eresource.CDOResource;
import org.eclipse.emf.cdo.examples.company.Category;
import org.eclipse.emf.cdo.examples.company.CompanyFactory;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

/**
 * This test shows that a custom ID Generator is used. You can find the annotation for the id generator in the ecore
 * model, at EPackage level and in the Category.name EAttribute.
 * <p/>
 * The ID generator is implemented in the org.eclipse.emf.cdo.examples.hibernate.server (see the
 * CDOExampleUUIDHexGenerator class).
 * <p/>
 * The custom id generator ensures that if an object has already an id that it is being re-used.
 * <p/>
 * Note this testcase will work with the hibernate mapping created using the latest version of the ecore file. When
 * using the generated hbm file then it does not work (as the generated hbm file does not necessarily contain the
 * required generator definitions).
 *
 * @author Martin Taal
 */
public class CustomIDGeneratorTest extends BaseTest
{

  /**
   * Create 2 categories, after persisting they should both have an id. One has a pre-set id, the other one a generated
   * id (as it was not pre-set).
   */
  public void testSetId() throws Exception
  {

    Category category1;
    Category category2;
    final String uuid = "MyUUID"; //$NON-NLS-1$
    {
      final CDOSession session = openSession();
      final CDOTransaction transaction = session.openTransaction();
      // get/create a resource
      CDOResource resource = transaction.getOrCreateResource("/res1"); //$NON-NLS-1$

      // clear any previous data
      resource.getContents().clear();

      category1 = CompanyFactory.eINSTANCE.createCategory();
      category1.setName(uuid);
      category2 = CompanyFactory.eINSTANCE.createCategory();

      resource.getContents().add(category1);
      resource.getContents().add(category2);

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

    // read back and test that the uuid is the same for one
    // and generated for the other
    {
      final CDOSession session = openSession();
      final CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource("/res1"); //$NON-NLS-1$
      final Category category1_read = (Category)resource.getContents().get(0);
      final Category category2_read = (Category)resource.getContents().get(1);

      // test that we really have different objects
      assertNotSame(category1, category1_read);
      assertNotSame(category2, category2_read);

      // but that the uuid for one is equal but not the same object
      assertEquals(uuid, category1_read.getName());
      assertNotSame(uuid, category1_read.getName());

      // and the other uuid has been set
      assertNotNull(category2_read.getName());
      assertEquals(false, uuid.equals(category2_read.getName()));
      transaction.commit();
      session.close();
    }
  }
}

Back to the top