View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Mock {@link org.eclipse.jgit.util.SystemReader} for tests.
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  			// Do nothing
82  		}
83  
84  		@Override
85  		public void save() throws IOException {
86  			// Do nothing
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; // Sat Aug 15 20:12:58 GMT-03:30 2009
101 
102 	final Map<String, String> values = new HashMap<>();
103 
104 	private FileBasedConfig userGitConfig;
105 
106 	FileBasedConfig systemGitConfig;
107 
108 	/**
109 	 * Set the user-level git config
110 	 *
111 	 * @param userGitConfig
112 	 *            set another user-level git config
113 	 * @return the old user-level git config
114 	 */
115 	public FileBasedConfig setUserGitConfig(FileBasedConfig userGitConfig) {
116 		FileBasedConfig old = this.userGitConfig;
117 		this.userGitConfig = userGitConfig;
118 		return old;
119 	}
120 
121 	/**
122 	 * Set the system-level git config
123 	 *
124 	 * @param systemGitConfig
125 	 *            the new system-level git config
126 	 * @return the old system-level config
127 	 */
128 	public FileBasedConfig setSystemGitConfig(FileBasedConfig systemGitConfig) {
129 		FileBasedConfig old = this.systemGitConfig;
130 		this.systemGitConfig = systemGitConfig;
131 		return old;
132 	}
133 
134 	/**
135 	 * Constructor for <code>MockSystemReader</code>
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 	 * Clear properties
155 	 */
156 	public void clearProperties() {
157 		values.clear();
158 	}
159 
160 	/**
161 	 * Set a property
162 	 *
163 	 * @param key
164 	 * @param value
165 	 */
166 	public void setProperty(String key, String value) {
167 		values.put(key, value);
168 	}
169 
170 	/** {@inheritDoc} */
171 	@Override
172 	public String getenv(String variable) {
173 		return values.get(variable);
174 	}
175 
176 	/** {@inheritDoc} */
177 	@Override
178 	public String getProperty(String key) {
179 		return values.get(key);
180 	}
181 
182 	/** {@inheritDoc} */
183 	@Override
184 	public FileBasedConfig openUserConfig(Config parent, FS fs) {
185 		assert parent == null || parent == systemGitConfig;
186 		return userGitConfig;
187 	}
188 
189 	/** {@inheritDoc} */
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 	/** {@inheritDoc} */
209 	@Override
210 	public String getHostname() {
211 		return "fake.host.example.com";
212 	}
213 
214 	/** {@inheritDoc} */
215 	@Override
216 	public long getCurrentTime() {
217 		return now;
218 	}
219 
220 	/** {@inheritDoc} */
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 					// Do not wait.
235 				}
236 			};
237 		};
238 	}
239 
240 	/**
241 	 * Adjusts the current time in seconds.
242 	 *
243 	 * @param secDelta
244 	 *            number of seconds to add to the current time.
245 	 * @since 4.2
246 	 */
247 	public void tick(int secDelta) {
248 		now += secDelta * 1000L;
249 	}
250 
251 	/** {@inheritDoc} */
252 	@Override
253 	public int getTimezone(long when) {
254 		return getTimeZone().getOffset(when) / (60 * 1000);
255 	}
256 
257 	/** {@inheritDoc} */
258 	@Override
259 	public TimeZone getTimeZone() {
260 		return TimeZone.getTimeZone("GMT-03:30");
261 	}
262 
263 	/** {@inheritDoc} */
264 	@Override
265 	public Locale getLocale() {
266 		return Locale.US;
267 	}
268 
269 	/** {@inheritDoc} */
270 	@Override
271 	public SimpleDateFormat getSimpleDateFormat(String pattern) {
272 		return new SimpleDateFormat(pattern, getLocale());
273 	}
274 
275 	/** {@inheritDoc} */
276 	@Override
277 	public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
278 		return DateFormat
279 				.getDateTimeInstance(dateStyle, timeStyle, getLocale());
280 	}
281 
282 	/**
283 	 * Assign some properties for the currently executing platform
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 	 * Emulate Windows
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 	 * Emulate Unix
307 	 */
308 	public void setUnix() {
309 		resetOsNames();
310 		setProperty("os.name", "*nix"); // Essentially anything but Windows
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 }