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.
21 package org
.jetbrains
.idea
.devkit
.build
;
23 import com
.intellij
.compiler
.ant
.BuildTargetsFactory
;
24 import com
.intellij
.compiler
.ant
.ModuleChunk
;
25 import com
.intellij
.openapi
.application
.ApplicationManager
;
26 import com
.intellij
.openapi
.module
.Module
;
27 import com
.intellij
.openapi
.roots
.CompilerModuleExtension
;
28 import com
.intellij
.openapi
.roots
.ModifiableRootModel
;
29 import com
.intellij
.openapi
.roots
.ModuleRootManager
;
30 import com
.intellij
.openapi
.util
.Comparing
;
31 import com
.intellij
.openapi
.util
.text
.StringUtil
;
32 import com
.intellij
.openapi
.vfs
.VirtualFile
;
33 import com
.intellij
.psi
.XmlElementFactory
;
34 import com
.intellij
.psi
.codeStyle
.CodeStyleManager
;
35 import com
.intellij
.psi
.xml
.XmlAttribute
;
36 import com
.intellij
.psi
.xml
.XmlTag
;
37 import com
.intellij
.testFramework
.IdeaTestCase
;
38 import com
.intellij
.util
.IncorrectOperationException
;
39 import org
.jetbrains
.idea
.devkit
.build
.ant
.BuildJarTarget
;
41 import java
.io
.PrintWriter
;
42 import java
.io
.StringWriter
;
44 public class GenerateAntTest
extends IdeaTestCase
{
46 public void testP1() throws Exception
{
47 ApplicationManager
.getApplication().runWriteAction(new Runnable() {
49 final ModifiableRootModel model
= ModuleRootManager
.getInstance(myModule
).getModifiableModel();
50 final VirtualFile parent
= myModule
.getModuleFile().getParent();
51 assertTrue(parent
!= null);
52 final CompilerModuleExtension extension
= model
.getModuleExtension(CompilerModuleExtension
.class);
53 extension
.inheritCompilerOutputPath(false);
54 extension
.setCompilerOutputPath(parent
.getUrl() + "/classes");
58 checkJarTarget(new ModuleChunk(new Module
[]{getModule()}));
61 private void checkJarTarget(ModuleChunk chunk
) throws Exception
{
62 final StringWriter targetText
= new StringWriter();
63 final PrintWriter dataOutput
= new PrintWriter(targetText
);
64 new BuildJarTarget(chunk
, BuildTargetsFactory
.getInstance().getDefaultOptions(getProject()), new PluginBuildConfiguration(getModule())).generate(dataOutput
);
66 final String lowercased
= StringUtil
.toLowerCase(myModule
.getName());
67 final String expected
= "<target name=\"plugin.build.jar."+
68 lowercased
+ "\" depends=\"compile.module." + lowercased
+
69 "\" description=\"Build plugin archive for module '" + myModule
.getName() + "'\">\n" +
70 " <jar destfile=\"${"+ lowercased
+ ".plugin.path.jar}\" duplicate=\"preserve\">\n" +
71 " <zipfileset dir=\"${module." + lowercased
+ ".basedir}/classes\"/>\n" +
72 " <zipfileset file=\"${module." + lowercased
+ ".basedir}/META-INF/plugin.xml\" prefix=\"META-INF\"/>\n" +
74 " <attribute name=\"Created-By\" value=\"IntelliJ IDEA\"/>\n" +
75 " <attribute name=\"Manifest-Version\" value=\"1.0\"/>\n" +
79 checkBuildsEqual(targetText
.toString(), expected
);
82 private void checkBuildsEqual(String generated
, String expected
) throws IncorrectOperationException
{
83 final CodeStyleManager manager
= CodeStyleManager
.getInstance(myProject
);
84 XmlTag genTag
= XmlElementFactory
.getInstance(myProject
).createTagFromText(StringUtil
.convertLineSeparators(generated
));
85 XmlTag expTag
= XmlElementFactory
.getInstance(myProject
).createTagFromText(StringUtil
.convertLineSeparators(expected
));
86 if (!tagsEqual(genTag
, expTag
)) {
87 genTag
= (XmlTag
)manager
.reformat(manager
.reformat(genTag
));
88 expTag
= (XmlTag
)manager
.reformat(manager
.reformat(expTag
));
89 assertEquals("Text mismatch: ", expTag
.getText(), genTag
.getText());
93 private static boolean tagsEqual(XmlTag genTag
, XmlTag expTag
) {
94 if (!attributesEqual(genTag
, expTag
)) return false;
95 final XmlTag
[] gsubTags
= genTag
.getSubTags();
96 final XmlTag
[] esubTags
= expTag
.getSubTags();
97 if (gsubTags
.length
!= esubTags
.length
) return false;
98 for (int i
= 0; i
< esubTags
.length
; i
++) {
99 XmlTag esubTag
= esubTags
[i
];
100 XmlTag gsubTag
= gsubTags
[i
];
101 if (!tagsEqual(gsubTag
, esubTag
)) return false;
106 private static boolean attributesEqual(XmlTag genTag
, XmlTag expTag
) {
107 final XmlAttribute
[] gattributes
= genTag
.getAttributes();
108 final XmlAttribute
[] eattributes
= expTag
.getAttributes();
109 if (gattributes
.length
!= eattributes
.length
) return false;
110 for (int i
= 0; i
< eattributes
.length
; i
++) {
111 XmlAttribute eattribute
= eattributes
[i
];
112 XmlAttribute gattribute
= gattributes
[i
];
113 // logical comparison of the attributes (namespace:localname and display value)
114 if (!Comparing
.strEqual(gattribute
.getLocalName(), eattribute
.getLocalName())) return false;
115 if (!Comparing
.strEqual(gattribute
.getNamespace(), eattribute
.getNamespace())) return false;
116 if (!Comparing
.strEqual(gattribute
.getDisplayValue(), eattribute
.getDisplayValue())) return false;