1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.eclipse.jgit.junit;
47
48 import java.io.File;
49 import java.io.IOException;
50 import java.lang.reflect.Field;
51 import java.text.DateFormat;
52 import java.text.SimpleDateFormat;
53 import java.time.Duration;
54 import java.util.HashMap;
55 import java.util.Locale;
56 import java.util.Map;
57 import java.util.TimeZone;
58 import java.util.concurrent.TimeUnit;
59
60 import org.eclipse.jgit.errors.ConfigInvalidException;
61 import org.eclipse.jgit.lib.Config;
62 import org.eclipse.jgit.lib.Constants;
63 import org.eclipse.jgit.lib.StoredConfig;
64 import org.eclipse.jgit.storage.file.FileBasedConfig;
65 import org.eclipse.jgit.util.FS;
66 import org.eclipse.jgit.util.SystemReader;
67 import org.eclipse.jgit.util.time.MonotonicClock;
68 import org.eclipse.jgit.util.time.ProposedTimestamp;
69
70
71
72
73 public class MockSystemReader extends SystemReader {
74 private static final class MockConfig extends FileBasedConfig {
75 private MockConfig(File cfgLocation, FS fs) {
76 super(cfgLocation, fs);
77 }
78
79 @Override
80 public void load() throws IOException, ConfigInvalidException {
81
82 }
83
84 @Override
85 public void save() throws IOException {
86
87 }
88
89 @Override
90 public boolean isOutdated() {
91 return false;
92 }
93
94 @Override
95 public String toString() {
96 return "MockConfig";
97 }
98 }
99
100 long now = 1250379778668L;
101
102 final Map<String, String> values = new HashMap<>();
103
104 private FileBasedConfig userGitConfig;
105
106 FileBasedConfig systemGitConfig;
107
108
109
110
111
112
113
114
115 public FileBasedConfig setUserGitConfig(FileBasedConfig userGitConfig) {
116 FileBasedConfig old = this.userGitConfig;
117 this.userGitConfig = userGitConfig;
118 return old;
119 }
120
121
122
123
124
125
126
127
128 public FileBasedConfig setSystemGitConfig(FileBasedConfig systemGitConfig) {
129 FileBasedConfig old = this.systemGitConfig;
130 this.systemGitConfig = systemGitConfig;
131 return old;
132 }
133
134
135
136
137 public MockSystemReader() {
138 init(Constants.OS_USER_NAME_KEY);
139 init(Constants.GIT_AUTHOR_NAME_KEY);
140 init(Constants.GIT_AUTHOR_EMAIL_KEY);
141 init(Constants.GIT_COMMITTER_NAME_KEY);
142 init(Constants.GIT_COMMITTER_EMAIL_KEY);
143 setProperty(Constants.OS_USER_DIR, ".");
144 userGitConfig = new MockConfig(null, null);
145 systemGitConfig = new MockConfig(null, null);
146 setCurrentPlatform();
147 }
148
149 private void init(String n) {
150 setProperty(n, n);
151 }
152
153
154
155
156 public void clearProperties() {
157 values.clear();
158 }
159
160
161
162
163
164
165
166 public void setProperty(String key, String value) {
167 values.put(key, value);
168 }
169
170
171 @Override
172 public String getenv(String variable) {
173 return values.get(variable);
174 }
175
176
177 @Override
178 public String getProperty(String key) {
179 return values.get(key);
180 }
181
182
183 @Override
184 public FileBasedConfig openUserConfig(Config parent, FS fs) {
185 assert parent == null || parent == systemGitConfig;
186 return userGitConfig;
187 }
188
189
190 @Override
191 public FileBasedConfig openSystemConfig(Config parent, FS fs) {
192 assert parent == null;
193 return systemGitConfig;
194 }
195
196 @Override
197 public StoredConfig getUserConfig()
198 throws IOException, ConfigInvalidException {
199 return userGitConfig;
200 }
201
202 @Override
203 public StoredConfig getSystemConfig()
204 throws IOException, ConfigInvalidException {
205 return systemGitConfig;
206 }
207
208
209 @Override
210 public String getHostname() {
211 return "fake.host.example.com";
212 }
213
214
215 @Override
216 public long getCurrentTime() {
217 return now;
218 }
219
220
221 @Override
222 public MonotonicClock getClock() {
223 return () -> {
224 long t = getCurrentTime();
225 return new ProposedTimestamp() {
226
227 @Override
228 public long read(TimeUnit unit) {
229 return unit.convert(t, TimeUnit.MILLISECONDS);
230 }
231
232 @Override
233 public void blockUntil(Duration maxWait) {
234
235 }
236 };
237 };
238 }
239
240
241
242
243
244
245
246
247 public void tick(int secDelta) {
248 now += secDelta * 1000L;
249 }
250
251
252 @Override
253 public int getTimezone(long when) {
254 return getTimeZone().getOffset(when) / (60 * 1000);
255 }
256
257
258 @Override
259 public TimeZone getTimeZone() {
260 return TimeZone.getTimeZone("GMT-03:30");
261 }
262
263
264 @Override
265 public Locale getLocale() {
266 return Locale.US;
267 }
268
269
270 @Override
271 public SimpleDateFormat getSimpleDateFormat(String pattern) {
272 return new SimpleDateFormat(pattern, getLocale());
273 }
274
275
276 @Override
277 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
278 return DateFormat
279 .getDateTimeInstance(dateStyle, timeStyle, getLocale());
280 }
281
282
283
284
285 public void setCurrentPlatform() {
286 resetOsNames();
287 setProperty("os.name", System.getProperty("os.name"));
288 setProperty("file.separator", System.getProperty("file.separator"));
289 setProperty("path.separator", System.getProperty("path.separator"));
290 setProperty("line.separator", System.getProperty("line.separator"));
291 }
292
293
294
295
296 public void setWindows() {
297 resetOsNames();
298 setProperty("os.name", "Windows");
299 setProperty("file.separator", "\\");
300 setProperty("path.separator", ";");
301 setProperty("line.separator", "\r\n");
302 setPlatformChecker();
303 }
304
305
306
307
308 public void setUnix() {
309 resetOsNames();
310 setProperty("os.name", "*nix");
311 setProperty("file.separator", "/");
312 setProperty("path.separator", ":");
313 setProperty("line.separator", "\n");
314 setPlatformChecker();
315 }
316
317 private void resetOsNames() {
318 Field field;
319 try {
320 field = SystemReader.class.getDeclaredField("isWindows");
321 field.setAccessible(true);
322 field.set(null, null);
323 field = SystemReader.class.getDeclaredField("isMacOS");
324 field.setAccessible(true);
325 field.set(null, null);
326 } catch (Exception e) {
327 e.printStackTrace();
328 }
329 }
330
331 @Override
332 public String toString() {
333 return "MockSystemReader";
334 }
335
336 }