Changeset 299 for trunk/org.modelica.mdt.test/src/org/modelica/mdt
- Timestamp:
- 02/08/06 11:46:34 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/TestModelicaRoot.java
r291 r299 46 46 import java.util.Vector; 47 47 48 import org.eclipse.core.resources.IFile; 49 import org.eclipse.core.resources.IFolder; 48 50 import org.eclipse.core.resources.IProject; 49 51 import org.eclipse.core.resources.ResourcesPlugin; 50 52 import org.eclipse.core.runtime.CoreException; 53 import org.eclipse.core.runtime.Path; 51 54 import org.eclipse.core.resources.IWorkspaceRoot; 52 55 56 import org.modelica.mdt.core.IModelicaClass; 57 import org.modelica.mdt.core.IModelicaElement; 53 58 import org.modelica.mdt.core.IModelicaElementChange; 54 59 import org.modelica.mdt.core.IModelicaElementChangeListener; 60 import org.modelica.mdt.core.IModelicaFolder; 55 61 import org.modelica.mdt.core.IModelicaProject; 56 62 import org.modelica.mdt.core.IModelicaRoot; 57 63 import org.modelica.mdt.core.ModelicaCore; 64 import org.modelica.mdt.core.compiler.CompilerInstantiationException; 65 import org.modelica.mdt.core.compiler.ConnectException; 66 import org.modelica.mdt.core.compiler.InvocationError; 67 import org.modelica.mdt.core.compiler.UnexpectedReplyException; 58 68 import org.modelica.mdt.internal.core.CorePlugin; 59 69 import org.modelica.mdt.test.util.Area51Projects; … … 67 77 public class TestModelicaRoot extends TestCase 68 78 { 69 /* this flags are set from the modelica element change listener */ 79 80 /* this flags are set from the workspace change listener */ 70 81 private boolean simpleProjectAdded = false; 71 82 private boolean modelicaProjectAdded = false; 72 83 private boolean simpleProjectRemoved = false; 73 84 private boolean modelicaProjectRemoved = false; 74 75 public class ElementListener implements IModelicaElementChangeListener 85 86 87 public class WorkspaceChangesListener 88 implements IModelicaElementChangeListener 76 89 { 77 90 … … 115 128 } 116 129 130 /* this flag is set from folder to package morph detector */ 131 private boolean morphedToPackage = false; 132 133 /** 134 * This class detect when the MORPH_FOLDER folder have morped into 135 * a package by listening to modelica change events. 136 * 137 * To engage this class register it as a listener with modelica root. When 138 * the morph is detected the morphedToPackage flag is set. 139 */ 140 public class FolderToPackageMorphDetector 141 implements IModelicaElementChangeListener 142 { 143 private boolean folderRemoved = false; 144 private boolean packageAdded = false; 145 146 public void elementsChanged(Collection<IModelicaElementChange> changes) 147 { 148 for (IModelicaElementChange ch : changes) 149 { 150 IModelicaElement element = ch.getElement(); 151 152 if (element.getElementName().equals(MORPH_FOLDER)) 153 { 154 /* 155 * we are waiting for a removed event for the 156 * folder event and added event for the package object 157 */ 158 switch(ch.getChangeType()) 159 { 160 case REMOVED: 161 folderRemoved = true; 162 break; 163 case ADDED: 164 assertTrue("element morphed into something wierd", 165 element instanceof IModelicaClass); 166 packageAdded = true; 167 break; 168 } 169 170 /* 171 * when both removed and added events recieved we know 172 * that the folder have morphed 173 */ 174 morphedToPackage = (folderRemoved && packageAdded); 175 } 176 } 177 } 178 } 179 180 /* this flag is set from folder to package morph detector */ 181 private boolean morphedToFolder = false; 182 183 /** 184 * This class detect when the MORPH_FOLDER package have morped back into 185 * a folder by listening to modelica change events. 186 * 187 * To engage this class register it as a listener with modelica root. When 188 * the morph is detected the morphedToFolder flag is set. 189 */ 190 public class PackageToFolderMorphDetector 191 implements IModelicaElementChangeListener 192 { 193 private boolean packageRemoved = false; 194 private boolean folderAdded = false; 195 196 public void elementsChanged(Collection<IModelicaElementChange> changes) 197 { 198 for (IModelicaElementChange ch : changes) 199 { 200 IModelicaElement element = ch.getElement(); 201 202 if (element.getElementName().equals(MORPH_FOLDER)) 203 { 204 /* 205 * we are waiting for a removed event for the 206 * folder event and added event for the package object 207 */ 208 switch(ch.getChangeType()) 209 { 210 case REMOVED: 211 packageRemoved = true; 212 break; 213 case ADDED: 214 assertTrue("element morphed into something wierd", 215 element instanceof IModelicaFolder); 216 folderAdded = true; 217 break; 218 } 219 220 /* 221 * when both removed and added events recieved we know 222 * that the folder have morphed 223 */ 224 morphedToFolder = (packageRemoved && folderAdded); 225 } 226 } 227 } 228 } 229 230 117 231 /* names of modelica projects */ 118 232 private Vector<String> modelicaProjects = new Vector<String>(2); … … 128 242 TestModelicaRoot.class.getName() + "3"; 129 243 244 private static final String MORPH_FOLDER = "morph_folder"; 245 130 246 /* the SIMple project which is added and removed */ 131 247 private static final String PROJECT_NAME_SIM_EXTRA = … … 145 261 private IModelicaRoot modelicaRoot = 146 262 ModelicaCore.getModelicaRoot(); 263 264 private IFolder morphFolder = null; 265 147 266 148 267 @Override … … 159 278 IModelicaProject moProj = 160 279 ModelicaCore.getModelicaRoot().createProject(PROJECT_NAME_1); 161 assertNotNull("failed to create project", moProj); 162 } 280 assertNotNull("failed to create project", moProj); 281 project = moProj.getProject(); 282 /* 283 * create the folder that will be morphed to a package and then 284 * back to folder (se testMorphing()) 285 */ 286 IFolder folder = project.getFolder(new Path(MORPH_FOLDER)); 287 folder.create(false, true, null); 288 289 } 290 morphFolder = project.getFolder(new Path(MORPH_FOLDER)); 163 291 164 292 /* … … 251 379 { 252 380 ModelicaCore.getModelicaRoot(). 253 addModelicaElementChangeListener(new ElementListener());381 addModelicaElementChangeListener(new WorkspaceChangesListener()); 254 382 255 383 /* … … 327 455 } 328 456 } 457 458 /** 459 * test that a IModelicaFolder changes to an IModelicaPackage 460 * when a package.mo with correct contents is added to it and 461 * that IModelicaPackage turns back to IModelicaFolder when 462 * the package.mo is removed 463 */ 464 public void testMorphing() 465 throws CoreException, ConnectException, UnexpectedReplyException, 466 InvocationError, CompilerInstantiationException 467 { 468 /* we need to make sure that the folder we are going to morph is loaded */ 469 Utility.getProject(PROJECT_NAME_1).getRootFolder().getChildren(); 470 471 /* 472 * check if folder will morph into a package when the package.mo 473 * file is added 474 */ 475 IModelicaElementChangeListener listener = 476 new FolderToPackageMorphDetector(); 477 478 ModelicaCore.getModelicaRoot().addModelicaElementChangeListener 479 (listener); 480 IFile file = morphFolder.getFile("package.mo"); 481 482 String contents = 483 "package " + MORPH_FOLDER + "\n" + 484 "\n" + 485 "end " + MORPH_FOLDER + ";"; 486 487 file.create(Utility.getByteStream(contents), true, null); 488 489 490 /* wait tops 5 seconds for the morph to kick in */ 491 long waitUntil = System.currentTimeMillis() + 5000; 492 while ((!morphedToPackage) && waitUntil > System.currentTimeMillis()) 493 { 494 Utility.sleep(this, 100); 495 } 496 497 ModelicaCore.getModelicaRoot(). 498 removeModelicaElementChangeListener(listener); 499 assertTrue("the folder didn't morph into package, waited to the"+ 500 " change around 5 seconds", morphedToPackage); 501 502 /* 503 * check if package will morph into a folder when the package.mo 504 * file is removed 505 */ 506 507 listener = new PackageToFolderMorphDetector(); 508 509 ModelicaCore.getModelicaRoot().addModelicaElementChangeListener 510 (listener); 511 file.delete(true, false, null); 512 513 /* wait tops 5 seconds for the morph to kick in */ 514 waitUntil = System.currentTimeMillis() + 5000; 515 while ((!morphedToFolder) && waitUntil > System.currentTimeMillis()) 516 { 517 Utility.sleep(this, 100); 518 } 519 520 ModelicaCore.getModelicaRoot(). 521 removeModelicaElementChangeListener(listener); 522 assertTrue("the package didn't morph into folder, waited to the"+ 523 " change around 5 seconds", morphedToFolder); 524 525 } 329 526 }
Note: See TracChangeset
for help on using the changeset viewer.