2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.testFramework
;
18 import com
.intellij
.ide
.util
.treeView
.AbstractTreeNode
;
19 import com
.intellij
.ide
.util
.treeView
.AbstractTreeStructure
;
20 import com
.intellij
.idea
.Bombed
;
21 import com
.intellij
.openapi
.Disposable
;
22 import com
.intellij
.openapi
.application
.ApplicationManager
;
23 import com
.intellij
.openapi
.extensions
.ExtensionPoint
;
24 import com
.intellij
.openapi
.extensions
.ExtensionPointName
;
25 import com
.intellij
.openapi
.extensions
.Extensions
;
26 import com
.intellij
.openapi
.extensions
.ExtensionsArea
;
27 import com
.intellij
.openapi
.util
.Comparing
;
28 import com
.intellij
.openapi
.util
.Condition
;
29 import com
.intellij
.openapi
.util
.Disposer
;
30 import com
.intellij
.openapi
.util
.text
.StringUtil
;
31 import com
.intellij
.util
.Alarm
;
32 import com
.intellij
.util
.SystemProperties
;
33 import com
.intellij
.util
.ui
.UIUtil
;
34 import junit
.framework
.Assert
;
35 import org
.jetbrains
.annotations
.NonNls
;
36 import org
.jetbrains
.annotations
.Nullable
;
39 import javax
.swing
.tree
.DefaultMutableTreeNode
;
40 import javax
.swing
.tree
.TreePath
;
46 public class PlatformTestUtil
{
47 public static <T
> void registerExtension(final ExtensionPointName
<T
> name
, final T t
, final Disposable parentDisposable
) {
48 registerExtension(Extensions
.getRootArea(), name
, t
, parentDisposable
);
51 public static <T
> void registerExtension(final ExtensionsArea area
, final ExtensionPointName
<T
> name
, final T t
, final Disposable parentDisposable
) {
52 final ExtensionPoint
<T
> extensionPoint
= area
.getExtensionPoint(name
.getName());
53 extensionPoint
.registerExtension(t
);
54 Disposer
.register(parentDisposable
, new Disposable() {
55 public void dispose() {
56 extensionPoint
.unregisterExtension(t
);
61 protected static String
toString(Object node
) {
62 if (node
instanceof AbstractTreeNode
) {
63 return ((AbstractTreeNode
)node
).getTestPresentation();
65 else if (node
== null) {
69 return node
.toString();
73 public static String
print(JTree tree
, boolean withSelection
) {
74 return print(tree
, withSelection
, null);
77 public static String
print(JTree tree
, boolean withSelection
, Condition
<String
> nodePrintCondition
) {
78 StringBuffer buffer
= new StringBuffer();
79 Object root
= tree
.getModel().getRoot();
80 printImpl(tree
, root
, buffer
, 0, withSelection
, nodePrintCondition
);
81 return buffer
.toString();
85 private static void printImpl(JTree tree
, Object root
, StringBuffer buffer
, int level
, boolean withSelection
, @Nullable Condition
<String
> nodePrintCondition
) {
86 DefaultMutableTreeNode defaultMutableTreeNode
= (DefaultMutableTreeNode
)root
;
89 final Object userObject
= defaultMutableTreeNode
.getUserObject();
91 if (userObject
!= null) {
92 nodeText
= toString(userObject
);
95 nodeText
= defaultMutableTreeNode
+ "";
99 if (nodePrintCondition
!= null && !nodePrintCondition
.value(nodeText
)) return;
101 boolean expanded
= tree
.isExpanded(new TreePath(defaultMutableTreeNode
.getPath()));
102 StringUtil
.repeatSymbol(buffer
, ' ', level
);
103 if (expanded
&& !defaultMutableTreeNode
.isLeaf()) {
107 if (!expanded
&& !defaultMutableTreeNode
.isLeaf()) {
111 final boolean selected
= tree
.getSelectionModel().isPathSelected(new TreePath(defaultMutableTreeNode
.getPath()));
113 if (withSelection
&& selected
) {
118 buffer
.append(nodeText
);
120 if (withSelection
&& selected
) {
125 int childCount
= tree
.getModel().getChildCount(root
);
127 for (int i
= 0; i
< childCount
; i
++) {
128 printImpl(tree
, tree
.getModel().getChild(root
, i
), buffer
, level
+ 1, withSelection
, nodePrintCondition
);
133 public static void assertTreeEqual(JTree tree
, @NonNls String expected
) {
134 assertTreeEqual(tree
, expected
, false);
137 public static void assertTreeEqual(JTree tree
, String expected
, boolean checkSelected
) {
138 String treeStringPresentation
= print(tree
, checkSelected
);
139 Assert
.assertEquals(expected
, treeStringPresentation
);
142 public static void waitForAlarm(final int delay
) throws InterruptedException
{
143 final boolean[] invoked
= new boolean[]{false};
144 final Alarm alarm
= new Alarm(Alarm
.ThreadToUse
.SWING_THREAD
);
145 alarm
.addRequest(new Runnable() {
147 ApplicationManager
.getApplication().invokeLater(new Runnable() {
149 alarm
.addRequest(new Runnable() {
159 UIUtil
.dispatchAllInvocationEvents();
161 while (!invoked
[0]) {
162 UIUtil
.dispatchAllInvocationEvents();
167 private static Date
raidDate(Bombed bombed
) {
168 final Calendar instance
= Calendar
.getInstance();
169 instance
.set(Calendar
.YEAR
, bombed
.year());
170 instance
.set(Calendar
.MONTH
, bombed
.month());
171 instance
.set(Calendar
.DAY_OF_MONTH
, bombed
.day());
172 instance
.set(Calendar
.HOUR_OF_DAY
, bombed
.time());
173 instance
.set(Calendar
.MINUTE
, 0);
174 Date time
= instance
.getTime();
179 public static boolean bombExplodes(Bombed bombedAnnotation
) {
180 Date now
= new Date();
181 return now
.after(raidDate(bombedAnnotation
));
184 public static boolean bombExplodes(int year
, int month
, int day
, int h
, int m
, String who
, String message
) {
185 final Calendar instance
= Calendar
.getInstance();
186 instance
.set(Calendar
.YEAR
, year
);
187 instance
.set(Calendar
.MONTH
, month
);
188 instance
.set(Calendar
.DAY_OF_MONTH
, day
);
189 instance
.set(Calendar
.HOUR_OF_DAY
, h
);
190 instance
.set(Calendar
.MINUTE
, m
);
191 Date time
= instance
.getTime();
192 return isItMe(who
) || new Date().after(time
);
195 public static boolean isRotten(Bombed bomb
) {
196 long bombRotPeriod
= 30L * 24 * 60 * 60 * 1000; // month
197 return new Date().after(new Date(raidDate(bomb
).getTime() + bombRotPeriod
));
200 private static boolean isItMe(final String who
) {
201 return Comparing
.equal(who
, SystemProperties
.getUserName(), false);
204 public static StringBuffer
print(AbstractTreeStructure structure
,
207 Comparator comparator
,
210 StringBuffer buffer
= new StringBuffer();
211 doPrint(buffer
, currentLevel
, node
, structure
, comparator
, maxRowCount
, 0, paddingChar
);
215 private static int doPrint(StringBuffer buffer
,
218 AbstractTreeStructure structure
,
219 Comparator comparator
,
223 if (currentLine
>= maxRowCount
&& maxRowCount
!= -1) return currentLine
;
225 StringUtil
.repeatSymbol(buffer
, paddingChar
, currentLevel
);
226 buffer
.append(toString(node
)).append("\n");
228 Object
[] children
= structure
.getChildElements(node
);
230 if (comparator
!= null) {
231 ArrayList
<?
> list
= new ArrayList
<Object
>(Arrays
.asList(children
));
232 Collections
.sort(list
, comparator
);
233 children
= list
.toArray(new Object
[list
.size()]);
235 for (Object child
: children
) {
236 currentLine
= doPrint(buffer
, currentLevel
+ 1, child
, structure
, comparator
, maxRowCount
, currentLine
, paddingChar
);
242 public static String
print(Object
[] objects
) {
243 return print(Arrays
.asList(objects
));
246 public static String
print(Collection c
) {
247 StringBuilder result
= new StringBuilder();
248 for (Iterator iterator
= c
.iterator(); iterator
.hasNext();) {
249 Object each
= iterator
.next();
250 result
.append(toString(each
));
251 if (iterator
.hasNext()) {
256 return result
.toString();
259 public static String
print(ListModel model
) {
260 StringBuilder result
= new StringBuilder();
261 for (int i
= 0; i
< model
.getSize(); i
++) {
262 result
.append(toString(model
.getElementAt(i
)));
265 return result
.toString();
268 public static String
print(JTree tree
) {
269 return print(tree
, false);
272 public static void assertTreeStructureEquals(final AbstractTreeStructure treeStructure
, final String expected
) {
273 Assert
.assertEquals(expected
, print(treeStructure
, treeStructure
.getRootElement(), 0, null, -1, ' ').toString());