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.util.HashMap;
54  import java.util.Locale;
55  import java.util.Map;
56  import java.util.TimeZone;
57  
58  import org.eclipse.jgit.errors.ConfigInvalidException;
59  import org.eclipse.jgit.lib.Config;
60  import org.eclipse.jgit.lib.Constants;
61  import org.eclipse.jgit.storage.file.FileBasedConfig;
62  import org.eclipse.jgit.util.FS;
63  import org.eclipse.jgit.util.SystemReader;
64  
65  /**
66   * Mock {@link SystemReader} for tests.
67   */
68  public class MockSystemReader extends SystemReader {
69  	private final class MockConfig extends FileBasedConfig {
70  		private MockConfig(File cfgLocation, FS fs) {
71  			super(cfgLocation, fs);
72  		}
73  
74  		@Override
75  		public void load() throws IOException, ConfigInvalidException {
76  			// Do nothing
77  		}
78  
79  		@Override
80  		public boolean isOutdated() {
81  			return false;
82  		}
83  	}
84  
85  	long now = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
86  
87  	final Map<String, String> values = new HashMap<String, String>();
88  
89  	FileBasedConfig userGitConfig;
90  
91  	FileBasedConfig systemGitConfig;
92  
93  	public MockSystemReader() {
94  		init(Constants.OS_USER_NAME_KEY);
95  		init(Constants.GIT_AUTHOR_NAME_KEY);
96  		init(Constants.GIT_AUTHOR_EMAIL_KEY);
97  		init(Constants.GIT_COMMITTER_NAME_KEY);
98  		init(Constants.GIT_COMMITTER_EMAIL_KEY);
99  		setProperty(Constants.OS_USER_DIR, ".");
100 		userGitConfig = new MockConfig(null, null);
101 		systemGitConfig = new MockConfig(null, null);
102 		setCurrentPlatform();
103 	}
104 
105 	private void init(final String n) {
106 		setProperty(n, n);
107 	}
108 
109 	public void clearProperties() {
110 		values.clear();
111 	}
112 
113 	public void setProperty(String key, String value) {
114 		values.put(key, value);
115 	}
116 
117 	@Override
118 	public String getenv(String variable) {
119 		return values.get(variable);
120 	}
121 
122 	@Override
123 	public String getProperty(String key) {
124 		return values.get(key);
125 	}
126 
127 	@Override
128 	public FileBasedConfig openUserConfig(Config parent, FS fs) {
129 		assert parent == null || parent == systemGitConfig;
130 		return userGitConfig;
131 	}
132 
133 	@Override
134 	public FileBasedConfig openSystemConfig(Config parent, FS fs) {
135 		assert parent == null;
136 		return systemGitConfig;
137 	}
138 
139 	@Override
140 	public String getHostname() {
141 		return "fake.host.example.com";
142 	}
143 
144 	@Override
145 	public long getCurrentTime() {
146 		return now;
147 	}
148 
149 	/**
150 	 * Adjusts the current time in seconds.
151 	 *
152 	 * @param secDelta
153 	 *            number of seconds to add to the current time.
154 	 * @since 4.2
155 	 */
156 	public void tick(final int secDelta) {
157 		now += secDelta * 1000L;
158 	}
159 
160 	@Override
161 	public int getTimezone(long when) {
162 		return getTimeZone().getOffset(when) / (60 * 1000);
163 	}
164 
165 	@Override
166 	public TimeZone getTimeZone() {
167 		return TimeZone.getTimeZone("GMT-03:30");
168 	}
169 
170 	@Override
171 	public Locale getLocale() {
172 		return Locale.US;
173 	}
174 
175 	@Override
176 	public SimpleDateFormat getSimpleDateFormat(String pattern) {
177 		return new SimpleDateFormat(pattern, getLocale());
178 	}
179 
180 	@Override
181 	public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
182 		return DateFormat
183 				.getDateTimeInstance(dateStyle, timeStyle, getLocale());
184 	}
185 
186 	/**
187 	 * Assign some properties for the currently executing platform
188 	 */
189 	public void setCurrentPlatform() {
190 		resetOsNames();
191 		setProperty("os.name", System.getProperty("os.name"));
192 		setProperty("file.separator", System.getProperty("file.separator"));
193 		setProperty("path.separator", System.getProperty("path.separator"));
194 		setProperty("line.separator", System.getProperty("line.separator"));
195 	}
196 
197 	/**
198 	 * Emulate Windows
199 	 */
200 	public void setWindows() {
201 		resetOsNames();
202 		setProperty("os.name", "Windows");
203 		setProperty("file.separator", "\\");
204 		setProperty("path.separator", ";");
205 		setProperty("line.separator", "\r\n");
206 		setPlatformChecker();
207 	}
208 
209 	/**
210 	 * Emulate Unix
211 	 */
212 	public void setUnix() {
213 		resetOsNames();
214 		setProperty("os.name", "*nix"); // Essentially anything but Windows
215 		setProperty("file.separator", "/");
216 		setProperty("path.separator", ":");
217 		setProperty("line.separator", "\n");
218 		setPlatformChecker();
219 	}
220 
221 	private void resetOsNames() {
222 		Field field;
223 		try {
224 			field = SystemReader.class.getDeclaredField("isWindows");
225 			field.setAccessible(true);
226 			field.set(null, null);
227 			field = SystemReader.class.getDeclaredField("isMacOS");
228 			field.setAccessible(true);
229 			field.set(null, null);
230 		} catch (Exception e) {
231 			e.printStackTrace();
232 		}
233 	}
234 }