1
2
3
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
22
23
24
25 public final class MavenEnvironmentMocker {
26
27
28
29
30 private final Environment.Mock ienv;
31
32
33
34
35 private MavenProjectMocker prj;
36
37
38
39
40 private final PlexusContainer container;
41
42
43
44
45 private Collection<String> ass;
46
47
48
49
50
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
65
66
67
68
69
70
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
80
81
82
83
84 public MavenEnvironmentMocker with(final MavenProjectMocker mocker) {
85 this.prj = mocker;
86 return this;
87 }
88
89
90
91
92
93
94
95
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
105
106
107
108
109
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
119
120
121
122
123 public MavenEnvironmentMocker withAsserts(
124 final Collection<String> asserts) {
125 this.ass = Collections.unmodifiableCollection(asserts);
126 return this;
127 }
128
129
130
131
132
133
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 }