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 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  	public MockSystemReader() {
98  		init(Constants.OS_USER_NAME_KEY);
99  		init(Constants.GIT_AUTHOR_NAME_KEY);
100 		init(Constants.GIT_AUTHOR_EMAIL_KEY);
101 		init(Constants.GIT_COMMITTER_NAME_KEY);
102 		init(Constants.GIT_COMMITTER_EMAIL_KEY);
103 		setProperty(Constants.OS_USER_DIR, ".");
104 		userGitConfig = new MockConfig(null, null);
105 		systemGitConfig = new MockConfig(null, null);
106 		setCurrentPlatform();
107 	}
108 
109 	private void init(final String n) {
110 		setProperty(n, n);
111 	}
112 
113 	public void clearProperties() {
114 		values.clear();
115 	}
116 
117 	public void setProperty(String key, String value) {
118 		values.put(key, value);
119 	}
120 
121 	@Override
122 	public String getenv(String variable) {
123 		return values.get(variable);
124 	}
125 
126 	@Override
127 	public String getProperty(String key) {
128 		return values.get(key);
129 	}
130 
131 	@Override
132 	public FileBasedConfig openUserConfig(Config parent, FS fs) {
133 		assert parent == null || parent == systemGitConfig;
134 		return userGitConfig;
135 	}
136 
137 	@Override
138 	public FileBasedConfig openSystemConfig(Config parent, FS fs) {
139 		assert parent == null;
140 		return systemGitConfig;
141 	}
142 
143 	@Override
144 	public String getHostname() {
145 		return "fake.host.example.com";
146 	}
147 
148 	@Override
149 	public long getCurrentTime() {
150 		return now;
151 	}
152 
153 	@Override
154 	public MonotonicClock getClock() {
155 		return new MonotonicClock() {
156 			@Override
157 			public ProposedTimestamp propose() {
158 				long t = getCurrentTime();
159 				return new ProposedTimestamp() {
160 					@Override
161 					public long read(TimeUnit unit) {
162 						return unit.convert(t, TimeUnit.MILLISECONDS);
163 					}
164 
165 					@Override
166 					public void blockUntil(Duration maxWait) {
167 						// Do not wait.
168 					}
169 				};
170 			}
171 		};
172 	}
173 
174 	/**
175 	 * Adjusts the current time in seconds.
176 	 *
177 	 * @param secDelta
178 	 *            number of seconds to add to the current time.
179 	 * @since 4.2
180 	 */
181 	public void tick(final int secDelta) {
182 		now += secDelta * 1000L;
183 	}
184 
185 	@Override
186 	public int getTimezone(long when) {
187 		return getTimeZone().getOffset(when) / (60 * 1000);
188 	}
189 
190 	@Override
191 	public TimeZone getTimeZone() {
192 		return TimeZone.getTimeZone("GMT-03:30");
193 	}
194 
195 	@Override
196 	public Locale getLocale() {
197 		return Locale.US;
198 	}
199 
200 	@Override
201 	public SimpleDateFormat getSimpleDateFormat(String pattern) {
202 		return new SimpleDateFormat(pattern, getLocale());
203 	}
204 
205 	@Override
206 	public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
207 		return DateFormat
208 				.getDateTimeInstance(dateStyle, timeStyle, getLocale());
209 	}
210 
211 	/**
212 	 * Assign some properties for the currently executing platform
213 	 */
214 	public void setCurrentPlatform() {
215 		resetOsNames();
216 		setProperty("os.name", System.getProperty("os.name"));
217 		setProperty("file.separator", System.getProperty("file.separator"));
218 		setProperty("path.separator", System.getProperty("path.separator"));
219 		setProperty("line.separator", System.getProperty("line.separator"));
220 	}
221 
222 	/**
223 	 * Emulate Windows
224 	 */
225 	public void setWindows() {
226 		resetOsNames();
227 		setProperty("os.name", "Windows");
228 		setProperty("file.separator", "\\");
229 		setProperty("path.separator", ";");
230 		setProperty("line.separator", "\r\n");
231 		setPlatformChecker();
232 	}
233 
234 	/**
235 	 * Emulate Unix
236 	 */
237 	public void setUnix() {
238 		resetOsNames();
239 		setProperty("os.name", "*nix"); // Essentially anything but Windows
240 		setProperty("file.separator", "/");
241 		setProperty("path.separator", ":");
242 		setProperty("line.separator", "\n");
243 		setPlatformChecker();
244 	}
245 
246 	private void resetOsNames() {
247 		Field field;
248 		try {
249 			field = SystemReader.class.getDeclaredField("isWindows");
250 			field.setAccessible(true);
251 			field.set(null, null);
252 			field = SystemReader.class.getDeclaredField("isMacOS");
253 			field.setAccessible(true);
254 			field.set(null, null);
255 		} catch (Exception e) {
256 			e.printStackTrace();
257 		}
258 	}
259 }