grails: skip grails-gradle test for Gradle newer than 2.1
[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.intellij.lang.annotations.Language;
33 import org.jetbrains.annotations.NonNls;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.plugins.gradle.VersionMatcherRule;
36 import org.jetbrains.plugins.gradle.settings.DistributionType;
37 import org.jetbrains.plugins.gradle.settings.GradleProjectSettings;
38 import org.jetbrains.plugins.gradle.settings.GradleSettings;
39 import org.jetbrains.plugins.gradle.util.GradleConstants;
40 import org.junit.Rule;
41 import org.junit.rules.TestName;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.Parameterized;
44
45 import java.io.File;
46 import java.io.IOException;
47 import java.io.StringWriter;
48 import java.net.URI;
49 import java.net.URISyntaxException;
50 import java.util.Arrays;
51 import java.util.Collection;
52 import java.util.Properties;
53
54 import static org.jetbrains.plugins.gradle.tooling.builder.AbstractModelBuilderTest.DistributionLocator;
55 import static org.jetbrains.plugins.gradle.tooling.builder.AbstractModelBuilderTest.SUPPORTED_GRADLE_VERSIONS;
56 import static org.junit.Assume.assumeThat;
57
58 /**
59  * @author Vladislav.Soroka
60  * @since 6/30/2014
61  */
62 @RunWith(value = Parameterized.class)
63 public abstract class GradleImportingTestCase extends ExternalSystemImportingTestCase {
64
65   private static final int GRADLE_DAEMON_TTL_MS = 10000;
66
67   @Rule public TestName name = new TestName();
68   @Rule public VersionMatcherRule versionMatcherRule = new VersionMatcherRule();
69
70   @NotNull
71   @org.junit.runners.Parameterized.Parameter(0)
72   public String gradleVersion;
73   private GradleProjectSettings myProjectSettings;
74
75   @Override
76   public void setUp() throws Exception {
77     super.setUp();
78     assumeThat(gradleVersion, versionMatcherRule.getMatcher());
79     myProjectSettings = new GradleProjectSettings();
80     GradleSettings.getInstance(myProject).setGradleVmOptions("-Xmx64m -XX:MaxPermSize=64m");
81     System.setProperty(ExternalSystemExecutionSettings.REMOTE_PROCESS_IDLE_TTL_IN_MS_KEY, String.valueOf(GRADLE_DAEMON_TTL_MS));
82     configureWrapper();
83   }
84
85   @Override
86   public void tearDown() throws Exception {
87     try {
88       Messages.setTestDialog(TestDialog.DEFAULT);
89       FileUtil.delete(BuildManager.getInstance().getBuildSystemDirectory());
90     }
91     finally {
92       super.tearDown();
93     }
94   }
95
96   @Override
97   public String getName() {
98     return name.getMethodName() == null ? super.getName() : FileUtil.sanitizeFileName(name.getMethodName());
99   }
100
101   @Parameterized.Parameters(name = "{index}: with Gradle-{0}")
102   public static Collection<Object[]> data() throws Throwable {
103     return Arrays.asList(SUPPORTED_GRADLE_VERSIONS);
104   }
105
106   @Override
107   protected String getTestsTempDir() {
108     return "gradleImportTests";
109   }
110
111   @Override
112   protected String getExternalSystemConfigFileName() {
113     return "build.gradle";
114   }
115
116   @Override
117   protected void importProject(@NonNls @Language("Groovy") String config) throws IOException {
118     super.importProject(config);
119   }
120
121   @Override
122   protected ExternalProjectSettings getCurrentExternalProjectSettings() {
123     return myProjectSettings;
124   }
125
126   @Override
127   protected ProjectSystemId getExternalSystemId() {
128     return GradleConstants.SYSTEM_ID;
129   }
130
131   protected VirtualFile createSettingsFile(@NonNls @Language("Groovy") String content) throws IOException {
132     return createProjectSubFile("settings.gradle", content);
133   }
134
135   private void configureWrapper() throws IOException, URISyntaxException {
136
137     final URI distributionUri = new DistributionLocator().getDistributionFor(GradleVersion.version(gradleVersion));
138
139     myProjectSettings.setDistributionType(DistributionType.DEFAULT_WRAPPED);
140     final VirtualFile wrapperJarFrom = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(wrapperJar());
141     assert wrapperJarFrom != null;
142
143     final VirtualFile wrapperJarFromTo = createProjectSubFile("gradle/wrapper/gradle-wrapper.jar");
144     new WriteAction() {
145       @Override
146       protected void run(@NotNull Result result) throws Throwable {
147         wrapperJarFromTo.setBinaryContent(wrapperJarFrom.contentsToByteArray());
148       }
149     }.execute().throwException();
150
151
152     Properties properties = new Properties();
153     properties.setProperty("distributionBase", "GRADLE_USER_HOME");
154     properties.setProperty("distributionPath", "wrapper/dists");
155     properties.setProperty("zipStoreBase", "GRADLE_USER_HOME");
156     properties.setProperty("zipStorePath", "wrapper/dists");
157     properties.setProperty("distributionUrl", distributionUri.toString());
158
159     StringWriter writer = new StringWriter();
160     properties.store(writer, null);
161
162     createProjectSubFile("gradle/wrapper/gradle-wrapper.properties", writer.toString());
163   }
164
165   private static File wrapperJar() {
166     URI location;
167     try {
168       location = GradleWrapperMain.class.getProtectionDomain().getCodeSource().getLocation().toURI();
169     }
170     catch (URISyntaxException e) {
171       throw new RuntimeException(e);
172     }
173     if (!location.getScheme().equals("file")) {
174       throw new RuntimeException(String.format("Cannot determine classpath for wrapper JAR from codebase '%s'.", location));
175     }
176     return new File(location.getPath());
177   }
178 }