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