View Javadoc
1   /*
2    * SPDX-FileCopyrightText: Copyright (c) 2011-2026 Yegor Bugayenko
3    * SPDX-License-Identifier: MIT
4    */
5   package com.qulice.maven;
6   
7   import com.qulice.spi.Environment;
8   import java.io.File;
9   import java.io.IOException;
10  import java.nio.charset.Charset;
11  import java.nio.charset.StandardCharsets;
12  import java.util.Collection;
13  import java.util.Collections;
14  import java.util.LinkedList;
15  import java.util.Map;
16  import java.util.Properties;
17  import org.apache.maven.monitor.logging.DefaultLog;
18  import org.apache.maven.project.MavenProject;
19  import org.codehaus.plexus.DefaultPlexusContainer;
20  import org.codehaus.plexus.PlexusContainer;
21  import org.codehaus.plexus.PlexusContainerException;
22  import org.codehaus.plexus.context.Context;
23  import org.codehaus.plexus.context.ContextException;
24  import org.codehaus.plexus.logging.console.ConsoleLogger;
25  import org.slf4j.impl.StaticLoggerBinder;
26  
27  /**
28   * Mocker of {@link MavenProject}.
29   * @since 0.4
30   */
31  @SuppressWarnings("PMD.TooManyMethods")
32  public final class MavenEnvironmentMocker {
33  
34      /**
35       * Env mocker.
36       */
37      private final Environment.Mock ienv;
38  
39      /**
40       * Project.
41       */
42      private MavenProjectMocker prj;
43  
44      /**
45       * Plexus container, mock.
46       */
47      private final PlexusContainer container;
48  
49      /**
50       * Xpath queries to test pom.xml.
51       */
52      private Collection<String> ass;
53  
54      /**
55       * Public ctor.
56       * @throws IOException If some IO problem inside
57       */
58      public MavenEnvironmentMocker() throws IOException {
59          this.prj = new MavenProjectMocker();
60          this.ass = new LinkedList<>();
61          this.ienv = new Environment.Mock();
62          try {
63              this.container = new DefaultPlexusContainer();
64          } catch (final PlexusContainerException ex) {
65              throw new IOException(ex);
66          }
67      }
68  
69      /**
70       * Inject this object into plexus container.
71       * @param role The role
72       * @param hint The hint
73       * @param object The object to return
74       * @return This object
75       * @throws Exception If something wrong happens inside
76       */
77      public MavenEnvironmentMocker inPlexus(final String role, final String hint,
78          final Object object) throws Exception {
79          this.container.addComponent(object, role);
80          return this;
81      }
82  
83      /**
84       * With this project mocker.
85       * @param mocker The project mocker
86       * @return This object
87       */
88      public MavenEnvironmentMocker with(final MavenProjectMocker mocker) {
89          this.prj = mocker;
90          return this;
91      }
92  
93      /**
94       * With this file on board.
95       * @param name File name related to basedir
96       * @param content File content to write
97       * @return This object
98       * @throws IOException If some IO problem
99       */
100     public MavenEnvironmentMocker withFile(final String name,
101         final String content) throws IOException {
102         this.ienv.withFile(name, content);
103         return this;
104     }
105 
106     /**
107      * With this file on board.
108      * @param name File name related to basedir
109      * @param bytes File content to write
110      * @return This object
111      * @throws IOException If some IO problem
112      */
113     public MavenEnvironmentMocker withFile(final String name,
114         final byte[] bytes) throws IOException {
115         this.ienv.withFile(name, bytes);
116         return this;
117     }
118 
119     /**
120      * With list of xpath queries to validate pom.xml.
121      * @param asserts Collection of xpath queries
122      * @return This object
123      */
124     public MavenEnvironmentMocker withAsserts(
125         final Collection<String> asserts) {
126         this.ass = Collections.unmodifiableCollection(asserts);
127         return this;
128     }
129 
130     /**
131      * Mock it.
132      * @return The environment just mocked
133      * @throws Exception If something wrong happens inside
134      */
135     public MavenEnvironment mock() throws Exception {
136         StaticLoggerBinder.getSingleton().setMavenLog(
137             new DefaultLog(
138                 new ConsoleLogger()
139             )
140         );
141         this.prj.inBasedir(this.ienv.basedir());
142         return new MavenEnvironment.Wrap(
143             this.ienv,
144             new MavenEnvironmentMocker.FakeMavenEnvironment(
145                 this.prj.mock(),
146                 new MavenEnvironmentMocker.FakeContext(this.container),
147                 this.ass
148             )
149         );
150     }
151 
152     /**
153      * FakeContext.
154      * A mock to a context.
155      * @since 0.24.1
156      */
157     private static final class FakeContext implements Context {
158 
159         /**
160          * Container.
161          */
162         private final PlexusContainer container;
163 
164         FakeContext(final PlexusContainer ctainer) {
165             this.container = ctainer;
166         }
167 
168         @Override
169         public boolean contains(final Object obj) {
170             return true;
171         }
172 
173         @Override
174         public void put(final Object obja, final Object objb) {
175             // Intentionally left blank
176         }
177 
178         @Override
179         public Object get(final Object obj) throws ContextException {
180             return this.container;
181         }
182 
183         @Override
184         public Map<Object, Object> getContextData() {
185             return Collections.emptyMap();
186         }
187     }
188 
189     /**
190      * FakeMavenEnvironment.
191      * A mock to MavenEnvironment.
192      * @since 0.24.1
193      */
194     private static final class FakeMavenEnvironment implements MavenEnvironment {
195 
196         /**
197          * Project.
198          */
199         private final MavenProject proj;
200 
201         /**
202          * Context.
203          */
204         private final Context ctx;
205 
206         /**
207          * Asserts.
208          */
209         private final Collection<String> assrts;
210 
211         FakeMavenEnvironment(
212             final MavenProject prj,
213             final Context ctx,
214             final Collection<String> asserts
215         ) {
216             this.proj = prj;
217             this.ctx = ctx;
218             this.assrts = asserts;
219         }
220 
221         @Override
222         public MavenProject project() {
223             return this.proj;
224         }
225 
226         @Override
227         public Properties properties() {
228             throw new UnsupportedOperationException("#properties()");
229         }
230 
231         @Override
232         public Context context() {
233             return this.ctx;
234         }
235 
236         @Override
237         public Properties config() {
238             throw new UnsupportedOperationException("#config()");
239         }
240 
241         @Override
242         public MojoExecutor executor() {
243             throw new UnsupportedOperationException("#executor()");
244         }
245 
246         @Override
247         public Collection<String> asserts() {
248             return this.assrts;
249         }
250 
251         @Override
252         public File basedir() {
253             return this.proj.getBasedir();
254         }
255 
256         @Override
257         public File tempdir() {
258             return new File("/tmp");
259         }
260 
261         @Override
262         public File outdir() {
263             return this.proj.getBasedir();
264         }
265 
266         @Override
267         public String param(final String name, final String value) {
268             return "";
269         }
270 
271         @Override
272         public ClassLoader classloader() {
273             return ClassLoader.getSystemClassLoader();
274         }
275 
276         @Override
277         public Collection<String> classpath() {
278             return Collections.emptyList();
279         }
280 
281         @Override
282         public Collection<File> files(final String pattern) {
283             return Collections.emptyList();
284         }
285 
286         @Override
287         public boolean exclude(final String check, final String name) {
288             return true;
289         }
290 
291         @Override
292         public Collection<String> excludes(final String checker) {
293             return Collections.emptyList();
294         }
295 
296         @Override
297         public Charset encoding() {
298             return StandardCharsets.UTF_8;
299         }
300     }
301 }