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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 () -> {
  197. long t = getCurrentTime();
  198. return new ProposedTimestamp() {
  199. @Override
  200. public long read(TimeUnit unit) {
  201. return unit.convert(t, TimeUnit.MILLISECONDS);
  202. }
  203. @Override
  204. public void blockUntil(Duration maxWait) {
  205. // Do not wait.
  206. }
  207. };
  208. };
  209. }
  210. /**
  211. * Adjusts the current time in seconds.
  212. *
  213. * @param secDelta
  214. * number of seconds to add to the current time.
  215. * @since 4.2
  216. */
  217. public void tick(int secDelta) {
  218. now += secDelta * 1000L;
  219. }
  220. /** {@inheritDoc} */
  221. @Override
  222. public int getTimezone(long when) {
  223. return getTimeZone().getOffset(when) / (60 * 1000);
  224. }
  225. /** {@inheritDoc} */
  226. @Override
  227. public TimeZone getTimeZone() {
  228. return TimeZone.getTimeZone("GMT-03:30");
  229. }
  230. /** {@inheritDoc} */
  231. @Override
  232. public Locale getLocale() {
  233. return Locale.US;
  234. }
  235. /** {@inheritDoc} */
  236. @Override
  237. public SimpleDateFormat getSimpleDateFormat(String pattern) {
  238. return new SimpleDateFormat(pattern, getLocale());
  239. }
  240. /** {@inheritDoc} */
  241. @Override
  242. public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
  243. return DateFormat
  244. .getDateTimeInstance(dateStyle, timeStyle, getLocale());
  245. }
  246. /**
  247. * Assign some properties for the currently executing platform
  248. */
  249. public void setCurrentPlatform() {
  250. resetOsNames();
  251. setProperty("os.name", System.getProperty("os.name"));
  252. setProperty("file.separator", System.getProperty("file.separator"));
  253. setProperty("path.separator", System.getProperty("path.separator"));
  254. setProperty("line.separator", System.getProperty("line.separator"));
  255. }
  256. /**
  257. * Emulate Windows
  258. */
  259. public void setWindows() {
  260. resetOsNames();
  261. setProperty("os.name", "Windows");
  262. setProperty("file.separator", "\\");
  263. setProperty("path.separator", ";");
  264. setProperty("line.separator", "\r\n");
  265. setPlatformChecker();
  266. }
  267. /**
  268. * Emulate Unix
  269. */
  270. public void setUnix() {
  271. resetOsNames();
  272. setProperty("os.name", "*nix"); // Essentially anything but Windows
  273. setProperty("file.separator", "/");
  274. setProperty("path.separator", ":");
  275. setProperty("line.separator", "\n");
  276. setPlatformChecker();
  277. }
  278. private void resetOsNames() {
  279. Field field;
  280. try {
  281. field = SystemReader.class.getDeclaredField("isWindows");
  282. field.setAccessible(true);
  283. field.set(null, null);
  284. field = SystemReader.class.getDeclaredField("isMacOS");
  285. field.setAccessible(true);
  286. field.set(null, null);
  287. } catch (Exception e) {
  288. e.printStackTrace();
  289. }
  290. }
  291. @Override
  292. public String toString() {
  293. return "MockSystemReader";
  294. }
  295. }