Java Code Examples for org.eclipse.emf.ecore.EPackage#setName()

The following examples show how to use org.eclipse.emf.ecore.EPackage#setName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EcoreResourceDescriptionManagerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testNestedPackage() throws Exception {
	Resource resource = new XMIResourceImpl();
	EPackage parent = EcoreFactory.eINSTANCE.createEPackage();
	parent.setName("parent");
	parent.setNsURI("http://parent");
	EPackage child = EcoreFactory.eINSTANCE.createEPackage();
	child.setName("child");
	child.setNsURI("http://child");
	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	child.getEClassifiers().add(eClass);
	parent.getESubpackages().add(child);
	resource.getContents().add(parent);
	Map<QualifiedName, IEObjectDescription> index = createIndex(resource);
	checkEntry(index, parent, false, "parent");
	checkEntry(index, child, false, "parent", "child");
	checkEntry(index, eClass, false, "parent", "child", "Test");
	checkEntry(index, parent, true, "http://parent");
	checkEntry(index, child, true, "http://child");
	checkEntry(index, eClass, true, "http://child", "Test");
	assertEquals(6,index.size());
}
 
Example 2
Source File: EcoreUtil2Test.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testGetAllReferencedObjects() {
	EClass a = createEClass("a");
	EClass b = createEClass("b");
	
	EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
	pack.setName("empty");
	pack.setNsPrefix("empty");
	pack.setNsURI("empty");
	pack.getEClassifiers().add(a);
	pack.getEClassifiers().add(b);
	
	EReference ref = EcoreFactory.eINSTANCE.createEReference();
	a.getEStructuralFeatures().add(ref);
	ref.setUpperBound(1);
	ref.setEType(b);
	EObject objA = pack.getEFactoryInstance().create(a);
	EObject objB = pack.getEFactoryInstance().create(b);
	List<EObject> res = EcoreUtil2.getAllReferencedObjects(objA, ref);
	assertNotNull(res);
	assertTrue(res.isEmpty());
	res = EcoreUtil2.getAllReferencedObjects(objA, ref);
	assertNotNull(res);
	objA.eSet(ref, objB);
}
 
Example 3
Source File: XtextLinkingService.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private List<EObject> createPackage(GeneratedMetamodel generatedMetamodel, ILeafNode text) {
	final String nsURI = getMetamodelNsURI(text);
	final URI uri = URI.createURI(nsURI);
	if (uri == null || isReferencedByUsedGrammar(generatedMetamodel, nsURI))
		return Collections.emptyList();
	EPackage pack = ((GeneratedMetamodelImpl)generatedMetamodel).basicGetEPackage();
	if (pack != null && !pack.eIsProxy())
		return singletonList((EObject)pack);
	final EPackage generatedEPackage = EcoreFactory.eINSTANCE.createEPackage();
	generatedEPackage.setName(generatedMetamodel.getName());
	generatedEPackage.setNsPrefix(generatedMetamodel.getName());
	generatedEPackage.setNsURI(nsURI);
	final Resource generatedPackageResource = new EcoreResourceFactoryImpl().createResource(uri);
	XtextResourceSet resourceSet = (XtextResourceSet) generatedMetamodel.eResource().getResourceSet();
	if (!resourceSet.getURIResourceMap().containsKey(generatedPackageResource.getURI())) {
		generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource);
	} else {
		generatedPackageResource.setURI(URI.createURI(nsURI+"_"+generatedMetamodel.hashCode()));
		generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource);
	}
	generatedPackageResource.getContents().add(generatedEPackage);
	return Collections.<EObject>singletonList(generatedEPackage);
}
 
Example 4
Source File: GenericResourceDescriptionManagerTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testCreateDeltaAndIsAffected() {
	GenericResourceDescriptionManager manager = getEmfResourceDescriptionsManager();
	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	eClass.getESuperTypes().add(EcorePackage.Literals.EPACKAGE);
	Resource resource = new XMIResourceImpl(URI.createFileURI("test.ecore"));
	resource.getContents().add(eClass);

	EPackage copyOfEPackage = EcoreUtil.copy(EcorePackage.eINSTANCE);
	Resource ecoreResource = new XMIResourceImpl(URI.createURI(copyOfEPackage.getNsURI()));
	ecoreResource.getContents().add(copyOfEPackage);
	
	IResourceDescription oldDescription = new CopiedResourceDescription(manager.getResourceDescription(ecoreResource));
	oldDescription.getExportedObjects();
	copyOfEPackage.setName("ecore_new");
	IResourceDescription newDescription = manager.getResourceDescription(ecoreResource);
	
	Delta delta = manager.createDelta(oldDescription, newDescription);
	assertTrue(delta.haveEObjectDescriptionsChanged());
	
	IResourceDescription referrerDescription = manager.getResourceDescription(resource);
	assertTrue(manager.isAffected(delta, referrerDescription));
}
 
