You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MockSystemReader.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. package org.eclipse.jgit.junit;
  46. import java.io.File;
  47. import java.io.IOException;
  48. import java.lang.reflect.Field;
  49. import java.text.DateFormat;
  50. import java.text.SimpleDateFormat;
  51. import java.time.Duration;
  52. import java.util.HashMap;
  53. import java.util.Locale;
  54. import java.util.Map;
  55. import java.util.TimeZone;
  56. import java.util.concurrent.TimeUnit;
  57. import org.eclipse.jgit.errors.ConfigInvalidException;
  58. import org.eclipse.jgit.lib.Config;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.StoredConfig;
  61. import org.eclipse.jgit.storage.file.FileBasedConfig;
  62. import org.eclipse.jgit.util.FS;
  63. import org.eclipse.jgit.util.SystemReader;
  64. import org.eclipse.jgit.util.time.MonotonicClock;
  65. import org.eclipse.jgit.util.time.ProposedTimestamp;
  66. /**
  67. * Mock {@link org.eclipse.jgit.util.SystemReader} for tests.
  68. */
  69. public class MockSystemReader extends SystemReader {
  70. private static final class MockConfig extends FileBasedConfig {
  71. private MockConfig(File cfgLocation, FS fs) {
  72. super(cfgLocation, fs);
  73. }
  74. @Override
  75. public void load() throws IOException, ConfigInvalidException {
  76. // Do nothing
  77. }
  78. @Override
  79. public void save() throws IOException {
  80. // Do nothing
  81. }
  82. @Override
  83. public boolean isOutdated() {
  84. return false;
  85. }
  86. @Override
  87. public String toString() {
  88. return "MockConfig";
  89. }
  90. }
  91. long now = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
  92. final Map<String, String> values = new HashMap<>();
  93. private FileBasedConfig userGitConfig;
  94. FileBasedConfig systemGitConfig;
  95. /**
  96. * Set the user-level git config
  97. *
  98. * @param userGitConfig
  99. * set another user-level git config
  100. * @return the old user-level git config
  101. */
  102. public FileBasedConfig setUserGitConfig(FileBasedConfig userGitConfig) {
  103. FileBasedConfig old = this.userGitConfig;
  104. this.userGitConfig = userGitConfig;
  105. return old;
  106. }
  107. /**
  108. * Set the system-level git config
  109. *
  110. * @param systemGitConfig
  111. * the new system-level git config
  112. * @return the old system-level config
  113. */
  114. public FileBasedConfig setSystemGitConfig(FileBasedConfig systemGitConfig) {
  115. FileBasedConfig old = this.systemGitConfig;
  116. this.systemGitConfig = systemGitConfig;
  117. return old;
  118. }
  119. /**
  120. * Constructor for <code>MockSystemReader</code>
  121. */
  122. public MockSystemReader() {
  123. init(Constants.OS_USER_NAME_KEY);
  124. init(Constants.GIT_AUTHOR_NAME_KEY);
  125. init(Constants.GIT_AUTHOR_EMAIL_KEY);
  126. init(Constants.GIT_COMMITTER_NAME_KEY);
  127. init(Constants.GIT_COMMITTER_EMAIL_KEY);
  128. setProperty(Constants.OS_USER_DIR, ".");
  129. userGitConfig = new MockConfig(null, null);
  130. systemGitConfig = new MockConfig(null, null);
  131. setCurrentPlatform();
  132. }
  133. private void init(String n) {
  134. setProperty(n, n);
  135. }
  136. /**
  137. * Clear properties
  138. */
  139. public void clearProperties() {
  140. values.clear();
  141. }
  142. /**
  143. * Set a property
  144. *
  145. * @param key
  146. * @param value
  147. */
  148. public void setProperty(String key, String value) {
  149. values.put(key, value);
  150. }
  151. /** {@inheritDoc} */
  152. @Override
  153. public String getenv(String variable) {
  154. return values.get(variable);
  155. }
  156. /** {@inheritDoc} */
  157. @Override
  158. public String getProperty(String key) {
  159. return values.get(key);
  160. }
  161. /** {@inheritDoc} */
  162. @Override
  163. public FileBasedConfig openUserConfig(Config parent, FS fs) {
  164. assert parent == null || parent == systemGitConfig;
  165. return userGitConfig;
  166. }
  167. /** {@inheritDoc} */
  168. @Override
  169. public FileBasedConfig openSystemConfig(Config parent, FS fs) {
  170. assert parent == null;
  171. return systemGitConfig;
  172. }
  173. @Override
  174. public StoredConfig getUserConfig()
  175. throws IOException, ConfigInvalidException {
  176. return userGitConfig;
  177. }
  178. @Override
  179. public StoredConfig getSystemConfig()
  180. throws IOException, ConfigInvalidException {
  181. return systemGitConfig;
  182. }
  183. /** {@inheritDoc} */
  184. @Override
  185. public String getHostname() {
  186. return "fake.host.example.com";
  187. }
  188. /** {@inheritDoc} */
  189. @Override
  190. public long getCurrentTime() {
  191. return now;
  192. }
  193. /** {@inheritDoc} */
  194. @Override
  195. public MonotonicClock getClock() {
  196. return new MonotonicClock() {
  197. @Override
  198. public ProposedTimestamp propose() {
  199. long t = getCurrentTime();
  200. return new ProposedTimestamp() {
  201. @Override
  202. public long read(TimeUnit unit) {
  203. return unit.convert(t, TimeUnit.MILLISECONDS);
  204. }
  205. @Override
  206. public void blockUntil(Duration maxWait) {
  207. // Do not wait.
  208. }
  209. };
  210. }
  211. };
  212. }
  213. /**
  214. * Adjusts the current time in seconds.
  215. *
  216. * @param secDelta
  217. * number of seconds to add to the current time.
  218. * @since 4.2
  219. */
  220. public void tick(int secDelta) {
  221. now += secDelta * 1000L;
  222. }
  223. /** {@inheritDoc} */
  224. @Override
  225. public int getTimezone(long when) {
  226. return getTimeZone().getOffset(when) / (60 * 1000);
  227. }
  228. /** {@inheritDoc} */
  229. @Override
  230. public TimeZone getTimeZone() {
  231. return TimeZone.getTimeZone("GMT-03:30");
  232. }
  233. /** {@inheritDoc} */
  234. @Override
  235. public Locale getLocale() {
  236. return Locale.US;
  237. }
  238. /** {@inheritDoc} */
  239. @Override
  240. public SimpleDateFormat getSimpleDateFormat(String pattern) {
  241. return new SimpleDateFormat(pattern, getLocale());
  242. }
  243. /** {@inheritDoc} */
  244. @Override
  245. public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
  246. return DateFormat
  247. .getDateTimeInstance(dateStyle, timeStyle, getLocale());
  248. }
  249. /**
  250. * Assign some properties for the currently executing platform
  251. */
  252. public void setCurrentPlatform() {
  253. resetOsNames();
  254. setProperty("os.name", System.getProperty("os.name"));
  255. setProperty("file.separator", System.getProperty("file.separator"));
  256. setProperty("path.separator", System.getProperty("path.separator"));
  257. setProperty("line.separator", System.getProperty("line.separator"));
  258. }
  259. /**
  260. * Emulate Windows
  261. */
  262. public void setWindows() {
  263. resetOsNames();
  264. setProperty("os.name", "Windows");
  265. setProperty("file.separator", "\\");
  266. setProperty("path.separator", ";");
  267. setProperty("line.separator", "\r\n");
  268. setPlatformChecker();
  269. }
  270. /**
  271. * Emulate Unix
  272. */
  273. public void setUnix() {
  274. resetOsNames();
  275. setProperty("os.name", "*nix"); // Essentially anything but Windows
  276. setProperty("file.separator", "/");
  277. setProperty("path.separator", ":");
  278. setProperty("line.separator", "\n");
  279. setPlatformChecker();
  280. }
  281. private void resetOsNames() {
  282. Field field;
  283. try {
  284. field = SystemReader.class.getDeclaredField("isWindows");
  285. field.setAccessible(true);
  286. field.set(null, null);
  287. field = SystemReader.class.getDeclaredField("isMacOS");
  288. field.setAccessible(true);
  289. field.set(null, null);
  290. } catch (Exception e) {
  291. e.printStackTrace();
  292. }
  293. }
  294. @Override
  295. public String toString() {
  296. return "MockSystemReader";
  297. }
  298. }