PageRenderTime 63ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/emf-2.8.0/org.eclipse.emf.test.codegen.ecore.xtext/src/org/eclipse/emf/test/codegen/ecore/xtext/GenModelResourceDescriptionManagerTest.java

#
Java | 150 lines | 118 code | 19 blank | 13 comment | 1 complexity | 395f24e302cafb2e905442200b5ded04 MD5 | raw file
  1. /**
  2. * Copyright (c) 2009 itemis AG (http://www.itemis.eu) and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. */
  8. package org.eclipse.emf.test.codegen.ecore.xtext;
  9. import java.util.Collections;
  10. import java.util.Map;
  11. import junit.framework.TestCase;
  12. import org.eclipse.emf.codegen.ecore.genmodel.GenClass;
  13. import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
  14. import org.eclipse.emf.codegen.ecore.genmodel.GenModelFactory;
  15. import org.eclipse.emf.codegen.ecore.genmodel.GenModelPackage;
  16. import org.eclipse.emf.codegen.ecore.xtext.GenModelRuntimeModule;
  17. import org.eclipse.emf.common.util.URI;
  18. import org.eclipse.emf.ecore.EClass;
  19. import org.eclipse.emf.ecore.EPackage;
  20. import org.eclipse.emf.ecore.EcoreFactory;
  21. import org.eclipse.emf.ecore.EcorePackage;
  22. import org.eclipse.emf.ecore.plugin.EcorePlugin;
  23. import org.eclipse.emf.ecore.resource.Resource;
  24. import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
  25. import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
  26. import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl;
  27. import org.eclipse.xtext.naming.IQualifiedNameConverter;
  28. import org.eclipse.xtext.naming.QualifiedName;
  29. import org.eclipse.xtext.resource.IEObjectDescription;
  30. import org.eclipse.xtext.resource.IResourceDescription;
  31. import org.eclipse.xtext.resource.generic.GenericResourceDescriptionManager;
  32. import com.google.common.collect.Maps;
  33. import com.google.inject.Guice;
  34. import com.google.inject.Injector;
  35. /**
  36. * @author Sven Efftinge - Initial contribution and API
  37. * @author Jan Koehnlein
  38. */
  39. public class GenModelResourceDescriptionManagerTest extends TestCase
  40. {
  41. private GenericResourceDescriptionManager testMe;
  42. private IQualifiedNameConverter converter;
  43. private Resource ecoreGenModel;
  44. @Override
  45. protected void setUp() throws Exception
  46. {
  47. super.setUp();
  48. Injector injector = Guice.createInjector(new GenModelRuntimeModule());
  49. testMe = injector.getInstance(GenericResourceDescriptionManager.class);
  50. converter = injector.getInstance(IQualifiedNameConverter.class);
  51. URI genmodelURI = URI.createURI(EcorePlugin.INSTANCE.getBaseURL().toExternalForm() + "model/Ecore.genmodel");
  52. GenModelPackage.eINSTANCE.eClass();
  53. ResourceSetImpl resourceSet = new ResourceSetImpl();
  54. resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
  55. Resource.Factory.Registry.DEFAULT_EXTENSION,
  56. new EcoreResourceFactoryImpl());
  57. ecoreGenModel = resourceSet.getResource(genmodelURI, true);
  58. assertNotNull(ecoreGenModel);
  59. }
  60. @Override
  61. protected void tearDown() throws Exception
  62. {
  63. testMe = null;
  64. converter = null;
  65. ecoreGenModel = null;
  66. super.tearDown();
  67. }
  68. public void testEcoreGenModel() throws Exception
  69. {
  70. Map<QualifiedName, IEObjectDescription> index = createIndex(ecoreGenModel);
  71. getAndCheckEntry(index, GenModelPackage.Literals.GEN_PACKAGE, "org.eclipse.emf.ecore");
  72. IEObjectDescription description = getAndCheckEntry(index, GenModelPackage.Literals.GEN_CLASS, "org.eclipse.emf.ecore.EClass");
  73. GenClass genClass = (GenClass)description.getEObjectOrProxy();
  74. assertEquals(EcorePackage.Literals.ECLASS.getName(), genClass.getEcoreClass().getName());
  75. // abstract types are available, too
  76. getAndCheckEntry(index, GenModelPackage.Literals.GEN_CLASS, "org.eclipse.emf.ecore.EClassifier");
  77. // GenStructuralFeatures are not indexed
  78. assertNoEntry(index, "org.eclipse.emf.ecore.EClassifier.defaultValue");
  79. assertNoEntry(index, "org.eclipse.emf.ecore.EClassifier.getDefaultValue");
  80. assertNoEntry(index, "org.eclipse.emf.ecore.EClassifier.DefaultValue");
  81. }
  82. public void testNestedPackage() throws Exception
  83. {
  84. Resource ecoreResource = new XMIResourceImpl();
  85. Resource genmodelResource = new XMIResourceImpl();
  86. EPackage parent = EcoreFactory.eINSTANCE.createEPackage();
  87. parent.setName("parent");
  88. parent.setNsURI("http://parent");
  89. EPackage child = EcoreFactory.eINSTANCE.createEPackage();
  90. child.setName("child");
  91. child.setNsURI("http://child");
  92. EClass eClass = EcoreFactory.eINSTANCE.createEClass();
  93. eClass.setName("Test");
  94. child.getEClassifiers().add(eClass);
  95. parent.getESubpackages().add(child);
  96. ecoreResource.getContents().add(parent);
  97. GenModel genModel = GenModelFactory.eINSTANCE.createGenModel();
  98. genModel.initialize(Collections.singleton(parent));
  99. genmodelResource.getContents().add(genModel);
  100. genModel.initialize(true);
  101. genModel.getGenPackages().get(0).setBasePackage("org.foo.bar");
  102. genModel.getGenPackages().get(0).getNestedGenPackages().get(0).setBasePackage("org.foo.zonk");
  103. Map<QualifiedName, IEObjectDescription> index = createIndex(genmodelResource);
  104. getAndCheckEntry(index, GenModelPackage.Literals.GEN_PACKAGE, "org.foo.bar.parent");
  105. getAndCheckEntry(index, GenModelPackage.Literals.GEN_PACKAGE, "org.foo.zonk.child");
  106. getAndCheckEntry(index, GenModelPackage.Literals.GEN_CLASS, "org.foo.zonk.child.Test");
  107. assertEquals(3, index.size());
  108. }
  109. protected IEObjectDescription getAndCheckEntry(Map<QualifiedName, IEObjectDescription> index, EClass expectedType, String name)
  110. {
  111. IEObjectDescription element = index.get(converter.toQualifiedName(name));
  112. assertNotNull(element);
  113. assertSame(expectedType, element.getEClass());
  114. return element;
  115. }
  116. protected void assertNoEntry(Map<QualifiedName, IEObjectDescription> index, String name)
  117. {
  118. IEObjectDescription element = index.get(converter.toQualifiedName(name));
  119. assertNull(element);
  120. }
  121. protected Map<QualifiedName, IEObjectDescription> createIndex(Resource ecoreResoure)
  122. {
  123. IResourceDescription description = testMe.getResourceDescription(ecoreResoure);
  124. Map<QualifiedName, IEObjectDescription> index = Maps.newHashMap();
  125. for (IEObjectDescription ieObjectDescription : description.getExportedObjects())
  126. {
  127. index.put(ieObjectDescription.getName(), ieObjectDescription);
  128. }
  129. return index;
  130. }
  131. }