2 * Copyright 2000-2009 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
.idea
.maven
;
18 import com
.intellij
.openapi
.application
.ApplicationManager
;
19 import com
.intellij
.openapi
.application
.Result
;
20 import com
.intellij
.openapi
.application
.WriteAction
;
21 import com
.intellij
.openapi
.module
.Module
;
22 import com
.intellij
.openapi
.module
.ModuleManager
;
23 import com
.intellij
.openapi
.module
.ModuleType
;
24 import com
.intellij
.openapi
.module
.StdModuleTypes
;
25 import com
.intellij
.openapi
.progress
.EmptyProgressIndicator
;
26 import com
.intellij
.openapi
.project
.Project
;
27 import com
.intellij
.openapi
.roots
.ModifiableRootModel
;
28 import com
.intellij
.openapi
.roots
.ModuleRootManager
;
29 import com
.intellij
.openapi
.util
.io
.FileUtil
;
30 import com
.intellij
.openapi
.vfs
.LocalFileSystem
;
31 import com
.intellij
.openapi
.vfs
.VirtualFile
;
32 import com
.intellij
.testFramework
.UsefulTestCase
;
33 import com
.intellij
.testFramework
.fixtures
.IdeaProjectTestFixture
;
34 import com
.intellij
.testFramework
.fixtures
.IdeaTestFixtureFactory
;
35 import org
.intellij
.lang
.annotations
.Language
;
36 import org
.jetbrains
.idea
.maven
.embedder
.MavenConsole
;
37 import org
.jetbrains
.idea
.maven
.project
.*;
38 import org
.jetbrains
.idea
.maven
.utils
.MavenProgressIndicator
;
41 import java
.io
.IOException
;
42 import java
.lang
.reflect
.Field
;
43 import java
.lang
.reflect
.Modifier
;
44 import java
.util
.ArrayList
;
45 import java
.util
.Arrays
;
46 import java
.util
.Collection
;
47 import java
.util
.List
;
49 public abstract class MavenTestCase
extends UsefulTestCase
{
50 protected static final MavenConsole NULL_MAVEN_CONSOLE
= new NullMavenConsole();
51 protected static final MavenProgressIndicator EMPTY_MAVEN_PROCESS
= new MavenProgressIndicator(new EmptyProgressIndicator());
53 private static File ourTempDir
;
55 protected IdeaProjectTestFixture myTestFixture
;
57 protected Project myProject
;
60 protected VirtualFile myProjectRoot
;
62 protected VirtualFile myProjectPom
;
63 protected List
<VirtualFile
> myAllPoms
= new ArrayList
<VirtualFile
>();
66 protected void setUp() throws Exception
{
69 ensureTempDirCreated();
71 myDir
= FileUtil
.createTempFile(ourTempDir
, "test", "", false);
76 myProject
= myTestFixture
.getProject();
78 MavenWorkspaceSettingsComponent
.getInstance(myProject
).loadState(new MavenWorkspaceSettings());
80 String home
= getTestMavenHome();
82 getMavenGeneralSettings().setMavenHome(home
);
85 restoreSettingsFile();
87 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
93 throw new RuntimeException(e
);
99 private void ensureTempDirCreated() {
100 if (ourTempDir
!= null) return;
102 ourTempDir
= new File(FileUtil
.getTempDirectory(), "mavenTests");
103 FileUtil
.delete(ourTempDir
);
107 protected void setUpFixtures() throws Exception
{
108 myTestFixture
= IdeaTestFixtureFactory
.getFixtureFactory().createFixtureBuilder().getFixture();
109 myTestFixture
.setUp();
112 protected void setUpInWriteAction() throws Exception
{
113 File projectDir
= new File(myDir
, "project");
115 myProjectRoot
= LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(projectDir
);
119 protected void tearDown() throws Exception
{
122 if (!FileUtil
.delete(myDir
)) {
123 System
.out
.println("Cannot delete " + myDir
);
124 myDir
.deleteOnExit();
127 resetClassFields(getClass());
131 protected void tearDownFixtures() throws Exception
{
132 myTestFixture
.tearDown();
135 private void resetClassFields(final Class
<?
> aClass
) {
136 if (aClass
== null) return;
138 final Field
[] fields
= aClass
.getDeclaredFields();
139 for (Field field
: fields
) {
140 final int modifiers
= field
.getModifiers();
141 if ((modifiers
& Modifier
.FINAL
) == 0
142 && (modifiers
& Modifier
.STATIC
) == 0
143 && !field
.getType().isPrimitive()) {
144 field
.setAccessible(true);
146 field
.set(this, null);
148 catch (IllegalAccessException e
) {
154 if (aClass
== MavenTestCase
.class) return;
155 resetClassFields(aClass
.getSuperclass());
159 protected void runTest() throws Throwable
{
160 if (runInWriteAction()) {
162 protected void run(Result result
) throws Throwable
{
163 MavenTestCase
.super.runTest();
165 }.executeSilently().throwException();
168 MavenTestCase
.super.runTest();
172 protected boolean runInWriteAction() {
176 protected MavenGeneralSettings
getMavenGeneralSettings() {
177 return MavenProjectsManager
.getInstance(myProject
).getGeneralSettings();
180 protected MavenImportingSettings
getMavenImporterSettings() {
181 return MavenProjectsManager
.getInstance(myProject
).getImportingSettings();
184 protected String
getRepositoryPath() {
185 String path
= getRepositoryFile().getPath();
186 return FileUtil
.toSystemIndependentName(path
);
189 protected File
getRepositoryFile() {
190 return getMavenGeneralSettings().getEffectiveLocalRepository();
193 protected void setRepositoryPath(String path
) {
194 getMavenGeneralSettings().setLocalRepository(path
);
197 protected String
getProjectPath() {
198 return myProjectRoot
.getPath();
201 protected String
getParentPath() {
202 return myProjectRoot
.getParent().getPath();
205 protected String
pathFromBasedir(String relPath
) {
206 return pathFromBasedir(myProjectRoot
, relPath
);
209 protected String
pathFromBasedir(VirtualFile root
, String relPath
) {
210 return FileUtil
.toSystemIndependentName(root
.getPath() + "/" + relPath
);
213 protected VirtualFile
updateSettingsXml(String content
) throws IOException
{
214 return updateSettingsXmlFully(createSettingsXmlContent(content
));
217 protected VirtualFile
updateSettingsXmlFully(String content
) throws IOException
{
218 File ioFile
= new File(myDir
, "settings.xml");
219 ioFile
.createNewFile();
220 VirtualFile f
= LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(ioFile
);
221 setFileContent(f
, content
);
222 getMavenGeneralSettings().setMavenSettingsFile(f
.getPath());
226 protected void deleteSettingsXml() throws IOException
{
227 VirtualFile f
= LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(new File(myDir
, "settings.xml"));
228 if (f
!= null) f
.delete(this);
231 private String
createSettingsXmlContent(String content
) {
232 String mirror
= System
.getProperty("idea.maven.test.mirror",
233 "http://maven.labs.intellij.net:8081/nexus/content/groups/public/");
234 return "<settings>" +
239 " <name>Nexus Public Mirror</name>" +
240 " <url>" + mirror
+ "</url>" +
241 " <mirrorOf>*</mirrorOf>" +
247 protected void restoreSettingsFile() throws IOException
{
248 updateSettingsXml("");
251 protected Module
createModule(String name
) throws IOException
{
252 return createModule(name
, StdModuleTypes
.JAVA
);
255 protected Module
createModule(String name
, ModuleType type
) throws IOException
{
256 VirtualFile f
= createProjectSubFile(name
+ "/" + name
+ ".iml");
257 Module module
= ModuleManager
.getInstance(myProject
).newModule(f
.getPath(), type
);
258 ModifiableRootModel model
= ModuleRootManager
.getInstance(module
).getModifiableModel();
259 model
.addContentEntry(f
.getParent());
264 protected void createProjectPom(String xml
) throws IOException
{
265 myProjectPom
= createPomFile(myProjectRoot
, xml
);
268 protected VirtualFile
createModulePom(String relativePath
, String xml
) throws IOException
{
269 return createPomFile(createProjectSubDir(relativePath
), xml
);
272 protected VirtualFile
createPomFile(VirtualFile dir
, String xml
) throws IOException
{
273 VirtualFile f
= dir
.findChild("pom.xml");
275 f
= dir
.createChildData(null, "pom.xml");
278 setFileContent(f
, createPomXml(xml
));
282 protected String
createPomXml(String xml
) {
283 return "<?xml version=\"1.0\"?>" +
284 "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"" +
285 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
286 " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">" +
287 " <modelVersion>4.0.0</modelVersion>" +
292 protected VirtualFile
createProfilesXmlOldStyle(String xml
) throws IOException
{
293 return createProfilesFile(myProjectRoot
, xml
, true);
296 protected VirtualFile
createProfilesXmlOldStyle(String relativePath
, String xml
) throws IOException
{
297 return createProfilesFile(createProjectSubDir(relativePath
), xml
, true);
300 protected VirtualFile
createProfilesXml(String xml
) throws IOException
{
301 return createProfilesFile(myProjectRoot
, xml
, false);
304 protected VirtualFile
createProfilesXml(String relativePath
, String xml
) throws IOException
{
305 return createProfilesFile(createProjectSubDir(relativePath
), xml
, false);
308 private VirtualFile
createProfilesFile(VirtualFile dir
, String xml
, boolean oldStyle
) throws IOException
{
309 VirtualFile f
= dir
.findChild("profiles.xml");
311 f
= dir
.createChildData(null, "profiles.xml");
313 setFileContent(f
, createValidProfiles(xml
, oldStyle
));
317 private String
createValidProfiles(String xml
, boolean oldStyle
) {
319 return "<?xml version=\"1.0\"?>" +
324 return "<?xml version=\"1.0\"?>" +
332 protected void deleteProfilesXml() throws IOException
{
333 VirtualFile f
= myProjectRoot
.findChild("profiles.xml");
334 if (f
!= null) f
.delete(this);
337 protected void createStdProjectFolders() {
338 createProjectSubDirs("src/main/java",
339 "src/main/resources",
341 "src/test/resources");
344 protected void createProjectSubDirs(String
... relativePaths
) {
345 for (String path
: relativePaths
) {
346 createProjectSubDir(path
);
350 protected VirtualFile
createProjectSubDir(String relativePath
) {
351 File f
= new File(getProjectPath(), relativePath
);
353 return LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(f
);
356 protected VirtualFile
createProjectSubFile(String relativePath
) throws IOException
{
357 File f
= new File(getProjectPath(), relativePath
);
358 f
.getParentFile().mkdirs();
360 return LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(f
);
363 protected VirtualFile
createProjectSubFile(String relativePath
, String content
) throws IOException
{
364 VirtualFile file
= createProjectSubFile(relativePath
);
365 setFileContent(file
, content
);
369 private void setFileContent(VirtualFile file
, String content
) throws IOException
{
370 file
.setBinaryContent(content
.getBytes(), -1, file
.getTimeStamp() + 1000);
373 protected void assertPathEquals(String expected
, String actual
) {
374 if (expected
!= null) expected
= FileUtil
.toSystemIndependentName(expected
);
375 if (actual
!= null) actual
= FileUtil
.toSystemIndependentName(actual
);
376 assertEquals(expected
, actual
);
379 protected <T
, U
> void assertOrderedElementsAreEqual(Collection
<U
> actual
, Collection
<T
> expected
) {
380 assertOrderedElementsAreEqual(actual
, expected
.toArray());
383 protected <T
, U
> void assertUnorderedElementsAreEqual(Collection
<U
> actual
, Collection
<T
> expected
) {
384 assertUnorderedElementsAreEqual(actual
, expected
.toArray());
387 protected <T
, U
> void assertUnorderedElementsAreEqual(U
[] actual
, T
... expected
) {
388 assertUnorderedElementsAreEqual(Arrays
.asList(actual
), expected
);
391 protected <T
, U
> void assertUnorderedElementsAreEqual(Collection
<U
> actual
, T
... expected
) {
392 String s
= "\nexpected: " + Arrays
.asList(expected
) + "\nactual: " + new ArrayList
<U
>(actual
);
393 assertEquals(s
, expected
.length
, actual
.size());
395 for (T eachExpected
: expected
) {
396 boolean found
= false;
397 for (U eachActual
: actual
) {
398 if (eachExpected
.equals(eachActual
)) {
403 assertTrue(s
, found
);
407 protected <T
, U
> void assertOrderedElementsAreEqual(Collection
<U
> actual
, T
... expected
) {
408 String s
= "\nexpected: " + Arrays
.asList(expected
) + "\nactual: " + new ArrayList
<U
>(actual
);
409 assertEquals(s
, expected
.length
, actual
.size());
411 List
<U
> actualList
= new ArrayList
<U
>(actual
);
412 for (int i
= 0; i
< expected
.length
; i
++) {
413 T expectedElement
= expected
[i
];
414 U actualElement
= actualList
.get(i
);
415 assertTrue(s
, expectedElement
.equals(actualElement
));
419 protected boolean ignore() {
420 System
.out
.println("Ignored: " + getClass().getSimpleName() + "." + getName());
424 protected boolean hasMavenInstallation() {
425 boolean result
= getTestMavenHome() != null;
426 if (!result
) System
.out
.println("Ignored, because Maven installation not found: " + getClass().getSimpleName() + "." + getName());
430 private String
getTestMavenHome() {
431 return System
.getProperty("idea.maven.test.home");