Changeset 1075 for trunk


Ignore:
Timestamp:
11/07/11 00:53:15 (13 years ago)
Author:
masberg
Message:

Add utility methods for printing project files. Formatting fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/util/Utility.java

    r1052 r1075  
    4242package org.modelica.mdt.test.util;
    4343
     44import java.io.BufferedReader;
    4445import java.io.ByteArrayInputStream;
    4546import java.io.IOException;
    4647import java.io.InputStream;
     48import java.io.InputStreamReader;
    4749import java.util.Collection;
    4850import java.util.concurrent.Semaphore;
     
    5153
    5254import org.eclipse.core.resources.IFile;
     55import org.eclipse.core.resources.IProject;
    5356import org.eclipse.core.resources.IWorkspace;
    5457import org.eclipse.core.resources.IWorkspaceDescription;
    55 import org.eclipse.core.resources.IWorkspaceRoot;
    5658import org.eclipse.core.resources.ResourcesPlugin;
    5759import org.eclipse.core.runtime.CoreException;
     
    6062import org.eclipse.jface.wizard.WizardDialog;
    6163import org.eclipse.swt.widgets.Button;
     64import org.eclipse.swt.widgets.Shell;
    6265import org.eclipse.swt.widgets.Widget;
    6366import org.eclipse.ui.IWorkbench;
     67import org.eclipse.ui.IWorkbenchWindow;
    6468import org.eclipse.ui.IWorkbenchWizard;
    6569import org.eclipse.ui.PlatformUI;
    6670import org.eclipse.ui.wizards.IWizardDescriptor;
     71import org.eclipse.ui.wizards.IWizardRegistry;
    6772import org.modelica.mdt.core.IModelicaClass;
    6873import org.modelica.mdt.core.IModelicaElement;
    69 import org.modelica.mdt.core.IModelicaExtends;
    7074import org.modelica.mdt.core.IModelicaRoot;
    7175import org.modelica.mdt.core.IModelicaSourceFile;
     
    9195 * This class contains some utility code for assisting the testcases
    9296 */
    93 public class Utility
    94 {
    95 
     97public class Utility {
    9698    /**
    9799     * creates and opens a wizard
    98      * 
     100     *
    99101     * @param wizardID the ID of the wizard to create and open
    100102     * @param selection the selection which is used to initialize the wizard
    101103     */
    102     public static IWorkbenchWizard openWizard(String wizardID,
    103             IStructuredSelection selection)
    104     {
    105        
     104    public static IWorkbenchWizard openWizard(String wizardID, IStructuredSelection selection) {
    106105        IWorkbench workbench = PlatformUI.getWorkbench();
    107         IWizardDescriptor wizDesc =
    108             workbench.getNewWizardRegistry().findWizard(wizardID); 
     106        IWizardRegistry wizardRegistry = workbench.getNewWizardRegistry();
     107        IWizardDescriptor wizDesc = wizardRegistry.findWizard(wizardID);
    109108        Assert.assertNotNull("wizard " + wizardID + " not found", wizDesc);
    110        
     109
    111110        IWorkbenchWizard wizard = null;
    112         try
    113         {
     111        try {
    114112            wizard = wizDesc.createWizard();
    115113        }
    116         catch (CoreException e)
    117         {
    118             Assert.fail("Could not create " + wizardID +
     114        catch (CoreException e) {
     115            Assert.fail("Could not create " + wizardID +
    119116                    " wizard, CoreException thrown\n" + e.getMessage());
    120117        }
    121118        Assert.assertNotNull(wizard);
    122        
     119
    123120        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);
    126124        dialog.create();
    127125
    128126        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() {
    134130                dialog.setBlockOnOpen(false);
    135131                dialog.open();
     
    137133            }
    138134        });
    139        
    140         try
    141         {
     135
     136        try {
    142137            sem.acquire();
    143         }
    144         catch (InterruptedException e)
    145         {
     138        }
     139        catch (InterruptedException e) {
    146140            Assert.fail("interruped while waiting for dialog to open");
    147141        }
    148        
     142
    149143        return wizard;
    150144    }
     145
    151146    /**
    152147     * creates and opens a wizard initialized with empty selection
    153      * 
     148     *
    154149     * @param wizardID the ID of the wizard to create and open
    155150     */
    156     public static IWorkbenchWizard openWizard(String wizardID)
    157     {
     151    public static IWorkbenchWizard openWizard(String wizardID) {
    158152        return openWizard(wizardID, StructuredSelection.EMPTY);
    159153    }
    160    
    161     /**
    162      * 
     154
     155    /**
     156     *
    163157     * @return currently displayed button with text '&Finish'
    164158     * this function will fail if not exactly one finish button is
    165159     * on the screen
    166160     */
    167     public static Button findButtonByText(String buttonText)
    168     {
     161    public static Button findButtonByText(String buttonText) {
    169162        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 {
    174166            return (Button) finder.find(new TextMatcher(buttonText));
    175167        }
    176         catch (WidgetNotFoundException e)
    177         {
     168        catch (WidgetNotFoundException e) {
    178169            Assert.fail("Finish button not found.");
    179         }
    180         catch (MultipleWidgetsFoundException e)
    181         {
     170        }
     171        catch (MultipleWidgetsFoundException e) {
    182172            Assert.fail("Multiple finish buttons found.");
    183173        }
    184        
     174
    185175        Assert.fail("this is not happening");
    186176        return null;
    187177    }
     178
    188179    /*
    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) {
    198186                mutex.wait(time);
    199187            }
    200         }
    201         catch (InterruptedException e)
    202         {
     188        }
     189        catch (InterruptedException e) {
    203190            e.printStackTrace();
    204191        }
     
    208195     * Tries to find a project by name, this method fails (Assert.fail)
    209196     * if a modelica project by that name is not found. This method also
    210      *  fails if any exception is thrown while searching for the project.
    211      * 
     197     * fails if any exception is thrown while searching for the project.
     198     *
    212199     * @param name name of the project to find
    213200     * @return the modelica project handle named name
    214201     */
    215     public static IModelicaProject getProject(String name)
    216     {
     202    public static IModelicaProject getProject(String name) {
    217203        for (Object proj : ModelicaCore.getModelicaRoot().getProjects())
    218204        {
    219             if (proj instanceof IModelicaProject)
    220             {
     205            if (proj instanceof IModelicaProject) {
    221206                if (((IModelicaProject)proj).getElementName().equals(name))
    222207                {
     
    225210            }
    226211        }
    227        
     212
    228213        Assert.fail("No modelica project named '" + name + "' found.");
    229214        return null; /* this is not happening */
    230215    }
    231    
    232     public static Widget getInstrumentedWidget(final String tag)
    233     {
     216
     217    public static Widget getInstrumentedWidget(final String tag) {
    234218        /*
    235219         * find classType combo by tag
     
    238222        BasicFinder finder = new BasicFinder(new TestHierarchy
    239223                (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) {
    247228                    Object widgetTag = w.getData("name");
    248229
    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                }
    256235            });
    257            
     236
    258237            return widget;
    259         }
    260         catch (WidgetNotFoundException e)
    261         {
     238        }
     239        catch (WidgetNotFoundException e) {
    262240            /* fall through */
    263         }
    264         catch (MultipleWidgetsFoundException e)
    265         {
     241        }
     242        catch (MultipleWidgetsFoundException e) {
    266243            /* fall through */
    267244        }
    268        
     245
    269246        /* exception thrown, no/more than one widgets found */
    270247        return null;
    271248    }
    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
    273279    /**
    274280     * Compares the content of a file with a provided string
    275281     * @param file
    276282     * @param expectedContent
    277      * @return true if file's content exactly matches the expectedContent 
     283     * @return true if file's content exactly matches the expectedContent
    278284     * string
    279285     */
    280     public static boolean compareContent(IFile file, String expectedContent)
    281     {
     286    public static boolean compareContent(IFile file, String expectedContent) {
    282287        InputStream fileContent = null;
    283        
    284         try
    285         {
     288
     289        try {
    286290            fileContent = file.getContents();
    287291        }
    288         catch (CoreException e)
    289         {
     292        catch (CoreException e) {
    290293            Assert.fail("could not fetch contents of the created class");
    291294        }
    292295
    293296        byte[] buf = new byte[expectedContent.length()];
    294        
    295         try
    296         {
     297
     298        try {
    297299            fileContent.read(buf);
    298300            int i = fileContent.read();
    299301
    300             if (-1 != i)
    301             {
     302            if (-1 != i) {
    302303                Assert.fail("file is to longer than expected");
    303304            }
    304305        }
    305         catch (IOException e)
    306         {
     306        catch (IOException e) {
    307307            Assert.fail("could not read contents of the file");
    308308        }
     309
    309310        return expectedContent.equals(new String(buf));
    310311    }
    311    
    312     /**
    313      * convinience method to 'convert' a string into a InputStream.
    314      * 
     312
     313    /**
     314     * convenience method to 'convert' a string into a InputStream.
     315     *
    315316     * @return an input stream that have the same content as the provided string
    316317     */
    317     public static InputStream getByteStream(String content)
    318     {
     318    public static InputStream getByteStream(String content) {
    319319        return new ByteArrayInputStream(content.getBytes());
    320320    }
    321    
    322     /**
    323      * Convinience 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 invocing 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     *
    327327     * @param folder the folder where to look
    328328     * @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;
    351344                }
    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() + "'");
    356350        return null; /* this is not happening */
    357 
    358351    }
    359352
    360353    public static boolean isAutoBuilding() {
    361354        IWorkspace workspace = ResourcesPlugin.getWorkspace();
    362        
     355
    363356        boolean isAutoBuilding = workspace.isAutoBuilding();
    364        
     357
    365358        return isAutoBuilding;
    366359    }
     
    368361    public static void setAutobuilding(boolean autoBuilding) {
    369362        IWorkspace workspace = ResourcesPlugin.getWorkspace();
    370        
     363
    371364        IWorkspaceDescription workspaceDescription = workspace.getDescription();
    372        
     365
    373366        workspaceDescription.setAutoBuilding(autoBuilding);
    374367    }
     
    376369    public static void listAllProjects() {
    377370        IModelicaRoot modelicaRoot = ModelicaCore.getModelicaRoot();
    378        
     371
    379372        IModelicaProject[] allProjects = modelicaRoot.getProjects();
    380        
     373
    381374        for (IModelicaProject modelicaProject : allProjects) {
    382375            String name = modelicaProject.getElementName();
    383            
     376
    384377            System.out.println(name);
    385378        }
    386379    }
    387    
     380
    388381    public static void goThroughStdlib(Collection<? extends IModelicaElement> aList) {
    389382        if (aList == null) {
    390383            IModelicaRoot modelicaRoot = ModelicaCore.getModelicaRoot();
    391            
     384
    392385            IStandardLibrary stdlib = modelicaRoot.getStandardLibrary(null);
    393            
     386
    394387            try {
    395388                aList = stdlib.getPackages();
     
    408401                    IModelicaClass cls = (IModelicaClass)elem;
    409402                    String name = cls.getFullName();
    410                    
     403
    411404                    System.out.println("Considering " + name);
    412                    
     405
    413406                    boolean isEncapsulated = cls.isEncapsulated();
    414                    
     407
    415408                    if (isEncapsulated) {
    416409                        System.out.println(name + " is indeed encapsulated!");
     
    419412                        //System.out.println(pkg.getFullName() + " is not encapsulated.");
    420413                    }
    421                    
     414
    422415                    Collection<? extends IModelicaElement> children = cls.getChildren();
    423                    
     416
    424417                    if (children != null) {
    425418                        goThroughStdlib(children);
Note: See TracChangeset for help on using the changeset viewer.