- Timestamp:
- 11/07/11 00:53:15 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/util/Utility.java
r1052 r1075 42 42 package org.modelica.mdt.test.util; 43 43 44 import java.io.BufferedReader; 44 45 import java.io.ByteArrayInputStream; 45 46 import java.io.IOException; 46 47 import java.io.InputStream; 48 import java.io.InputStreamReader; 47 49 import java.util.Collection; 48 50 import java.util.concurrent.Semaphore; … … 51 53 52 54 import org.eclipse.core.resources.IFile; 55 import org.eclipse.core.resources.IProject; 53 56 import org.eclipse.core.resources.IWorkspace; 54 57 import org.eclipse.core.resources.IWorkspaceDescription; 55 import org.eclipse.core.resources.IWorkspaceRoot;56 58 import org.eclipse.core.resources.ResourcesPlugin; 57 59 import org.eclipse.core.runtime.CoreException; … … 60 62 import org.eclipse.jface.wizard.WizardDialog; 61 63 import org.eclipse.swt.widgets.Button; 64 import org.eclipse.swt.widgets.Shell; 62 65 import org.eclipse.swt.widgets.Widget; 63 66 import org.eclipse.ui.IWorkbench; 67 import org.eclipse.ui.IWorkbenchWindow; 64 68 import org.eclipse.ui.IWorkbenchWizard; 65 69 import org.eclipse.ui.PlatformUI; 66 70 import org.eclipse.ui.wizards.IWizardDescriptor; 71 import org.eclipse.ui.wizards.IWizardRegistry; 67 72 import org.modelica.mdt.core.IModelicaClass; 68 73 import org.modelica.mdt.core.IModelicaElement; 69 import org.modelica.mdt.core.IModelicaExtends;70 74 import org.modelica.mdt.core.IModelicaRoot; 71 75 import org.modelica.mdt.core.IModelicaSourceFile; … … 91 95 * This class contains some utility code for assisting the testcases 92 96 */ 93 public class Utility 94 { 95 97 public class Utility { 96 98 /** 97 99 * creates and opens a wizard 98 * 100 * 99 101 * @param wizardID the ID of the wizard to create and open 100 102 * @param selection the selection which is used to initialize the wizard 101 103 */ 102 public static IWorkbenchWizard openWizard(String wizardID, 103 IStructuredSelection selection) 104 { 105 104 public static IWorkbenchWizard openWizard(String wizardID, IStructuredSelection selection) { 106 105 IWorkbench workbench = PlatformUI.getWorkbench(); 107 IWizard Descriptor wizDesc =108 workbench.getNewWizardRegistry().findWizard(wizardID);106 IWizardRegistry wizardRegistry = workbench.getNewWizardRegistry(); 107 IWizardDescriptor wizDesc = wizardRegistry.findWizard(wizardID); 109 108 Assert.assertNotNull("wizard " + wizardID + " not found", wizDesc); 110 109 111 110 IWorkbenchWizard wizard = null; 112 try 113 { 111 try { 114 112 wizard = wizDesc.createWizard(); 115 113 } 116 catch (CoreException e) 117 { 118 Assert.fail("Could not create " + wizardID + 114 catch (CoreException e) { 115 Assert.fail("Could not create " + wizardID + 119 116 " wizard, CoreException thrown\n" + e.getMessage()); 120 117 } 121 118 Assert.assertNotNull(wizard); 122 119 123 120 wizard.init(workbench, selection); 124 final WizardDialog dialog = 125 new WizardDialog(workbench.getActiveWorkbenchWindow().getShell(), wizard); 121 IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow(); 122 Shell shell = activeWorkbenchWindow.getShell(); 123 final WizardDialog dialog = new WizardDialog(shell, wizard); 126 124 dialog.create(); 127 125 128 126 final Semaphore sem = new Semaphore(0); 129 130 dialog.getShell().getDisplay().syncExec(new Runnable() 131 { 132 public void run() 133 { 127 128 dialog.getShell().getDisplay().syncExec(new Runnable() { 129 public void run() { 134 130 dialog.setBlockOnOpen(false); 135 131 dialog.open(); … … 137 133 } 138 134 }); 139 140 try 141 { 135 136 try { 142 137 sem.acquire(); 143 } 144 catch (InterruptedException e) 145 { 138 } 139 catch (InterruptedException e) { 146 140 Assert.fail("interruped while waiting for dialog to open"); 147 141 } 148 142 149 143 return wizard; 150 144 } 145 151 146 /** 152 147 * creates and opens a wizard initialized with empty selection 153 * 148 * 154 149 * @param wizardID the ID of the wizard to create and open 155 150 */ 156 public static IWorkbenchWizard openWizard(String wizardID) 157 { 151 public static IWorkbenchWizard openWizard(String wizardID) { 158 152 return openWizard(wizardID, StructuredSelection.EMPTY); 159 153 } 160 161 /** 162 * 154 155 /** 156 * 163 157 * @return currently displayed button with text '&Finish' 164 158 * this function will fail if not exactly one finish button is 165 159 * on the screen 166 160 */ 167 public static Button findButtonByText(String buttonText) 168 { 161 public static Button findButtonByText(String buttonText) { 169 162 BasicFinder finder = /* find finish button */ 170 new BasicFinder(new TestHierarchy(PlatformUI.getWorkbench().getDisplay())); 171 172 try 173 { 163 new BasicFinder(new TestHierarchy(PlatformUI.getWorkbench().getDisplay())); 164 165 try { 174 166 return (Button) finder.find(new TextMatcher(buttonText)); 175 167 } 176 catch (WidgetNotFoundException e) 177 { 168 catch (WidgetNotFoundException e) { 178 169 Assert.fail("Finish button not found."); 179 } 180 catch (MultipleWidgetsFoundException e) 181 { 170 } 171 catch (MultipleWidgetsFoundException e) { 182 172 Assert.fail("Multiple finish buttons found."); 183 173 } 184 174 185 175 Assert.fail("this is not happening"); 186 176 return null; 187 177 } 178 188 179 /* 189 * sleeps for approx time seconds 190 * this method does not garantee that it will sleep any particular time 191 */ 192 public static void sleep(Object mutex, long time) 193 { 194 try 195 { 196 synchronized (mutex) 197 { 180 * sleeps for approximately time seconds 181 * this method does not guarantee that it will sleep any particular time 182 */ 183 public static void sleep(Object mutex, long time) { 184 try { 185 synchronized (mutex) { 198 186 mutex.wait(time); 199 187 } 200 } 201 catch (InterruptedException e) 202 { 188 } 189 catch (InterruptedException e) { 203 190 e.printStackTrace(); 204 191 } … … 208 195 * Tries to find a project by name, this method fails (Assert.fail) 209 196 * if a modelica project by that name is not found. This method also 210 * 211 * 197 * fails if any exception is thrown while searching for the project. 198 * 212 199 * @param name name of the project to find 213 200 * @return the modelica project handle named name 214 201 */ 215 public static IModelicaProject getProject(String name) 216 { 202 public static IModelicaProject getProject(String name) { 217 203 for (Object proj : ModelicaCore.getModelicaRoot().getProjects()) 218 204 { 219 if (proj instanceof IModelicaProject) 220 { 205 if (proj instanceof IModelicaProject) { 221 206 if (((IModelicaProject)proj).getElementName().equals(name)) 222 207 { … … 225 210 } 226 211 } 227 212 228 213 Assert.fail("No modelica project named '" + name + "' found."); 229 214 return null; /* this is not happening */ 230 215 } 231 232 public static Widget getInstrumentedWidget(final String tag) 233 { 216 217 public static Widget getInstrumentedWidget(final String tag) { 234 218 /* 235 219 * find classType combo by tag … … 238 222 BasicFinder finder = new BasicFinder(new TestHierarchy 239 223 (PlatformUI.getWorkbench().getDisplay())); 240 241 try 242 { 243 widget = finder.find(new Matcher() 244 { 245 public boolean matches(Widget w) 246 { 224 225 try { 226 widget = finder.find(new Matcher() { 227 public boolean matches(Widget w) { 247 228 Object widgetTag = w.getData("name"); 248 229 249 if (widgetTag == null || !(widgetTag instanceof String)) 250 { 251 return false; 252 } 253 return ((String)widgetTag).equals(tag); 254 } 255 230 if (widgetTag == null || !(widgetTag instanceof String)) { 231 return false; 232 } 233 return ((String)widgetTag).equals(tag); 234 } 256 235 }); 257 236 258 237 return widget; 259 } 260 catch (WidgetNotFoundException e) 261 { 238 } 239 catch (WidgetNotFoundException e) { 262 240 /* fall through */ 263 } 264 catch (MultipleWidgetsFoundException e) 265 { 241 } 242 catch (MultipleWidgetsFoundException e) { 266 243 /* fall through */ 267 244 } 268 245 269 246 /* exception thrown, no/more than one widgets found */ 270 247 return null; 271 248 } 272 249 250 public static void printFile(IProject project, String fileName) { 251 IFile file = project.getFile(fileName); 252 try { 253 InputStream is = file.getContents(); 254 InputStreamReader isr = new InputStreamReader(is); 255 BufferedReader br = new BufferedReader(isr); 256 257 String total = ""; 258 String line; 259 260 while ((line = br.readLine()) != null) { 261 total += line + "\n"; 262 } 263 264 System.out.println(total); 265 } 266 catch (CoreException e) { 267 e.printStackTrace(); 268 } 269 catch (IOException e) { 270 e.printStackTrace(); 271 } 272 } 273 274 public static void printFile(IModelicaProject modelicaProject, String fileName) { 275 IProject project = modelicaProject.getWrappedProject(); 276 printFile(project, fileName); 277 } 278 273 279 /** 274 280 * Compares the content of a file with a provided string 275 281 * @param file 276 282 * @param expectedContent 277 * @return true if file's content exactly matches the expectedContent 283 * @return true if file's content exactly matches the expectedContent 278 284 * string 279 285 */ 280 public static boolean compareContent(IFile file, String expectedContent) 281 { 286 public static boolean compareContent(IFile file, String expectedContent) { 282 287 InputStream fileContent = null; 283 284 try 285 { 288 289 try { 286 290 fileContent = file.getContents(); 287 291 } 288 catch (CoreException e) 289 { 292 catch (CoreException e) { 290 293 Assert.fail("could not fetch contents of the created class"); 291 294 } 292 295 293 296 byte[] buf = new byte[expectedContent.length()]; 294 295 try 296 { 297 298 try { 297 299 fileContent.read(buf); 298 300 int i = fileContent.read(); 299 301 300 if (-1 != i) 301 { 302 if (-1 != i) { 302 303 Assert.fail("file is to longer than expected"); 303 304 } 304 305 } 305 catch (IOException e) 306 { 306 catch (IOException e) { 307 307 Assert.fail("could not read contents of the file"); 308 308 } 309 309 310 return expectedContent.equals(new String(buf)); 310 311 } 311 312 /** 313 * conv inience method to 'convert' a string into a InputStream.314 * 312 313 /** 314 * convenience method to 'convert' a string into a InputStream. 315 * 315 316 * @return an input stream that have the same content as the provided string 316 317 */ 317 public static InputStream getByteStream(String content) 318 { 318 public static InputStream getByteStream(String content) { 319 319 return new ByteArrayInputStream(content.getBytes()); 320 320 } 321 322 /** 323 * Conv inience method to find a modelica file in a modelica folder.324 * If the file is not found, then this method will fail 325 * (e.g. call Assert.fail()) the invo cing test.326 * 321 322 /** 323 * Convenience method to find a modelica file in a modelica folder. 324 * If the file is not found, then this method will fail 325 * (e.g. call Assert.fail()) the invoking test. 326 * 327 327 * @param folder the folder where to look 328 328 * @param fileName the name of the file to look for 329 * 330 * @return the modelica file found 331 * @throws CoreException 332 * @throws InvocationError 333 * @throws UnexpectedReplyException 334 * @throws ConnectException 335 * @throws CompilerInstantiationException 336 */ 337 public static IModelicaSourceFile findModelicaFileInFolder(IModelicaFolder folder, 338 String fileName) 339 throws ConnectException, UnexpectedReplyException, 340 InvocationError, CoreException, CompilerInstantiationException 341 { 342 for (Object child : folder.getChildren()) 343 { 344 if (child instanceof IModelicaSourceFile) 345 { 346 IModelicaSourceFile f = (IModelicaSourceFile) child; 347 if (f.getElementName().equals(fileName)) 348 { 349 return f; 350 } 329 * 330 * @return the modelica file found 331 * @throws CoreException 332 * @throws InvocationError 333 * @throws UnexpectedReplyException 334 * @throws ConnectException 335 * @throws CompilerInstantiationException 336 */ 337 public static IModelicaSourceFile findModelicaFileInFolder(IModelicaFolder folder, String fileName) 338 throws ConnectException, UnexpectedReplyException, InvocationError, CoreException, CompilerInstantiationException { 339 for (Object child : folder.getChildren()) { 340 if (child instanceof IModelicaSourceFile) { 341 IModelicaSourceFile f = (IModelicaSourceFile)child; 342 if (f.getElementName().equals(fileName)) { 343 return f; 351 344 } 352 } 353 354 Assert.fail("No modelica file named '" + fileName + 355 "' found in folder '" + folder.getElementName() + "'"); 345 } 346 } 347 348 Assert.fail("No modelica file named '" + fileName + 349 "' found in folder '" + folder.getElementName() + "'"); 356 350 return null; /* this is not happening */ 357 358 351 } 359 352 360 353 public static boolean isAutoBuilding() { 361 354 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 362 355 363 356 boolean isAutoBuilding = workspace.isAutoBuilding(); 364 357 365 358 return isAutoBuilding; 366 359 } … … 368 361 public static void setAutobuilding(boolean autoBuilding) { 369 362 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 370 363 371 364 IWorkspaceDescription workspaceDescription = workspace.getDescription(); 372 365 373 366 workspaceDescription.setAutoBuilding(autoBuilding); 374 367 } … … 376 369 public static void listAllProjects() { 377 370 IModelicaRoot modelicaRoot = ModelicaCore.getModelicaRoot(); 378 371 379 372 IModelicaProject[] allProjects = modelicaRoot.getProjects(); 380 373 381 374 for (IModelicaProject modelicaProject : allProjects) { 382 375 String name = modelicaProject.getElementName(); 383 376 384 377 System.out.println(name); 385 378 } 386 379 } 387 380 388 381 public static void goThroughStdlib(Collection<? extends IModelicaElement> aList) { 389 382 if (aList == null) { 390 383 IModelicaRoot modelicaRoot = ModelicaCore.getModelicaRoot(); 391 384 392 385 IStandardLibrary stdlib = modelicaRoot.getStandardLibrary(null); 393 386 394 387 try { 395 388 aList = stdlib.getPackages(); … … 408 401 IModelicaClass cls = (IModelicaClass)elem; 409 402 String name = cls.getFullName(); 410 403 411 404 System.out.println("Considering " + name); 412 405 413 406 boolean isEncapsulated = cls.isEncapsulated(); 414 407 415 408 if (isEncapsulated) { 416 409 System.out.println(name + " is indeed encapsulated!"); … … 419 412 //System.out.println(pkg.getFullName() + " is not encapsulated."); 420 413 } 421 414 422 415 Collection<? extends IModelicaElement> children = cls.getChildren(); 423 416 424 417 if (children != null) { 425 418 goThroughStdlib(children);
Note: See TracChangeset
for help on using the changeset viewer.