Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3bf0a3dab47544b073c1716f7e233e7852f4440c (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
/*
 * Copyright (c) 2013 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:
 *    Christian W. Damus (CEA) - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

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

import java.net.URL;

/**
 * Bug 404318: Tests that we don't get certain NPEs when processing dynamic objects whose
 * EClasses have been unloaded.
 *
 * @author Christian W. Damus (CEA)
 */
public class Bugzilla_404318_Test extends AbstractCDOTest
{
  public void testUnloadedEClass() throws Exception
  {
    CDOSession session = openSession();

    CDOTransaction transaction = session.openTransaction();

    // get our dynamic package
    EPackage epackage = getTestPackage(transaction.getResourceSet());

    // and the instance model
    Resource resource = getTestInstance(transaction.getResourceSet());

    session.getPackageRegistry().putEPackage(epackage);

    EObject anA = resource.getContents().get(0);
    EObject aB = resource.getContents().get(1);

    // sanity check
    assertSame(anA, aB.eGet(aB.eClass().getEStructuralFeature("a")));

    try
    {
      // unload the models. This would NPE without the fix
      epackage.eResource().unload();
      resource.unload();

      // unloaded EClass still supports the unloaded instance
      assertSame(anA, aB.eGet(aB.eClass().getEStructuralFeature("a")));
    }
    catch (Exception e)
    {
      e.printStackTrace();
      fail("Threw exception: " + e.getLocalizedMessage());
    }
  }

  //
  // test framework

  protected EPackage getTestPackage(ResourceSet rset) throws Exception
  {
    rset.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new EcoreResourceFactoryImpl());
    URL location = getClass().getClassLoader().getResource("Bug404318.ecore");
    Resource result = rset.getResource(URI.createURI(location.toExternalForm(), true), true);
    return (EPackage)result.getContents().get(0);
  }

  protected Resource getTestInstance(ResourceSet rset) throws Exception
  {
    rset.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
    URL location = getClass().getClassLoader().getResource("Bug404318.xmi");
    Resource result = rset.getResource(URI.createURI(location.toExternalForm(), true), true);
    return result;
  }
}

Back to the top