Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

RepositoryTestCase.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2006-2007, Shawn O. Pearce <spearce@spearce.org>
  5. * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.lib;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.FileOutputStream;
  50. import java.io.IOException;
  51. import java.io.InputStreamReader;
  52. import java.io.Reader;
  53. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  54. import org.eclipse.jgit.storage.file.FileRepository;
  55. /**
  56. * Base class for most JGit unit tests.
  57. *
  58. * Sets up a predefined test repository and has support for creating additional
  59. * repositories and destroying them when the tests are finished.
  60. */
  61. public abstract class RepositoryTestCase extends LocalDiskRepositoryTestCase {
  62. protected static void copyFile(final File src, final File dst)
  63. throws IOException {
  64. final FileInputStream fis = new FileInputStream(src);
  65. try {
  66. final FileOutputStream fos = new FileOutputStream(dst);
  67. try {
  68. final byte[] buf = new byte[4096];
  69. int r;
  70. while ((r = fis.read(buf)) > 0) {
  71. fos.write(buf, 0, r);
  72. }
  73. } finally {
  74. fos.close();
  75. }
  76. } finally {
  77. fis.close();
  78. }
  79. }
  80. protected File writeTrashFile(final String name, final String data)
  81. throws IOException {
  82. File path = new File(db.getWorkTree(), name);
  83. write(path, data);
  84. return path;
  85. }
  86. protected static void checkFile(File f, final String checkData)
  87. throws IOException {
  88. Reader r = new InputStreamReader(new FileInputStream(f), "ISO-8859-1");
  89. try {
  90. char[] data = new char[(int) f.length()];
  91. if (f.length() != r.read(data))
  92. throw new IOException("Internal error reading file data from "+f);
  93. assertEquals(checkData, new String(data));
  94. } finally {
  95. r.close();
  96. }
  97. }
  98. /** Test repository, initialized for this test case. */
  99. protected FileRepository db;
  100. /** Working directory of {@link #db}. */
  101. protected File trash;
  102. @Override
  103. protected void setUp() throws Exception {
  104. super.setUp();
  105. db = createWorkRepository();
  106. trash = db.getWorkTree();
  107. }
  108. }