342c1b582d0ee1eb4d940dbb8511ddd2e98d9a91
[idea/community.git] / plugins / maven / src / test / java / org / jetbrains / idea / maven / compiler / ResourceCopyingTest.java
1 /*
2  * Copyright 2000-2009 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.idea.maven.compiler;
17
18 import com.intellij.compiler.CompilerConfiguration;
19 import com.intellij.compiler.CompilerConfigurationImpl;
20 import com.intellij.openapi.application.AccessToken;
21 import com.intellij.openapi.application.WriteAction;
22 import com.intellij.openapi.command.WriteCommandAction;
23 import com.intellij.openapi.compiler.options.ExcludeEntryDescription;
24 import com.intellij.openapi.module.Module;
25 import com.intellij.openapi.module.ModuleManager;
26 import com.intellij.openapi.vfs.VirtualFile;
27 import com.intellij.testFramework.PsiTestUtil;
28
29 import java.io.File;
30
31 public class ResourceCopyingTest extends MavenCompilingTestCase {
32
33   @Override
34   protected void setUpInWriteAction() throws Exception {
35     super.setUpInWriteAction();
36
37     CompilerConfiguration.getInstance(myProject).addResourceFilePattern("!*.xxx");
38     CompilerConfiguration.getInstance(myProject).addResourceFilePattern("!*.yyy");
39     CompilerConfiguration.getInstance(myProject).addResourceFilePattern("!*.zzz");
40   }
41
42   public void testBasic() throws Exception {
43     createProjectSubFile("src/main/resources/dir/file.properties");
44
45     importProject("<groupId>test</groupId>" +
46                   "<artifactId>project</artifactId>" +
47                   "<version>1</version>");
48     compileModules("project");
49
50     assertCopied("target/classes/dir/file.properties");
51   }
52
53   public void testCustomResources() throws Exception {
54     createProjectSubFile("res/dir1/file1.properties");
55     createProjectSubFile("testRes/dir2/file2.properties");
56
57     importProject("<groupId>test</groupId>" +
58                   "<artifactId>project</artifactId>" +
59                   "<version>1</version>" +
60
61                   "<build>" +
62                   "  <resources>" +
63                   "    <resource><directory>res</directory></resource>" +
64                   "  </resources>" +
65                   "  <testResources>" +
66                   "    <testResource><directory>testRes</directory></testResource>" +
67                   "  </testResources>" +
68                   "</build>");
69
70     compileModules("project");
71
72     assertCopied("target/classes/dir1/file1.properties");
73     assertCopied("target/test-classes/dir2/file2.properties");
74   }
75
76   public void testCustomTargetPath() throws Exception {
77     createProjectSubFile("res/dir/file.properties");
78
79     importProject("<groupId>test</groupId>" +
80                   "<artifactId>project</artifactId>" +
81                   "<version>1</version>" +
82
83                   "<build>" +
84                   "  <resources>" +
85                   "    <resource>" +
86                   "      <directory>res</directory>" +
87                   "      <targetPath>foo</targetPath>" +
88                   "    </resource>" +
89                   "  </resources>" +
90                   "</build>");
91
92     compileModules("project");
93     assertCopied("target/classes/foo/dir/file.properties");
94   }
95
96   public void testResourcesPluginCustomTargetPath() throws Exception {
97     createProjectSubFile("res/dir/file.properties");
98
99     importProject("<groupId>test</groupId>" +
100                   "<artifactId>project</artifactId>" +
101                   "<version>1</version>" +
102
103                   "<build>" +
104                   "  <plugins>" +
105                   "    <plugin>" +
106                   "      <artifactId>maven-resources-plugin</artifactId>" +
107                   "      <version>2.6</version>" +
108                   "      <configuration>" +
109                   "        <outputDirectory>${basedir}/target/resourceOutput</outputDirectory>" +
110                   "      </configuration>" +
111                   "    </plugin>" +
112                   "  </plugins>" +
113                   "  <resources>" +
114                   "    <resource>" +
115                   "      <directory>res</directory>" +
116                   "      <targetPath>foo</targetPath>" +
117                   "    </resource>" +
118                   "  </resources>" +
119                   "</build>");
120
121     compileModules("project");
122     assertCopied("target/resourceOutput/foo/dir/file.properties");
123   }
124
125   public void testAbsoluteCustomTargetPath() throws Exception {
126     createProjectSubFile("res/foo/file.properties");
127
128     importProject("<groupId>test</groupId>" +
129                   "<artifactId>project</artifactId>" +
130                   "<version>1</version>" +
131
132                   "<build>" +
133                   "  <resources>" +
134                   "    <resource>" +
135                   "      <directory>res</directory>" +
136                   "      <targetPath>${build.directory}/anotherDir</targetPath>" +
137                   "    </resource>" +
138                   "  </resources>" +
139                   "</build>");
140
141     compileModules("project");
142     assertCopied("target/anotherDir/foo/file.properties");
143   }
144
145   public void testMavenSpecifiedPattern() throws Exception {
146     createProjectSubFile("res/subdir/a.txt");
147     createProjectSubFile("res/b.txt");
148
149     importProject("<groupId>test</groupId>" +
150                   "<artifactId>project</artifactId>" +
151                   "<version>1</version>" +
152
153                   "<build>" +
154                   "  <resources>" +
155                   "    <resource>" +
156                   "      <directory>res</directory>" +
157                   "      <includes>" +
158                   "        <include>**/**</include>" +
159                   "      </includes>" +
160                   "    </resource>" +
161                   "  </resources>" +
162                   "</build>");
163
164     compileModules("project");
165
166     assertCopied("target/classes/subdir/a.txt");
167     assertCopied("target/classes/b.txt");
168   }
169
170   public void testMavenSpecifiedPatternEndSlash() throws Exception {
171     createProjectSubFile("res/subdir/a.txt");
172     createProjectSubFile("res/b.txt");
173
174     importProject("<groupId>test</groupId>" +
175                   "<artifactId>project</artifactId>" +
176                   "<version>1</version>" +
177
178                   "<build>" +
179                   "  <resources>" +
180                   "    <resource>" +
181                   "      <directory>res</directory>" +
182                   "      <includes>" +
183                   "        <include>**/</include>" +
184                   "      </includes>" +
185                   "    </resource>" +
186                   "  </resources>" +
187                   "</build>");
188
189     compileModules("project");
190
191     assertCopied("target/classes/subdir/a.txt");
192     assertCopied("target/classes/b.txt");
193   }
194
195   public void testIncludesAndExcludes() throws Exception {
196     createProjectSubFile("res/dir/file.xxx");
197     createProjectSubFile("res/dir/file.yyy");
198     createProjectSubFile("res/file.xxx");
199     createProjectSubFile("res/file.yyy");
200     createProjectSubFile("res/file.zzz");
201
202     importProject("<groupId>test</groupId>" +
203                   "<artifactId>project</artifactId>" +
204                   "<version>1</version>" +
205
206                   "<build>" +
207                   "  <resources>" +
208                   "    <resource>" +
209                   "      <directory>res</directory>" +
210                   "      <includes>" +
211                   "        <include>**/*.xxx</include>" +
212                   "        <include>**/*.yyy</include>" +
213                   "      </includes>" +
214                   "      <excludes>" +
215                   "        <exclude>*.xxx</exclude>" +
216                   "        <exclude>dir/*.yyy</exclude>" +
217                   "      </excludes>" +
218                   "    </resource>" +
219                   "  </resources>" +
220                   "</build>");
221
222     compileModules("project");
223
224     assertCopied("target/classes/dir/file.xxx");
225     assertNotCopied("target/classes/dir/file.yyy");
226     assertNotCopied("target/classes/file.xxx");
227     assertCopied("target/classes/file.yyy");
228     assertNotCopied("target/classes/file.zzz");
229   }
230
231   public void testDoNotCopyIgnoredFiles() throws Exception {
232     createProjectSubFile("res/CVS/file.properties");
233     createProjectSubFile("res/.svn/file.properties");
234     createProjectSubFile("res/zzz/file.properties");
235
236     importProject("<groupId>test</groupId>" +
237                   "<artifactId>project</artifactId>" +
238                   "<version>1</version>" +
239
240                   "<build>" +
241                   "  <resources>" +
242                   "    <resource>" +
243                   "      <directory>res</directory>" +
244                   "      <includes>" +
245                   "        <include>**/*.properties</include>" +
246                   "      </includes>" +
247                   "    </resource>" +
248                   "  </resources>" +
249                   "</build>");
250
251     compileModules("project");
252
253     assertNotCopied("target/classes/CVS");
254     assertNotCopied("target/classes/.svn");
255     assertCopied("target/classes/zzz/file.properties");
256   }
257
258   public void testDeletingFilesThatWasCopiedAndThenDeleted() throws Exception {
259     final VirtualFile file = createProjectSubFile("res/file.properties");
260
261     importProject("<groupId>test</groupId>" +
262                   "<artifactId>project</artifactId>" +
263                   "<version>1</version>" +
264
265                   "<build>" +
266                   "  <resources>" +
267                   "    <resource>" +
268                   "      <directory>res</directory>" +
269                   "    </resource>" +
270                   "  </resources>" +
271                   "</build>");
272
273     compileModules("project");
274     assertCopied("target/classes/file.properties");
275
276     new WriteCommandAction.Simple(myProject) {
277       @Override
278       protected void run() throws Throwable {
279         file.delete(this);
280       }
281     }.execute().throwException();
282
283     compileModules("project");
284     assertNotCopied("target/classes/file.properties");
285   }
286
287   public void testDeletingFilesThatWasCopiedAndThenExcluded() throws Exception {
288     createProjectSubFile("res/file.properties");
289
290     importProject("<groupId>test</groupId>" +
291                   "<artifactId>project</artifactId>" +
292                   "<version>1</version>" +
293
294                   "<build>" +
295                   "  <resources>" +
296                   "    <resource>" +
297                   "      <directory>res</directory>" +
298                   "    </resource>" +
299                   "  </resources>" +
300                   "</build>");
301
302     compileModules("project");
303     assertCopied("target/classes/file.properties");
304
305     createProjectPom("<groupId>test</groupId>" +
306                      "<artifactId>project</artifactId>" +
307                      "<version>1</version>" +
308
309                      "<build>" +
310                      "  <resources>" +
311                      "    <resource>" +
312                      "      <directory>res</directory>" +
313                      "      <excludes>" +
314                      "        <exclude>**/*</exclude>" +
315                      "      </excludes>" +
316                      "    </resource>" +
317                      "  </resources>" +
318                      "</build>");
319     importProject();
320
321     compileModules("project");
322     assertNotCopied("target/classes/file.properties");
323   }
324
325   public void testDoNotCopyExcludedStandardResources() throws Exception {
326
327     CompilerConfigurationImpl configuration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
328     configuration.addResourceFilePattern("*.zzz");
329
330     createProjectSubFile("res/file.xxx");
331     createProjectSubFile("res/file.zzz");
332
333     importProject("<groupId>test</groupId>" +
334                   "<artifactId>project</artifactId>" +
335                   "<version>1</version>" +
336
337                   "<build>" +
338                   "  <resources>" +
339                   "    <resource>" +
340                   "      <directory>res</directory>" +
341                   "      <includes>" +
342                   "        <include>**/*.xxx</include>" +
343                   "      </includes>" +
344                   "      <excludes>" +
345                   "        <exclude>**/*.zzz</exclude>" +
346                   "      </excludes>" +
347                   "    </resource>" +
348                   "  </resources>" +
349                   "</build>");
350
351     compileModules("project");
352     assertCopied("target/classes/file.xxx");
353     assertNotCopied("target/classes/file.zzz");
354   }
355
356   public void testDoNotDeleteFilesFromOtherModulesOutput() throws Exception {
357     createProjectSubFile("m1/resources/file.xxx");
358     createProjectSubFile("m2/resources/file.yyy");
359
360     createProjectPom("<groupId>test</groupId>" +
361                      "<artifactId>project</artifactId>" +
362                      "<version>1</version>" +
363
364                      "<modules>" +
365                      "  <module>m1</module>" +
366                      "  <module>m2</module>" +
367                      "</modules>");
368
369     createModulePom("m1",
370                     "<groupId>test</groupId>" +
371                     "<artifactId>m1</artifactId>" +
372                     "<version>1</version>" +
373
374                     "<build>" +
375                     "  <resources>" +
376                     "    <resource>" +
377                     "      <directory>resources</directory>" +
378                     "    </resource>" +
379                     "  </resources>" +
380                     "</build>");
381
382     createModulePom("m2",
383                     "<groupId>test</groupId>" +
384                     "<artifactId>m2</artifactId>" +
385                     "<version>1</version>" +
386
387                     "<build>" +
388                     "  <resources>" +
389                     "    <resource>" +
390                     "      <directory>resources</directory>" +
391                     "    </resource>" +
392                     "  </resources>" +
393                     "</build>");
394     importProject();
395
396     compileModules("project", "m1", "m2");
397     assertCopied("m1/target/classes/file.xxx");
398     assertCopied("m2/target/classes/file.yyy");
399
400     compileModules("m1");
401     assertCopied("m1/target/classes/file.xxx");
402     assertCopied("m2/target/classes/file.yyy");
403
404     compileModules("m2");
405     assertCopied("m1/target/classes/file.xxx");
406     assertCopied("m2/target/classes/file.yyy");
407
408     compileModules("project");
409     assertCopied("m1/target/classes/file.xxx");
410     assertCopied("m2/target/classes/file.yyy");
411   }
412
413   public void testDoNotDeleteFilesFromOtherModulesOutputWhenOutputIsTheSame() throws Exception {
414     createProjectSubFile("resources/file.xxx");
415
416     createProjectPom("<groupId>test</groupId>" +
417                      "<artifactId>project</artifactId>" +
418                      "<version>1</version>" +
419
420                      "<modules>" +
421                      "  <module>m1</module>" +
422                      "  <module>m2</module>" +
423                      "</modules>");
424
425     createModulePom("m1",
426                     "<groupId>test</groupId>" +
427                     "<artifactId>m1</artifactId>" +
428                     "<version>1</version>" +
429
430                     "<build>" +
431                     "  <resources>" +
432                     "    <resource>" +
433                     "      <directory>../resources</directory>" +
434                     "    </resource>" +
435                     "  </resources>" +
436                     "</build>");
437
438     createModulePom("m2",
439                     "<groupId>test</groupId>" +
440                     "<artifactId>m2</artifactId>" +
441                     "<version>1</version>" +
442
443                     "<build>" +
444                     "  <resources>" +
445                     "    <resource>" +
446                     "      <directory>../resources</directory>" +
447                     "    </resource>" +
448                     "  </resources>" +
449                     "</build>");
450     importProject();
451
452     new WriteCommandAction.Simple(myProject) {
453       @Override
454       protected void run() throws Throwable {
455         setModulesOutput(myProjectRoot.createChildDirectory(this, "output"), "project", "m1", "m2");
456       }
457     }.execute().throwException();
458
459
460     compileModules("project", "m1", "m2");
461     assertCopied("output/file.xxx");
462
463     compileModules("m1");
464     assertCopied("output/file.xxx");
465
466     compileModules("m2");
467     assertCopied("output/file.xxx");
468
469     compileModules("project");
470     assertCopied("output/file.xxx");
471   }
472
473   private void setModulesOutput(final VirtualFile output, final String... moduleNames) {
474     AccessToken accessToken = WriteAction.start();
475     try {
476       for (String each : moduleNames) {
477         PsiTestUtil.setCompilerOutputPath(getModule(each), output.getUrl(), false);
478         PsiTestUtil.setCompilerOutputPath(getModule(each), output.getUrl(), true);
479       }
480     }
481     finally {
482       accessToken.finish();
483     }
484   }
485
486   public void testWebResources() throws Exception {
487     if (ignore()) return;
488
489     createProjectSubFile("res/dir/file.properties");
490     createProjectSubFile("res/dir/file.xml");
491     createProjectSubFile("res/file.properties");
492     createProjectSubFile("res/file.xml");
493     createProjectSubFile("res/file.txt");
494
495     importProject("<groupId>test</groupId>" +
496                   "<artifactId>project</artifactId>" +
497                   "<version>1</version>" +
498                   "<packaging>war</packaging>" +
499
500                   "<build>" +
501                   "  <plugins>" +
502                   "    <plugin>" +
503                   "      <groupId>org.apache.maven.plugins</groupId>" +
504                   "      <artifactId>maven-war-plugin</artifactId>" +
505                   "      <configuration>" +
506                   "        <webResources>" +
507                   "          <directory>res</directory>" +
508                   "          <includes>" +
509                   "            <include>**/*.properties</include>" +
510                   "            <include>**/*.xml</include>" +
511                   "          </includes>" +
512                   "          <excludes>" +
513                   "            <exclude>*.properties</exclude>" +
514                   "            <exclude>dir/*.xml</exclude>" +
515                   "          </excludes>" +
516                   "        </webResources>" +
517                   "      </configuration>" +
518                   "    </plugin>" +
519                   "  </plugins>" +
520                   "</build>");
521
522     compileModules("project");
523
524     assertCopied("target/classes/dir/file.properties");
525     assertNotCopied("target/classes/dir/file.xml");
526     assertNotCopied("target/classes/file.properties");
527     assertCopied("target/classes/file.xml");
528     assertNotCopied("target/classes/file.txt");
529   }
530
531   public void testOverridingWebResourceFilters() throws Exception {
532     if (ignore()) return;
533
534     createProjectPom("<groupId>test</groupId>" +
535                      "<artifactId>project</artifactId>" +
536                      "<version>1</version>" +
537                      "<packaging>war</packaging>" +
538
539                      "<build>" +
540                      "  <plugins>" +
541                      "    <plugin>" +
542                      "      <groupId>org.apache.maven.plugins</groupId>" +
543                      "      <artifactId>maven-war-plugin</artifactId>" +
544                      "      <configuration>\n" +
545                      "        <!-- the default value is the filter list under build -->\n" +
546                      "        <!-- specifying a filter will override the filter list under build -->\n" +
547                      "        <filters>\n" +
548                      "          <filter>properties/config.prop</filter>\n" +
549                      "        </filters>\n" +
550                      "        <nonFilteredFileExtensions>\n" +
551                      "          <!-- default value contains jpg,jpeg,gif,bmp,png -->\n" +
552                      "          <nonFilteredFileExtension>pdf</nonFilteredFileExtensions>\n" +
553                      "        </nonFilteredFileExtensions>\n" +
554                      "        <webResources>\n" +
555                      "          <resource>\n" +
556                      "            <directory>resource2</directory>\n" +
557                      "            <!-- it's not a good idea to filter binary files -->\n" +
558                      "            <filtering>false</filtering>\n" +
559                      "          </resource>\n" +
560                      "          <resource>\n" +
561                      "            <directory>configurations</directory>\n" +
562                      "            <!-- enable filtering -->\n" +
563                      "            <filtering>true</filtering>\n" +
564                      "            <excludes>\n" +
565                      "              <exclude>**/properties</exclude>\n" +
566                      "            </excludes>\n" +
567                      "          </resource>\n" +
568                      "        </webResources>\n" +
569                      "      </configuration>" +
570                      "    </plugin>" +
571                      "  </plugins>" +
572                      "</build>");
573   }
574
575   public void testCopingNonMavenResources() throws Exception {
576     if (ignore()) return;
577
578     createProjectSubFile("src/resources/a.txt", "a");
579
580     VirtualFile configDir = createProjectSubDir("src/config");
581     createProjectSubFile("src/config/b.txt", "b");
582     createProjectSubFile("src/config/JavaClass.java", "class JavaClass {}");
583     createProjectSubFile("src/config/xxx.xxx", "xxx"); // *.xxx is excluded from resource coping, see setUpInWriteAction()
584
585     final VirtualFile excludedDir = createProjectSubDir("src/excluded");
586     createProjectSubFile("src/excluded/c.txt", "c");
587
588     importProject("<groupId>test</groupId>" +
589                   "<artifactId>project</artifactId>" +
590                   "<version>1</version>");
591
592     Module module = ModuleManager.getInstance(myProject).findModuleByName("project");
593     PsiTestUtil.addSourceRoot(module, configDir);
594     PsiTestUtil.addSourceRoot(module, excludedDir);
595
596     new WriteCommandAction.Simple(myProject) {
597       @Override
598       protected void run() throws Throwable {
599         CompilerConfiguration.getInstance(myProject).getExcludedEntriesConfiguration()
600           .addExcludeEntryDescription(new ExcludeEntryDescription(excludedDir, true, false, getTestRootDisposable()));
601
602         setModulesOutput(myProjectRoot.createChildDirectory(this, "output"), "project", "m1", "m2");
603       }
604     }.execute().throwException();
605
606     compileModules("project");
607
608     assertCopied("output/a.txt");
609     assertCopied("output/b.txt");
610
611     assertNotCopied("output/JavaClass.java");
612     assertNotCopied("output/xxx.xxx");
613     assertNotCopied("output/c.txt");
614   }
615
616   private void assertCopied(String path) {
617     assertTrue(new File(myProjectPom.getParent().getPath(), path).exists());
618   }
619
620   private void assertNotCopied(String path) {
621     assertFalse(new File(myProjectPom.getParent().getPath(), path).exists());
622   }
623 }