2 * Copyright 2000-2012 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 org.jetbrains.ether;
18 import com.intellij.openapi.diagnostic.Logger;
19 import junit.framework.TestCase;
20 import junitx.framework.FileAssert;
21 import org.apache.log4j.Level;
22 import org.apache.log4j.PropertyConfigurator;
23 import org.jetbrains.annotations.NonNls;
24 import org.jetbrains.annotations.Nullable;
25 import org.jetbrains.jps.Project;
26 import org.jetbrains.jps.api.CanceledStatus;
27 import org.jetbrains.jps.idea.IdeaProjectLoader;
28 import org.jetbrains.jps.incremental.AllProjectScope;
29 import org.jetbrains.jps.incremental.BuilderRegistry;
30 import org.jetbrains.jps.incremental.FSState;
31 import org.jetbrains.jps.incremental.IncProjectBuilder;
32 import org.jetbrains.jps.incremental.storage.BuildDataManager;
33 import org.jetbrains.jps.incremental.storage.ProjectTimestamps;
34 import org.jetbrains.jps.server.ProjectDescriptor;
37 import java.util.Properties;
40 * Created by IntelliJ IDEA.
44 * To change this template use File | Settings | File Templates.
46 public abstract class IncrementalTestCase extends TestCase {
47 private static class RootStripper {
50 void setRoot(final String root) {
54 String strip(final String s){
55 if (s.startsWith(root)) {
56 return s.substring(root.length());
64 Logger.setFactory(new Logger.Factory() {
66 public Logger getLoggerInstance(String category) {
67 final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(category);
69 final boolean affectedLogger = category.equals("#org.jetbrains.jps.incremental.java.JavaBuilder") ||
70 category.equals("#org.jetbrains.jps.incremental.IncProjectBuilder");
74 public boolean isDebugEnabled() {
75 return affectedLogger;
79 public void debug(@NonNls String message) {
83 public void debug(@Nullable Throwable t) {
87 public void debug(@NonNls String message, @Nullable Throwable t) {
91 public void error(@NonNls String message, @Nullable Throwable t, @NonNls String... details) {
95 public void info(@NonNls String message) {
97 logger.info(stripper.strip(message));
102 public void info(@NonNls String message, @Nullable Throwable t) {
106 public void warn(@NonNls String message, @Nullable Throwable t) {
110 public void setLevel(Level level) {
117 private static RootStripper stripper = new RootStripper();
119 private final String groupName;
120 private final String tempDir = System.getProperty("java.io.tmpdir");
122 private String baseDir;
123 private String workDir;
125 protected IncrementalTestCase(final String name) throws Exception {
131 protected void setUp() throws Exception {
134 baseDir = "jps/testData" + File.separator + "incremental" + File.separator;
136 for (int i = 0; ; i++) {
137 final File tmp = new File(tempDir + File.separator + "__temp__" + i);
139 workDir = tmp.getPath() + File.separator;
144 copy(new File(getBaseDir()), new File(getWorkDir()));
148 protected void tearDown() throws Exception {
150 // delete(new File(workDir));
153 private String getDir(final String prefix) {
154 final String name = getName();
156 assert (name.startsWith("test"));
158 final String result = Character.toLowerCase(name.charAt("test".length())) + name.substring("test".length() + 1);
160 return prefix + groupName + File.separator + result;
163 private String getBaseDir() {
164 return getDir(baseDir);
167 private String getWorkDir() {
168 return getDir(workDir);
171 private void delete(final File file) throws Exception {
172 if (file.isDirectory()) {
173 final File[] files = file.listFiles();
176 for (File f : files) {
182 if (!file.delete()) throw new IOException("could not delete file or directory " + file.getPath());
185 private static void copy(final File input, final File output) throws Exception {
186 if (input.isDirectory()) {
187 if (output.mkdirs()) {
188 final File[] files = input.listFiles();
191 for (File f : files) {
192 copy(f, new File(output.getPath() + File.separator + f.getName()));
197 throw new IOException("unable to create directory " + output.getPath());
200 else if (input.isFile()) {
201 final FileReader in = new FileReader(input);
202 final FileWriter out = new FileWriter(output);
206 while ((c = in.read()) != -1) out.write(c);
215 private void modify() throws Exception {
216 final File dir = new File(getBaseDir());
217 final File[] files = dir.listFiles(new FileFilter() {
218 public boolean accept(final File pathname) {
219 final String name = pathname.getName();
221 return name.endsWith(".java.new") || name.endsWith(".java.remove");
225 for (File input : files) {
226 final String name = input.getName();
228 final boolean copy = name.endsWith(".java.new");
229 final String postfix = name.substring(0, name.length() - (copy ? ".new" : ".remove").length());
230 final int pathSep = postfix.indexOf("$");
231 final String basename = pathSep == -1 ? postfix : postfix.substring(pathSep + 1);
233 getWorkDir() + File.separator + (pathSep == -1 ? "src" : postfix.substring(0, pathSep).replace('-', File.separatorChar));
234 final File output = new File(path + File.separator + basename);
245 private void initLoggers() {
246 final Properties properties = new Properties();
248 properties.setProperty("log4j.rootCategory", "INFO, A1");
249 properties.setProperty("log4j.appender.A1", "org.apache.log4j.FileAppender");
250 properties.setProperty("log4j.appender.A1.file", getWorkDir() + ".log");
251 properties.setProperty("log4j.appender.A1.layout", "org.apache.log4j.PatternLayout");
252 properties.setProperty("log4j.appender.A1.layout.ConversionPattern", "%m%n");
254 PropertyConfigurator.configure(properties);
257 public void doTest() throws Exception {
258 stripper.setRoot(getWorkDir() + File.separator);
262 final String projectPath = getWorkDir() + File.separator + ".idea";
263 final Project project = new Project();
265 IdeaProjectLoader.loadFromPath(project, projectPath, "");
267 final ProjectDescriptor projectDescriptor =
268 new ProjectDescriptor(projectPath, project, new FSState(true), new ProjectTimestamps(projectPath),
269 new BuildDataManager(projectPath, true));
270 final IncProjectBuilder builder = new IncProjectBuilder(projectDescriptor, BuilderRegistry.getInstance(), CanceledStatus.NULL);
272 builder.build(new AllProjectScope(project, true), false, true);
278 builder.build(new AllProjectScope(project, false), true, false);
280 projectDescriptor.release();
282 FileAssert.assertEquals(new File(getBaseDir() + ".log"), new File(getWorkDir() + ".log"));