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 7.3KB

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