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 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 						// Do not wait.
236 					}
237 				};
238 			}
239 		};
240 	}
241 
242 	/**
243 	 * Adjusts the current time in seconds.
244 	 *
245 	 * @param secDelta
246 	 *            number of seconds to add to the current time.
247 	 * @since 4.2
248 	 */
249 	public void tick(int secDelta) {
250 		now += secDelta * 1000L;
251 	}
252 
253 	/** {@inheritDoc} */
254 	@Override
255 	public int getTimezone(long when) {
256 		return getTimeZone().getOffset(when) / (60 * 1000);
257 	}
258 
259 	/** {@inheritDoc} */
260 	@Override
261 	public TimeZone getTimeZone() {
262 		return TimeZone.getTimeZone("GMT-03:30");
263 	}
264 
265 	/** {@inheritDoc} */
266 	@Override
267 	public Locale getLocale() {
268 		return Locale.US;
269 	}
270 
271 	/** {@inheritDoc} */
272 	@Override
273 	public SimpleDateFormat getSimpleDateFormat(String pattern) {
274 		return new SimpleDateFormat(pattern, getLocale());
275 	}
276 
277 	/** {@inheritDoc} */
278 	@Override
279 	public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
280 		return DateFormat
281 				.getDateTimeInstance(dateStyle, timeStyle, getLocale());
282 	}
283 
284 	/**
285 	 * Assign some properties for the currently executing platform
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 	 * Emulate Windows
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 	 * Emulate Unix
309 	 */
310 	public void setUnix() {
311 		resetOsNames();
312 		setProperty("os.name", "*nix"); // Essentially anything but Windows
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 }