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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.storage.file.FileBasedConfig;
  61. import org.eclipse.jgit.util.FS;
  62. import org.eclipse.jgit.util.SystemReader;
  63. import org.eclipse.jgit.util.time.MonotonicClock;
  64. import org.eclipse.jgit.util.time.ProposedTimestamp;
  65. /**
  66. * Mock {@link org.eclipse.jgit.util.SystemReader} for tests.
  67. */
  68. public class MockSystemReader extends SystemReader {
  69. private static final class MockConfig extends FileBasedConfig {
  70. private MockConfig(File cfgLocation, FS fs) {
  71. super(cfgLocation, fs);
  72. }
  73. @Override
  74. public void load() throws IOException, ConfigInvalidException {
  75. // Do nothing
  76. }
  77. @Override
  78. public void save() throws IOException {
  79. // Do nothing
  80. }
  81. @Override
  82. public boolean isOutdated() {
  83. return false;
  84. }
  85. }
  86. long now = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
  87. final Map<String, String> values = new HashMap<>();
  88. FileBasedConfig userGitConfig;
  89. FileBasedConfig systemGitConfig;
  90. /**
  91. * Constructor for <code>MockSystemReader</code>
  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. private void init(String n) {
  105. setProperty(n, n);
  106. }
  107. /**
  108. * Clear properties
  109. */
  110. public void clearProperties() {
  111. values.clear();
  112. }
  113. /**
  114. * Set a property
  115. *
  116. * @param key
  117. * @param value
  118. */
  119. public void setProperty(String key, String value) {
  120. values.put(key, value);
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public String getenv(String variable) {
  125. return values.get(variable);
  126. }
  127. /** {@inheritDoc} */
  128. @Override
  129. public String getProperty(String key) {
  130. return values.get(key);
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public FileBasedConfig openUserConfig(Config parent, FS fs) {
  135. assert parent == null || parent == systemGitConfig;
  136. return userGitConfig;
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. public FileBasedConfig openSystemConfig(Config parent, FS fs) {
  141. assert parent == null;
  142. return systemGitConfig;
  143. }
  144. /** {@inheritDoc} */
  145. @Override
  146. public String getHostname() {
  147. return "fake.host.example.com";
  148. }
  149. /** {@inheritDoc} */
  150. @Override
  151. public long getCurrentTime() {
  152. return now;
  153. }
  154. /** {@inheritDoc} */
  155. @Override
  156. public MonotonicClock getClock() {
  157. return new MonotonicClock() {
  158. @Override
  159. public ProposedTimestamp propose() {
  160. long t = getCurrentTime();
  161. return new ProposedTimestamp() {
  162. @Override
  163. public long read(TimeUnit unit) {
  164. return unit.convert(t, TimeUnit.MILLISECONDS);
  165. }
  166. @Override
  167. public void blockUntil(Duration maxWait) {
  168. // Do not wait.
  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(int secDelta) {
  182. now += secDelta * 1000L;
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. public int getTimezone(long when) {
  187. return getTimeZone().getOffset(when) / (60 * 1000);
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public TimeZone getTimeZone() {
  192. return TimeZone.getTimeZone("GMT-03:30");
  193. }
  194. /** {@inheritDoc} */
  195. @Override
  196. public Locale getLocale() {
  197. return Locale.US;
  198. }
  199. /** {@inheritDoc} */
  200. @Override
  201. public SimpleDateFormat getSimpleDateFormat(String pattern) {
  202. return new SimpleDateFormat(pattern, getLocale());
  203. }
  204. /** {@inheritDoc} */
  205. @Override
  206. public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
  207. return DateFormat
  208. .getDateTimeInstance(dateStyle, timeStyle, getLocale());
  209. }
  210. /**
  211. * Assign some properties for the currently executing platform
  212. */
  213. public void setCurrentPlatform() {
  214. resetOsNames();
  215. setProperty("os.name", System.getProperty("os.name"));
  216. setProperty("file.separator", System.getProperty("file.separator"));
  217. setProperty("path.separator", System.getProperty("path.separator"));
  218. setProperty("line.separator", System.getProperty("line.separator"));
  219. }
  220. /**
  221. * Emulate Windows
  222. */
  223. public void setWindows() {
  224. resetOsNames();
  225. setProperty("os.name", "Windows");
  226. setProperty("file.separator", "\\");
  227. setProperty("path.separator", ";");
  228. setProperty("line.separator", "\r\n");
  229. setPlatformChecker();
  230. }
  231. /**
  232. * Emulate Unix
  233. */
  234. public void setUnix() {
  235. resetOsNames();
  236. setProperty("os.name", "*nix"); // Essentially anything but Windows
  237. setProperty("file.separator", "/");
  238. setProperty("path.separator", ":");
  239. setProperty("line.separator", "\n");
  240. setPlatformChecker();
  241. }
  242. private void resetOsNames() {
  243. Field field;
  244. try {
  245. field = SystemReader.class.getDeclaredField("isWindows");
  246. field.setAccessible(true);
  247. field.set(null, null);
  248. field = SystemReader.class.getDeclaredField("isMacOS");
  249. field.setAccessible(true);
  250. field.set(null, null);
  251. } catch (Exception e) {
  252. e.printStackTrace();
  253. }
  254. }
  255. }