diff options
3 files changed, 140 insertions, 10 deletions
diff --git a/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/bugzilla/Bugzilla_399279_Test.java b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/bugzilla/Bugzilla_399279_Test.java new file mode 100644 index 0000000000..784cb36397 --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.tests/src/org/eclipse/emf/cdo/tests/bugzilla/Bugzilla_399279_Test.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2004 - 2013 Eike Stepper (Berlin, 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.cdo.view.CDOViewSet; + +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; + +/** + * Bug 399279: Tests the support for removing the {@link CDOViewSet} from the adapter list + * of a {@link ResourceSet}. + */ +public class Bugzilla_399279_Test extends AbstractCDOTest +{ + public void testRemoveAdapterWhileResourcesStillLoaded() throws Exception + { + CDOSession session = openSession(); + CDOTransaction transaction = session.openTransaction(); + + Resource resource = transaction.getOrCreateResource(getResourcePath("test.model5")); + ResourceSet resourceSet = resource.getResourceSet(); + resource.getContents().add(getModel5Factory().createParent()); + transaction.commit(); + + try + { + resourceSet.eAdapters().clear(); + fail("Did not throw IllegalArgumentException on attempt to remove CDOViewSet from adapter list."); + } + catch (IllegalArgumentException expected) + { + // pass + } + + assertEquals(true, resourceSet.eAdapters().contains(transaction.getViewSet())); + } + + public void testRemoveAdapterWhileViewStillOpen() throws Exception + { + CDOSession session = openSession(); + CDOTransaction transaction = session.openTransaction(); + + Resource resource = transaction.getOrCreateResource(getResourcePath("test.model5")); + ResourceSet resourceSet = resource.getResourceSet(); + resource.getContents().add(getModel5Factory().createParent()); + transaction.commit(); + + resource.unload(); + resourceSet.getResources().remove(resource); + + try + { + resourceSet.eAdapters().clear(); + fail("Did not throw IllegalArgumentException on attempt to remove CDOViewSet from adapter list."); + } + catch (IllegalArgumentException expected) + { + // pass + } + + assertEquals(true, resourceSet.eAdapters().contains(transaction.getViewSet())); + } + + public void testRemoveAdapterViewsClosedAndResourcesRemoved() throws Exception + { + CDOSession session = openSession(); + CDOTransaction transaction = session.openTransaction(); + + Resource resource = transaction.getOrCreateResource(getResourcePath("test.model5")); + ResourceSet resourceSet = resource.getResourceSet(); + resource.getContents().add(getModel5Factory().createParent()); + transaction.commit(); + + resource.unload(); + resourceSet.getResources().remove(resource); + + transaction.close(); + resourceSet.eAdapters().clear(); // Should not throw a RuntimeException + } +} diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/messages/messages.properties b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/messages/messages.properties index 29408c576e..7664ea2615 100644 --- a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/messages/messages.properties +++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/messages/messages.properties @@ -7,6 +7,7 @@ # Contributors: # Victor Roldan Betancort - initial API and implementation # Eike Stepper - maintenance +# Christian W. Damus (CEA) - bug 399279 AbstractObjectConflictResolver.0=Object has feature-level conflicts CDOAdapterPolicy.0=CDO @@ -62,6 +63,8 @@ CDOViewSetImpl.1=Do not know which CDOView to take since no authority has been s CDOViewSetImpl.2=Only one view per repository can be open for the same resource set CDOViewSetImpl.3=Unsupported target: {0} CDOViewSetImpl.4=Cannot associate more than 1 resource set with this view set +CDOViewSetImpl.5=Cannot disconnect view set from resource set that still contains resources +CDOViewSetImpl.6=Cannot disconnect view set from resource set while some views remain open CDOXACommitContextImpl.0=Object should be an EObject: {0} CDOXATransactionImpl.0=ViewSet is already attached to this XATransaction CDOXATransactionImpl.1=ViewSet is not attached to this XATransaction diff --git a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewSetImpl.java b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewSetImpl.java index 3c64a57c96..cedf5a0c98 100644 --- a/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewSetImpl.java +++ b/plugins/org.eclipse.emf.cdo/src/org/eclipse/emf/internal/cdo/view/CDOViewSetImpl.java @@ -9,6 +9,7 @@ * Simon McDuff - initial API and implementation * Eike Stepper - maintenance * Victor Roldan Betancort - bug 338921 + * Christian W. Damus (CEA) - bug 399279: support removal from resource set adapters */ package org.eclipse.emf.internal.cdo.view; @@ -195,24 +196,58 @@ public class CDOViewSetImpl extends NotifierImpl implements InternalCDOViewSet public void setTarget(Notifier newTarget) { - if (!isAdapterForType(newTarget)) + if (newTarget == resourceSet) { - throw new IllegalArgumentException(MessageFormat.format(Messages.getString("CDOViewSetImpl.3"), newTarget)); //$NON-NLS-1$ + return; } - if (resourceSet != null) + if (newTarget == null && resourceSet != null) { - throw new IllegalStateException(Messages.getString("CDOViewSetImpl.4")); //$NON-NLS-1$ + if (!resourceSet.getResources().isEmpty()) + { + if (!resourceSet.eAdapters().contains(this)) + { + resourceSet.eAdapters().add(this); // add me back to the resource set's adapters + } + + throw new IllegalArgumentException(Messages.getString("CDOViewSetImpl.5")); //$NON-NLS-1$ + } + + if (getViews().length > 0) + { + if (!resourceSet.eAdapters().contains(this)) + { + resourceSet.eAdapters().add(this); // add me back to the resource set's adapters + } + + throw new IllegalArgumentException(Messages.getString("CDOViewSetImpl.6")); //$NON-NLS-1$ + } + } + else + { + if (!isAdapterForType(newTarget)) + { + throw new IllegalArgumentException(MessageFormat.format(Messages.getString("CDOViewSetImpl.3"), newTarget)); //$NON-NLS-1$ + } + + if (resourceSet != null) + { + throw new IllegalStateException(Messages.getString("CDOViewSetImpl.4")); //$NON-NLS-1$ + } } resourceSet = (ResourceSet)newTarget; - EPackage.Registry oldPackageRegistry = resourceSet.getPackageRegistry(); - packageRegistry = new CDOViewSetPackageRegistryImpl(this, oldPackageRegistry); - resourceSet.setPackageRegistry(packageRegistry); - Registry registry = resourceSet.getResourceFactoryRegistry(); - Map<String, Object> map = registry.getProtocolToFactoryMap(); - map.put(CDOProtocolConstants.PROTOCOL_NAME, getResourceFactory()); + if (resourceSet != null) + { + EPackage.Registry oldPackageRegistry = resourceSet.getPackageRegistry(); + packageRegistry = new CDOViewSetPackageRegistryImpl(this, oldPackageRegistry); + resourceSet.setPackageRegistry(packageRegistry); + + Registry registry = resourceSet.getResourceFactoryRegistry(); + Map<String, Object> map = registry.getProtocolToFactoryMap(); + map.put(CDOProtocolConstants.PROTOCOL_NAME, getResourceFactory()); + } } public boolean isAdapterForType(Object type) |