KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > PartTester


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.ui.IEditorInput;
15 import org.eclipse.ui.IEditorPart;
16 import org.eclipse.ui.IPersistableElement;
17 import org.eclipse.ui.IPropertyListener;
18 import org.eclipse.ui.IViewPart;
19 import org.eclipse.ui.IWorkbenchPart;
20 import org.eclipse.ui.IWorkbenchPart2;
21
22 public class PartTester {
23     private PartTester() {
24     }
25     
26     /**
27      * Sanity-check the public interface of the editor. This is called on every editor after it
28      * is fully initiallized, but before it is actually connected to the editor reference or the
29      * layout. Calls as much of the editor's public interface as possible to test for exceptions,
30      * and tests the return values for glaring faults. This does not need to be an exhaustive conformance
31      * test, as it is called every time an editor is opened and it needs to be efficient.
32      * The part should be unmodified when the method exits.
33      *
34      * @param part
35      */

36     public static void testEditor(IEditorPart part) throws Exception JavaDoc {
37         testWorkbenchPart(part);
38         
39         Assert.isTrue(part.getEditorSite() == part.getSite(),
40                 "The part's editor site must be the same as the part's site"); //$NON-NLS-1$
41
IEditorInput input = part.getEditorInput();
42         Assert.isNotNull(input, "The editor input must be non-null"); //$NON-NLS-1$
43
testEditorInput(input);
44         
45         part.isDirty();
46         part.isSaveAsAllowed();
47         part.isSaveOnCloseNeeded();
48     }
49     
50     public static void testEditorInput(IEditorInput input) throws Exception JavaDoc {
51         input.getAdapter(Object JavaDoc.class);
52         
53         // Don't test input.getImageDescriptor() -- the workbench never uses that
54
// method and most editor inputs would fail the test. It should really be
55
// deprecated.
56

57         Assert.isNotNull(input.getName(),
58                 "The editor input must have a non-null name"); //$NON-NLS-1$
59
Assert.isNotNull(input.getToolTipText(),
60                 "The editor input must have a non-null tool tip"); //$NON-NLS-1$
61

62         // Persistable element may be null
63
IPersistableElement persistableElement = input.getPersistable();
64         if (persistableElement != null) {
65             Assert
66                     .isNotNull(persistableElement.getFactoryId(),
67                             "The persistable element for the editor input must have a non-null factory id"); //$NON-NLS-1$
68
}
69     }
70     
71     /**
72      * Sanity-checks a workbench part. Excercises the public interface and tests for any
73      * obviously bogus return values. The part should be unmodified when the method exits.
74      *
75      * @param part
76      * @throws Exception
77      */

78     private static void testWorkbenchPart(IWorkbenchPart part) throws Exception JavaDoc {
79         IPropertyListener testListener = new IPropertyListener() {
80             public void propertyChanged(Object JavaDoc source, int propId) {
81                 
82             }
83         };
84         
85         // Test addPropertyListener
86
part.addPropertyListener(testListener);
87         
88         // Test removePropertyListener
89
part.removePropertyListener(testListener);
90         
91         // Test equals
92
Assert.isTrue(part.equals(part), "A part must be equal to itself"); //$NON-NLS-1$
93
Assert.isTrue(!part.equals(new Integer JavaDoc(32)),
94                 "A part must have a meaningful equals method"); //$NON-NLS-1$
95

96         // Test getAdapter
97
Object JavaDoc partAdapter = part.getAdapter(part.getClass());
98         Assert.isTrue(partAdapter == null || partAdapter == part,
99                 "A part must adapter to itself or return null"); //$NON-NLS-1$
100

101         // Test getTitle
102
Assert.isNotNull(part.getTitle(), "A part's title must be non-null"); //$NON-NLS-1$
103

104         // Test getTitleImage
105
Assert.isNotNull(part.getTitleImage(),
106                 "A part's title image must be non-null"); //$NON-NLS-1$
107

108         // Test getTitleToolTip
109
Assert.isNotNull(part.getTitleToolTip(),
110                 "A part's title tool tip must be non-null"); //$NON-NLS-1$
111

112         // Test toString
113
Assert.isNotNull(part.toString(),
114                 "A part's toString method must return a non-null value"); //$NON-NLS-1$
115

116         // Compute hashCode
117
part.hashCode();
118         
119         if (part instanceof IWorkbenchPart2) {
120             testWorkbenchPart2((IWorkbenchPart2)part);
121         }
122     }
123
124     private static void testWorkbenchPart2(IWorkbenchPart2 part)
125             throws Exception JavaDoc {
126         Assert.isNotNull(part.getContentDescription(),
127                 "A part must return a non-null content description"); //$NON-NLS-1$
128
Assert.isNotNull(part.getPartName(),
129                 "A part must return a non-null part name"); //$NON-NLS-1$
130
}
131     
132     /**
133      * Sanity-check the public interface of a view. This is called on every view after it
134      * is fully initiallized, but before it is actually connected to the part reference or the
135      * layout. Calls as much of the part's public interface as possible without modifying the part
136      * to test for exceptions and check the return values for glaring faults. This does not need
137      * to be an exhaustive conformance test, as it is called every time an editor is opened and
138      * it needs to be efficient.
139      *
140      * @param part
141      */

142     public static void testView(IViewPart part) throws Exception JavaDoc {
143        Assert.isTrue(part.getSite() == part.getViewSite(),
144                 "A part's site must be the same as a part's view site"); //$NON-NLS-1$
145
testWorkbenchPart(part);
146     }
147 }
148
Popular Tags