Maven: resolving system/env properties
authorAnton Makeev <Anton.Makeev@jetbrains.com>
Fri, 10 Jul 2009 07:52:50 +0000 (11:52 +0400)
committerAnton Makeev <Anton.Makeev@jetbrains.com>
Fri, 10 Jul 2009 07:52:50 +0000 (11:52 +0400)
15 files changed:
plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/MavenDomUtil.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/MavenPropertyResolver.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/converters/MavenDomPropertyReferenceInjector.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/converters/MavenModuleConverter.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/converters/MavenPropertyPsiReference.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/embedder/MavenEmbedderFactory.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenProjectReader.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/vfs/MavenPropertiesVirtualFile.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/vfs/MavenPropertiesVirtualFileSystem.java
plugins/maven/src/main/resources/META-INF/plugin.xml
plugins/maven/src/test/java/org/jetbrains/idea/maven/MavenTestCase.java
plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenCompletionAndResolutionTestCase.java
plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenModuleCompletionAndResolutionTest.java
plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenPropertyCompletionAndResolutionTest.java

index 7cdbb0646bd4b50301ed117acb3311ce53133126..d0d5d506ad10dbc11e702b44817605035d1dc835 100644 (file)
@@ -1,10 +1,13 @@
 package org.jetbrains.idea.maven.dom;
 
+import com.intellij.lang.properties.psi.Property;
+import com.intellij.lang.properties.psi.PropertiesFile;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.Pair;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.openapi.vfs.VirtualFileSystem;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiManager;
 import com.intellij.psi.search.GlobalSearchScope;
@@ -23,6 +26,7 @@ import org.jetbrains.idea.maven.project.MavenId;
 import org.jetbrains.idea.maven.project.MavenProject;
 import org.jetbrains.idea.maven.project.MavenProjectsManager;
 import org.jetbrains.idea.maven.utils.MavenConstants;
+import org.jetbrains.idea.maven.vfs.MavenPropertiesVirtualFileSystem;
 
 import java.io.File;
 import java.util.List;
@@ -181,6 +185,18 @@ public class MavenDomUtil {
     return children[index];
   }
 
