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

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