Example 5
Source File: EcoreResourceDescriptionManagerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testMissingMiddleName() {
	Resource resource = new XMIResourceImpl();
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsURI("http://test");
	EClass unnamedEClass = EcoreFactory.eINSTANCE.createEClass();
	EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
	eAttribute.setName("test");
	unnamedEClass.getEStructuralFeatures().add(eAttribute);
	ePackage.getEClassifiers().add(unnamedEClass);
	resource.getContents().add(ePackage);
	Map<QualifiedName, IEObjectDescription> index = createIndex(resource);
	checkEntry(index, ePackage, false, "test");
	checkEntry(index, ePackage, true, "http://test");
	assertEquals(2,index.size());
	
	unnamedEClass.setName("Test");
	index = createIndex(resource);
	checkEntry(index, ePackage, false, "test");
	checkEntry(index, ePackage, true, "http://test");
	checkEntry(index, unnamedEClass, false, "test", "Test");
	checkEntry(index, unnamedEClass, true, "http://test", "Test");
	checkEntry(index, eAttribute, false, "test", "Test", "test");
	checkEntry(index, eAttribute, true, "http://test", "Test", "test");
	assertEquals(6,index.size());
}
 
Example 6
Source File: LinkedEditingRefactoringIntegrationTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRefactorEcoreCrossLanguage() throws Exception {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsPrefix("test");
	ePackage.setNsURI("http://test");
	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName(TEST_CLASS);
	ePackage.getEClassifiers().add(eClass);
	Resource ecoreResource = new ResourceSetImpl().createResource(URI.createPlatformResourceURI(TEST_PROJECT + "/Test.ecore", true));
	ecoreResource.getContents().add(ePackage);
	ecoreResource.save(null);
	ecoreResource.unload();
	project.refreshLocal(IResource.DEPTH_INFINITE, null);
	
	String model = "ref test." + TEST_CLASS;
	IFile file = createFile(TEST_PROJECT + "/ref.referringtestlanguage", model);
	waitForBuild();
	final XtextEditor editor = openEditor(file);
	final TextSelection selection = new TextSelection(model.indexOf(TEST_CLASS), TEST_CLASS.length());
	editor.getSelectionProvider().setSelection(selection);
	waitForDisplay();
	IRenameElementContext context = editor.getDocument().readOnly(new IUnitOfWork<IRenameElementContext, XtextResource>() {
		@Override
		public IRenameElementContext exec(XtextResource state) throws Exception {
			Reference ref = (Reference) state.getContents().get(0).eContents().get(0);
			EObject referenced = ref.getReferenced();
			assertNotNull(referenced);
			return new IRenameElementContext.Impl(EcoreUtil.getURI(referenced), referenced.eClass(), editor, selection, state.getURI());
		}
	});
	renameRefactoringController.startRefactoring(context);
	waitForDisplay();
	pressKeys(editor, "NewTestClass\n");
	waitForReconciler(editor);
	waitForDisplay();
	waitForBuild();
	ecoreResource.load(null);
	assertEquals("NewTestClass", ((EPackage)ecoreResource.getContents().get(0)).getEClassifiers().get(0).getName());
}
 
Example 7
Source File: EcoreResourceDescriptionManagerTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testMissingNsURI() {
	Resource resource = new XMIResourceImpl();
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	ePackage.getEClassifiers().add(eClass);
	resource.getContents().add(ePackage);
	Map<QualifiedName, IEObjectDescription> index = createIndex(resource);
	checkEntry(index, ePackage, false, "test");
	checkEntry(index, eClass, false, "test", "Test");
	assertEquals(2,index.size());
}
 
Example 8
Source File: TypeHierarchyHelperTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	errorAcceptorMock = new FailOnErrorAcceptor();
	metamodel = XtextFactory.eINSTANCE.createGeneratedMetamodel();
	Grammar grammar = XtextFactory.eINSTANCE.createGrammar();
	grammar.getMetamodelDeclarations().add(metamodel);
	infos = new EClassifierInfos(grammar);
	INT = EcoreFactory.eINSTANCE.createEDataType();
	STRING = EcoreFactory.eINSTANCE.createEDataType();
	EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
	pack.setNsURI("myURI");
	pack.setName("myName");
	pack.setNsPrefix("myPrefix");
	metamodel.setEPackage(pack);
}
 
