1   package org.apache.maven.plugin.idea;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.idea.stubs.TestCounter;
23  import org.dom4j.Document;
24  import org.dom4j.Element;
25  
26  import java.util.List;
27  
28  /**
29   * @author Edwin Punzalan
30   */
31  public class IdeaProjectTest
32      extends AbstractIdeaTestCase
33  {
34      public void testIdeaProjectTestEnvironment()
35          throws Exception
36      {
37          Document iprDocument = executeMojo( "src/test/project-plugin-configs/min-plugin-config.xml" );
38  
39          testJdkName( iprDocument, null, null );
40      }
41  
42      public void testIdeaProjectVersion4()
43          throws Exception
44      {
45          Document iprDocument = executeMojo( "src/test/project-plugin-configs/plugin-config-idea4.xml" );
46  
47          Element root = iprDocument.getRootElement();
48  
49          Element component = findComponent( root, "ProjectRootManager" );
50  
51          String jdkName = component.attributeValue( "project-jdk-name" );
52  
53          String javaVersion = System.getProperty( "java.version" );
54  
55          assertEquals( "Default jdkName should be from System.Properties",
56                        jdkName, "java version "" + javaVersion + """ );
57  
58          component = findComponent( root, "CompilerConfiguration" );
59  
60          Element patterns = findElementByNameAttribute( component, "wildcardResourcePatterns", null );
61  
62          findElementByNameAttribute( patterns, "entry", "!?*.java" );
63      }
64  
65      public void testIdeaProjectJdk11()
66          throws Exception
67      {
68          Document iprDocument = executeMojo( "src/test/project-plugin-configs/plugin-config-jdk11.xml" );
69  
70          testJdkName( iprDocument, "1.1", "java version 1.1" );
71      }
72  
73      public void testIdeaProjectJdk15()
74          throws Exception
75      {
76          Document iprDocument = executeMojo( "src/test/project-plugin-configs/plugin-config-jdk15.xml" );
77  
78          testJdkName( iprDocument, "1.5", "java version 1.5" );
79      }
80  
81      public void testIdeaProjectWithModules()
82          throws Exception
83      {
84          Document iprDocument = executeMojo( "src/test/project-plugin-configs/plugin-config-modules.xml" );
85  
86          Element component = findComponent( iprDocument.getRootElement(), "ProjectModuleManager" );
87  
88          Element el = findElementByNameAttribute( component, "modules", null );
89  
90          List modules = findElementsByName( el, "module" );
91  
92          assertEquals( "Must have 4 modules", 4, modules.size() );
93  
94          el = (Element) modules.get( 0 );
95          assertEquals( "Test project module",
96                        "$PROJECT_DIR$/plugin-test-" + TestCounter.currentCount() + ".iml",
97                        el.attributeValue( "filepath" ) );
98  
99          el = (Element) modules.get( 1 );
100         assertEquals( "Test module 1",
101                       "$PROJECT_DIR$/module-1/module-1.iml",
102                       el.attributeValue( "filepath" ) );
103 
104         el = (Element) modules.get( 2 );
105         assertEquals( "Test module 2",
106                       "$PROJECT_DIR$/module-2/module-2.iml",
107                       el.attributeValue( "filepath" ) );
108 
109         el = (Element) modules.get( 3 );
110         assertEquals( "Test module 3",
111                       "$PROJECT_DIR$/module-3/module-3.iml",
112                       el.attributeValue( "filepath" ) );
113     }
114 
115     private void testJdkName( Document document, String jdkLevel, String expected )
116         throws Exception
117     {
118         Element root = document.getRootElement();
119 
120         Element component = findComponent( root, "ProjectRootManager" );
121 
122         String jdkName = component.attributeValue( "project-jdk-name" );
123 
124         if ( jdkLevel == null )
125         {
126             jdkLevel = System.getProperty( "java.specification.version" );
127         }
128 
129         if ( jdkLevel.startsWith( "1.4" ) )
130         {
131             assertEquals( "assert-keyword must be true for jdk 1.4",
132                           "true", component.attributeValue( "assert-keyword" ) );
133 
134             assertEquals( "jdk-15 must be false for jdk 1.4",
135                           "false", component.attributeValue( "jdk-15") );
136         }
137         else if ( jdkLevel.compareTo( "1.5" ) >= 0 )
138         {
139             assertEquals( "assert-keyword must be true for jdk >= 1.5",
140                           "true", component.attributeValue( "assert-keyword" ) );
141 
142             assertEquals( "jdk-15 must be true for jdk >= 1.5",
143                           "true", component.attributeValue( "jdk-15") );
144         }
145         else
146         {
147             assertEquals( "assert-keyword must be true for jdk >= 1.5",
148                           "false", component.attributeValue( "assert-keyword" ) );
149         }
150 
151         if ( expected != null )
152         {
153             assertEquals( "Expected jdkName test", jdkName, expected );
154         }
155     }
156 
157     protected Document executeMojo( String pluginXml )
158         throws Exception
159     {
160         return super.executeMojo( "project", pluginXml, "ipr" );
161     }
162 }