source: trunk/org.modelica.mdt.test/src/org/modelica/mdt/test/TestNewClassWizard.java @ 123

Last change on this file since 123 was 123, checked in by boris, 19 years ago
  • cosmetic change on generated class files
  • more tests for New Modelica Class Wizard
File size: 21.2 KB
Line 
1/*
2 * This file is part of Modelica Development Tooling.
3 *
4 * Copyright (c) 2005, Link�pings universitet, Department of
5 * Computer and Information Science, PELAB
6 *
7 * All rights reserved.
8 *
9 * (The new BSD license, see also
10 * http://www.opensource.org/licenses/bsd-license.php)
11 *
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
15 * met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 *   notice, this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above copyright
21 *   notice, this list of conditions and the following disclaimer in
22 *   the documentation and/or other materials provided with the
23 *   distribution.
24 *
25 * * Neither the name of Link�pings universitet nor the names of its
26 *   contributors may be used to endorse or promote products derived from
27 *   this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42package org.modelica.mdt.test;
43
44import java.io.IOException;
45import java.io.InputStream;
46
47import org.eclipse.core.resources.IFile;
48import org.eclipse.core.resources.IProject;
49import org.eclipse.core.resources.ResourcesPlugin;
50import org.eclipse.core.runtime.CoreException;
51import org.eclipse.jface.viewers.StructuredSelection;
52import org.eclipse.jface.wizard.IWizard;
53import org.eclipse.swt.widgets.Button;
54import org.eclipse.swt.widgets.Combo;
55import org.eclipse.swt.widgets.Text;
56import org.eclipse.swt.widgets.Widget;
57import org.eclipse.ui.PlatformUI;
58import org.modelica.mdt.core.ModelicaCore;
59import org.modelica.mdt.test.util.Utility;
60import org.modelica.mdt.ui.wizards.NewClassWizard;
61
62import abbot.finder.swt.BasicFinder;
63import abbot.finder.swt.Matcher;
64import abbot.finder.swt.MultipleWidgetsFoundException;
65import abbot.finder.swt.TestHierarchy;
66import abbot.finder.swt.WidgetNotFoundException;
67import abbot.tester.swt.ButtonTester;
68import abbot.tester.swt.ComboTester;
69import abbot.tester.swt.TextTester;
70
71import junit.framework.TestCase;
72
73/**
74 * @author Elmir Jagudin
75 *
76 * Test the New Class Wizard functionality
77 */
78public class TestNewClassWizard extends TestCase
79{
80    private static final String PROJECT_NAME_1 = 
81            TestNewClassWizard.class.getName() + "1";
82    private StructuredSelection fileDestination; 
83   
84    private IProject project;
85    private TextTester ttester;
86    private ButtonTester btester;
87    private ComboTester ctester;
88
89    private Text sourceFolder;
90    private Text className;
91    private Combo classType;
92    private Button initialEquation;
93    private Button partialClass;
94    private Button externalBody;
95   
96    private Button finish;
97   
98
99    @Override
100    //protected void setUp() throws Exception
101    public void setUp() throws Exception
102    {
103        /*
104         * setup project
105         */
106        project = 
107            ModelicaCore.createProject(PROJECT_NAME_1,
108                    PlatformUI.getWorkbench().getActiveWorkbenchWindow());
109        assertNotNull("failed to create project", project);
110       
111        /*
112         * create the selection that points at the root of the created project
113         */
114        fileDestination = 
115            new StructuredSelection(ResourcesPlugin.getWorkspace().getRoot().
116                    getProject(PROJECT_NAME_1));
117       
118        /*
119         * setup testing support objects
120         */
121        ttester = TextTester.getTextTester();
122        btester = ButtonTester.getButtonTester();
123        ctester = new ComboTester();
124               
125    }
126   
127    /**
128     * Compares the content of a file with a provided string
129     * @param file
130     * @param expectedContent
131     * @return true if file's content exactly matches the expectedContent
132     * string
133     */
134    private boolean compareContent(IFile file, String expectedContent)
135    {
136        InputStream fileContent = null;
137       
138        try
139        {
140            fileContent = file.getContents();
141        }
142        catch (CoreException e) 
143        {
144            fail("could not fetch contents of the created class");
145        }
146
147        byte[] buf = new byte[expectedContent.length()];
148       
149        try
150        {
151            fileContent.read(buf);
152            int i = fileContent.read();
153           
154            assertEquals("file is to long", i, -1);
155        }
156        catch (IOException e)
157        {
158            fail("could not read contents of the file");
159        }
160       
161        return expectedContent.equals(new String(buf));
162    }
163
164   
165    public void openWizardAndFetchWidgets()
166    {
167        /*
168         * pop-up the wizard
169         */
170        IWizard wizard = 
171            Utility.openWizard("org.modelica.mdt.NewClassWizard",
172                    fileDestination);
173       
174        assertFalse(wizard.canFinish());
175
176       
177       
178        /* fetch widgets */
179        className = 
180            TextTester.getInstrumentedText(NewClassWizard.CLASS_NAME_TAG);
181        sourceFolder = 
182            TextTester.getInstrumentedText(NewClassWizard.SOURCE_FOLDER_TAG);
183        initialEquation = 
184            ButtonTester.getInstrumentedButton(NewClassWizard.INITIAL_EQUATION_TAG);
185        externalBody =
186            ButtonTester.getInstrumentedButton(NewClassWizard.EXTERNAL_BODY_TAG);
187        partialClass = 
188            ButtonTester.getInstrumentedButton(NewClassWizard.PARTIAL_CLASS_TAG);
189        finish = 
190            Utility.findFinishButton();
191       
192   
193        /*
194         * find classType combo by tag
195         */
196        BasicFinder finder = new BasicFinder(new TestHierarchy
197                (PlatformUI.getWorkbench().getDisplay()));
198       
199        try 
200        {           
201            classType = (Combo)finder.find(new Matcher()
202            {
203                public boolean matches(Widget w) 
204                {
205                    Object tag = w.getData("name");
206                    if (tag == null || !(tag instanceof String))
207                    {
208                        return false;
209                    }
210                                   
211                    return ((String)tag).equals(NewClassWizard.CLASS_TYPE_TAG);
212                }
213                           
214            });
215        } 
216        catch (WidgetNotFoundException e) 
217        {
218            fail("multiple classType combos found " + e.getMessage());
219        } 
220        catch (MultipleWidgetsFoundException e) 
221        {
222            fail("classType combo widget not found " + e.getMessage());
223        }
224       
225        /* make some checks on the state of the wizards */
226        assertEquals("Wrong source folder selected", 
227                sourceFolder.getText(), PROJECT_NAME_1);       
228        assertEquals("Junk present in class name field", 
229                className.getText(), "");
230        assertFalse("initial equation unexpectedly selected",
231                initialEquation.getSelection());
232        assertFalse("partial class unexpectedly selected",
233                partialClass.getSelection());
234        assertFalse("Finish button not disabled", 
235                finish.getEnabled());
236       
237
238    }
239   
240    public void testCreateModel()
241    {
242        openWizardAndFetchWidgets();       
243       
244        /*
245         * create model
246         */
247        ttester.actionEnterText(className, "m1");
248        assertTrue(finish.getEnabled());
249
250        /* wait for the name change to propogate to enable the finish button */
251        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
252        btester.actionClick(finish);
253       
254        while(!project.isOpen()){ Utility.sleep(this, 100); }       
255       
256        /*
257         * check that the generated source code is sane
258         */
259        boolean same = 
260            compareContent(project.getFile("m1.mo"), 
261                "model m1\n"+
262                "\n"+
263                "equation\n"+
264                "\n"+
265                "end m1;");
266        assertTrue("unexpected conted created in the source file", same);
267   
268    }
269       
270    public void testCreateModelWithInitEquation()
271    {
272        openWizardAndFetchWidgets();       
273       
274        /*
275         * create model
276         */
277        ttester.actionEnterText(className, "m2");
278        assertTrue(finish.getEnabled());
279       
280        btester.actionClick(initialEquation);
281       
282        assertTrue(initialEquation.getSelection());
283
284        /* wait for the name change to propogate to enable the finish button */
285        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
286        btester.actionClick(finish);
287       
288       
289        while(!project.isOpen()){ Utility.sleep(this, 100); }
290       
291        /*
292         * check that the generated source code is sane
293         */
294        boolean same = 
295            compareContent(project.getFile("m2.mo"), 
296                "model m2\n"+
297                "\n"+
298                "equation\n"+
299                "\n"+
300                "initial equation\n"+
301                "\n"+
302                "end m2;");
303        assertTrue("unexpected conted created in the source file", same);
304    }
305
306    public void testCreatePartialModel()
307    {
308        openWizardAndFetchWidgets();       
309       
310        String name = "m3";
311        /*
312         * create model
313         */
314        ttester.actionEnterText(className, name);
315        assertTrue(finish.getEnabled());
316       
317        btester.actionClick(partialClass);
318       
319        assertTrue(partialClass.getSelection());
320
321        /* wait for the name change to propogate to enable the finish button */
322        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
323        btester.actionClick(finish);
324       
325       
326        while(!project.isOpen()){ Utility.sleep(this, 100); }
327       
328        /*
329         * check that the generated source code is sane
330         */
331        boolean same = 
332            compareContent(project.getFile(name + ".mo"), 
333                "partial model "+ name +"\n"+
334                "\n"+
335                "equation\n"+
336                "\n"+
337                "end "+ name + ";");
338        assertTrue("unexpected conted created in the source file", same);
339   
340    }
341
342    public void testCreatePartialModelWithInitBlock()
343    {
344        openWizardAndFetchWidgets();       
345       
346        String name = "m4";
347        /*
348         * create model
349         */
350        ttester.actionEnterText(className, name);
351        assertTrue(finish.getEnabled());
352       
353        btester.actionClick(partialClass);     
354        assertTrue(partialClass.getSelection());
355
356        btester.actionClick(initialEquation);       
357        assertTrue(initialEquation.getSelection());
358       
359       
360        /* wait for the name change to propogate to enable the finish button */
361        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
362        btester.actionClick(finish);
363       
364       
365        while(!project.isOpen()){ Utility.sleep(this, 100); }
366       
367        /*
368         * check that the generated source code is sane
369         */
370        boolean same = 
371            compareContent(project.getFile(name + ".mo"), 
372                "partial model "+ name +"\n"+
373                "\n"+
374                "equation\n"+
375                "\n"+
376                "initial equation\n"+
377                "\n"+
378                "end "+ name + ";");
379        assertTrue("unexpected conted created in the source file", same);
380    }
381
382    public void testCreateClass()
383    {
384        openWizardAndFetchWidgets();       
385       
386        String name = "c1";
387       
388        /*
389         * create class
390         */
391        ttester.actionEnterText(className, name);
392        ctester.actionSelectItem(classType, "class");
393       
394       
395        /* wait for the name change to propogate to enable the finish button */
396        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
397        btester.actionClick(finish);
398       
399       
400        while(!project.isOpen()){ Utility.sleep(this, 100); }
401       
402        /*
403         * check that the generated source code is sane
404         */
405        boolean same = 
406            compareContent(project.getFile(name + ".mo"), 
407                "class "+ name +"\n"+
408                "\n"+
409                "equation\n"+
410                "\n"+
411                "end "+ name + ";");
412        assertTrue("unexpected conted created in the source file", same);
413    }
414
415    public void testCreateClassWithInitBlock()
416    {
417        openWizardAndFetchWidgets();
418       
419        String name = "c2";
420       
421        /*
422         * create class
423         */
424        ttester.actionEnterText(className, name);
425        ctester.actionSelectItem(classType, "class");
426        btester.actionClick(initialEquation);
427       
428       
429        /* wait for the name change to propogate to enable the finish button */
430        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
431        btester.actionClick(finish);
432       
433       
434        while(!project.isOpen()){ Utility.sleep(this, 100); }
435       
436        /*
437         * check that the generated source code is sane
438         */
439        boolean same = 
440            compareContent(project.getFile(name + ".mo"), 
441                "class "+ name +"\n"+
442                "\n"+
443                "equation\n"+
444                "\n"+
445                "initial equation\n"+
446                "\n"+
447                "end "+ name + ";");
448        assertTrue("unexpected conted created in the source file", same);
449       
450    }
451
452    public void testCreatePartialClass()
453    {
454        openWizardAndFetchWidgets();
455       
456        String name = "c3";
457       
458        /*
459         * create class
460         */
461        ttester.actionEnterText(className, name);
462        ctester.actionSelectItem(classType, "class");
463        btester.actionClick(partialClass);
464       
465       
466        /* wait for the name change to propogate to enable the finish button */
467        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
468        btester.actionClick(finish);
469       
470       
471        while(!project.isOpen()){ Utility.sleep(this, 100); }
472       
473        /*
474         * check that the generated source code is sane
475         */
476        boolean same = 
477            compareContent(project.getFile(name + ".mo"), 
478                "partial class "+ name +"\n"+
479                "\n"+
480                "equation\n"+
481                "\n"+
482                "end "+ name + ";");
483        assertTrue("unexpected conted created in the source file", same);
484    }
485
486    public void testCreatePartialClassWithInitBlock()
487    {
488        openWizardAndFetchWidgets();
489       
490        String name = "c4";
491       
492        /*
493         * create class
494         */
495        ttester.actionEnterText(className, name);
496        ctester.actionSelectItem(classType, "class");
497        btester.actionClick(partialClass);
498        btester.actionClick(initialEquation);
499       
500       
501        /* wait for the name change to propogate to enable the finish button */
502        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
503        btester.actionClick(finish);
504       
505       
506        while(!project.isOpen()){ Utility.sleep(this, 100); }
507       
508        /*
509         * check that the generated source code is sane
510         */
511        boolean same = 
512            compareContent(project.getFile(name + ".mo"), 
513                "partial class "+ name +"\n"+
514                "\n"+
515                "equation\n"+
516                "\n"+
517                "initial equation\n"+
518                "\n"+               
519                "end "+ name + ";");
520        assertTrue("unexpected conted created in the source file", same);
521       
522    }
523   
524   
525    public void testCreateConnector()
526    {
527        openWizardAndFetchWidgets();
528       
529        String name = "con1";
530       
531        /*
532         * create class
533         */
534        ttester.actionEnterText(className, name);
535        ctester.actionSelectItem(classType, "connector");
536       
537       
538        /* wait for the name change to propogate to enable the finish button */
539        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
540        btester.actionClick(finish);
541       
542       
543        while(!project.isOpen()){ Utility.sleep(this, 100); }
544       
545        /*
546         * check that the generated source code is sane
547         */
548        boolean same = 
549            compareContent(project.getFile(name + ".mo"), 
550                "connector "+ name +"\n" +
551                "\n" +
552                "end "+ name + ";");
553        assertTrue("unexpected conted created in the source file", same);
554    }
555
556    public void testCreatePartialConnector()
557    {
558        openWizardAndFetchWidgets();
559       
560        String name = "con2";
561       
562        /*
563         * create class
564         */
565        ttester.actionEnterText(className, name);
566        ctester.actionSelectItem(classType, "connector");
567        btester.actionClick(partialClass);     
568       
569       
570        /* wait for the name change to propogate to enable the finish button */
571        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
572        btester.actionClick(finish);
573       
574       
575        while(!project.isOpen()){ Utility.sleep(this, 100); }
576       
577        /*
578         * check that the generated source code is sane
579         */
580        boolean same = 
581            compareContent(project.getFile(name + ".mo"), 
582                "partial connector "+ name +"\n" +
583                "\n" +
584                "end "+ name + ";");
585        assertTrue("unexpected conted created in the source file", same);
586       
587    }
588
589    public void testCreateBlock()
590    {
591        openWizardAndFetchWidgets();
592       
593        String name = "b1";
594       
595        /*
596         * create class
597         */
598        ttester.actionEnterText(className, name);
599        ctester.actionSelectItem(classType, "block");
600
601       
602       
603        /* wait for the name change to propogate to enable the finish button */
604        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
605        btester.actionClick(finish);
606       
607       
608        while(!project.isOpen()){ Utility.sleep(this, 100); }
609       
610        /*
611         * check that the generated source code is sane
612         */
613        boolean same = 
614            compareContent(project.getFile(name + ".mo"), 
615                "block "+ name +"\n" +
616                "\n" +
617                "equation\n"+
618                "\n"+
619                "end "+ name + ";");
620        assertTrue("unexpected conted created in the source file", same);
621       
622    }
623    public void testCreateBlockWithInitBlock()
624    {
625        openWizardAndFetchWidgets();
626       
627        String name = "b2";
628       
629        /*
630         * create class
631         */
632        ttester.actionEnterText(className, name);
633        ctester.actionSelectItem(classType, "block");
634        btester.actionClick(initialEquation);       
635
636       
637       
638        /* wait for the name change to propogate to enable the finish button */
639        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
640        btester.actionClick(finish);
641       
642       
643        while(!project.isOpen()){ Utility.sleep(this, 100); }
644       
645        /*
646         * check that the generated source code is sane
647         */
648        boolean same = 
649            compareContent(project.getFile(name + ".mo"), 
650                "block "+ name +"\n" +
651                "\n" +
652                "equation\n"+
653                "\n"+
654                "initial equation\n"+
655                "\n"+                               
656                "end "+ name + ";");
657        assertTrue("unexpected conted created in the source file", same);
658       
659    }
660
661    public void testCreatePartialBlock()
662    {
663        openWizardAndFetchWidgets();
664       
665        String name = "b3";
666       
667        /*
668         * create class
669         */
670        ttester.actionEnterText(className, name);
671        ctester.actionSelectItem(classType, "block");
672        btester.actionClick(partialClass);     
673
674       
675       
676        /* wait for the name change to propogate to enable the finish button */
677        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
678        btester.actionClick(finish);
679       
680       
681        while(!project.isOpen()){ Utility.sleep(this, 100); }
682       
683        /*
684         * check that the generated source code is sane
685         */
686        boolean same = 
687            compareContent(project.getFile(name + ".mo"), 
688                "partial block "+ name +"\n" +
689                "\n" +
690                "equation\n"+
691                "\n"+                               
692                "end "+ name + ";");
693        assertTrue("unexpected conted created in the source file", same);
694   
695    }
696    public void testCreatePartialBlockWithInitBlock()
697    {
698        openWizardAndFetchWidgets();
699       
700        String name = "b4";
701       
702        /*
703         * create class
704         */
705        ttester.actionEnterText(className, name);
706        ctester.actionSelectItem(classType, "block");
707        btester.actionClick(partialClass);
708        btester.actionClick(initialEquation);       
709
710       
711       
712        /* wait for the name change to propogate to enable the finish button */
713        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
714        btester.actionClick(finish);
715       
716       
717        while(!project.isOpen()){ Utility.sleep(this, 100); }
718       
719        /*
720         * check that the generated source code is sane
721         */
722        boolean same = 
723            compareContent(project.getFile(name + ".mo"), 
724                "partial block "+ name +"\n" +
725                "\n" +
726                "equation\n"+
727                "\n"+
728                "initial equation\n"+
729                "\n"+               
730                "end "+ name + ";");
731        assertTrue("unexpected conted created in the source file", same);
732    }
733
734    public void testCreateType()
735    {
736        openWizardAndFetchWidgets();
737       
738        String name = "t1";
739       
740        /*
741         * create class
742         */
743        ttester.actionEnterText(className, name);
744        ctester.actionSelectItem(classType, "type");
745
746       
747       
748        /* wait for the name change to propogate to enable the finish button */
749        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
750        btester.actionClick(finish);
751       
752       
753        while(!project.isOpen()){ Utility.sleep(this, 100); }
754       
755        /*
756         * check that the generated source code is sane
757         */
758        boolean same = 
759            compareContent(project.getFile(name + ".mo"), 
760                "type "+ name +"\n" +
761                ";");
762        assertTrue("unexpected conted created in the source file", same);
763       
764    }
765
766    public void testCreateFunction()
767    {
768        openWizardAndFetchWidgets();
769       
770        String name = "f1";
771       
772        /*
773         * create class
774         */
775        ctester.actionSelectItem(classType, "function");
776        ttester.actionEnterText(className, name);
777       
778       
779        /* wait for the name change to propogate to enable the finish button */
780        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
781        btester.actionClick(finish);
782       
783       
784        while(!project.isOpen()){ Utility.sleep(this, 100); }
785       
786        /*
787         * check that the generated source code is sane
788         */
789        boolean same = 
790            compareContent(project.getFile(name + ".mo"), 
791                "function "+ name +"\n" +
792                "\n" +
793                "algorithm\n"+
794                "\n"+               
795                "end "+ name + ";");
796        assertTrue("unexpected conted created in the source file", same);
797    }
798
799    public void testCreateFunctionWithExternalBody()
800    {
801        openWizardAndFetchWidgets();
802       
803        String name = "f2";
804       
805        /*
806         * create class
807         */
808        ctester.actionSelectItem(classType, "function");
809        ttester.actionEnterText(className, name);
810
811        /*
812         * wait for the class type change to propogate to the
813         * external body  checkboc
814         */
815        while (!externalBody.getEnabled()) { Utility.sleep(this, 100); }
816
817        btester.actionClick(externalBody);     
818       
819        /* wait for the name change to propogate to enable the finish button */
820        while (!finish.getEnabled()) { Utility.sleep(this, 100); }
821        btester.actionClick(finish);
822       
823       
824        while(!project.isOpen()){ Utility.sleep(this, 100); }
825       
826        /*
827         * check that the generated source code is sane
828         */
829        boolean same = 
830            compareContent(project.getFile(name + ".mo"), 
831                "function "+ name +"\n" +
832                "\n" +
833                "external\n"+
834                "end "+ name + ";");
835        assertTrue("unexpected conted created in the source file", same);
836       
837    }
838
839     //some ideas for more tests
840        /*
841         * TODO enter some text into source folder field and
842         * check the error/warning messages
843         */
844       
845        /*
846         * TODO test to create a class in a sub-directory
847         */
848}
Note: See TracBrowser for help on using the repository browser.