Example 9
Source File: EcoreUtilN4Test.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private EPackage epack(String n, EObject... contents) {
	EPackage p = EcoreFactory.eINSTANCE.createEPackage();
	p.setName(n);

	for (EObject eobj : contents) {
		if (eobj instanceof EPackage) {
			p.getESubpackages().add((EPackage) eobj);
		} else {
			p.getEClassifiers().add((EClassifier) eobj);
		}
	}

	return p;
}
 
Example 10
Source File: Schema.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public EPackage createEPackage(String name) {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName(name);
	ePackage.setNsPrefix(name);
	ePackage.setNsURI(name);
	packages.put(ePackage.getName(), ePackage);
	return ePackage;
}
 
Example 11
Source File: UimaTypeSystem2Ecore.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Gets or creates an EPackage for a UIMA namespace. Actually will create a whole chain of nested
 * EPackages, one for each component of the UIMA namespace, but only the leaf node of the chain
 * will be returned.
 * 
 * @param uimaNamespace
 *          UIMA namespace
 * @return EPackage corresponding to this namespace.
 */
private static EPackage uimaNamespace2EPackage(String uimaNamespace) {
  // convert UIMA namespace (dotted string) to namespace URI
  String nsUri = uimaNamespace2NamespaceUri(uimaNamespace);
  // see if package already exists for this URI
  EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(nsUri);
  if (ePackage == null) {
    // package name is last component of namespace.
    // all other components form the parent namespace
    String parentNamespace = null;
    String packageName;
    if (uimaNamespace != null) {
      int lastDot = uimaNamespace.lastIndexOf('.');
      packageName = uimaNamespace.substring(lastDot + 1);
      if (lastDot > 0) {
        parentNamespace = uimaNamespace.substring(0, lastDot);
      }
    } else {
      packageName = "noNamespace";
    }

    // create Package
    ePackage = EcoreFactory.eINSTANCE.createEPackage();
    ePackage.setNsURI(nsUri);
    ePackage.setName(packageName);
    EPackage.Registry.INSTANCE.put(nsUri, ePackage);

    // get or create SuperPackage if any
    if (parentNamespace != null) {
      EPackage superPackage = uimaNamespace2EPackage(parentNamespace);
      superPackage.getESubpackages().add(ePackage);
    }
  }
  return ePackage;
}
 
Example 12
Source File: ModelDefinitionItemProviderTests.java    From M2Doc with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Initializes test resources.
 */
@Before
public void before() {
    final ResourceSet rs = new ResourceSetImpl();
    rs.getURIConverter().getURIHandlers().add(0, uriHandler);
    rs.getResourceFactoryRegistry().getContentTypeToFactoryMap().put("*", new XMIResourceFactoryImpl());
    final Resource res = rs
            .createResource(URI.createURI(TestMemoryURIHandler.PROTOCOL + "://resources/test.genconf", false));

    final Generation generation = GenconfPackage.eINSTANCE.getGenconfFactory().createGeneration();
    generation.setTemplateFileName("test.docx");
    res.getContents().add(generation);

    definition = GenconfPackage.eINSTANCE.getGenconfFactory().createModelDefinition();
    definition.setKey("self");
    generation.getDefinitions().add(definition);

    final EPackage ePkg1 = EcorePackage.eINSTANCE.getEcoreFactory().createEPackage();
    ePkg1.setName("package1");
    final EPackage ePkg2 = EcorePackage.eINSTANCE.getEcoreFactory().createEPackage();
    ePkg2.setName("package2");
    final EPackage ePkg3 = EcorePackage.eINSTANCE.getEcoreFactory().createEPackage();
    ePkg3.setName("package3");
    final EClass eCls1 = EcorePackage.eINSTANCE.getEcoreFactory().createEClass();
    eCls1.setName("Class1");
    final EClass eCls2 = EcorePackage.eINSTANCE.getEcoreFactory().createEClass();
    eCls2.setName("Class2");
    final EClass eCls3 = EcorePackage.eINSTANCE.getEcoreFactory().createEClass();
    eCls3.setName("Class3");

    res.getContents().add(ePkg1);
    res.getContents().add(ePkg2);
    res.getContents().add(ePkg3);

    res.getContents().add(eCls1);
    res.getContents().add(eCls2);
    res.getContents().add(eCls3);

    try {
        res.save(null);
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }

    document = new XWPFDocument();

    properties = new TemplateCustomProperties(document);
    properties.getPackagesURIs().add(GenconfPackage.eINSTANCE.getNsURI());
}
 
