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 new MonotonicClock() {
224 @Override
225 public ProposedTimestamp propose() {
226 long t = getCurrentTime();
227 return new ProposedTimestamp() {
228 @Override
229 public long read(TimeUnit unit) {
230 return unit.convert(t, TimeUnit.MILLISECONDS);
231 }
232
233 @Override
234 public void blockUntil(Duration maxWait) {
235
236 }
237 };
238 }
239 };
240 }
241
242
243
244
245
246
247
248
249 public void tick(int secDelta) {
250 now += secDelta * 1000L;
251 }
252
253
254 @Override
255 public int getTimezone(long when) {
256 return getTimeZone().getOffset(when) / (60 * 1000);
257 }
258
259
260 @Override
261 public TimeZone getTimeZone() {
262 return TimeZone.getTimeZone("GMT-03:30");
263 }
264
265
266 @Override
267 public Locale getLocale() {
268 return Locale.US;
269 }
270
271
272 @Override
273 public SimpleDateFormat getSimpleDateFormat(String pattern) {
274 return new SimpleDateFormat(pattern, getLocale());
275 }
276
277
278 @Override
279 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
280 return DateFormat
281 .getDateTimeInstance(dateStyle, timeStyle, getLocale());
282 }
283
284
285
286
287 public void setCurrentPlatform() {
288 resetOsNames();
289 setProperty("os.name", System.getProperty("os.name"));
290 setProperty("file.separator", System.getProperty("file.separator"));
291 setProperty("path.separator", System.getProperty("path.separator"));
292 setProperty("line.separator", System.getProperty("line.separator"));
293 }
294
295
296
297
298 public void setWindows() {
299 resetOsNames();
300 setProperty("os.name", "Windows");
301 setProperty("file.separator", "\\");
302 setProperty("path.separator", ";");
303 setProperty("line.separator", "\r\n");
304 setPlatformChecker();
305 }
306
307
308
309
310 public void setUnix() {
311 resetOsNames();
312 setProperty("os.name", "*nix");
313 setProperty("file.separator", "/");
314 setProperty("path.separator", ":");
315 setProperty("line.separator", "\n");
316 setPlatformChecker();
317 }
318
319 private void resetOsNames() {
320 Field field;
321 try {
322 field = SystemReader.class.getDeclaredField("isWindows");
323 field.setAccessible(true);
324 field.set(null, null);
325 field = SystemReader.class.getDeclaredField("isMacOS");
326 field.setAccessible(true);
327 field.set(null, null);
328 } catch (Exception e) {
329 e.printStackTrace();
330 }
331 }
332
333 @Override
334 public String toString() {
335 return "MockSystemReader";
336 }
337
338 }