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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 boolean isOutdated() {
  79. return false;
  80. }
  81. }
  82. long now = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
  83. final Map<String, String> values = new HashMap<>();
  84. FileBasedConfig userGitConfig;
  85. FileBasedConfig systemGitConfig;
  86. /**
  87. * Constructor for <code>MockSystemReader</code>
  88. */
  89. public MockSystemReader() {
  90. init(Constants.OS_USER_NAME_KEY);
  91. init(Constants.GIT_AUTHOR_NAME_KEY);
  92. init(Constants.GIT_AUTHOR_EMAIL_KEY);
  93. init(Constants.GIT_COMMITTER_NAME_KEY);
  94. init(Constants.GIT_COMMITTER_EMAIL_KEY);
  95. setProperty(Constants.OS_USER_DIR, ".");
  96. userGitConfig = new MockConfig(null, null);
  97. systemGitConfig = new MockConfig(null, null);
  98. setCurrentPlatform();
  99. }
  100. private void init(String n) {
  101. setProperty(n, n);
  102. }
  103. /**
  104. * Clear properties
  105. */
  106. public void clearProperties() {
  107. values.clear();
  108. }
  109. /**
  110. * Set a property
  111. *
  112. * @param key
  113. * @param value
  114. */
  115. public void setProperty(String key, String value) {
  116. values.put(key, value);
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public String getenv(String variable) {
  121. return values.get(variable);
  122. }
  123. /** {@inheritDoc} */
  124. @Override
  125. public String getProperty(String key) {
  126. return values.get(key);
  127. }
  128. /** {@inheritDoc} */
  129. @Override
  130. public FileBasedConfig openUserConfig(Config parent, FS fs) {
  131. assert parent == null || parent == systemGitConfig;
  132. return userGitConfig;
  133. }
  134. /** {@inheritDoc} */
  135. @Override
  136. public FileBasedConfig openSystemConfig(Config parent, FS fs) {
  137. assert parent == null;
  138. return systemGitConfig;
  139. }
  140. /** {@inheritDoc} */
  141. @Override
  142. public String getHostname() {
  143. return "fake.host.example.com";
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. public long getCurrentTime() {
  148. return now;
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public MonotonicClock getClock() {
  153. return new MonotonicClock() {
  154. @Override
  155. public ProposedTimestamp propose() {
  156. long t = getCurrentTime();
  157. return new ProposedTimestamp() {
  158. @Override
  159. public long read(TimeUnit unit) {
  160. return unit.convert(t, TimeUnit.MILLISECONDS);
  161. }
  162. @Override
  163. public void blockUntil(Duration maxWait) {
  164. // Do not wait.
  165. }
  166. };
  167. }
  168. };
  169. }
  170. /**
  171. * Adjusts the current time in seconds.
  172. *
  173. * @param secDelta
  174. * number of seconds to add to the current time.
  175. * @since 4.2
  176. */
  177. public void tick(int secDelta) {
  178. now += secDelta * 1000L;
  179. }
  180. /** {@inheritDoc} */
  181. @Override
  182. public int getTimezone(long when) {
  183. return getTimeZone().getOffset(when) / (60 * 1000);
  184. }
  185. /** {@inheritDoc} */
  186. @Override
  187. public TimeZone getTimeZone() {
  188. return TimeZone.getTimeZone("GMT-03:30");
  189. }
  190. /** {@inheritDoc} */
  191. @Override
  192. public Locale getLocale() {
  193. return Locale.US;
  194. }
  195. /** {@inheritDoc} */
  196. @Override
  197. public SimpleDateFormat getSimpleDateFormat(String pattern) {
  198. return new SimpleDateFormat(pattern, getLocale());
  199. }
  200. /** {@inheritDoc} */
  201. @Override
  202. public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
  203. return DateFormat
  204. .getDateTimeInstance(dateStyle, timeStyle, getLocale());
  205. }
  206. /**
  207. * Assign some properties for the currently executing platform
  208. */
  209. public void setCurrentPlatform() {
  210. resetOsNames();
  211. setProperty("os.name", System.getProperty("os.name"));
  212. setProperty("file.separator", System.getProperty("file.separator"));
  213. setProperty("path.separator", System.getProperty("path.separator"));
  214. setProperty("line.separator", System.getProperty("line.separator"));
  215. }
  216. /**
  217. * Emulate Windows
  218. */
  219. public void setWindows() {
  220. resetOsNames();
  221. setProperty("os.name", "Windows");
  222. setProperty("file.separator", "\\");
  223. setProperty("path.separator", ";");
  224. setProperty("line.separator", "\r\n");
  225. setPlatformChecker();
  226. }
  227. /**
  228. * Emulate Unix
  229. */
  230. public void setUnix() {
  231. resetOsNames();
  232. setProperty("os.name", "*nix"); // Essentially anything but Windows
  233. setProperty("file.separator", "/");
  234. setProperty("path.separator", ":");
  235. setProperty("line.separator", "\n");
  236. setPlatformChecker();
  237. }
  238. private void resetOsNames() {
  239. Field field;
  240. try {
  241. field = SystemReader.class.getDeclaredField("isWindows");
  242. field.setAccessible(true);
  243. field.set(null, null);
  244. field = SystemReader.class.getDeclaredField("isMacOS");
  245. field.setAccessible(true);
  246. field.set(null, null);
  247. } catch (Exception e) {
  248. e.printStackTrace();
  249. }
  250. }
  251. }