View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.testing;
20  
21  import java.io.File;
22  import java.io.StringReader;
23  import java.util.Map;
24  
25  import org.codehaus.plexus.configuration.PlexusConfiguration;
26  import org.codehaus.plexus.util.xml.Xpp3Dom;
27  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
28  import org.junit.Before;
29  import org.junit.Rule;
30  import org.junit.Test;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertFalse;
34  import static org.junit.Assert.assertTrue;
35  
36  /**
37   * @author Mirko Friedenhagen
38   */
39  public class MojoRuleTest {
40  
41      private boolean beforeWasCalled = false;
42  
43      @Rule
44      public MojoRule rule = new MojoRule() {
45  
46          @Override
47          protected void before() throws Throwable {
48              beforeWasCalled = true;
49          }
50      };
51  
52      private String pom;
53  
54      private Xpp3Dom pomDom;
55  
56      private PlexusConfiguration pluginConfiguration;
57  
58      /** {@inheritDoc} */
59      @Before
60      public void setUp() throws Exception {
61  
62          pom = "<project>" + "<build>"
63                  + "<plugins>"
64                  + "<plugin>"
65                  + "<artifactId>maven-simple-plugin</artifactId>"
66                  + "<configuration>"
67                  + "<keyOne>valueOne</keyOne>"
68                  + "<keyTwo>valueTwo</keyTwo>"
69                  + "</configuration>"
70                  + "</plugin>"
71                  + "</plugins>"
72                  + "</build>"
73                  + "</project>";
74  
75          pomDom = Xpp3DomBuilder.build(new StringReader(pom));
76  
77          pluginConfiguration = rule.extractPluginConfiguration("maven-simple-plugin", pomDom);
78      }
79  
80      /**
81       * @throws Exception if any
82       */
83      @Test
84      public void testPluginConfigurationExtraction() throws Exception {
85          assertEquals("valueOne", pluginConfiguration.getChild("keyOne").getValue());
86  
87          assertEquals("valueTwo", pluginConfiguration.getChild("keyTwo").getValue());
88      }
89  
90      /**
91       * @throws Exception if any
92       */
93      @Test
94      public void testMojoConfiguration() throws Exception {
95          SimpleMojo mojo = new SimpleMojo();
96  
97          mojo = rule.configureMojo(mojo, pluginConfiguration);
98  
99          assertEquals("valueOne", mojo.getKeyOne());
100 
101         assertEquals("valueTwo", mojo.getKeyTwo());
102     }
103 
104     /**
105      * @throws Exception if any
106      */
107     @Test
108     public void testVariableAccessWithoutGetter() throws Exception {
109         SimpleMojo mojo = new SimpleMojo();
110 
111         mojo = rule.configureMojo(mojo, pluginConfiguration);
112 
113         assertEquals("valueOne", rule.getVariableValueFromObject(mojo, "keyOne"));
114 
115         assertEquals("valueTwo", rule.getVariableValueFromObject(mojo, "keyTwo"));
116     }
117 
118     /**
119      * @throws Exception if any
120      */
121     @Test
122     public void testVariableAccessWithoutGetter2() throws Exception {
123         SimpleMojo mojo = new SimpleMojo();
124 
125         mojo = rule.configureMojo(mojo, pluginConfiguration);
126 
127         Map<String, Object> map = rule.getVariablesAndValuesFromObject(mojo);
128 
129         assertEquals("valueOne", map.get("keyOne"));
130 
131         assertEquals("valueTwo", map.get("keyTwo"));
132     }
133 
134     /**
135      * @throws Exception if any
136      */
137     @Test
138     public void testSettingMojoVariables() throws Exception {
139         SimpleMojo mojo = new SimpleMojo();
140 
141         mojo = rule.configureMojo(mojo, pluginConfiguration);
142 
143         rule.setVariableValueToObject(mojo, "keyOne", "myValueOne");
144 
145         assertEquals("myValueOne", rule.getVariableValueFromObject(mojo, "keyOne"));
146     }
147 
148     @Test
149     @WithoutMojo
150     public void testNoRuleWrapper() throws Exception {
151         assertFalse("before executed although WithMojo annotation was added", beforeWasCalled);
152     }
153 
154     @Test
155     public void testWithRuleWrapper() throws Exception {
156         assertTrue("before executed because WithMojo annotation was not added", beforeWasCalled);
157     }
158 
159     /**
160      * @throws Exception if any
161      */
162     @Test
163     public void testLookupInitializedMojo() throws Exception {
164         File pomBaseDir = new File("src/test/projects/property");
165         ParametersMojo mojo = rule.lookupConfiguredMojo(pomBaseDir, "parameters");
166         assertEquals("default", rule.getVariableValueFromObject(mojo, "withDefault"));
167         assertEquals("propertyValue", rule.getVariableValueFromObject(mojo, "withProperty"));
168         assertEquals("propertyValue", rule.getVariableValueFromObject(mojo, "withPropertyAndDefault"));
169     }
170 }