1 /*
2 * Copyright (c) 2011-2025 Yegor Bugayenko
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met: 1) Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer. 2) Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution. 3) Neither the name of the Qulice.com nor
14 * the names of its contributors may be used to endorse or promote
15 * products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
20 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 package com.qulice.maven;
32
33 import com.qulice.spi.Environment;
34 import java.io.IOException;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.LinkedList;
38 import org.apache.maven.plugin.logging.Log;
39 import org.apache.maven.project.MavenProject;
40 import org.codehaus.plexus.PlexusContainer;
41 import org.codehaus.plexus.context.Context;
42 import org.mockito.Mockito;
43 import org.slf4j.impl.StaticLoggerBinder;
44
45 /**
46 * Mocker of {@link MavenProject}.
47 * @since 0.4
48 */
49 @SuppressWarnings("PMD.TooManyMethods")
50 public final class MavenEnvironmentMocker {
51
52 /**
53 * Env mocker.
54 */
55 private final Environment.Mock ienv;
56
57 /**
58 * Project.
59 */
60 private MavenProjectMocker prj;
61
62 /**
63 * Plexus container, mock.
64 */
65 private final PlexusContainer container;
66
67 /**
68 * Xpath queries to test pom.xml.
69 */
70 private Collection<String> ass;
71
72 /**
73 * Public ctor.
74 * @throws IOException If some IO problem inside
75 */
76 public MavenEnvironmentMocker() throws IOException {
77 this.prj = new MavenProjectMocker();
78 this.container = Mockito.mock(PlexusContainer.class);
79 this.ass = new LinkedList<>();
80 this.ienv = new Environment.Mock();
81 }
82
83 /**
84 * Inject this object into plexus container.
85 * @param role The role
86 * @param hint The hint
87 * @param object The object to return
88 * @return This object
89 * @throws Exception If something wrong happens inside
90 */
91 public MavenEnvironmentMocker inPlexus(final String role, final String hint,
92 final Object object) throws Exception {
93 Mockito.doReturn(object).when(this.container).lookup(role, hint);
94 return this;
95 }
96
97 /**
98 * With this project mocker.
99 * @param mocker The project mocker
100 * @return This object
101 */
102 public MavenEnvironmentMocker with(final MavenProjectMocker mocker) {
103 this.prj = mocker;
104 return this;
105 }
106
107 /**
108 * With this file on board.
109 * @param name File name related to basedir
110 * @param content File content to write
111 * @return This object
112 * @throws IOException If some IO problem
113 */
114 public MavenEnvironmentMocker withFile(final String name,
115 final String content) throws IOException {
116 this.ienv.withFile(name, content);
117 return this;
118 }
119
120 /**
121 * With this file on board.
122 * @param name File name related to basedir
123 * @param bytes File content to write
124 * @return This object
125 * @throws IOException If some IO problem
126 */
127 public MavenEnvironmentMocker withFile(final String name,
128 final byte[] bytes) throws IOException {
129 this.ienv.withFile(name, bytes);
130 return this;
131 }
132
133 /**
134 * With list of xpath queries to validate pom.xml.
135 * @param asserts Collection of xpath queries
136 * @return This object
137 */
138 public MavenEnvironmentMocker withAsserts(
139 final Collection<String> asserts) {
140 this.ass = Collections.unmodifiableCollection(asserts);
141 return this;
142 }
143
144 /**
145 * Mock it.
146 * @return The environment just mocked
147 * @throws Exception If something wrong happens inside
148 */
149 public MavenEnvironment mock() throws Exception {
150 StaticLoggerBinder.getSingleton().setMavenLog(Mockito.mock(Log.class));
151 this.prj.inBasedir(this.ienv.basedir());
152 final MavenProject project = this.prj.mock();
153 final Environment parent = this.ienv;
154 final MavenEnvironment env = Mockito.mock(MavenEnvironment.class);
155 Mockito.doReturn(project).when(env).project();
156 final Context context = Mockito.mock(Context.class);
157 Mockito.doReturn(context).when(env).context();
158 Mockito.doReturn(this.container).when(context).get(Mockito.anyString());
159 Mockito.doReturn(this.ass).when(env).asserts();
160 return new MavenEnvironment.Wrap(parent, env);
161 }
162
163 }