[duplicates] enable duplicates analysis in PyCharm/WebStorm/PhpStorm/RubyMine
[idea/community.git] / java / compiler / tests / com / intellij / compiler / artifacts / ArtifactUtilTest.java
1 package com.intellij.compiler.artifacts;
2
3 import com.intellij.openapi.module.Module;
4 import com.intellij.openapi.util.Pair;
5 import com.intellij.openapi.vfs.VirtualFile;
6 import com.intellij.packaging.artifacts.Artifact;
7 import com.intellij.packaging.elements.CompositePackagingElement;
8 import com.intellij.packaging.elements.PackagingElement;
9 import com.intellij.packaging.impl.artifacts.*;
10 import org.jetbrains.annotations.NotNull;
11
12 import java.util.List;
13
14 /**
15  * @author nik
16  */
17 public class ArtifactUtilTest extends PackagingElementsTestCase {
18   public void testProcessElementsWithRelativePath() {
19     final Artifact a = addArtifact(root().dir("lib").file(createFile("a.txt")));
20     final ElementToStringCollector processor = new ElementToStringCollector(true);
21     ArtifactUtil.processElementsByRelativePath(a.getRootElement(), "lib/a.txt", getContext(), PlainArtifactType.getInstance(),
22                                                PackagingElementPath.EMPTY, processor);
23     assertEquals("/lib/file:" + getProjectBasePath() + "/a.txt\n", processor.getOutput());
24   }
25
26   public void testProcessDirectory() {
27     final ElementToStringCollector processor = new ElementToStringCollector();
28     final Artifact a = addArtifact(root().dir("lib").file(createFile("x.jar")));
29     processDirectoryChildren(a.getRootElement(), "lib/", processor);
30     assertEquals("file:" + getProjectBasePath() + "/x.jar\n", processor.getOutput());
31
32     final Artifact b = addArtifact(root().artifact(a).dir("lib").file(createFile("y.jar")));
33     processDirectoryChildren(b.getRootElement(), "lib", processor);
34     assertEquals("file:" + getProjectBasePath() + "/x.jar\n" +
35                  "file:" + getProjectBasePath() + "/y.jar\n", processor.getOutput());
36   }
37
38   public void testProcessParents() {
39     final Artifact exploded = addArtifact("exploded:", root());
40     final Artifact war = addArtifact("war", PlainArtifactType.getInstance(),
41                                     archive("web.war")
42                                         .dir("dir")
43                                            .artifact(exploded)
44                                         .build());
45     addArtifact("ear", PlainArtifactType.getInstance(),
46                 archive("ear.ear")
47                     .artifact(war)
48                     .build());
49     final MyParentElementProcessor processor = new MyParentElementProcessor();
50
51     ArtifactUtil.processParents(exploded, getContext(), processor, 2);
52     assertEquals("war:dir\n" +
53                  "war:web.war/dir\n" +
54                  "ear:ear.ear/web.war/dir\n", processor.getLog());
55
56     ArtifactUtil.processParents(exploded, getContext(), processor, 1);
57     assertEquals("war:dir\n" +
58                  "war:web.war/dir\n", processor.getLog());
59
60     ArtifactUtil.processParents(exploded, getContext(), processor, 0);
61     assertEquals("war:dir\n", processor.getLog());
62
63     ArtifactUtil.processParents(war, getContext(), processor, 2);
64     assertEquals("war:web.war\n" +
65                  "ear:ear.ear/web.war\n", processor.getLog());
66
67   }
68
69   public void testGetArtifactsContainingModuleOutput() {
70     Module m = addModule("m", null);
71     Artifact a = addArtifact("a", root().dir("d").module(m));
72     Artifact b = addArtifact("b", root().artifact(a));
73     addArtifact("c", root().file(createFile("x.txt")));
74     assertSameElements(ArtifactUtil.getArtifactsContainingModuleOutput(m), a, b);
75   }
76
77   public void testCopyElement() {
78     VirtualFile file = createFile("a.txt");
79     final Artifact a = addArtifact(root().dir("lib").file(file));
80     CompositePackagingElement<?> copy = ArtifactUtil.copyFromRoot(a.getRootElement(), myProject);
81     assertLayout(copy, "<root>\n" +
82                        " lib/\n" +
83                        "  file:" + file.getPath() + "\n");
84   }
85
86   private void processDirectoryChildren(final CompositePackagingElement<?> rootElement, final String relativePath, ElementToStringCollector processor) {
87     ArtifactUtil.processDirectoryChildren(rootElement, PackagingElementPath.EMPTY, relativePath, getContext(), PlainArtifactType.getInstance(), processor);
88   }
89
90   private static class ElementToStringCollector extends PackagingElementProcessor<PackagingElement<?>> {
91     private final StringBuilder myBuilder = new StringBuilder();
92     private final boolean myAddParentPaths;
93
94     private ElementToStringCollector() {
95       myAddParentPaths = false;
96     }
97
98     private ElementToStringCollector(boolean addParentPaths) {
99       myAddParentPaths = addParentPaths;
100     }
101
102     @Override
103     public boolean process(@NotNull PackagingElement<?> element, @NotNull PackagingElementPath path) {
104       if (myAddParentPaths) {
105         myBuilder.append(path.getPathString()).append("/");
106       }
107       myBuilder.append(element.toString()).append("\n");
108       return true;
109     }
110
111     public String getOutput() {
112       final String output = myBuilder.toString();
113       myBuilder.setLength(0);
114       return output;
115     }
116   }
117
118
119   private static class MyParentElementProcessor extends ParentElementProcessor {
120     private final StringBuilder myLog = new StringBuilder();
121
122     @Override
123     public boolean process(@NotNull CompositePackagingElement<?> element, @NotNull List<Pair<Artifact,CompositePackagingElement<?>>> parents, @NotNull Artifact artifact) {
124       myLog.append(artifact.getName()).append(":").append(element.getName());
125       for (Pair<Artifact, CompositePackagingElement<?>> parent : parents) {
126         myLog.append("/").append(parent.getSecond().getName());
127       }
128       myLog.append("\n");
129       return true;
130     }
131
132     public String getLog() {
133       final String output = myLog.toString();
134       myLog.setLength(0);
135       return output;
136     }
137   }
138 }