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.StringReader;
22  import java.util.Map;
23  
24  import org.apache.maven.api.plugin.testing.MojoTest;
25  import org.codehaus.plexus.configuration.PlexusConfiguration;
26  import org.codehaus.plexus.util.xml.Xpp3Dom;
27  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
28  
29  /**
30   * @author Jason van Zyl
31   */
32  @MojoTest
33  public class MojoTestCaseTest extends AbstractMojoTestCase {
34      private String pom;
35  
36      private Xpp3Dom pomDom;
37  
38      private PlexusConfiguration pluginConfiguration;
39  
40      /** {@inheritDoc} */
41      @Override
42      protected void setUp() throws Exception {
43          super.setUp();
44  
45          pom = "<project>" + "<build>"
46                  + "<plugins>"
47                  + "<plugin>"
48                  + "<artifactId>maven-simple-plugin</artifactId>"
49                  + "<configuration>"
50                  + "<keyOne>valueOne</keyOne>"
51                  + "<keyTwo>valueTwo</keyTwo>"
52                  + "</configuration>"
53                  + "</plugin>"
54                  + "</plugins>"
55                  + "</build>"
56                  + "</project>";
57  
58          pomDom = Xpp3DomBuilder.build(new StringReader(pom));
59  
60          pluginConfiguration = extractPluginConfiguration("maven-simple-plugin", pomDom);
61      }
62  
63      /**
64       * @throws Exception if any
65       */
66      public void testPluginConfigurationExtraction() throws Exception {
67          assertEquals("valueOne", pluginConfiguration.getChild("keyOne").getValue());
68  
69          assertEquals("valueTwo", pluginConfiguration.getChild("keyTwo").getValue());
70      }
71  
72      /**
73       * @throws Exception if any
74       */
75      public void testMojoConfiguration() throws Exception {
76          SimpleMojo mojo = new SimpleMojo();
77  
78          mojo = (SimpleMojo) configureMojo(mojo, pluginConfiguration);
79  
80          assertEquals("valueOne", mojo.getKeyOne());
81  
82          assertEquals("valueTwo", mojo.getKeyTwo());
83      }
84  
85      /**
86       * @throws Exception if any
87       */
88      public void testVariableAccessWithoutGetter() throws Exception {
89          SimpleMojo mojo = new SimpleMojo();
90  
91          mojo = (SimpleMojo) configureMojo(mojo, pluginConfiguration);
92  
93          assertEquals("valueOne", (String) getVariableValueFromObject(mojo, "keyOne"));
94  
95          assertEquals("valueTwo", (String) getVariableValueFromObject(mojo, "keyTwo"));
96      }
97  
98      /**
99       * @throws Exception if any
100      */
101     public void testVariableAccessWithoutGetter2() throws Exception {
102         SimpleMojo mojo = new SimpleMojo();
103 
104         mojo = (SimpleMojo) configureMojo(mojo, pluginConfiguration);
105 
106         Map<String, Object> map = getVariablesAndValuesFromObject(mojo);
107 
108         assertEquals("valueOne", (String) map.get("keyOne"));
109 
110         assertEquals("valueTwo", (String) map.get("keyTwo"));
111     }
112 
113     /**
114      * @throws Exception if any
115      */
116     public void testSettingMojoVariables() throws Exception {
117         SimpleMojo mojo = new SimpleMojo();
118 
119         mojo = (SimpleMojo) configureMojo(mojo, pluginConfiguration);
120 
121         setVariableValueToObject(mojo, "keyOne", "myValueOne");
122 
123         assertEquals("myValueOne", (String) getVariableValueFromObject(mojo, "keyOne"));
124     }
125 }