+  @Nullable
+  public static Property findProperty(@NotNull Project project, @NotNull String fileName, @NotNull String propName) {
+    VirtualFileSystem fs = MavenPropertiesVirtualFileSystem.getInstance();
+    VirtualFile file = fs.findFileByPath(fileName);
+    if (file == null) return null;
+
+    PsiFile psiFile = PsiManager.getInstance(project).findFile(file);
+    if (!(psiFile instanceof PropertiesFile)) return null;
+
+    return ((PropertiesFile)psiFile).findPropertyByKey(propName);
+  }
+
   public static List<DomFileElement<MavenDomProjectModel>> collectProjectPoms(final Project p) {
     return DomService.getInstance().getFileElements(MavenDomProjectModel.class,
                                                     p,
index 2c57133e387052119f9650d0dd2fa072681eec16..bbbae578cada3b8b2b746cd834d4c292c778ef20 100644 (file)
@@ -3,9 +3,6 @@ package org.jetbrains.idea.maven.dom;
 import com.intellij.openapi.module.Module;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.psi.xml.XmlTag;
-import com.intellij.util.xml.DomFileElement;
-import com.intellij.util.xml.DomUtil;
-import com.intellij.util.xml.GenericDomValue;
 import org.apache.commons.beanutils.BeanAccessLanguageException;
 import org.apache.commons.beanutils.BeanUtils;
 import org.jetbrains.idea.maven.dom.model.MavenDomProfile;
@@ -16,9 +13,9 @@ import org.jetbrains.idea.maven.project.MavenProject;
 import org.jetbrains.idea.maven.project.MavenProjectsManager;
 
 import java.lang.reflect.InvocationTargetException;
+import java.util.List;
 import java.util.Properties;
 import java.util.Stack;
-import java.util.List;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -36,14 +33,6 @@ public class MavenPropertyResolver {
     return doResolve(text, mavenProject, additionalProperties, propertyEscapeString, escapedCharacters, new Stack<String>());
   }
 
-  public static String resolve(GenericDomValue<String> value) {
-    String text = value.getStringValue();
-    if (text == null) return null;
-
-    DomFileElement<MavenDomProjectModel> dom = DomUtil.getFileElement(value);
-    return resolve(text, dom.getRootElement());
-  }
-
   public static String resolve(String text, MavenDomProjectModel projectDom) {
     MavenProject mavenProject = MavenDomUtil.findProject(projectDom);
     if (mavenProject == null) return text;
index 8a80ba5c6536d47a8c1bbd51cfe98a6c21cb14f2..2c216fd297f83a2e6820dc15e46a364797de82e7 100644 (file)
@@ -47,8 +47,4 @@ public class MavenDomPropertyReferenceInjector implements DomReferenceInjector {
     VirtualFile virtualFile = dom.getOriginalFile().getVirtualFile();
     return Pair.create(virtualFile, dom);
   }
-
-  protected VirtualFile getFile(ConvertContext context) {
-    return getFileAndDom(context).first;
-  }
 }
index 53fff750b6c8119848f418adff27271b4b17f051..499992cf8b794459c65bc82eb279d46c1ed5164c 100644 (file)
@@ -23,10 +23,10 @@ public class MavenModuleConverter extends MavenReferenceConverter<PsiFile> {
   }
 
   protected PsiReference createReference(PsiElement element,
-                                            String text,
-                                            TextRange range,
-                                            VirtualFile virtualFile,
-                                            XmlFile psiFile) {
+                                         String text,
+                                         TextRange range,
+                                         VirtualFile virtualFile,
+                                         XmlFile psiFile) {
     return new MavenModulePsiReference(element, text, range, virtualFile, psiFile);
   }
 }
index b4a15aabff37469195b507368b75b70e09166252..9a1685960af10b225e12d6dbc6862b74402e744e 100644 (file)
@@ -3,13 +3,9 @@ package org.jetbrains.idea.maven.dom.converters;
 import com.intellij.openapi.project.Project;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.openapi.util.text.StringUtil;
-import com.intellij.openapi.vfs.VfsUtil;
 import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.openapi.vfs.VirtualFileManager;
-import com.intellij.openapi.vfs.VirtualFileSystem;
 import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiManager;
-import com.intellij.psi.impl.PsiManagerEx;
 import com.intellij.psi.util.PsiUtil;
 import com.intellij.psi.xml.XmlTag;
 import com.intellij.psi.xml.XmlTagChild;
@@ -20,13 +16,10 @@ import gnu.trove.THashSet;
 import org.jetbrains.idea.maven.dom.MavenDomUtil;
 import org.jetbrains.idea.maven.dom.model.*;
 import org.jetbrains.idea.maven.project.*;
-import org.jetbrains.idea.maven.utils.MavenLog;
 import org.jetbrains.idea.maven.utils.MavenUtil;
 import org.jetbrains.idea.maven.vfs.MavenPropertiesVirtualFileSystem;
 
-import java.io.IOException;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
 public class MavenPropertyPsiReference extends MavenPsiReference {
@@ -65,6 +58,14 @@ public class MavenPropertyPsiReference extends MavenPsiReference {
   // 7. profiles in parent pom.xml
   // 8. parent pom.xml
   private PsiElement doResolve() {
+    if (myText.startsWith("env.")) {
+      return resolveEnvPropety();
+    }
+
+    if (myText.equals("basedir")) {
+      return resolveBasedir();
+    }
+
     PsiElement result = resolveSystemPropety();
     if (result != null) return result;
 
@@ -72,10 +73,11 @@ public class MavenPropertyPsiReference extends MavenPsiReference {
     DomFileElement<DomElement> fileElement = DomUtil.getFileElement(domElement);
     MavenDomProjectModel domProject = (MavenDomProjectModel)fileElement.getRootElement(); // todo hard-cast for now
 
-    if (myText.startsWith("project.") || myText.startsWith("pom.")) {
+    if (myText.startsWith("project.") || myText.startsWith("pom.") || myText.equals("version")) {
       String path = myText.startsWith("pom.")
                     ? "project." + myText.substring("pom.".length())
                     : myText;
+      if (myText.equals("version")) path = "project.version";
       return resolveModelProperty(domProject, path, new THashSet<DomElement>());
     }
 
@@ -92,30 +94,19 @@ public class MavenPropertyPsiReference extends MavenPsiReference {
   }
 
   private PsiElement resolveSystemPropety() {
-    if (true) return null;
-    if (System.getProperty(myText) == null) return null;
-
-    StringBuilder builder = new StringBuilder();
-    for (Map.Entry<Object, Object> each : System.getProperties().entrySet()) {
-      builder.append(each.getKey());
-      builder.append("=");
-      builder.append(each.getValue());
-      builder.append("\n");
-    }
+    return MavenDomUtil.findProperty(myProject,
+                                     MavenPropertiesVirtualFileSystem.SYSTEM_PROPERTIES_FILE,
+                                     myText);
+  }
 
-    VirtualFileSystem fs = VirtualFileManager.getInstance().getFileSystem(MavenPropertiesVirtualFileSystem.PROTOCOL);
-    final VirtualFile propertiesFile = fs.findFileByPath("System.properties");
-    try {
-      VfsUtil.saveText(propertiesFile, builder.toString());
-    }
-    catch (IOException e) {
-      MavenLog.LOG.error(e);
-      return null;
-    }
-    PsiManagerEx psiManager = (PsiManagerEx)PsiManager.getInstance(myProject);
-    //FileManager fileManager = psiManager.getFileManager();
-    //fileManager.setViewProvider(propertiesFile, fileManager.createFileViewProvider(propertiesFile, false));
-    return psiManager.findFile(propertiesFile);
+  private PsiElement resolveEnvPropety() {
+    return MavenDomUtil.findProperty(myProject,
+                                     MavenPropertiesVirtualFileSystem.ENV_PROPERTIES_FILE,
+                                     myText.substring("env.".length()));
+  }
+
+  private PsiElement resolveBasedir() {
+    return PsiManager.getInstance(myProject).findDirectory(myVirtualFile.getParent());
   }
 
   private PsiElement resolveModelProperty(MavenDomProjectModel projectDom,
index 91a35f471acb084f8b57ef89695449116c3f4b76..549d20ac52fd1099330a1e6242621ebf4981cb15 100644 (file)
@@ -2,13 +2,13 @@ package org.jetbrains.idea.maven.embedder;
 
 import com.intellij.openapi.util.text.StringUtil;
 import org.apache.maven.embedder.*;
-import org.codehaus.plexus.util.cli.CommandLineUtils;
 import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.Nullable;
 import org.jetbrains.idea.maven.project.MavenGeneralSettings;
 import org.jetbrains.idea.maven.utils.JDOMReader;
 import org.jetbrains.idea.maven.utils.MavenConstants;
 import org.jetbrains.idea.maven.utils.MavenLog;
+import org.jetbrains.idea.maven.utils.MavenUtil;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -182,18 +182,12 @@ public class MavenEmbedderFactory {
   public static Properties collectSystemProperties() {
     if (mySystemPropertiesCache == null) {
       Properties result = new Properties();
-      result.putAll(System.getProperties());
+      result.putAll(MavenUtil.getSystemProperties());
 
-      try {
-        Properties envVars = CommandLineUtils.getSystemEnvVars();
-        for (Map.Entry<Object, Object> each : envVars.entrySet()) {
-          result.setProperty("env." + each.getKey().toString(), each.getValue().toString());
-        }
-      }
-      catch (IOException e) {
-        MavenLog.LOG.warn(e);
+      Properties envVars = MavenUtil.getEnvProperties();
+      for (Map.Entry<Object, Object> each : envVars.entrySet()) {
+        result.setProperty("env." + each.getKey().toString(), each.getValue().toString());
       }
-
       mySystemPropertiesCache = result;
     }
 
index 84989183e03807528f41212b44600c4a41d9835c..e2c5162757b92723a471c5e8bc8037bc695e3373 100644 (file)
@@ -272,7 +272,7 @@ public class MavenProjectReader {
     List<Profile> activated = new ArrayList<Profile>();
     List<Profile> activeByDefault = new ArrayList<Profile>();
 
-    ProfileActivationContext context = new DefaultProfileActivationContext(System.getProperties(), true);
+    ProfileActivationContext context = new DefaultProfileActivationContext(MavenUtil.getSystemProperties(), true);
 
     List<Profile> rawProfiles = model.getProfiles();
     List<Profile> expandedProfiles = null;
index 8693574369a0c4c7c900ff4a21a4a0da48e37ecf..4f016ab83e9a6df4f0dd252ce0654e42730b2884 100644 (file)
@@ -30,10 +30,7 @@ import java.io.Serializable;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.net.URL;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.regex.Pattern;
@@ -107,6 +104,24 @@ public class MavenUtil {
     return LaterInvocator.isInModalContext();
   }
 
+  public static Properties getSystemProperties() {
+    Properties result = (Properties)System.getProperties().clone();
+    for (String each : new THashSet<String>((Set)result.keySet())) {
+      if (each.startsWith("idea.")) {
+        result.remove(each);
+      }
+    }
+    return result;
+  }
+
+  public static Properties getEnvProperties() {
+    Properties reuslt = new Properties();
+    for (Map.Entry<String, String> each : System.getenv().entrySet()) {
+      reuslt.put(each.getKey(), each.getValue());
+    }
+    return reuslt;
+  }
+
   public static File getPluginSystemDir(String folder) {
     // PathManager.getSystemPath() may return relative path
     return new File(PathManager.getSystemPath(), "Maven" + "/" + folder).getAbsoluteFile();
index 54b4cc8f2aff4dba73fe7b4682f28f76bd8eeecc..c1f691428a7514d0957d7b87a9f23e45631657c3 100644 (file)
@@ -1,25 +1,46 @@
 package org.jetbrains.idea.maven.vfs;
 
+import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.DeprecatedVirtualFile;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.VirtualFileSystem;
 import org.jetbrains.annotations.NotNull;
 
-import java.io.*;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Properties;
+import java.util.Set;
+import java.util.TreeSet;
 
 public class MavenPropertiesVirtualFile extends DeprecatedVirtualFile {
-  private final String myName;
+  private final String myPath;
   private final VirtualFileSystem myFS;
-  private volatile byte[] myContent = new byte[0];
+  private final byte[] myContent;
 
-  public MavenPropertiesVirtualFile(String name, VirtualFileSystem FS) {
-    myName = name;
+  public MavenPropertiesVirtualFile(String path, Properties properties, VirtualFileSystem FS) {
+    myPath = path;
     myFS = FS;
+
+    myContent = createContent(properties);
+  }
+
+  private byte[] createContent(Properties properties) {
+    StringBuilder builder = new StringBuilder();
+    TreeSet<String> sortedKeys = new TreeSet<String>((Set)properties.keySet());
+    for (String each : sortedKeys) {
+      builder.append(StringUtil.escapeProperty(each, true));
+      builder.append("=");
+      builder.append(StringUtil.escapeProperty(properties.getProperty(each), false));
+      builder.append("\n");
+    }
+    return builder.toString().getBytes();
   }
 
   @NotNull
   public String getName() {
-    return myName;
+    return myPath;
   }
 
   @NotNull
@@ -28,7 +49,7 @@ public class MavenPropertiesVirtualFile extends DeprecatedVirtualFile {
   }
 
   public String getPath() {
-    return myName;
+    return myPath;
   }
 
   public boolean isWritable() {
@@ -51,19 +72,9 @@ public class MavenPropertiesVirtualFile extends DeprecatedVirtualFile {
     return null;
   }
 
-  @NotNull
-  public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException {
-    return new ByteArrayOutputStream() {
-      @Override
-      public void close() throws IOException {
-        super.close();
-        myContent = toByteArray();
-      }
-    };
-  }
-
   @NotNull
   public byte[] contentsToByteArray() throws IOException {
+    if (myContent == null) throw new IOException();
     return myContent;
   }
 
@@ -86,4 +97,9 @@ public class MavenPropertiesVirtualFile extends DeprecatedVirtualFile {
   public InputStream getInputStream() throws IOException {
     return new ByteArrayInputStream(myContent);
   }
+
+  @NotNull
+  public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException {
+    throw new UnsupportedOperationException();
+  }
 }
index 47f8e65b77a5de5077f2180b98c6c3d571b8ab23..0a52c2502255f4101d03636b85cc194d240c4a3a 100644 (file)
@@ -1,21 +1,68 @@
 package org.jetbrains.idea.maven.vfs;
 
+import com.intellij.openapi.application.ApplicationManager;
+import com.intellij.openapi.components.ApplicationComponent;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.openapi.vfs.ex.dummy.DummyFileSystem;
+import gnu.trove.THashMap;
 import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.idea.maven.utils.MavenUtil;
 
-public class MavenPropertiesVirtualFileSystem extends DummyFileSystem {
+import java.util.Map;
+
+public class MavenPropertiesVirtualFileSystem extends DummyFileSystem implements ApplicationComponent {
   @NonNls public static final String PROTOCOL = "maven-properties";
 
+  @NonNls public static final String SYSTEM_PROPERTIES_FILE = "System.properties";
+  @NonNls public static final String ENV_PROPERTIES_FILE = "Environment.properties";
+
+  private final Map<String, VirtualFile> myFiles = new THashMap<String, VirtualFile>();
+
+  public static MavenPropertiesVirtualFileSystem getInstance() {
+    return ApplicationManager.getApplication().getComponent(MavenPropertiesVirtualFileSystem.class);
+  }
+
+  @NotNull
+  public String getComponentName() {
+    return MavenPropertiesVirtualFileSystem.class.getName();
+  }
+
+  public void initComponent() {
+  }
+
+  public void disposeComponent() {
+  }
+
   public String getProtocol() {
     return PROTOCOL;
   }
 
-  public VirtualFile findFileByPath(@NotNull @NonNls String path) {
-    return new MavenPropertiesVirtualFile(path, this);
+  //@Override
+  //public boolean isPhysical() {
+  //  return false;
+  //}
+
+  public synchronized VirtualFile findFileByPath(@NotNull @NonNls String path) {
+    VirtualFile result = myFiles.get(path);
+    if (result != null) return result;
+
+    result = createFile(path);
+    if (result != null) {
+      myFiles.put(path, result);
+    }
+    return result;
+  }
+
+  @Nullable
+  private VirtualFile createFile(String path) {
+    if (SYSTEM_PROPERTIES_FILE.equals(path)) return new MavenPropertiesVirtualFile(path, MavenUtil.getSystemProperties(), this);
+    if (ENV_PROPERTIES_FILE.equals(path)) return new MavenPropertiesVirtualFile(path, MavenUtil.getEnvProperties(), this);
+    return null;
   }
 
+
   public void refresh(boolean asynchronous) {
   }
 
@@ -23,12 +70,6 @@ public class MavenPropertiesVirtualFileSystem extends DummyFileSystem {
     return findFileByPath(path);
   }
 
-  @NotNull
-  @Override
-  public String getComponentName() {
-    return "foo";
-  }
-
   //protected void deleteFile(Object requestor, VirtualFile vFile) throws IOException {
   //  throw new UnsupportedOperationException();
   //}
index c80b84286d94cc70581b519668e558daf144985f..c42c9d70ede359bce53ac6e724e4fae7c4f34cb0 100644 (file)
@@ -72,9 +72,9 @@
     <component>
       <implementation-class>org.jetbrains.idea.maven.utils.MavenEnvironmentRegistrar</implementation-class>
     </component>
-    <!--<component>-->
-      <!--<implementation-class>org.jetbrains.idea.maven.vfs.MavenPropertiesVirtualFileSystem</implementation-class>-->
-    <!--</component>-->
+    <component>
+      <implementation-class>org.jetbrains.idea.maven.vfs.MavenPropertiesVirtualFileSystem</implementation-class>
+    </component>
   </application-components>
 
   <project-components>
index 19cb8bee295e59c4eb52873571c74c592fe106e1..e2644f4c9fbda6dc160e00a34b94d5416c9bfe48 100644 (file)
@@ -248,12 +248,16 @@ public abstract class MavenTestCase extends TestCase {
     return createProfilesFile(myProjectRoot, xml, true);
   }
 
+  protected VirtualFile createProfilesXmlOldStyle(String relativePath, String xml) throws IOException {
+    return createProfilesFile(createProjectSubDir(relativePath), xml, true);
+  }
+
   protected VirtualFile createProfilesXml(String xml) throws IOException {
     return createProfilesFile(myProjectRoot, xml, false);
   }
 
-  protected VirtualFile createProfilesXmlOldStyle(String relativePath, String xml) throws IOException {
-    return createProfilesFile(createProjectSubDir(relativePath), xml, true);
+  protected VirtualFile createProfilesXml(String relativePath, String xml) throws IOException {
+    return createProfilesFile(createProjectSubDir(relativePath), xml, false);
   }
 
   private VirtualFile createProfilesFile(VirtualFile dir, String xml, boolean oldStyle) throws IOException {
index fe364d4b19ebf7c4309ae777f722310c1fa0275c..5661d2fef5da804effeec5c943a58561500ef7d5 100644 (file)
@@ -1,8 +1,8 @@
 package org.jetbrains.idea.maven.dom;
 
 import com.intellij.codeInsight.CodeInsightSettings;
-import com.intellij.codeInsight.intention.IntentionAction;
 import com.intellij.codeInsight.documentation.DocumentationManager;
+import com.intellij.codeInsight.intention.IntentionAction;
 import com.intellij.codeInsight.lookup.LookupElement;
 import com.intellij.lang.documentation.DocumentationProvider;
 import com.intellij.openapi.editor.CaretModel;
@@ -12,11 +12,13 @@ import com.intellij.psi.PsiElement;
 import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiManager;
 import com.intellij.psi.PsiReference;
+import com.intellij.psi.xml.XmlTag;
 import com.intellij.testFramework.fixtures.CodeInsightTestFixture;
-import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
 import com.intellij.testFramework.fixtures.CodeInsightTestUtil;
+import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory;
 import org.jetbrains.idea.maven.MavenImportingTestCase;
 import org.jetbrains.idea.maven.dom.converters.PsiElementWrapper;
+import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -52,13 +54,41 @@ public abstract class MavenCompletionAndResolutionTestCase extends MavenImportin
     return myCodeInsightFixture.getFile().findReferenceAt(e.getOffset());
   }
 
+  protected XmlTag findTag(String path) {
+    return findTag(myProjectPom, path);
+  }
+
+  protected XmlTag findTag(VirtualFile file, String path) {
+    return findTag(file, path, MavenDomProjectModel.class);
+  }
+
+  protected XmlTag findTag(VirtualFile file, String path, Class<? extends MavenDomElement> clazz) {
+    return MavenDomUtil.findTag(MavenDomUtil.getMavenDomModel(myProject, file, clazz), path);
+  }
+
   protected void assertUnresolved(VirtualFile file) throws IOException {
     PsiReference ref = getReferenceAtCaret(file);
     assertNotNull(ref);
     assertNull(ref.resolve());
   }
 
+  protected void assertUnresolved(VirtualFile file, String expectedText) throws IOException {
+    PsiReference ref = getReferenceAtCaret(file);
+    assertNotNull(ref);
+    assertNull(ref.resolve());
+    assertEquals(expectedText, ref.getCanonicalText());
+  }
+
   protected void assertResolved(VirtualFile file, PsiElement expected) throws IOException {
+    doAssertResolved(file, expected);
+  }
+
+  protected void assertResolved(VirtualFile file, PsiElement expected, String expectedText) throws IOException {
+    PsiReference ref = doAssertResolved(file, expected);
+    assertEquals(expectedText, ref.getCanonicalText());
+  }
+
+  private PsiReference doAssertResolved(VirtualFile file, PsiElement expected) throws IOException {
     assertNotNull("expected reference is null", expected);
 
     PsiReference ref = getReferenceAtCaret(file);
@@ -68,13 +98,7 @@ public abstract class MavenCompletionAndResolutionTestCase extends MavenImportin
       resolved = ((PsiElementWrapper)resolved).getWrappee();
     }
     assertEquals(expected, resolved);
-  }
-
-  protected void assertResolved(VirtualFile file, PsiElement expected, String expectedText) throws IOException {
-    PsiReference ref = getReferenceAtCaret(file);
-    assertNotNull(ref);
-    assertEquals(expected, ref.resolve());
-    assertEquals(expectedText, ref.getCanonicalText());
+    return ref;
   }
 
   protected void assertCompletionVariants(VirtualFile f, String... expected) throws IOException {
@@ -87,6 +111,13 @@ public abstract class MavenCompletionAndResolutionTestCase extends MavenImportin
     assertTrue(actual.toString(), actual.containsAll(Arrays.asList(expected)));
   }
 
+  protected void assertCompletionVariantsIncludeOrdered(VirtualFile f, String... expected) throws IOException {
+    List<String> expectedList = Arrays.asList(expected);
+    List<String> actual = getCompletionVariants(f);
+    actual.retainAll(expectedList);
+    assertEquals(expectedList, actual);
+  }
+
   protected void assertCompletionVariantsDoNotInclude(VirtualFile f, String... expected) throws IOException {
     List<String> actual = getCompletionVariants(f);
     assertFalse(actual.toString(), new ArrayList<String>(Arrays.asList(expected)).removeAll(actual));
@@ -116,7 +147,9 @@ public abstract class MavenCompletionAndResolutionTestCase extends MavenImportin
     assertEquals(expectedText, provider.generateDoc(targetElement, originalElement));
 
     // should work for lookup as well as for tags
-    PsiElement lookupElement = provider.getDocumentationElementForLookupItem(PsiManager.getInstance(myProject), originalElement.getText(), originalElement);
+    PsiElement lookupElement = provider.getDocumentationElementForLookupItem(PsiManager.getInstance(myProject),
+                                                                             originalElement.getText(),
+                                                                             originalElement);
     assertSame(targetElement, lookupElement);
   }
 
index 91725674e75edad3410cd45f69ad9e1ea70f88f6..5ae506206eb96d7b11a1d84fb6b777d5ac7548ad 100644 (file)
@@ -172,7 +172,7 @@ public class MavenModuleCompletionAndResolutionTest extends MavenCompletionAndRe
                      "  <module>unknown<caret>Module</module>" +
                      "</modules>");
 
-    assertResolved(myProjectPom, null, "unknownModule");
+    assertUnresolved(myProjectPom, "unknownModule");
   }
 
   public void testResolutionWithSlashes() throws Exception {
@@ -251,6 +251,21 @@ public class MavenModuleCompletionAndResolutionTest extends MavenCompletionAndRe
                      "</modules>");
 
     assertResolved(myProjectPom, getPsiFile(m), "subDir/m");
+
+    createProjectPom("<groupId>test</groupId>" +
+                     "<artifactId>project</artifactId>" +
+                     "<version>1</version>" +
+                     "<packaging>pom</packaging>" +
+
+                     "<properties>" +
+                     "  <dirName>subDir</dirName>" +
+                     "</properties>" +
+
+                     "<modules>" +
+                     "  <module>${<caret>dirName}/m</module>" +
+                     "</modules>");
+
+    assertResolved(myProjectPom, findTag(myProjectPom, "project.properties.dirName"));
   }
 
   public void testCreatePomQuickFix() throws Throwable {
index 1dc844d1a882aef8c565b462869e24b568840e22..170eec5f5075f28489340056abe565db8edc184e 100644 (file)
@@ -1,11 +1,11 @@
 package org.jetbrains.idea.maven.dom;
 
 import com.intellij.openapi.vfs.VirtualFile;
-import com.intellij.psi.xml.XmlTag;
+import com.intellij.psi.PsiManager;
 import org.jetbrains.idea.maven.dom.model.MavenDomProfiles;
 import org.jetbrains.idea.maven.dom.model.MavenDomProfilesModel;
-import org.jetbrains.idea.maven.dom.model.MavenDomProjectModel;
 import org.jetbrains.idea.maven.dom.model.MavenDomSettingsModel;
+import org.jetbrains.idea.maven.vfs.MavenPropertiesVirtualFileSystem;
 
 public class MavenPropertyCompletionAndResolutionTest extends MavenCompletionAndResolutionTestCase {
   @Override
@@ -37,6 +37,26 @@ public class MavenPropertyCompletionAndResolutionTest extends MavenCompletionAnd
     assertResolved(myProjectPom, findTag("project.version"));
   }
 
+  public void testBuiltInVersionProperty() throws Exception {
+    createProjectPom("<groupId>test</groupId" +
+                     "<artifactId>project</artifactId>" +
+                     "<version>1</version>" +
+
+                     "<name>${<caret>version}</name>");
+
+    assertResolved(myProjectPom, findTag("project.version"));
+  }
+
+  public void testBuiltInBasedirProperty() throws Exception {
+    createProjectPom("<groupId>test</groupId" +
+                     "<artifactId>project</artifactId>" +
+                     "<version>1</version>" +
+
+                     "<name>${<caret>basedir}</name>");
+
+    assertResolved(myProjectPom, PsiManager.getInstance(myProject).findDirectory(myProjectPom.getParent()));
+  }
+
   public void testResolutionWithSeveralProperties() throws Exception {
     createProjectPom("<groupId>test</groupId>" +
                      "<artifactId>project</artifactId>" +
@@ -75,6 +95,16 @@ public class MavenPropertyCompletionAndResolutionTest extends MavenCompletionAnd
     assertUnresolved(myProjectPom);
   }
 
+  //public void testResolutionToAbsentProjectProperty() throws Exception {
+  //  createProjectPom("<groupId>test</groupId>" +
+  //                   "<artifactId>project</artifactId>" +
+  //                   "<version>1</version>" +
+  //
+  //                   "<name>${<caret>project.description}</name>");
+  //
+  //  assertResolved(myProjectPom, findTag("project.version"));
+  //}
+
   public void testResolutionToPomProperty() throws Exception {
     createProjectPom("<groupId>test</groupId>" +
                      "<artifactId>project</artifactId>" +
@@ -392,20 +422,109 @@ public class MavenPropertyCompletionAndResolutionTest extends MavenCompletionAnd
                      "<artifactId>project</artifactId>" +
                      "<version>1</version>" +
 
-                     "<name>${user.home}</name>");
+                     "<name>${<caret>user.home}</name>");
 
-    //assertResolved(myProjectPom, findTag(parent, "project.properties.foo"));
+    assertResolved(myProjectPom, MavenDomUtil.findProperty(myProject,
+                                                           MavenPropertiesVirtualFileSystem.SYSTEM_PROPERTIES_FILE,
+                                                           "user.home"));
   }
 
-  private XmlTag findTag(String path) {
-    return findTag(myProjectPom, path);
-  }
+  public void testEnvProperties() throws Exception {
+    createProjectPom("<groupId>test</groupId" +
+                     "<artifactId>project</artifactId>" +
+                     "<version>1</version>" +
 
-  private XmlTag findTag(VirtualFile file, String path) {
-    return findTag(file, path, MavenDomProjectModel.class);
-  }
+                     "<name>${<caret>env.TEMP}</name>");
 
-  private XmlTag findTag(VirtualFile file, String path, Class<? extends MavenDomElement> clazz) {
-    return MavenDomUtil.findTag(MavenDomUtil.getMavenDomModel(myProject, file, clazz), path);
+    assertResolved(myProjectPom, MavenDomUtil.findProperty(myProject,
+                                                           MavenPropertiesVirtualFileSystem.ENV_PROPERTIES_FILE,
+                                                           "TEMP"));
   }
+
+  //public void testCompletion() throws Exception {
+  //  importProjectWithProfiles("one");
+  //
+  //  createProjectPom("<groupId>test</groupId" +
+  //                   "<artifactId>project</artifactId>" +
+  //                   "<version>1</version>" +
+  //
+  //                   "<parent>" +
+  //                   "  <groupId>test</groupId" +
+  //                   "  <artifactId>parent</artifactId>" +
+  //                   "  <version>1</version>" +
+  //                   "  <relativePath>./parent/pom.xml</version>" +
+  //                   "</parent>" +
+  //
+  //                   "<properties>" +
+  //                   "  <pomProp>value</pomProp>" +
+  //                   "</properties>" +
+  //
+  //                   "<profiles>" +
+  //                   "  <profile>" +
+  //                   "    <id>one</id>" +
+  //                   "    <properties>" +
+  //                   "      <pomProfilesProp>value</pomProfilesProp>" +
+  //                   "    </properties>" +
+  //                   "  </profile>" +
+  //                   "  <profile>" +
+  //                   "    <id>two</id>" +
+  //                   "    <properties>" +
+  //                   "      <pomProfilesPropInactive>value</pomProfilesPropInactive>" +
+  //                   "    </properties>" +
+  //                   "  </profile>" +
+  //                   "</profiles>" +
+  //
+  //                   "<name>${<caret>}</name>");
+  //
+  //  createProfilesXml("<profile>" +
+  //                    "  <id>one</id>" +
+  //                    "  <properties>" +
+  //                    "    <profilesXmlProp>value</profilesXmlProp>" +
+  //                    "  </properties>" +
+  //                    "</profile>");
+  //
+  //  createModulePom("parent",
+  //                  "<groupId>test</groupId" +
+  //                  "<artifactId>parent</artifactId>" +
+  //                  "<version>1</version>" +
+  //
+  //                  "<properties>" +
+  //                  "  <parentPomProp>value</parentPomProp>" +
+  //                  "</properties>" +
+  //
+  //                  "<profiles>" +
+  //                  "  <profile>" +
+  //                  "    <id>one</id>" +
+  //                  "    <properties>" +
+  //                  "      <parentPomProfilesProp>value</parentPomProfilesProp>" +
+  //                  "    </properties>" +
+  //                  "  </profile>" +
+  //                  "</profiles>");
+  //
+  //  createProfilesXml("parent",
+  //                    "<profile>" +
+  //                    "  <id>one</id>" +
+  //                    "  <properties>" +
+  //                    "    <parentProfilesXmlProp>value</parentProfilesXmlProp>" +
+  //                    "  </properties>" +
+  //                    "</profile>");
+  //
+  //  updateSettingsXml("<profiles>" +
+  //                    "  <profile>" +
+  //                    "    <id>one</id>" +
+  //                    "    <properties>" +
+  //                    "      <settingsXmlProp>value</settingsXmlProp>" +
+  //                    "    </properties>" +
+  //                    "  </profile>" +
+  //                    "</profiles>");
+  //
+  //  assertCompletionVariantsIncludeOrdered(myProjectPom,
+  //                                         "pomProp", "pomProfilesProp", "profilesXmlProp",
+  //                                         "parentPomProp", "parentPomProfilesProp", "parentPomProfilesPropInactive", "parentProfilesXmlProp",
+  //                                         "settingsXmlProp",
+  //                                         "project.artifactId", "pom.artifactId",
+  //                                         "version", "basedir",
+  //                                         "settings.localRepository",
+  //                                         "user.home", "env.TEMP");
+  //}
 }