| package org.eclipse.uml2.uml.cdo.tests; |
| |
| import java.io.File; |
| import java.lang.reflect.Field; |
| import java.lang.reflect.Modifier; |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.Collections; |
| import java.util.HashSet; |
| import java.util.Map; |
| import java.util.Set; |
| |
| import org.eclipse.emf.cdo.common.CDOCommonRepository.IDGenerationLocation; |
| import org.eclipse.emf.cdo.ecore.impl.CDOEcoreFactoryImpl; |
| import org.eclipse.emf.cdo.eresource.CDOResource; |
| import org.eclipse.emf.cdo.server.db.mapping.IMappingStrategy; |
| import org.eclipse.emf.cdo.tests.AbstractCDOTest; |
| import org.eclipse.emf.cdo.tests.config.IScenario; |
| import org.eclipse.emf.cdo.tests.config.impl.RepositoryConfig; |
| import org.eclipse.emf.cdo.tests.config.impl.Scenario; |
| import org.eclipse.emf.cdo.tests.db.H2Config; |
| import org.eclipse.emf.cdo.transaction.CDOTransaction; |
| import org.eclipse.emf.common.util.BasicEList; |
| import org.eclipse.emf.common.util.URI; |
| import org.eclipse.emf.ecore.EFactory; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.EPackage; |
| import org.eclipse.emf.ecore.EcoreFactory; |
| import org.eclipse.emf.ecore.EcorePackage; |
| import org.eclipse.emf.ecore.impl.EPackageImpl; |
| import org.eclipse.emf.ecore.impl.EcoreFactoryImpl; |
| 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 org.eclipse.net4j.util.WrappedException; |
| import org.eclipse.uml2.common.util.CacheAdapter; |
| import org.eclipse.uml2.uml.UMLFactory; |
| import org.eclipse.uml2.uml.UMLPackage; |
| import org.eclipse.uml2.uml.cdo.internal.util.CDOCacheAdapter; |
| import org.eclipse.uml2.uml.internal.resource.UMLResourceFactoryImpl; |
| import org.eclipse.uml2.uml.resource.UMLResource; |
| import org.eclipse.uml2.uml.tests.util.StandaloneSupport; |
| |
| /** |
| * @author Eike Stepper |
| */ |
| public abstract class AbstractUMLTest |
| extends AbstractCDOTest { |
| |
| protected final EcoreFactory ECORE = plainEMF() |
| ? new EcoreFactoryImpl() |
| : new CDOEcoreFactoryImpl(); |
| |
| protected final UMLFactory UML = plainEMF() |
| ? new org.eclipse.uml2.uml.internal.impl.UMLFactoryImpl() |
| : new org.eclipse.uml2.uml.cdo.internal.impl.UMLFactoryImpl(); |
| |
| private static final String RESOURCE_EXTENSION = "uml"; |
| |
| private Set<Resource> resources = new HashSet<Resource>(); |
| |
| public boolean plainEMF() { |
| return false; |
| } |
| |
| @Override |
| public String toString() { |
| return "[" + (plainEMF() |
| ? "EMF" |
| : "CDO") + "] " + super.toString(); |
| } |
| |
| @Override |
| protected void doSetUp() |
| throws Exception { |
| if (!plainEMF()) { |
| super.doSetUp(); |
| } |
| |
| wirePackageAndfactory(EcorePackage.eINSTANCE, EcoreFactory.class, |
| ECORE); |
| wirePackageAndfactory(UMLPackage.eINSTANCE, UMLFactory.class, UML); |
| |
| CacheAdapter cacheAdapter = createCacheAdapter(); |
| CDOCacheAdapter.register(cacheAdapter); |
| } |
| |
| @Override |
| protected void doTearDown() |
| throws Exception { |
| if (!plainEMF()) { |
| super.doTearDown(); |
| } |
| } |
| |
| @Override |
| protected IScenario getDefaultScenario() { |
| if (dbScenario()) { |
| RepositoryConfig repositoryConfig = new H2Config() { |
| |
| private static final long serialVersionUID = 1L; |
| |
| @Override |
| protected java.util.Map<String, String> createMappingStrategyProperties() { |
| Map<String, String> props = super.createMappingStrategyProperties(); |
| props.put(IMappingStrategy.Props.FORCE_NAMES_WITH_ID, |
| "true"); |
| return props; |
| }; |
| }.idGenerationLocation(IDGenerationLocation.STORE); |
| |
| IScenario scenario = new Scenario(); |
| scenario.setRepositoryConfig(repositoryConfig); |
| scenario.setSessionConfig(JVM); |
| scenario.setModelConfig(NATIVE); |
| return scenario; |
| } |
| |
| return super.getDefaultScenario(); |
| } |
| |
| protected boolean dbScenario() { |
| return Boolean.getBoolean("DB_SCENARIO"); |
| } |
| |
| protected CacheAdapter createCacheAdapter() { |
| if (plainEMF()) { |
| return new CacheAdapter(); |
| } |
| |
| return new CDOCacheAdapter(); |
| } |
| |
| protected final Resource createResource(final EObject... rootObjects) { |
| class DataList |
| extends BasicEList<EObject> { |
| |
| private static final long serialVersionUID = 1L; |
| |
| public DataList() { |
| super(rootObjects.length, rootObjects); |
| } |
| } |
| |
| return createResource(new DataList()); |
| } |
| |
| protected final Resource createResource( |
| Collection<? extends EObject> rootObjects) { |
| String name = "res" + (resources.size() + 1) + "." + RESOURCE_EXTENSION; |
| Resource resource; |
| |
| Resource anyResource = getAnyResource(); |
| if (plainEMF()) { |
| FolderResourceSet resourceSet; |
| if (anyResource == null) { |
| resourceSet = new FolderResourceSet(); |
| resourceSet.getResourceFactoryRegistry() |
| .getExtensionToFactoryMap() |
| .put(RESOURCE_EXTENSION, new UMLResourceFactoryImpl()); |
| } else { |
| resourceSet = (FolderResourceSet) anyResource.getResourceSet(); |
| } |
| |
| resource = resourceSet.createResource(name); |
| } else { |
| CDOTransaction transaction; |
| if (anyResource == null) { |
| // The session and transaction are automatically closed. |
| transaction = openSession().openTransaction(); |
| } else { |
| transaction = getTransaction(anyResource); |
| } |
| |
| resource = transaction.createResource(getResourcePath(name)); |
| } |
| |
| for (EObject rootObject : new ArrayList<EObject>(rootObjects)) { |
| resource.getContents().add(rootObject); |
| } |
| |
| resources.add(resource); |
| System.out.println("Created resource " + resource.getURI()); |
| |
| return resource; |
| } |
| |
| protected final void saveResources() |
| throws Exception { |
| Resource anyResource = getAnyResource(); |
| if (anyResource != null) { |
| if (plainEMF()) { |
| System.out.println("Saving EMF resources..."); |
| for (Resource resource : resources) { |
| resource.save(Collections.emptyMap()); |
| } |
| } else { |
| System.out.println("Committing CDO transaction..."); |
| CDOTransaction transaction = getTransaction(anyResource); |
| transaction.commit(); |
| } |
| } else { |
| System.out.println("Nothing to save"); |
| } |
| } |
| |
| private Resource getAnyResource() { |
| if (resources.isEmpty()) { |
| return null; |
| } |
| return resources.iterator().next(); |
| } |
| |
| private CDOTransaction getTransaction(Resource resource) { |
| if (resource instanceof CDOResource) { |
| return (CDOTransaction) ((CDOResource) resource).cdoView(); |
| } |
| |
| return null; |
| } |
| |
| private void wirePackageAndfactory(EPackage ePackage, |
| Class<? extends EFactory> factoryInterface, EFactory eFactory) { |
| |
| ((EPackageImpl) ePackage).basicSetEFactoryInstance(eFactory, null); |
| eFactory.setEPackage(ePackage); |
| |
| try { |
| Field field = factoryInterface.getDeclaredField("eINSTANCE"); |
| field.setAccessible(true); |
| |
| Field modifiersField = Field.class.getDeclaredField("modifiers"); |
| modifiersField.setAccessible(true); |
| modifiersField.setInt(field, |
| field.getModifiers() & ~Modifier.FINAL); |
| |
| field.set(null, eFactory); |
| } catch (Exception ex) { |
| throw WrappedException.wrap(ex); |
| } |
| } |
| |
| public static void configureUMLResourceSet(ResourceSet resourceSet) { |
| if (StandaloneSupport.isStandalone()) { |
| StandaloneSupport.init(resourceSet); |
| |
| if (resourceSet != null) { |
| resourceSet.getResourceFactoryRegistry() |
| .getExtensionToFactoryMap() |
| .put("xmi", new XMIResourceFactoryImpl()); |
| } |
| } |
| } |
| |
| public static ResourceSet createUMLResourceSet() { |
| ResourceSet resourceSet = new ResourceSetImpl(); |
| configureUMLResourceSet(resourceSet); |
| return resourceSet; |
| } |
| |
| public static UMLResource createUMLResource(String path) { |
| ResourceSet resourceSet = createUMLResourceSet(); |
| return (UMLResource) resourceSet |
| .createResource(URI.createFileURI(path)); |
| } |
| |
| public static UMLResource loadUMLResource(String path) { |
| ResourceSet resourceSet = createUMLResourceSet(); |
| return (UMLResource) resourceSet.getResource(URI.createFileURI(path), |
| true); |
| } |
| |
| /** |
| * @author Eike Stepper |
| */ |
| private final class FolderResourceSet |
| extends ResourceSetImpl { |
| |
| private File folder; |
| |
| public FolderResourceSet() { |
| folder = createTempFolder("umltests-"); |
| } |
| |
| public Resource createResource(String name) { |
| return createResource( |
| URI.createFileURI(new File(folder, name).getAbsolutePath())); |
| } |
| } |
| } |