Example 13
Source File: XtextGrammarRefactoringIntegrationTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testRefactorXtextGrammarWithGeneratedClassifierAndModelWithRefToClassifier() throws Exception {
	ResourceSet rs = resourceSetProvider.get();
	EcoreFactory eInstance = EcoreFactory.eINSTANCE;
	Resource ecoreModelResource = createEcoreModel(rs, ecoreURI, initialModelRoot);
	EClass greetingClass = getGreetingClass(ecoreModelResource);
	EReference greetingRefLocal = getReferenceoGreeting(ecoreModelResource, greetingClass);
	String greetingClassFragment = EcoreUtil.getURI(greetingClass).fragment();
	String greetingRefFragment = EcoreUtil.getURI(greetingRefLocal).fragment();

	EPackage refPackage = eInstance.createEPackage();
	refPackage.setName("myDsl2");
	refPackage.setNsPrefix("myDsl2");
	refPackage.setNsURI("http://testrefactoring2");
	EClass modelRefClass = eInstance.createEClass();
	refPackage.getEClassifiers().add(modelRefClass);
	modelRefClass.setName("ModelRef");
	EReference reference = eInstance.createEReference();
	reference.setName("ref");
	reference.setLowerBound(0);
	reference.setUpperBound(-1);
	reference.setEType(greetingClass);
	modelRefClass.getEStructuralFeatures().add(reference);
	Resource refToGreetingResource = createEcoreModel(rs,
			URI.createPlatformResourceURI(TEST_PROJECT + "/src/org/xtext/example/mydsl/" + "MyDsl2.ecore", true),
			refPackage);
	refToGreetingResource.unload();
	ecoreModelResource.unload();
	waitForBuild();
	waitForDisplay();
	XtextEditor editor = openEditor(grammarFile);
	doRefactoring(editor);
	waitForBuild();
	checkConsistenceOfGrammar(editor);
	ecoreModelResource.load(null);
	String renamedGreetingClassFragment = greetingClassFragment.replaceFirst(CLASSIFIERNAME, REFACTOREDCLASSIFIERNAME);
	EObject renamedGreetingClass = ecoreModelResource.getEObject(renamedGreetingClassFragment);
	assertNotNull(renamedGreetingClass);
	assertEquals(REFACTOREDCLASSIFIERNAME, SimpleAttributeResolver.NAME_RESOLVER.apply(renamedGreetingClass));
	EReference greetingReference = (EReference) ecoreModelResource.getEObject(greetingRefFragment);
	EClassifier eType = greetingReference.getEType();
	assertFalse(eType.eIsProxy());
	assertEquals(REFACTOREDCLASSIFIERNAME, eType.getName());

	refToGreetingResource.load(null);
	EReference externalReferenceToGreeting = getReferenceoGreeting(refToGreetingResource, eType);
	assertFalse(externalReferenceToGreeting.getEType().eIsProxy());
	assertEquals(REFACTOREDCLASSIFIERNAME, externalReferenceToGreeting.getEType().getName());

}
 
Example 14
Source File: NamesAreUniqueValidationHelperTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
private EPackage createEPackage() {
	EPackage result = EcoreFactory.eINSTANCE.createEPackage();
	result.setName(String.valueOf(created++));
	return result;
}
 
