copy package-info with package annotations (IDEA-79980)
authoranna <Anna.Kozlova@jetbrains.com>
Tue, 17 Jan 2012 16:52:10 +0000 (17:52 +0100)
committeranna <Anna.Kozlova@jetbrains.com>
Tue, 17 Jan 2012 16:54:01 +0000 (17:54 +0100)
java/java-psi-impl/src/com/intellij/psi/impl/source/PsiJavaFileBaseImpl.java
java/java-tests/testData/refactoring/copy/multifile/packageInfo/from/package-info.java [new file with mode: 0644]
java/java-tests/testData/refactoring/copy/multifile/packageInfo/to/package-info.expected.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/CopyTest.java

index bf1dfb0fe3fe2cb8948f410a261fee50bec65014..a086956e16fc48fbf9ef7f32991ecce4ecafb039 100644 (file)
@@ -111,7 +111,8 @@ public abstract class PsiJavaFileBaseImpl extends PsiFileImpl implements PsiJava
     final PsiElementFactory factory = JavaPsiFacade.getInstance(getProject()).getElementFactory();
     if (packageStatement != null) {
       if (packageName.length() > 0) {
-        packageStatement.replace(factory.createPackageStatement(packageName));
+        final PsiJavaCodeReferenceElement reference = packageStatement.getPackageReference();
+        reference.replace(factory.createReferenceFromText(packageName, packageStatement));
       }
       else {
         packageStatement.delete();
diff --git a/java/java-tests/testData/refactoring/copy/multifile/packageInfo/from/package-info.java b/java/java-tests/testData/refactoring/copy/multifile/packageInfo/from/package-info.java
new file mode 100644 (file)
index 0000000..2135ff2
--- /dev/null
@@ -0,0 +1,3 @@
+@javax.xml.bind.annotation.XmlSchema(elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
+        namespace = "http://www.mynamespaceurl.com/myHits")
+package from;
diff --git a/java/java-tests/testData/refactoring/copy/multifile/packageInfo/to/package-info.expected.java b/java/java-tests/testData/refactoring/copy/multifile/packageInfo/to/package-info.expected.java
new file mode 100644 (file)
index 0000000..dedc521
--- /dev/null
@@ -0,0 +1,3 @@
+@javax.xml.bind.annotation.XmlSchema(elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
+        namespace = "http://www.mynamespaceurl.com/myHits")
+package to;
index a8131c0a62362597846810d0b91cfd5be704e630..a0eee1b2a83d67dd5650a7c7ecd4e272f1feb2cc 100644 (file)
@@ -93,4 +93,30 @@ public class CopyTest extends CodeInsightTestCase {
     assertNotNull(root.findFileByRelativePath("to/1.txt"));
     assertNotNull(root.findFileByRelativePath("to/2.txt"));
   }
+
+  public void testPackageInfo() throws Exception {
+    String rootBefore = getRoot();
+    PsiTestUtil.removeAllRoots(myModule, JavaSdkImpl.getMockJdk17());
+    final VirtualFile root = PsiTestUtil.createTestProjectStructure(myProject, myModule, rootBefore, myFilesToDelete);
+
+    final VirtualFile first = root.findFileByRelativePath("from/package-info.java");
+    assertNotNull(first);
+    
+    final PsiFile firstPsi = myPsiManager.findFile(first);
+    
+    assertTrue(CopyHandler.canCopy(new PsiElement[]{firstPsi}));
+
+    final VirtualFile toDir = root.findChild("to");
+    assertNotNull(toDir);
+    final PsiDirectory targetDirectory = myPsiManager.findDirectory(toDir);
+
+    CopyHandler.doCopy(new PsiElement[]{firstPsi}, targetDirectory);
+
+    final VirtualFile dest = root.findFileByRelativePath("to/package-info.java");
+    assertNotNull(dest);
+
+    VirtualFile fileExpected = root.findFileByRelativePath("to/package-info.expected.java");
+    
+    PlatformTestUtil.assertFilesEqual(fileExpected, dest);
+  }
 }