Test harness and fix for Bug 347546 - [model] role creation wizard leaks working copies
diff --git a/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/TypeCreator.java b/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/TypeCreator.java
index 8b70bf1..b66a33a 100644
--- a/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/TypeCreator.java
+++ b/plugins/org.eclipse.objectteams.otdt.ui/src/org/eclipse/objectteams/otdt/internal/ui/wizards/typecreation/TypeCreator.java
@@ -143,7 +143,7 @@
 
 		monitor.beginTask(NewWizardMessages.NewTypeWizardPage_operationdesc, 10); 
 		
-		ICompilationUnit createdWorkingCopy= null;
+		List<ICompilationUnit> createdWorkingCopies= new ArrayList<ICompilationUnit>();
 		
 		try
 		{
@@ -170,10 +170,11 @@
 			CompilationUnit newAST= null;
 
 			if (enclosingType != null) {
+				// if we have an enclosing type, we may indeed not to write to it (RoFi may add imports to enclosing team)
 				teamCU = enclosingType.getCompilationUnit();
 				needsSave= !teamCU.isWorkingCopy();
 				teamCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); // cu is now for sure (primary) a working copy
-				createdWorkingCopy= teamCU;
+				createdWorkingCopies.add(teamCU);
 
 				teamAST= createASTForImports(teamCU);
 			} else {
@@ -192,7 +193,7 @@
 				// create a working copy with a new owner
 				needsSave= true;
 				newCU.becomeWorkingCopy(new SubProgressMonitor(monitor, 1)); // cu is now a (primary) working copy
-				createdWorkingCopy= newCU;
+				createdWorkingCopies.add(newCU);
 				
 				IBuffer buffer= newCU.getBuffer();
 				
@@ -325,10 +326,8 @@
 		}
 		finally 
 		{
-			if (createdWorkingCopy != null) 
-			{
-				createdWorkingCopy.discardWorkingCopy();
-			}
+			for (ICompilationUnit wc : createdWorkingCopies)
+				wc.discardWorkingCopy();
 			monitor.done();
 		}