Example 15
Source File: DefaultReferenceDescriptionTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testSpecialReferences() {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsPrefix("test");
	ePackage.setNsURI("test");

	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	eClass.getESuperTypes().add(EcorePackage.Literals.EPACKAGE);
	ePackage.getEClassifiers().add(eClass);

	EReference eReference1 = EcoreFactory.eINSTANCE.createEReference();
	eReference1.setContainment(false);
	eReference1.setName("onlyExportedRef");
	eReference1.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference1);

	EReference eReference2 = EcoreFactory.eINSTANCE.createEReference();
	eReference2.setContainment(true);
	eReference2.setName("containmentRef");
	eReference2.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference2);

	EReference eReference3 = EcoreFactory.eINSTANCE.createEReference();
	eReference3.setContainment(false);
	eReference3.setTransient(true);
	eReference3.setName("transientRef");
	eReference3.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference3);

	EReference eReference4 = EcoreFactory.eINSTANCE.createEReference();
	eReference4.setContainment(false);
	eReference4.setVolatile(true);
	eReference4.setName("volatileRef");
	eReference4.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference4);

	EReference eReference5 = EcoreFactory.eINSTANCE.createEReference();
	eReference5.setContainment(false);
	eReference5.setDerived(true);
	eReference5.setName("derivedRef");
	eReference5.setEType(EcorePackage.Literals.EPACKAGE);
	eClass.getEStructuralFeatures().add(eReference5);

	EObject object = ePackage.getEFactoryInstance().create(eClass);
	object.eSet(EcorePackage.Literals.ENAMED_ELEMENT__NAME, "testname");
	object.eSet(eReference1, EcorePackage.eINSTANCE);
	object.eSet(eReference2, ePackage.getEFactoryInstance().create(eClass));
	object.eSet(eReference3, EcorePackage.eINSTANCE);
	object.eSet(eReference4, EcorePackage.eINSTANCE);
	object.eSet(eReference5, EcorePackage.eINSTANCE);

	Resource testResource = new XMIResourceImpl(URI.createPlatformResourceURI("test.ecore", true));
	testResource.getContents().add(object);
	IResourceDescription resourceDescription = createResourceDescription(testResource);
	assertEquals("Only one external reference expected", 1, size(resourceDescription.getReferenceDescriptions()));
	IReferenceDescription referenceDescription = resourceDescription.getReferenceDescriptions().iterator().next();
	assertEquals(-1, referenceDescription.getIndexInList());
	assertEquals(EcoreUtil.getURI(object), referenceDescription.getSourceEObjectUri());
	assertEquals(eReference1, referenceDescription.getEReference());
	assertEquals(EcoreUtil.getURI(EcorePackage.eINSTANCE), referenceDescription.getTargetEObjectUri());
	assertEquals(EcoreUtil.getURI(object), referenceDescription.getContainerEObjectURI());
}
 
Example 16
Source File: DefaultReferenceDescriptionTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testCrossResourceContainment() {
	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsPrefix("test");
	ePackage.setNsURI("test");

	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	ePackage.getEClassifiers().add(eClass);
	
	EAttribute nameAttribute = EcoreFactory.eINSTANCE.createEAttribute();
	nameAttribute.setName("name");
	nameAttribute.setID(true);
	nameAttribute.setEType(EcorePackage.Literals.ESTRING);
	eClass.getEStructuralFeatures().add(nameAttribute);

	EReference containmentRef = EcoreFactory.eINSTANCE.createEReference();
	containmentRef.setContainment(true);
	containmentRef.setName("crossResourceContainment");
	containmentRef.setEType(eClass);
	containmentRef.setResolveProxies(true);
	eClass.getEStructuralFeatures().add(containmentRef);
	
	EReference containerRef = EcoreFactory.eINSTANCE.createEReference();
	containerRef.setName("containerRef");
	containerRef.setEType(eClass);
	containerRef.setResolveProxies(true);
	containerRef.setEOpposite(containmentRef);
	containmentRef.setEOpposite(containerRef);
	eClass.getEStructuralFeatures().add(containerRef);

	EObject container = ePackage.getEFactoryInstance().create(eClass);
	EObject child = ePackage.getEFactoryInstance().create(eClass);
	
	Resource containerResource = new XMIResourceImpl(URI.createPlatformResourceURI("container.ecore", true));
	Resource childResource = new XMIResourceImpl(URI.createPlatformResourceURI("child.ecore", true));
	ResourceSet resourceSet = new ResourceSetImpl();
	resourceSet.getResources().add(containerResource);
	resourceSet.getResources().add(childResource);
	
	containerResource.getContents().add(container);
	childResource.getContents().add(child);
	
	container.eSet(containmentRef, child);
	assertTrue(container.eResource() != child.eResource());
	
	{ 
		IResourceDescription containerDescription = createResourceDescription(containerResource);
		IReferenceDescription onlyContainerElement = Iterables.getOnlyElement(containerDescription.getReferenceDescriptions());
		assertEquals(-1, onlyContainerElement.getIndexInList());
		assertEquals(EcoreUtil.getURI(container), onlyContainerElement.getSourceEObjectUri());
		assertEquals(containmentRef, onlyContainerElement.getEReference());
		assertEquals(EcoreUtil.getURI(child), onlyContainerElement.getTargetEObjectUri());
	}
	{
		IResourceDescription childDescription = createResourceDescription(childResource);
		IReferenceDescription onlyChildElement = Iterables.getOnlyElement(childDescription.getReferenceDescriptions());
		assertEquals(-1, onlyChildElement.getIndexInList());
		assertEquals(EcoreUtil.getURI(child), onlyChildElement.getSourceEObjectUri());
		assertEquals(containerRef, onlyChildElement.getEReference());
		assertEquals(EcoreUtil.getURI(container), onlyChildElement.getTargetEObjectUri());
	}
}
 
