034dcedda347b247d6da91adfd42d9ad7d2017ba
[idea/community.git] / plugins / gradle / testSources / org / jetbrains / plugins / gradle / importing / GradleImportingTestCase.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.jetbrains.plugins.gradle.importing;
17
18 import com.intellij.compiler.server.BuildManager;
19 import com.intellij.openapi.application.Result;
20 import com.intellij.openapi.application.WriteAction;
21 import com.intellij.openapi.externalSystem.model.ProjectSystemId;
22 import com.intellij.openapi.externalSystem.model.settings.ExternalSystemExecutionSettings;
23 import com.intellij.openapi.externalSystem.settings.ExternalProjectSettings;
24 import com.intellij.openapi.externalSystem.test.ExternalSystemImportingTestCase;
25 import com.intellij.openapi.ui.Messages;
26 import com.intellij.openapi.ui.TestDialog;
27 import com.intellij.openapi.util.io.FileUtil;
28 import com.intellij.openapi.vfs.LocalFileSystem;
29 import com.intellij.openapi.vfs.VirtualFile;
30 import org.gradle.util.GradleVersion;
31 import org.gradle.wrapper.GradleWrapperMain;
32 import org.hamcrest.CoreMatchers;
33 import org.hamcrest.CustomMatcher;
34 import org.hamcrest.Matcher;
35 import org.intellij.lang.annotations.Language;
36 import org.jetbrains.annotations.NonNls;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
39 import org.jetbrains.plugins.gradle.settings.DistributionType;
40 import org.jetbrains.plugins.gradle.settings.GradleProjectSettings;
41 import org.jetbrains.plugins.gradle.settings.GradleSettings;
42 import org.jetbrains.plugins.gradle.tooling.annotation.TargetVersions;
43 import org.jetbrains.plugins.gradle.tooling.util.VersionMatcher;
44 import org.jetbrains.plugins.gradle.util.GradleConstants;
45 import org.junit.Rule;
46 import org.junit.rules.TestName;
47 import org.junit.rules.TestWatcher;
48 import org.junit.runner.Description;
49 import org.junit.runner.RunWith;
50 import org.junit.runners.Parameterized;
51
52 import java.io.File;
53 import java.io.IOException;
54 import java.io.StringWriter;
55 import java.net.URI;
56 import java.net.URISyntaxException;
57 import java.util.Arrays;
58 import java.util.Collection;
59 import java.util.Properties;
60
61 import static org.jetbrains.plugins.gradle.tooling.builder.AbstractModelBuilderTest.DistributionLocator;
62 import static org.jetbrains.plugins.gradle.tooling.builder.AbstractModelBuilderTest.SUPPORTED_GRADLE_VERSIONS;
63 import static org.junit.Assume.assumeThat;
64
65 /**
66  * @author Vladislav.Soroka
67  * @since 6/30/2014
68  */
69 @RunWith(value = Parameterized.class)
70 public abstract class GradleImportingTestCase extends ExternalSystemImportingTestCase {
71
72   private static final int GRADLE_DAEMON_TTL_MS = 10000;
73
74   @Rule public TestName name = new TestName();
75   @Rule public VersionMatcherRule versionMatcherRule = new VersionMatcherRule();
76
77   @NotNull
78   @org.junit.runners.Parameterized.Parameter(0)
79   public String gradleVersion;
80   private GradleProjectSettings myProjectSettings;
81
82   @Override
83   public void setUp() throws Exception {
84     super.setUp();
85     assumeThat(gradleVersion, versionMatcherRule.getMatcher());
86     myProjectSettings = new GradleProjectSettings();
87     GradleSettings.getInstance(myProject).setGradleVmOptions("-Xmx64m -XX:MaxPermSize=64m");
88     System.setProperty(ExternalSystemExecutionSettings.REMOTE_PROCESS_IDLE_TTL_IN_MS_KEY, String.valueOf(GRADLE_DAEMON_TTL_MS));
89     configureWrapper();
90   }
91
92   @Override
93   public void tearDown() throws Exception {
94     try {
95       Messages.setTestDialog(TestDialog.DEFAULT);
96       FileUtil.delete(BuildManager.getInstance().getBuildSystemDirectory());
97     }
98     finally {
99       super.tearDown();
100     }
101   }
102
103   @Override
104   public String getName() {
105     return name.getMethodName() == null ? super.getName() : FileUtil.sanitizeFileName(name.getMethodName());
106   }
107
108   @Parameterized.Parameters(name = "{index}: with Gradle-{0}")
109   public static Collection<Object[]> data() throws Throwable {
110     return Arrays.asList(SUPPORTED_GRADLE_VERSIONS);
111   }
112
113   @Override
114   protected String getTestsTempDir() {
115     return "gradleImportTests";
116   }
117
118   @Override
119   protected String getExternalSystemConfigFileName() {
120     return "build.gradle";
121   }
122
123   @Override
124   protected void importProject(@NonNls @Language("Groovy") String config) throws IOException {
125     super.importProject(config);
126   }
127
128   @Override
129   protected ExternalProjectSettings getCurrentExternalProjectSettings() {
130     return myProjectSettings;
131   }
132
133   @Override
134   protected ProjectSystemId getExternalSystemId() {
135     return GradleConstants.SYSTEM_ID;
136   }
137
138   protected VirtualFile createSettingsFile(@NonNls @Language("Groovy") String content) throws IOException {
139     return createProjectSubFile("settings.gradle", content);
140   }
141
142   private void configureWrapper() throws IOException, URISyntaxException {
143
144     final URI distributionUri = new DistributionLocator().getDistributionFor(GradleVersion.version(gradleVersion));
145
146     myProjectSettings.setDistributionType(DistributionType.DEFAULT_WRAPPED);
147     final VirtualFile wrapperJarFrom = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(wrapperJar());
148     assert wrapperJarFrom != null;
149
150     final VirtualFile wrapperJarFromTo = createProjectSubFile("gradle/wrapper/gradle-wrapper.jar");
151     new WriteAction() {
152       @Override
153       protected void run(@NotNull Result result) throws Throwable {
154         wrapperJarFromTo.setBinaryContent(wrapperJarFrom.contentsToByteArray());
155       }
156     }.execute().throwException();
157
158
159     Properties properties = new Properties();
160     properties.setProperty("distributionBase", "GRADLE_USER_HOME");
161     properties.setProperty("distributionPath", "wrapper/dists");
162     properties.setProperty("zipStoreBase", "GRADLE_USER_HOME");
163     properties.setProperty("zipStorePath", "wrapper/dists");
164     properties.setProperty("distributionUrl", distributionUri.toString());
165
166     StringWriter writer = new StringWriter();
167     properties.store(writer, null);
168
169     createProjectSubFile("gradle/wrapper/gradle-wrapper.properties", writer.toString());
170   }
171
172   private static File wrapperJar() {
173     URI location;
174     try {
175       location = GradleWrapperMain.class.getProtectionDomain().getCodeSource().getLocation().toURI();
176     }
177     catch (URISyntaxException e) {
178       throw new RuntimeException(e);
179     }
180     if (!location.getScheme().equals("file")) {
181       throw new RuntimeException(String.format("Cannot determine classpath for wrapper JAR from codebase '%s'.", location));
182     }
183     return new File(location.getPath());
184   }
185
186   private static class VersionMatcherRule extends TestWatcher {
187
188     @Nullable
189     private CustomMatcher myMatcher;
190
191     @NotNull
192     public Matcher getMatcher() {
193       return myMatcher != null ? myMatcher : CoreMatchers.anything();
194     }
195
196     @Override
197     protected void starting(Description d) {
198       final TargetVersions targetVersions = d.getAnnotation(TargetVersions.class);
199       if (targetVersions == null) return;
200
201       myMatcher = new CustomMatcher<String>("Gradle version '" + targetVersions.value() + "'") {
202         @Override
203         public boolean matches(Object item) {
204           return item instanceof String && new VersionMatcher(GradleVersion.version(item.toString())).isVersionMatch(targetVersions);
205         }
206       };
207     }
208   }
209 }