summaryrefslogtreecommitdiff
path: root/java/testFramework/src/com/intellij/testFramework/CompilerTester.java
blob: 00780b9b76cec84d2bcac770736c4f3afe0cf5bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.testFramework;

import com.intellij.compiler.CompilerTestUtil;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.compiler.*;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl;
import com.intellij.openapi.roots.CompilerModuleExtension;
import com.intellij.openapi.roots.CompilerProjectExtension;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.testFramework.fixtures.TempDirTestFixture;
import com.intellij.testFramework.fixtures.impl.TempDirTestFixtureImpl;
import com.intellij.util.Consumer;
import com.intellij.util.ObjectUtils;
import com.intellij.util.concurrency.Semaphore;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author peter
 */
public class CompilerTester {

  private Module myModule;
  private TempDirTestFixture myMainOutput;

  public CompilerTester(Module module) throws Exception {
    myModule = module;
    myMainOutput = new TempDirTestFixtureImpl();
    myMainOutput.setUp();

    new WriteCommandAction(getProject()) {
      @Override
      protected void run(Result result) throws Throwable {
        //noinspection ConstantConditions
        CompilerProjectExtension.getInstance(getProject()).setCompilerOutputUrl(myMainOutput.findOrCreateDir("out").getUrl());
        CompilerTestUtil.enableExternalCompiler();
        ModuleRootModificationUtil.setModuleSdk(myModule, JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk());
      }
    }.execute();

  }

  public void tearDown() {
    CompilerTestUtil.disableExternalCompiler(getProject());

    try {
      myMainOutput.tearDown();
    }
    catch (Exception e) {
      throw new RuntimeException(e);
    } 
    finally {
      myMainOutput = null;
      myModule = null;
    }
  }

  private Project getProject() {
    return myModule.getProject();
  }

  public void deleteClassFile(final String className) throws IOException {
    AccessToken token = WriteAction.start();
    try {
        //noinspection ConstantConditions
        touch(JavaPsiFacade.getInstance(getProject()).findClass(className, GlobalSearchScope.allScope(getProject())).getContainingFile().getVirtualFile());
    }
    finally {
      token.finish();
    }
  }

  @Nullable
  public VirtualFile findClassFile(String className, Module module) {
    //noinspection ConstantConditions
    VirtualFile path = ModuleRootManager.getInstance(module).getModuleExtension(CompilerModuleExtension.class).getCompilerOutputPath();
    path.getChildren();
    assert path != null;
    path.refresh(false, true);
    return path.findChild(className.replace('.', '/') + ".class");
  }

  public void touch(VirtualFile file) throws IOException {
    file.setBinaryContent(file.contentsToByteArray(), -1, file.getTimeStamp() + 1);
    File ioFile = VfsUtil.virtualToIoFile(file);
    assert ioFile.setLastModified(ioFile.lastModified() - 100000);
    file.refresh(false, false);
  }

  public void setFileText(final PsiFile file, final String text) throws IOException {
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {
      @Override
      public void run() {
        try {
          final VirtualFile virtualFile = file.getVirtualFile();
          VfsUtil.saveText(ObjectUtils.assertNotNull(virtualFile), text);
        }
        catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
    });
    touch(file.getVirtualFile());
  }

  public void setFileName(final PsiFile file, final String name) {
    new WriteCommandAction(getProject()) {
      @Override
      protected void run(Result result) throws Throwable {
        file.setName(name);
      }
    }.execute();
  }


  public List<CompilerMessage> make() {
    return runCompiler(new Consumer<ErrorReportingCallback>() {
      @Override
      public void consume(ErrorReportingCallback callback) {
        CompilerManager.getInstance(getProject()).make(callback);
      }
    });
  }

  public List<CompilerMessage> rebuild() {
    return runCompiler(new Consumer<ErrorReportingCallback>() {
      @Override
      public void consume(ErrorReportingCallback callback) {
        CompilerManager.getInstance(getProject()).rebuild(callback);
      }
    });
  }

  public List<CompilerMessage> compileModule(final Module module) {
    return runCompiler(new Consumer<ErrorReportingCallback>() {
      @Override
      public void consume(ErrorReportingCallback callback) {
        CompilerManager.getInstance(getProject()).compile(module, callback);
      }
    });
  }

  public List<CompilerMessage> compileFiles(final VirtualFile... files) {
    return runCompiler(new Consumer<ErrorReportingCallback>() {
      @Override
      public void consume(ErrorReportingCallback callback) {
        CompilerManager.getInstance(getProject()).compile(files, callback);
      }
    });
  }

  private List<CompilerMessage> runCompiler(final Consumer<ErrorReportingCallback> runnable) {
    final Semaphore semaphore = new Semaphore();
    semaphore.down();
    final ErrorReportingCallback callback = new ErrorReportingCallback(semaphore);
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {
      @Override
      public void run() {
        try {
          getProject().save();
          CompilerTestUtil.saveApplicationSettings();
          final VirtualFile moduleFile = myModule.getModuleFile();
          File ioFile = VfsUtil.virtualToIoFile(moduleFile);
          if (!ioFile.exists()) {
            getProject().save();
            assert ioFile.exists() : "File does not exist: " + ioFile.getPath();
          }
          runnable.consume(callback);
        }
        catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    });

    //tests run in awt
    while (!semaphore.waitFor(100)) {
      if (SwingUtilities.isEventDispatchThread()) {
        UIUtil.dispatchAllInvocationEvents();
      }
    }
    callback.throwException();
    return callback.getMessages();
  }

  private static class ErrorReportingCallback implements CompileStatusNotification {
    private final Semaphore mySemaphore;
    private Throwable myError;
    private final List<CompilerMessage> myMessages = new ArrayList<CompilerMessage>();

    public ErrorReportingCallback(Semaphore semaphore) {
      mySemaphore = semaphore;
    }

    @Override
    public void finished(boolean aborted, int errors, int warnings, final CompileContext compileContext) {
      try {
        for (CompilerMessageCategory category : CompilerMessageCategory.values()) {
          CompilerMessage[] messages = compileContext.getMessages(category);
          for (CompilerMessage message : messages) {
            final String text = message.getMessage();
            if (category != CompilerMessageCategory.INFORMATION || !(text.startsWith("Compilation completed successfully") || text.startsWith("Using javac"))) {
              myMessages.add(message);
            }
          }
        }
        Assert.assertFalse("Code did not compile!", aborted);
      }
      catch (Throwable t) {
        myError = t;
      }
      finally {
        mySemaphore.up();
      }
    }

    void throwException() {
      if (myError != null) {
        throw new RuntimeException(myError);
      }
    }

    public List<CompilerMessage> getMessages() {
      return myMessages;
    }
  }


}