Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2f6f46716ba725b1b4a536bb2804c938e1849a1a (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
/*
 * Copyright (c) 2015, 2019 Eike Stepper (Loehne, 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:
 *    Esteban Dugueperoux - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.model6.ContainmentObject;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Bug 468268 about NPE on RevisionWithoutID.hashCode() call with TRANSIENT CDOObject.
 *
 * @author Esteban Dugueperoux
 */
public class Bugzilla_468268_Test extends AbstractCDOTest
{
  public void testHashCodeCallOnAllFields() throws Exception
  {
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("model6", new XMIResourceFactoryImpl());

    URI localResourceURI = URI.createFileURI(createTempFile("main", ".model6").getCanonicalPath());
    Resource localResource = resourceSet.createResource(localResourceURI);

    ContainmentObject mainContainmentObject = getModel6Factory().createContainmentObject();
    testHashCodeOnAllFieds(mainContainmentObject);
    localResource.getContents().add(mainContainmentObject);
    testHashCodeOnAllFieds(mainContainmentObject);
    localResource.save(Collections.emptyMap());
    testHashCodeOnAllFieds(mainContainmentObject);
  }

  private void testHashCodeOnAllFieds(EObject eObject) throws Exception
  {
    Set<Field> fields = getFields(eObject);

    for (Field field : fields)
    {
      Object value = getValue(field, eObject);
      if (value != null)
      {
        value.hashCode();
      }
    }
  }

  private Object getValue(Field field, EObject eObject) throws Exception
  {
    Object value;
    final boolean oldAccessible = field.isAccessible();

    try
    {
      field.setAccessible(true);
      value = field.get(eObject);
    }
    finally
    {
      field.setAccessible(oldAccessible);
    }

    return value;
  }

  private Set<Field> getFields(EObject eObject)
  {
    Set<Field> allFields = new HashSet<>();
    Field[] fields = eObject.getClass().getDeclaredFields();
    allFields.addAll(Arrays.asList(fields));
    Class<?> currentClass = eObject.getClass().getSuperclass();

    while (currentClass != Object.class)
    {
      fields = currentClass.getDeclaredFields();
      allFields.addAll(Arrays.asList(fields));
      currentClass = currentClass.getSuperclass();
    }

    return allFields;
  }
}

Back to the top