View Javadoc
1   /*
2    * Copyright (C) 2016 Ronald Jack Jenkins Jr.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package info.ronjenkins.maven.rtr.steps;
17  
18  import info.ronjenkins.maven.rtr.RTRConfig;
19  import info.ronjenkins.maven.rtr.exceptions.SmartReactorSanityCheckException;
20  
21  import java.util.List;
22  
23  import mockit.Expectations;
24  import mockit.Injectable;
25  import mockit.Mock;
26  import mockit.MockUp;
27  import mockit.Mocked;
28  
29  import org.apache.maven.MavenExecutionException;
30  import org.apache.maven.execution.MavenSession;
31  import org.apache.maven.project.MavenProject;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  import util.TestLogger;
36  import util.TestUtils;
37  
38  public final class PerformSmartReactorSanityChecksTest {
39    @Injectable
40    MavenSession session;
41    @Mocked
42    MavenProject root;
43    @Mocked
44    RTRConfig    config;
45  
46    @Test
47    public void singleProjectNonPomReactorAlwaysWorks() {
48      final PerformSmartReactorSanityChecks step = new PerformSmartReactorSanityChecks();
49      final TestLogger logger = TestUtils.addLogger(step);
50      new Expectations() {
51        {
52          PerformSmartReactorSanityChecksTest.this.session.getProjects().size();
53          this.result = 1;
54          PerformSmartReactorSanityChecksTest.this.session.getTopLevelProject();
55          this.result = PerformSmartReactorSanityChecksTest.this.root;
56          PerformSmartReactorSanityChecksTest.this.root.getArtifact().getType();
57          this.result = "jar";
58        }
59      };
60      try {
61        step.execute(this.session, null);
62      }
63      catch (final MavenExecutionException e) {
64        Assert.fail();
65      }
66      Assert.assertTrue(logger.getErrorLog().isEmpty());
67    }
68  
69    @Test
70    public void singleProjectPomReactorFailsIfNotAllowed() {
71      final PerformSmartReactorSanityChecks step = new PerformSmartReactorSanityChecks();
72      final TestLogger logger = TestUtils.addLogger(step);
73      new Expectations() {
74        {
75          PerformSmartReactorSanityChecksTest.this.session.getProjects().size();
76          this.result = 1;
77          PerformSmartReactorSanityChecksTest.this.session.getTopLevelProject();
78          this.result = PerformSmartReactorSanityChecksTest.this.root;
79          PerformSmartReactorSanityChecksTest.this.root.getArtifact().getType();
80          this.result = "pom";
81          RTRConfig.isSinglePomReactorAllowed(
82              PerformSmartReactorSanityChecksTest.this.session,
83              PerformSmartReactorSanityChecksTest.this.root);
84          this.result = false;
85        }
86      };
87      try {
88        step.execute(this.session, null);
89      }
90      catch (final MavenExecutionException e) {
91        Assert.assertTrue(e instanceof SmartReactorSanityCheckException);
92      }
93      Assert.assertFalse(logger.getErrorLog().isEmpty());
94    }
95  
96    @Test
97    public void singleProjectPomReactorWorksIfAllowed() {
98      final PerformSmartReactorSanityChecks step = new PerformSmartReactorSanityChecks();
99      final TestLogger logger = TestUtils.addLogger(step);
100     new Expectations() {
101       {
102         PerformSmartReactorSanityChecksTest.this.session.getProjects().size();
103         this.result = 1;
104         PerformSmartReactorSanityChecksTest.this.session.getTopLevelProject();
105         this.result = PerformSmartReactorSanityChecksTest.this.root;
106         PerformSmartReactorSanityChecksTest.this.root.getArtifact().getType();
107         this.result = "pom";
108         RTRConfig.isSinglePomReactorAllowed(
109             PerformSmartReactorSanityChecksTest.this.session,
110             PerformSmartReactorSanityChecksTest.this.root);
111         this.result = true;
112       }
113     };
114     try {
115       step.execute(this.session, null);
116     }
117     catch (final MavenExecutionException e) {
118       Assert.fail();
119     }
120     Assert.assertTrue(logger.getErrorLog().isEmpty());
121   }
122 
123   @Test
124   public void twoProjectReactorAlwaysWorks() {
125     final PerformSmartReactorSanityChecks step = new PerformSmartReactorSanityChecks();
126     final TestLogger logger = TestUtils.addLogger(step);
127     final List<MavenProject> projects = new MockUp<List<MavenProject>>() {
128       /*
129        * TODO I tried session.getProjects().size(); result = 2; in the
130        * expectations block but it kept returning 1. Investigate further and
131        * file a bug with JMockit if necessary.
132        */
133       @Mock
134       int size() {
135         return 2;
136       }
137     }.getMockInstance();
138     new Expectations() {
139       {
140         PerformSmartReactorSanityChecksTest.this.session.getProjects();
141         this.result = projects;
142       }
143     };
144     try {
145       step.execute(this.session, null);
146     }
147     catch (final MavenExecutionException e) {
148       Assert.fail();
149     }
150     Assert.assertTrue(logger.getErrorLog().isEmpty());
151   }
152 }