1   package org.apache.maven.shared.release.phase;
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.shared.release.ReleaseExecutionException;
23  import org.apache.maven.shared.release.config.ReleaseDescriptor;
24  import org.apache.maven.shared.release.exec.MavenExecutor;
25  import org.apache.maven.shared.release.exec.MavenExecutorException;
26  import org.codehaus.plexus.PlexusTestCase;
27  import org.jmock.Mock;
28  import org.jmock.core.Constraint;
29  import org.jmock.core.constraint.IsAnything;
30  import org.jmock.core.constraint.IsEqual;
31  import org.jmock.core.constraint.IsNull;
32  import org.jmock.core.matcher.InvokeOnceMatcher;
33  import org.jmock.core.matcher.TestFailureMatcher;
34  import org.jmock.core.stub.ThrowStub;
35  
36  import java.io.File;
37  
38  /**
39   * Test the simple test running phase.
40   *
41   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42   */
43  public class RunGoalsPhaseTest
44      extends PlexusTestCase
45  {
46      private RunGoalsPhase phase;
47  
48      protected void setUp()
49          throws Exception
50      {
51          super.setUp();
52  
53          phase = (RunGoalsPhase) lookup( ReleasePhase.ROLE, "run-preparation-goals" );
54      }
55  
56      public void testExecute()
57          throws ReleaseExecutionException
58      {
59          File testFile = getTestFile( "target/working-directory" );
60  
61          ReleaseDescriptor config = new ReleaseDescriptor();
62          config.setPreparationGoals( "clean integration-test" );
63          config.setWorkingDirectory( testFile.getAbsolutePath() );
64  
65          Mock mock = new Mock( MavenExecutor.class );
66          Constraint[] constraints = new Constraint[]{new IsEqual( testFile ), new IsEqual( "clean integration-test" ),
67              new IsEqual( Boolean.TRUE ), new IsNull(), new IsAnything()};
68  
69          mock.expects( new InvokeOnceMatcher() ).method( "executeGoals" ).with( constraints );
70  
71          phase.setMavenExecutor( (MavenExecutor) mock.proxy() );
72  
73          phase.execute( config, null, null );
74  
75          // just needs to survive the mock
76          assertTrue( true );
77      }
78  
79      public void testSimulate()
80          throws ReleaseExecutionException
81      {
82          File testFile = getTestFile( "target/working-directory" );
83  
84          ReleaseDescriptor config = new ReleaseDescriptor();
85          config.setPreparationGoals( "clean integration-test" );
86          config.setWorkingDirectory( testFile.getAbsolutePath() );
87  
88          Mock mock = new Mock( MavenExecutor.class );
89          Constraint[] constraints = new Constraint[]{new IsEqual( testFile ), new IsEqual( "clean integration-test" ),
90              new IsEqual( Boolean.TRUE ), new IsNull(), new IsAnything()};
91          mock.expects( new InvokeOnceMatcher() ).method( "executeGoals" ).with( constraints );
92  
93          phase.setMavenExecutor( (MavenExecutor) mock.proxy() );
94  
95          phase.simulate( config, null, null );
96  
97          // just needs to survive the mock
98          assertTrue( true );
99      }
100 
101     public void testExecuteException()
102     {
103         File testFile = getTestFile( "target/working-directory" );
104 
105         ReleaseDescriptor config = new ReleaseDescriptor();
106         config.setPreparationGoals( "clean integration-test" );
107         config.setWorkingDirectory( testFile.getAbsolutePath() );
108 
109         Mock mock = new Mock( MavenExecutor.class );
110         Constraint[] constraints = new Constraint[]{new IsEqual( testFile ), new IsEqual( "clean integration-test" ),
111             new IsEqual( Boolean.TRUE ), new IsNull(), new IsAnything()};
112         mock.expects( new InvokeOnceMatcher() ).method( "executeGoals" ).with( constraints ).will(
113             new ThrowStub( new MavenExecutorException( "...", new Exception() ) ) );
114 
115         phase.setMavenExecutor( (MavenExecutor) mock.proxy() );
116 
117         try
118         {
119             phase.execute( config, null, null );
120 
121             fail( "Should have thrown an exception" );
122         }
123         catch ( ReleaseExecutionException e )
124         {
125             assertEquals( "Check cause", MavenExecutorException.class, e.getCause().getClass() );
126         }
127     }
128 
129     public void testSimulateException()
130     {
131         File testFile = getTestFile( "target/working-directory" );
132 
133         ReleaseDescriptor config = new ReleaseDescriptor();
134         config.setPreparationGoals( "clean integration-test" );
135         config.setWorkingDirectory( testFile.getAbsolutePath() );
136 
137         Mock mock = new Mock( MavenExecutor.class );
138         Constraint[] constraints = new Constraint[]{new IsEqual( testFile ), new IsEqual( "clean integration-test" ),
139             new IsEqual( Boolean.TRUE ), new IsNull(), new IsAnything()};
140         mock.expects( new InvokeOnceMatcher() ).method( "executeGoals" ).with( constraints ).will(
141             new ThrowStub( new MavenExecutorException( "...", new Exception() ) ) );
142 
143         phase.setMavenExecutor( (MavenExecutor) mock.proxy() );
144 
145         try
146         {
147             phase.simulate( config, null, null );
148 
149             fail( "Should have thrown an exception" );
150         }
151         catch ( ReleaseExecutionException e )
152         {
153             assertEquals( "Check cause", MavenExecutorException.class, e.getCause().getClass() );
154         }
155     }
156 
157     public void testEmptyGoals()
158         throws Exception
159     {
160         File testFile = getTestFile( "target/working-directory" );
161 
162         ReleaseDescriptor config = new ReleaseDescriptor();
163         config.setPreparationGoals( "" );
164         config.setWorkingDirectory( testFile.getAbsolutePath() );
165 
166         Mock mock = new Mock( MavenExecutor.class );
167         mock.expects( new TestFailureMatcher( "Shouldn't invoke executeGoals" ) ).method( "executeGoals" );
168 
169         phase.setMavenExecutor( (MavenExecutor) mock.proxy() );
170 
171         phase.execute( config, null, null );
172 
173         // just needs to survive the mock
174         assertTrue( true );
175     }
176 }