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.storage.file.FileBasedConfig;
64 import org.eclipse.jgit.util.FS;
65 import org.eclipse.jgit.util.SystemReader;
66 import org.eclipse.jgit.util.time.MonotonicClock;
67 import org.eclipse.jgit.util.time.ProposedTimestamp;
68
69
70
71
72 public class MockSystemReader extends SystemReader {
73 private final class MockConfig extends FileBasedConfig {
74 private MockConfig(File cfgLocation, FS fs) {
75 super(cfgLocation, fs);
76 }
77
78 @Override
79 public void load() throws IOException, ConfigInvalidException {
80
81 }
82
83 @Override
84 public boolean isOutdated() {
85 return false;
86 }
87 }
88
89 long now = 1250379778668L;
90
91 final Map<String, String> values = new HashMap<>();
92
93 FileBasedConfig userGitConfig;
94
95 FileBasedConfig systemGitConfig;
96
97
98
99
100 public MockSystemReader() {
101 init(Constants.OS_USER_NAME_KEY);
102 init(Constants.GIT_AUTHOR_NAME_KEY);
103 init(Constants.GIT_AUTHOR_EMAIL_KEY);
104 init(Constants.GIT_COMMITTER_NAME_KEY);
105 init(Constants.GIT_COMMITTER_EMAIL_KEY);
106 setProperty(Constants.OS_USER_DIR, ".");
107 userGitConfig = new MockConfig(null, null);
108 systemGitConfig = new MockConfig(null, null);
109 setCurrentPlatform();
110 }
111
112 private void init(final String n) {
113 setProperty(n, n);
114 }
115
116
117
118
119 public void clearProperties() {
120 values.clear();
121 }
122
123
124
125
126
127
128
129 public void setProperty(String key, String value) {
130 values.put(key, value);
131 }
132
133
134 @Override
135 public String getenv(String variable) {
136 return values.get(variable);
137 }
138
139
140 @Override
141 public String getProperty(String key) {
142 return values.get(key);
143 }
144
145
146 @Override
147 public FileBasedConfig openUserConfig(Config parent, FS fs) {
148 assert parent == null || parent == systemGitConfig;
149 return userGitConfig;
150 }
151
152
153 @Override
154 public FileBasedConfig openSystemConfig(Config parent, FS fs) {
155 assert parent == null;
156 return systemGitConfig;
157 }
158
159
160 @Override
161 public String getHostname() {
162 return "fake.host.example.com";
163 }
164
165
166 @Override
167 public long getCurrentTime() {
168 return now;
169 }
170
171
172 @Override
173 public MonotonicClock getClock() {
174 return new MonotonicClock() {
175 @Override
176 public ProposedTimestamp propose() {
177 long t = getCurrentTime();
178 return new ProposedTimestamp() {
179 @Override
180 public long read(TimeUnit unit) {
181 return unit.convert(t, TimeUnit.MILLISECONDS);
182 }
183
184 @Override
185 public void blockUntil(Duration maxWait) {
186
187 }
188 };
189 }
190 };
191 }
192
193
194
195
196
197
198
199
200 public void tick(final int secDelta) {
201 now += secDelta * 1000L;
202 }
203
204
205 @Override
206 public int getTimezone(long when) {
207 return getTimeZone().getOffset(when) / (60 * 1000);
208 }
209
210
211 @Override
212 public TimeZone getTimeZone() {
213 return TimeZone.getTimeZone("GMT-03:30");
214 }
215
216
217 @Override
218 public Locale getLocale() {
219 return Locale.US;
220 }
221
222
223 @Override
224 public SimpleDateFormat getSimpleDateFormat(String pattern) {
225 return new SimpleDateFormat(pattern, getLocale());
226 }
227
228
229 @Override
230 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
231 return DateFormat
232 .getDateTimeInstance(dateStyle, timeStyle, getLocale());
233 }
234
235
236
237
238 public void setCurrentPlatform() {
239 resetOsNames();
240 setProperty("os.name", System.getProperty("os.name"));
241 setProperty("file.separator", System.getProperty("file.separator"));
242 setProperty("path.separator", System.getProperty("path.separator"));
243 setProperty("line.separator", System.getProperty("line.separator"));
244 }
245
246
247
248
249 public void setWindows() {
250 resetOsNames();
251 setProperty("os.name", "Windows");
252 setProperty("file.separator", "\\");
253 setProperty("path.separator", ";");
254 setProperty("line.separator", "\r\n");
255 setPlatformChecker();
256 }
257
258
259
260
261 public void setUnix() {
262 resetOsNames();
263 setProperty("os.name", "*nix");
264 setProperty("file.separator", "/");
265 setProperty("path.separator", ":");
266 setProperty("line.separator", "\n");
267 setPlatformChecker();
268 }
269
270 private void resetOsNames() {
271 Field field;
272 try {
273 field = SystemReader.class.getDeclaredField("isWindows");
274 field.setAccessible(true);
275 field.set(null, null);
276 field = SystemReader.class.getDeclaredField("isMacOS");
277 field.setAccessible(true);
278 field.set(null, null);
279 } catch (Exception e) {
280 e.printStackTrace();
281 }
282 }
283 }