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.IOException;
9   import java.util.ArrayList;
10  import java.util.Collection;
11  import java.util.Collections;
12  import org.apache.maven.monitor.logging.DefaultLog;
13  import org.apache.maven.project.MavenProject;
14  import org.codehaus.plexus.DefaultPlexusContainer;
15  import org.codehaus.plexus.PlexusContainer;
16  import org.codehaus.plexus.PlexusContainerException;
17  import org.codehaus.plexus.logging.console.ConsoleLogger;
18  import org.slf4j.impl.StaticLoggerBinder;
19  
20  /**
21   * Mocker of {@link MavenProject}.
22   *
23   * @since 0.4
24   */
25  public final class MavenEnvironmentMocker {
26  
27      /**
28       * Env mocker.
29       */
30      private final Environment.Mock ienv;
31  
32      /**
33       * Project.
34       */
35      private MavenProjectMocker prj;
36  
37      /**
38       * Plexus container, mock.
39       */
40      private final PlexusContainer container;
41  
42      /**
43       * Xpath queries to test pom.xml.
44       */
45      private Collection<String> ass;
46  
47      /**
48       * Public ctor.
49       *
50       * @throws IOException If some IO problem inside
51       */
52      public MavenEnvironmentMocker() throws IOException {
53          this.prj = new MavenProjectMocker();
54          this.ass = new ArrayList<>(0);
55          this.ienv = new Environment.Mock();
56          try {
57              this.container = new DefaultPlexusContainer();
58          } catch (final PlexusContainerException ex) {
59              throw new IOException(ex);
60          }
61      }
62  
63      /**
64       * Inject this object into plexus container.
65       *
66       * @param role The role
67       * @param hint The hint
68       * @param object The object to return
69       * @return This object
70       * @throws Exception If something wrong happens inside
71       */
72      public MavenEnvironmentMocker inPlexus(final String role, final String hint,
73          final Object object) throws Exception {
74          this.container.addComponent(object, role);
75          return this;
76      }
77  
78      /**
79       * With this project mocker.
80       *
81       * @param mocker The project mocker
82       * @return This object
83       */
84      public MavenEnvironmentMocker with(final MavenProjectMocker mocker) {
85          this.prj = mocker;
86          return this;
87      }
88  
89      /**
90       * With this file on board.
91       *
92       * @param name File name related to basedir
93       * @param content File content to write
94       * @return This object
95       * @throws IOException If some IO problem
96       */
97      public MavenEnvironmentMocker withFile(final String name,
98          final String content) throws IOException {
99          this.ienv.withFile(name, content);
100         return this;
101     }
102 
103     /**
104      * With this file on board.
105      *
106      * @param name File name related to basedir
107      * @param bytes File content to write
108      * @return This object
109      * @throws IOException If some IO problem
110      */
111     public MavenEnvironmentMocker withFile(final String name,
112         final byte[] bytes) throws IOException {
113         this.ienv.withFile(name, bytes);
114         return this;
115     }
116 
117     /**
118      * With list of xpath queries to validate pom.xml.
119      *
120      * @param asserts Collection of xpath queries
121      * @return This object
122      */
123     public MavenEnvironmentMocker withAsserts(
124         final Collection<String> asserts) {
125         this.ass = Collections.unmodifiableCollection(asserts);
126         return this;
127     }
128 
129     /**
130      * Mock it.
131      *
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 FakeMavenEnvironment(
145                 this.prj.mock(),
146                 new FakeContext(this.container),
147                 this.ass
148             )
149         );
150     }
151 }