migration parser - transactions support improved
[fedora-idea.git] / ExtendedApi / src / com / intellij / testFramework / UsefulTestCase.java
blobfb27648943d13a6f1a7163106d62993e90467a3f
1 /*
2 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
3 */
4 package com.intellij.testFramework;
6 import com.intellij.codeInsight.CodeInsightSettings;
7 import com.intellij.openapi.Disposable;
8 import com.intellij.openapi.application.ApplicationManager;
9 import com.intellij.openapi.fileTypes.StdFileTypes;
10 import com.intellij.openapi.util.Disposer;
11 import com.intellij.openapi.util.JDOMExternalizable;
12 import com.intellij.openapi.util.JDOMUtil;
13 import com.intellij.openapi.util.io.FileUtil;
14 import com.intellij.openapi.util.text.StringUtil;
15 import com.intellij.psi.codeStyle.CodeStyleSettings;
16 import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
17 import com.intellij.util.Consumer;
18 import com.intellij.util.Function;
19 import com.intellij.util.containers.ContainerUtil;
20 import gnu.trove.THashSet;
21 import junit.framework.AssertionFailedError;
22 import junit.framework.TestCase;
23 import org.jdom.Element;
24 import org.jetbrains.annotations.NonNls;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.lang.reflect.Field;
29 import java.lang.reflect.Modifier;
30 import java.util.*;
32 /**
33 * @author peter
35 public abstract class UsefulTestCase extends TestCase {
36 protected final Disposable myTestRootDisposable = new Disposable() {
37 public void dispose() {
40 private static final String DEFAULT_SETTINGS_EXTERNALIZED;
41 private static CodeStyleSettings myOldCodeStyleSettings;
43 static {
44 try {
45 CodeInsightSettings defaultSettings = new CodeInsightSettings();
46 Element oldS = new Element("temp");
47 defaultSettings.writeExternal(oldS);
48 DEFAULT_SETTINGS_EXTERNALIZED = JDOMUtil.writeElement(oldS, "\n");
50 catch (Exception e) {
51 throw new RuntimeException(e);
55 protected void tearDown() throws Exception {
56 Disposer.dispose(myTestRootDisposable);
57 super.tearDown();
60 protected void checkForSettingsDamage() throws Exception {
61 if (!isPerformanceTest() && ApplicationManager.getApplication() != null) {
62 final CodeInsightSettings settings = CodeInsightSettings.getInstance();
63 Element newS = new Element("temp");
64 settings.writeExternal(newS);
65 assertEquals("Code insight settings damaged", DEFAULT_SETTINGS_EXTERNALIZED, JDOMUtil.writeElement(newS, "\n"));
68 CodeStyleSettings codeStyleSettings = getCurrentCodeStyleSettings();
69 codeStyleSettings.getIndentOptions(StdFileTypes.JAVA);
70 checkSettingsEqual(myOldCodeStyleSettings, codeStyleSettings, "Code style settings damaged");
71 codeStyleSettings.clearCodeStyleSettings();
72 myOldCodeStyleSettings = null;
76 protected void storeSettings() {
77 if (!isPerformanceTest() && ApplicationManager.getApplication() != null) {
78 myOldCodeStyleSettings = getCurrentCodeStyleSettings().clone();
79 myOldCodeStyleSettings.getIndentOptions(StdFileTypes.JAVA);
83 protected CodeStyleSettings getCurrentCodeStyleSettings() {
84 return CodeStyleSettingsManager.getInstance().getCurrentSettings();
87 protected Disposable getTestRootDisposable() {
88 return myTestRootDisposable;
91 @NonNls
92 public static String toString(Collection<?> collection) {
93 if (collection.isEmpty()) {
94 return "<empty>";
97 final StringBuilder builder = new StringBuilder();
98 for (final Object o : collection) {
99 if (o instanceof THashSet) {
100 builder.append(new TreeSet<Object>((Collection<Object>)o));
102 else {
103 builder.append(o);
105 builder.append("\n");
107 return builder.toString();
110 public static <T> void assertOrderedEquals(T[] actual, T... expected) {
111 assertOrderedEquals(Arrays.asList(actual), expected);
114 public static <T> void assertOrderedEquals(Collection<T> actual, T... expected) {
115 assertNotNull(actual);
116 assertNotNull(expected);
117 assertOrderedEquals(actual, Arrays.asList(expected));
120 public static <T> void assertOrderedEquals(final Collection<? extends T> actual, final Collection<? extends T> expected) {
121 if (!new ArrayList<T>(actual).equals(new ArrayList<T>(expected))) {
122 assertEquals(toString(expected), toString(actual));
123 fail();
127 public static <T> void assertOrderedCollection(T[] collection, Consumer<T>... checkers) {
128 assertNotNull(collection);
129 assertOrderedCollection(Arrays.asList(collection), checkers);
132 public static <T> void assertSameElements(T[] collection, T... expected) {
133 assertSameElements(Arrays.asList(collection), expected);
136 public static <T> void assertSameElements(Collection<? extends T> collection, T... expected) {
137 if (collection.size() != expected.length || !new HashSet<T>(Arrays.asList(expected)).equals(new HashSet<T>(collection))) {
138 assertEquals(toString(expected, "\n"), toString(collection, "\n"));
139 assertEquals(new HashSet<T>(Arrays.asList(expected)), new HashSet<T>(collection));
144 public static String toString(Object[] collection, String separator) {
145 return toString(Arrays.asList(collection), separator);
148 public static String toString(Collection<?> collection, String separator) {
149 List<String> list = ContainerUtil.map2List(collection, new Function<Object,String>() {
150 public String fun(final Object o) {
151 return String.valueOf(o);
154 Collections.sort(list);
155 StringBuilder builder = new StringBuilder();
156 boolean flag = false;
157 for (final String o : list) {
158 if (flag) {
159 builder.append(separator);
161 builder.append(o);
162 flag = true;
164 return builder.toString();
167 public static <T> void assertOrderedCollection(Collection<? extends T> collection, Consumer<T>... checkers) {
168 assertNotNull(collection);
169 if (collection.size() != checkers.length) {
170 fail(toString(collection));
172 int i = 0;
173 for (final T actual : collection) {
174 try {
175 checkers[i].consume(actual);
177 catch (AssertionFailedError e) {
178 System.out.println(i + ": " + actual);
179 throw e;
181 i++;
185 public static <T> void assertUnorderedCollection(T[] collection, Consumer<T>... checkers) {
186 assertUnorderedCollection(Arrays.asList(collection), checkers);
189 public static <T> void assertUnorderedCollection(Collection<? extends T> collection, Consumer<T>... checkers) {
190 assertNotNull(collection);
191 if (collection.size() != checkers.length) {
192 fail(toString(collection));
194 Set<Consumer<T>> checkerSet = new HashSet<Consumer<T>>(Arrays.asList(checkers));
195 int i = 0;
196 Throwable lastError = null;
197 for (final T actual : collection) {
198 boolean flag = true;
199 for (final Consumer<T> condition : checkerSet) {
200 Throwable error = accepts(condition, actual);
201 if (error == null) {
202 checkerSet.remove(condition);
203 flag = false;
204 break;
206 else {
207 lastError = error;
210 if (flag) {
211 lastError.printStackTrace();
212 fail("Incorrect element(" + i + "): " + actual);
214 i++;
218 private static <T> Throwable accepts(final Consumer<T> condition, final T actual) {
219 try {
220 condition.consume(actual);
221 return null;
223 catch (Throwable e) {
224 return e;
228 public static <T> T assertInstanceOf(Object o, Class<T> aClass) {
229 assertNotNull(o);
230 assertTrue(o.getClass().getName(), aClass.isInstance(o));
231 return (T)o;
234 public static <T> T assertOneElement(Collection<T> collection) {
235 assertNotNull(collection);
236 assertEquals(toString(collection), 1, collection.size());
237 return collection.iterator().next();
240 public static <T> T assertOneElement(T[] ts) {
241 assertNotNull(ts);
242 assertEquals(1, ts.length);
243 return ts[0];
246 public static void printThreadDump() {
247 final Map<Thread,StackTraceElement[]> traces = Thread.getAllStackTraces();
248 for (final Map.Entry<Thread, StackTraceElement[]> entry : traces.entrySet()) {
249 System.out.println("\n" + entry.getKey().getName() + "\n");
250 final StackTraceElement[] value = entry.getValue();
251 for (final StackTraceElement stackTraceElement : value) {
252 System.out.println(stackTraceElement);
257 public static void assertEmpty(final Object[] array) {
258 assertOrderedEquals(array);
261 public static void assertEmpty(final Collection<?> collection) {
262 assertOrderedEquals(collection);
265 protected <T extends Disposable> T disposeOnTearDown(final T disposable) {
266 Disposer.register(myTestRootDisposable, disposable);
267 return disposable;
270 public static void assertSameLines(String expected, String actual) {
271 String expectedText = StringUtil.convertLineSeparators(expected.trim());
272 String actualText = StringUtil.convertLineSeparators(actual.trim());
273 assertEquals(expectedText, actualText);
276 protected String getTestName(boolean lowercaseFirstLetter) {
277 String name = getName();
278 assertTrue(name.startsWith("test"));
279 name = name.substring("test".length());
280 if (lowercaseFirstLetter) {
281 name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
283 return name;
286 protected String getTestDirectoryName() {
287 final String testName = getTestName(true);
288 return testName.replaceAll("_.*", "");
291 protected static void assertSameLinesWithFile(final String filePath, final String actualText) {
292 String fileText;
293 try {
294 final FileReader reader = new FileReader(filePath);
295 fileText = FileUtil.loadTextAndClose(reader);
297 catch (IOException e) {
298 throw new RuntimeException(e);
300 assertSameLines(fileText, actualText);
303 public static void clearFields(final Object test) throws IllegalAccessException {
304 Class aClass = test.getClass();
305 while (aClass != null) {
306 clearDeclaredFields(test, aClass);
307 aClass = aClass.getSuperclass();
311 public static void clearDeclaredFields(Object test, Class aClass) throws IllegalAccessException {
312 if (aClass == null) return;
313 for (final Field field : aClass.getDeclaredFields()) {
314 @NonNls final String name = field.getDeclaringClass().getName();
315 if (!name.startsWith("junit.framework.") && !name.startsWith("com.intellij.testFramework.")) {
316 final int modifiers = field.getModifiers();
317 if ((modifiers & Modifier.FINAL) == 0 && (modifiers & Modifier.STATIC) == 0 && !field.getType().isPrimitive()) {
318 field.setAccessible(true);
319 field.set(test, null);
325 protected static void checkSettingsEqual(JDOMExternalizable expected, JDOMExternalizable settings, String message) throws Exception {
326 if (expected == null) {
327 return;
329 if (settings == null) {
330 return;
332 Element oldS = new Element("temp");
333 expected.writeExternal(oldS);
334 Element newS = new Element("temp");
335 settings.writeExternal(newS);
337 String newString = JDOMUtil.writeElement(newS, "\n");
338 String oldString = JDOMUtil.writeElement(oldS, "\n");
339 assertEquals(message, oldString, newString);
342 public boolean isPerformanceTest() {
343 String name = getName();
344 return name != null && name.contains("Performance") || getClass().getName().contains("Performance");