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.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   * Mock {@link org.eclipse.jgit.util.SystemReader} for tests.
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  			// Do nothing
81  		}
82  
83  		@Override
84  		public boolean isOutdated() {
85  			return false;
86  		}
87  	}
88  
89  	long now = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
90  
91  	final Map<String, String> values = new HashMap<>();
92  
93  	FileBasedConfig userGitConfig;
94  
95  	FileBasedConfig systemGitConfig;
96  
97  	/**
98  	 * Constructor for <code>MockSystemReader</code>
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 	 * Clear properties
118 	 */
119 	public void clearProperties() {
120 		values.clear();
121 	}
122 
123 	/**
124 	 * Set a property
125 	 *
126 	 * @param key
127 	 * @param value
128 	 */
129 	public void setProperty(String key, String value) {
130 		values.put(key, value);
131 	}
132 
133 	/** {@inheritDoc} */
134 	@Override
135 	public String getenv(String variable) {
136 		return values.get(variable);
137 	}
138 
139 	/** {@inheritDoc} */
140 	@Override
141 	public String getProperty(String key) {
142 		return values.get(key);
143 	}
144 
145 	/** {@inheritDoc} */
146 	@Override
147 	public FileBasedConfig openUserConfig(Config parent, FS fs) {
148 		assert parent == null || parent == systemGitConfig;
149 		return userGitConfig;
150 	}
151 
152 	/** {@inheritDoc} */
153 	@Override
154 	public FileBasedConfig openSystemConfig(Config parent, FS fs) {
155 		assert parent == null;
156 		return systemGitConfig;
157 	}
158 
159 	/** {@inheritDoc} */
160 	@Override
161 	public String getHostname() {
162 		return "fake.host.example.com";
163 	}
164 
165 	/** {@inheritDoc} */
166 	@Override
167 	public long getCurrentTime() {
168 		return now;
169 	}
170 
171 	/** {@inheritDoc} */
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 						// Do not wait.
187 					}
188 				};
189 			}
190 		};
191 	}
192 
193 	/**
194 	 * Adjusts the current time in seconds.
195 	 *
196 	 * @param secDelta
197 	 *            number of seconds to add to the current time.
198 	 * @since 4.2
199 	 */
200 	public void tick(final int secDelta) {
201 		now += secDelta * 1000L;
202 	}
203 
204 	/** {@inheritDoc} */
205 	@Override
206 	public int getTimezone(long when) {
207 		return getTimeZone().getOffset(when) / (60 * 1000);
208 	}
209 
210 	/** {@inheritDoc} */
211 	@Override
212 	public TimeZone getTimeZone() {
213 		return TimeZone.getTimeZone("GMT-03:30");
214 	}
215 
216 	/** {@inheritDoc} */
217 	@Override
218 	public Locale getLocale() {
219 		return Locale.US;
220 	}
221 
222 	/** {@inheritDoc} */
223 	@Override
224 	public SimpleDateFormat getSimpleDateFormat(String pattern) {
225 		return new SimpleDateFormat(pattern, getLocale());
226 	}
227 
228 	/** {@inheritDoc} */
229 	@Override
230 	public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
231 		return DateFormat
232 				.getDateTimeInstance(dateStyle, timeStyle, getLocale());
233 	}
234 
235 	/**
236 	 * Assign some properties for the currently executing platform
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 	 * Emulate Windows
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 	 * Emulate Unix
260 	 */
261 	public void setUnix() {
262 		resetOsNames();
263 		setProperty("os.name", "*nix"); // Essentially anything but Windows
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 }