1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
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      
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  
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  
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 
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 
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 
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 
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 }