2 * Copyright 2000-2008 JetBrains s.r.o.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 package org.jetbrains.plugins.groovy.util;
18 import com.intellij.openapi.application.PluginPathManager;
19 import com.intellij.openapi.fileTypes.FileTypeManager;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.util.io.FileUtil;
22 import com.intellij.openapi.util.text.StringUtil;
23 import com.intellij.psi.PsiFile;
24 import com.intellij.psi.PsiFileFactory;
25 import com.intellij.util.IncorrectOperationException;
26 import com.intellij.util.LocalTimeCounter;
27 import org.junit.Assert;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
35 * Utility class, that contains various methods for testing
39 public abstract class TestUtils {
40 public static final String TEMP_FILE = "temp.groovy";
41 public static final String GSP_TEMP_FILE = "temp.gsp";
42 public static final String CARET_MARKER = "<caret>";
43 public static final String BEGIN_MARKER = "<begin>";
44 public static final String END_MARKER = "<end>";
45 public static final String GROOVY_JAR = "groovy-all.jar";
46 public static final String GROOVY_JAR_17 = "groovy-all-1.7.jar";
47 public static final String GROOVY_JAR_18 = "groovy-1.8.0-beta-2.jar";
49 public static String getMockJdkHome() {
50 return getAbsoluteTestDataPath() + "/mockJDK";
53 public static String getMockGroovyLibraryHome() {
54 return getAbsoluteTestDataPath() + "/mockGroovyLib";
57 public static String getMockGroovy1_7LibraryHome() {
58 return getAbsoluteTestDataPath() + "/mockGroovyLib1.7";
61 public static String getMockGroovy1_7LibraryName() {
62 return getMockGroovy1_7LibraryHome()+"/groovy-all-1.7.3.jar";
65 public static String getMockGroovy1_8LibraryHome() {
66 return getAbsoluteTestDataPath() + "/mockGroovyLib1.8";
69 public static String getMockGroovy1_8LibraryName() {
70 return getMockGroovy1_8LibraryHome()+"/"+GROOVY_JAR_18;
73 public static PsiFile createPseudoPhysicalGroovyFile(final Project project, final String text) throws IncorrectOperationException {
74 return createPseudoPhysicalFile(project, TEMP_FILE, text);
78 public static PsiFile createPseudoPhysicalFile(final Project project, final String fileName, final String text) throws IncorrectOperationException {
79 return PsiFileFactory.getInstance(project).createFileFromText(
81 FileTypeManager.getInstance().getFileTypeByFileName(fileName),
83 LocalTimeCounter.currentTime(),
87 public static String getAbsoluteTestDataPath() {
88 return FileUtil.toSystemIndependentName(PluginPathManager.getPluginHomePath("groovy")) + "/testdata/";
91 public static String getTestDataPath() {
92 return FileUtil.toSystemIndependentName(PluginPathManager.getPluginHomePathRelative("groovy")) + "/testdata/";
95 public static String removeBeginMarker(String text) {
96 int index = text.indexOf(BEGIN_MARKER);
97 return text.substring(0, index) + text.substring(index + BEGIN_MARKER.length());
100 public static String removeEndMarker(String text) {
101 int index = text.indexOf(END_MARKER);
102 return text.substring(0, index) + text.substring(index + END_MARKER.length());
105 public static List<String> readInput(String filePath) {
108 content = new String(FileUtil.loadFileText(new File(filePath)));
110 catch (IOException e) {
111 throw new RuntimeException(e);
113 Assert.assertNotNull(content);
115 List<String> input = new ArrayList<String>();
118 content = StringUtil.replace(content, "\r", ""); // for MACs
120 // Adding input before -----
121 while ((separatorIndex = content.indexOf("-----")) >= 0) {
122 input.add(content.substring(0, separatorIndex - 1));
123 content = content.substring(separatorIndex);
124 while (StringUtil.startsWithChar(content, '-')) {
125 content = content.substring(1);
127 if (StringUtil.startsWithChar(content, '\n')) {
128 content = content.substring(1);
131 // Result - after -----
132 if (content.endsWith("\n")) {
133 content = content.substring(0, content.length() - 1);
137 Assert.assertTrue("No data found in source file", input.size() > 0);
138 Assert.assertNotNull("Test output points to null", input.size() > 1);