Example 17
Source File: DefaultReferenceDescriptionTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/** @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=330812 */
@Test public void testLazyLinkingProxyReferences() {
	URI resourceUri = URI.createPlatformResourceURI("test.ecore", true);
	LazyURIEncoder lazyURIEncoder = new LazyURIEncoder();

	ResourceSet resourceSet = new ResourceSetImpl();
	Resource testResource = resourceSet.createResource(resourceUri);

	EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
	ePackage.setName("test");
	ePackage.setNsPrefix("test");
	ePackage.setNsURI("test");
	testResource.getContents().add(ePackage);

	EClass eClass = EcoreFactory.eINSTANCE.createEClass();
	eClass.setName("Test");
	ePackage.getEClassifiers().add(eClass);

	EClass multiRefType = EcoreFactory.eINSTANCE.createEClass();
	((InternalEObject) multiRefType).eSetProxyURI(EcoreUtil.getURI(EcorePackage.Literals.EPACKAGE));
	eClass.getESuperTypes().add(multiRefType);

	EClass multiRefType2 = EcoreFactory.eINSTANCE.createEClass();
	URI dummyProxyUri = resourceUri.appendFragment(lazyURIEncoder.encode(eClass, EcorePackage.Literals.ECLASS__ESUPER_TYPES, null));
	((InternalEObject) multiRefType2).eSetProxyURI(dummyProxyUri);
	eClass.getESuperTypes().add(multiRefType2);

	EAttribute nameAttribute = EcoreFactory.eINSTANCE.createEAttribute();
	nameAttribute.setName("name");
	eClass.getEStructuralFeatures().add(nameAttribute);
	EDataType singleRefType = EcoreFactory.eINSTANCE.createEDataType();
	((InternalEObject) singleRefType).eSetProxyURI(EcoreUtil.getURI(EcorePackage.Literals.ESTRING));
	nameAttribute.setEType(singleRefType);

	assertTrue(multiRefType.eIsProxy());
	assertTrue(multiRefType2.eIsProxy());
	assertTrue(lazyURIEncoder.isCrossLinkFragment(testResource, EcoreUtil.getURI(multiRefType2).fragment()));
	assertTrue(singleRefType.eIsProxy());

	IResourceDescription resourceDescription = createResourceDescription(testResource);
	Iterable<IReferenceDescription> referenceDescriptions = resourceDescription.getReferenceDescriptions();
	assertEquals("Unexpected additional resources were loaded", 1, resourceSet.getResources().size());
	assertEquals("Unexpected reference was exported", 3, Iterables.size(referenceDescriptions));

	IReferenceDescription referenceDescription = Iterables.get(referenceDescriptions, 0);
	assertEquals(0, referenceDescription.getIndexInList());
	assertEquals(EcoreUtil.getURI(eClass), referenceDescription.getSourceEObjectUri());
	assertEquals(EcorePackage.Literals.ECLASS__ESUPER_TYPES, referenceDescription.getEReference());
	assertEquals(EcoreUtil.getURI(EcorePackage.Literals.EPACKAGE), referenceDescription.getTargetEObjectUri());

	referenceDescription = Iterables.get(referenceDescriptions, 1);
	assertEquals(-1, referenceDescription.getIndexInList());
	assertEquals(EcoreUtil.getURI(nameAttribute.getEGenericType()), referenceDescription.getSourceEObjectUri());
	assertEquals(EcorePackage.Literals.EGENERIC_TYPE__ECLASSIFIER, referenceDescription.getEReference());
	assertEquals(EcoreUtil.getURI(EcorePackage.Literals.ESTRING), referenceDescription.getTargetEObjectUri());

	referenceDescription = Iterables.get(referenceDescriptions, 2);
	assertEquals(-1, referenceDescription.getIndexInList());
	assertEquals(EcoreUtil.getURI(eClass.getEGenericSuperTypes().get(0)), referenceDescription.getSourceEObjectUri());
	assertEquals(EcorePackage.Literals.EGENERIC_TYPE__ECLASSIFIER, referenceDescription.getEReference());
	assertEquals(EcoreUtil.getURI(EcorePackage.Literals.EPACKAGE), referenceDescription.getTargetEObjectUri());
}