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.

PullCommandTest.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertFalse;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import java.io.ByteArrayOutputStream;
  50. import java.io.File;
  51. import java.io.FileInputStream;
  52. import java.io.FileOutputStream;
  53. import java.io.IOException;
  54. import java.util.concurrent.Callable;
  55. import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
  56. import org.eclipse.jgit.api.MergeResult.MergeStatus;
  57. import org.eclipse.jgit.api.errors.NoHeadException;
  58. import org.eclipse.jgit.junit.RepositoryTestCase;
  59. import org.eclipse.jgit.lib.Constants;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. import org.eclipse.jgit.lib.RefUpdate;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.eclipse.jgit.lib.RepositoryState;
  64. import org.eclipse.jgit.lib.StoredConfig;
  65. import org.eclipse.jgit.revwalk.RevCommit;
  66. import org.eclipse.jgit.revwalk.RevSort;
  67. import org.eclipse.jgit.revwalk.RevWalk;
  68. import org.eclipse.jgit.transport.RefSpec;
  69. import org.eclipse.jgit.transport.RemoteConfig;
  70. import org.eclipse.jgit.transport.URIish;
  71. import org.junit.Before;
  72. import org.junit.Test;
  73. public class PullCommandTest extends RepositoryTestCase {
  74. /** Second Test repository */
  75. protected Repository dbTarget;
  76. private Git source;
  77. private Git target;
  78. private File sourceFile;
  79. private File targetFile;
  80. @Test
  81. public void testPullFastForward() throws Exception {
  82. PullResult res = target.pull().call();
  83. // nothing to update since we don't have different data yet
  84. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  85. assertTrue(res.getMergeResult().getMergeStatus().equals(
  86. MergeStatus.ALREADY_UP_TO_DATE));
  87. assertFileContentsEqual(targetFile, "Hello world");
  88. // change the source file
  89. writeToFile(sourceFile, "Another change");
  90. source.add().addFilepattern("SomeFile.txt").call();
  91. source.commit().setMessage("Some change in remote").call();
  92. res = target.pull().call();
  93. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  94. assertEquals(res.getMergeResult().getMergeStatus(),
  95. MergeStatus.FAST_FORWARD);
  96. assertFileContentsEqual(targetFile, "Another change");
  97. assertEquals(RepositoryState.SAFE, target.getRepository()
  98. .getRepositoryState());
  99. res = target.pull().call();
  100. assertEquals(res.getMergeResult().getMergeStatus(),
  101. MergeStatus.ALREADY_UP_TO_DATE);
  102. }
  103. @Test
  104. public void testPullMerge() throws Exception {
  105. PullResult res = target.pull().call();
  106. // nothing to update since we don't have different data yet
  107. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  108. assertTrue(res.getMergeResult().getMergeStatus()
  109. .equals(MergeStatus.ALREADY_UP_TO_DATE));
  110. writeToFile(sourceFile, "Source change");
  111. source.add().addFilepattern("SomeFile.txt");
  112. RevCommit sourceCommit = source.commit()
  113. .setMessage("Source change in remote").call();
  114. File targetFile2 = new File(dbTarget.getWorkTree(), "OtherFile.txt");
  115. writeToFile(targetFile2, "Unconflicting change");
  116. target.add().addFilepattern("OtherFile.txt").call();
  117. RevCommit targetCommit = target.commit()
  118. .setMessage("Unconflicting change in local").call();
  119. res = target.pull().call();
  120. MergeResult mergeResult = res.getMergeResult();
  121. ObjectId[] mergedCommits = mergeResult.getMergedCommits();
  122. assertEquals(targetCommit.getId(), mergedCommits[0]);
  123. assertEquals(sourceCommit.getId(), mergedCommits[1]);
  124. RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult
  125. .getNewHead());
  126. String message = "Merge branch 'master' of "
  127. + db.getWorkTree().getAbsolutePath();
  128. assertEquals(message, mergeCommit.getShortMessage());
  129. }
  130. @Test
  131. public void testPullConflict() throws Exception {
  132. PullResult res = target.pull().call();
  133. // nothing to update since we don't have different data yet
  134. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  135. assertTrue(res.getMergeResult().getMergeStatus().equals(
  136. MergeStatus.ALREADY_UP_TO_DATE));
  137. assertFileContentsEqual(targetFile, "Hello world");
  138. // change the source file
  139. writeToFile(sourceFile, "Source change");
  140. source.add().addFilepattern("SomeFile.txt").call();
  141. source.commit().setMessage("Source change in remote").call();
  142. // change the target file
  143. writeToFile(targetFile, "Target change");
  144. target.add().addFilepattern("SomeFile.txt").call();
  145. target.commit().setMessage("Target change in local").call();
  146. res = target.pull().call();
  147. String sourceChangeString = "Source change\n>>>>>>> branch 'master' of "
  148. + target.getRepository().getConfig().getString("remote",
  149. "origin", "url");
  150. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  151. assertEquals(res.getMergeResult().getMergeStatus(),
  152. MergeStatus.CONFLICTING);
  153. String result = "<<<<<<< HEAD\nTarget change\n=======\n"
  154. + sourceChangeString + "\n";
  155. assertFileContentsEqual(targetFile, result);
  156. assertEquals(RepositoryState.MERGING, target.getRepository()
  157. .getRepositoryState());
  158. }
  159. @Test
  160. public void testPullLocalConflict() throws Exception {
  161. target.branchCreate().setName("basedOnMaster").setStartPoint(
  162. "refs/heads/master").setUpstreamMode(SetupUpstreamMode.TRACK)
  163. .call();
  164. target.getRepository().updateRef(Constants.HEAD).link(
  165. "refs/heads/basedOnMaster");
  166. PullResult res = target.pull().call();
  167. // nothing to update since we don't have different data yet
  168. assertNull(res.getFetchResult());
  169. assertTrue(res.getMergeResult().getMergeStatus().equals(
  170. MergeStatus.ALREADY_UP_TO_DATE));
  171. assertFileContentsEqual(targetFile, "Hello world");
  172. // change the file in master
  173. target.getRepository().updateRef(Constants.HEAD).link(
  174. "refs/heads/master");
  175. writeToFile(targetFile, "Master change");
  176. target.add().addFilepattern("SomeFile.txt").call();
  177. target.commit().setMessage("Source change in master").call();
  178. // change the file in slave
  179. target.getRepository().updateRef(Constants.HEAD).link(
  180. "refs/heads/basedOnMaster");
  181. writeToFile(targetFile, "Slave change");
  182. target.add().addFilepattern("SomeFile.txt").call();
  183. target.commit().setMessage("Source change in based on master").call();
  184. res = target.pull().call();
  185. String sourceChangeString = "Master change\n>>>>>>> branch 'master' of local repository";
  186. assertNull(res.getFetchResult());
  187. assertEquals(res.getMergeResult().getMergeStatus(),
  188. MergeStatus.CONFLICTING);
  189. String result = "<<<<<<< HEAD\nSlave change\n=======\n"
  190. + sourceChangeString + "\n";
  191. assertFileContentsEqual(targetFile, result);
  192. assertEquals(RepositoryState.MERGING, target.getRepository()
  193. .getRepositoryState());
  194. }
  195. @Test(expected = NoHeadException.class)
  196. public void testPullEmptyRepository() throws Exception {
  197. Repository empty = createWorkRepository();
  198. RefUpdate delete = empty.updateRef(Constants.HEAD, true);
  199. delete.setForceUpdate(true);
  200. delete.delete();
  201. Git.wrap(empty).pull().call();
  202. }
  203. @Test
  204. public void testPullMergeProgrammaticConfiguration() throws Exception {
  205. // create another commit on another branch in source
  206. source.checkout().setCreateBranch(true).setName("other").call();
  207. sourceFile = new File(db.getWorkTree(), "file2.txt");
  208. writeToFile(sourceFile, "content");
  209. source.add().addFilepattern("file2.txt").call();
  210. RevCommit sourceCommit = source.commit()
  211. .setMessage("source commit on branch other").call();
  212. File targetFile2 = new File(dbTarget.getWorkTree(), "OtherFile.txt");
  213. writeToFile(targetFile2, "Unconflicting change");
  214. target.add().addFilepattern("OtherFile.txt").call();
  215. RevCommit targetCommit = target.commit()
  216. .setMessage("Unconflicting change in local").call();
  217. PullResult res = target.pull().setRemote("origin")
  218. .setRemoteBranchName("other")
  219. .setRebase(false).call();
  220. MergeResult mergeResult = res.getMergeResult();
  221. ObjectId[] mergedCommits = mergeResult.getMergedCommits();
  222. assertEquals(targetCommit.getId(), mergedCommits[0]);
  223. assertEquals(sourceCommit.getId(), mergedCommits[1]);
  224. RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult
  225. .getNewHead());
  226. String message = "Merge branch 'other' of "
  227. + db.getWorkTree().getAbsolutePath();
  228. assertEquals(message, mergeCommit.getShortMessage());
  229. }
  230. @Test
  231. public void testPullMergeProgrammaticConfigurationImpliedTargetBranch()
  232. throws Exception {
  233. // create another commit on another branch in source
  234. source.checkout().setCreateBranch(true).setName("other").call();
  235. sourceFile = new File(db.getWorkTree(), "file2.txt");
  236. writeToFile(sourceFile, "content");
  237. source.add().addFilepattern("file2.txt").call();
  238. RevCommit sourceCommit = source.commit()
  239. .setMessage("source commit on branch other").call();
  240. target.checkout().setCreateBranch(true).setName("other").call();
  241. File targetFile2 = new File(dbTarget.getWorkTree(), "OtherFile.txt");
  242. writeToFile(targetFile2, "Unconflicting change");
  243. target.add().addFilepattern("OtherFile.txt").call();
  244. RevCommit targetCommit = target.commit()
  245. .setMessage("Unconflicting change in local").call();
  246. // the source branch "other" matching the target branch should be
  247. // implied
  248. PullResult res = target.pull().setRemote("origin").setRebase(false)
  249. .call();
  250. MergeResult mergeResult = res.getMergeResult();
  251. ObjectId[] mergedCommits = mergeResult.getMergedCommits();
  252. assertEquals(targetCommit.getId(), mergedCommits[0]);
  253. assertEquals(sourceCommit.getId(), mergedCommits[1]);
  254. RevCommit mergeCommit = new RevWalk(dbTarget).parseCommit(mergeResult
  255. .getNewHead());
  256. String message = "Merge branch 'other' of "
  257. + db.getWorkTree().getAbsolutePath() + " into other";
  258. assertEquals(message, mergeCommit.getShortMessage());
  259. }
  260. private enum TestPullMode {
  261. MERGE, REBASE, REBASE_PREASERVE
  262. }
  263. @Test
  264. /** global rebase config should be respected */
  265. public void testPullWithRebasePreserve1Config() throws Exception {
  266. Callable<PullResult> setup = new Callable<PullResult>() {
  267. public PullResult call() throws Exception {
  268. StoredConfig config = dbTarget.getConfig();
  269. config.setString("pull", null, "rebase", "preserve");
  270. config.save();
  271. return target.pull().call();
  272. }
  273. };
  274. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  275. }
  276. @Test
  277. /** the branch-local config should win over the global config */
  278. public void testPullWithRebasePreserveConfig2() throws Exception {
  279. Callable<PullResult> setup = new Callable<PullResult>() {
  280. public PullResult call() throws Exception {
  281. StoredConfig config = dbTarget.getConfig();
  282. config.setString("pull", null, "rebase", "false");
  283. config.setString("branch", "master", "rebase", "preserve");
  284. config.save();
  285. return target.pull().call();
  286. }
  287. };
  288. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  289. }
  290. @Test
  291. /** the branch-local config should be respected */
  292. public void testPullWithRebasePreserveConfig3() throws Exception {
  293. Callable<PullResult> setup = new Callable<PullResult>() {
  294. public PullResult call() throws Exception {
  295. StoredConfig config = dbTarget.getConfig();
  296. config.setString("branch", "master", "rebase", "preserve");
  297. config.save();
  298. return target.pull().call();
  299. }
  300. };
  301. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  302. }
  303. @Test
  304. /** global rebase config should be respected */
  305. public void testPullWithRebaseConfig1() throws Exception {
  306. Callable<PullResult> setup = new Callable<PullResult>() {
  307. public PullResult call() throws Exception {
  308. StoredConfig config = dbTarget.getConfig();
  309. config.setString("pull", null, "rebase", "true");
  310. config.save();
  311. return target.pull().call();
  312. }
  313. };
  314. doTestPullWithRebase(setup, TestPullMode.REBASE);
  315. }
  316. @Test
  317. /** the branch-local config should win over the global config */
  318. public void testPullWithRebaseConfig2() throws Exception {
  319. Callable<PullResult> setup = new Callable<PullResult>() {
  320. public PullResult call() throws Exception {
  321. StoredConfig config = dbTarget.getConfig();
  322. config.setString("pull", null, "rebase", "preserve");
  323. config.setString("branch", "master", "rebase", "true");
  324. config.save();
  325. return target.pull().call();
  326. }
  327. };
  328. doTestPullWithRebase(setup, TestPullMode.REBASE);
  329. }
  330. @Test
  331. /** the branch-local config should be respected */
  332. public void testPullWithRebaseConfig3() throws Exception {
  333. Callable<PullResult> setup = new Callable<PullResult>() {
  334. public PullResult call() throws Exception {
  335. StoredConfig config = dbTarget.getConfig();
  336. config.setString("branch", "master", "rebase", "true");
  337. config.save();
  338. return target.pull().call();
  339. }
  340. };
  341. doTestPullWithRebase(setup, TestPullMode.REBASE);
  342. }
  343. @Test
  344. /** without config it should merge */
  345. public void testPullWithoutConfig() throws Exception {
  346. Callable<PullResult> setup = new Callable<PullResult>() {
  347. public PullResult call() throws Exception {
  348. return target.pull().call();
  349. }
  350. };
  351. doTestPullWithRebase(setup, TestPullMode.MERGE);
  352. }
  353. @Test
  354. /** the branch local config should win over the global config */
  355. public void testPullWithMergeConfig() throws Exception {
  356. Callable<PullResult> setup = new Callable<PullResult>() {
  357. public PullResult call() throws Exception {
  358. StoredConfig config = dbTarget.getConfig();
  359. config.setString("pull", null, "rebase", "true");
  360. config.setString("branch", "master", "rebase", "false");
  361. config.save();
  362. return target.pull().call();
  363. }
  364. };
  365. doTestPullWithRebase(setup, TestPullMode.MERGE);
  366. }
  367. @Test
  368. /** the branch local config should win over the global config */
  369. public void testPullWithMergeConfig2() throws Exception {
  370. Callable<PullResult> setup = new Callable<PullResult>() {
  371. public PullResult call() throws Exception {
  372. StoredConfig config = dbTarget.getConfig();
  373. config.setString("pull", null, "rebase", "false");
  374. config.save();
  375. return target.pull().call();
  376. }
  377. };
  378. doTestPullWithRebase(setup, TestPullMode.MERGE);
  379. }
  380. private void doTestPullWithRebase(Callable<PullResult> pullSetup,
  381. TestPullMode expectedPullMode) throws Exception {
  382. // simple upstream change
  383. writeToFile(sourceFile, "content");
  384. source.add().addFilepattern(sourceFile.getName()).call();
  385. RevCommit sourceCommit = source.commit().setMessage("source commit")
  386. .call();
  387. // create a merge commit in target
  388. File loxalFile = new File(dbTarget.getWorkTree(), "local.txt");
  389. writeToFile(loxalFile, "initial\n");
  390. target.add().addFilepattern("local.txt").call();
  391. RevCommit t1 = target.commit().setMessage("target commit 1").call();
  392. target.checkout().setCreateBranch(true).setName("side").call();
  393. String newContent = "initial\n" + "and more\n";
  394. writeToFile(loxalFile, newContent);
  395. target.add().addFilepattern("local.txt").call();
  396. RevCommit t2 = target.commit().setMessage("target commit 2").call();
  397. target.checkout().setName("master").call();
  398. MergeResult mergeResult = target.merge()
  399. .setFastForward(MergeCommand.FastForwardMode.NO_FF).include(t2)
  400. .call();
  401. assertEquals(MergeStatus.MERGED, mergeResult.getMergeStatus());
  402. assertFileContentsEqual(loxalFile, newContent);
  403. ObjectId merge = mergeResult.getNewHead();
  404. // pull
  405. PullResult res = pullSetup.call();
  406. assertNotNull(res.getFetchResult());
  407. if (expectedPullMode == TestPullMode.MERGE) {
  408. assertEquals(MergeStatus.MERGED, res.getMergeResult()
  409. .getMergeStatus());
  410. assertNull(res.getRebaseResult());
  411. } else {
  412. assertNull(res.getMergeResult());
  413. assertEquals(RebaseResult.OK_RESULT, res.getRebaseResult());
  414. }
  415. assertFileContentsEqual(sourceFile, "content");
  416. RevWalk rw = new RevWalk(dbTarget);
  417. rw.sort(RevSort.TOPO);
  418. rw.markStart(rw.parseCommit(dbTarget.resolve("refs/heads/master")));
  419. RevCommit next;
  420. if (expectedPullMode == TestPullMode.MERGE) {
  421. next = rw.next();
  422. assertEquals(2, next.getParentCount());
  423. assertEquals(merge, next.getParent(0));
  424. assertEquals(sourceCommit, next.getParent(1));
  425. // since both parents are known do no further checks here
  426. } else {
  427. if (expectedPullMode == TestPullMode.REBASE_PREASERVE) {
  428. next = rw.next();
  429. assertEquals(2, next.getParentCount());
  430. }
  431. next = rw.next();
  432. assertEquals(t2.getShortMessage(), next.getShortMessage());
  433. next = rw.next();
  434. assertEquals(t1.getShortMessage(), next.getShortMessage());
  435. next = rw.next();
  436. assertEquals(sourceCommit, next);
  437. next = rw.next();
  438. assertEquals("Initial commit for source", next.getShortMessage());
  439. next = rw.next();
  440. assertNull(next);
  441. }
  442. rw.release();
  443. }
  444. @Override
  445. @Before
  446. public void setUp() throws Exception {
  447. super.setUp();
  448. dbTarget = createWorkRepository();
  449. source = new Git(db);
  450. target = new Git(dbTarget);
  451. // put some file in the source repo
  452. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  453. writeToFile(sourceFile, "Hello world");
  454. // and commit it
  455. source.add().addFilepattern("SomeFile.txt").call();
  456. source.commit().setMessage("Initial commit for source").call();
  457. // configure the target repo to connect to the source via "origin"
  458. StoredConfig targetConfig = dbTarget.getConfig();
  459. targetConfig.setString("branch", "master", "remote", "origin");
  460. targetConfig
  461. .setString("branch", "master", "merge", "refs/heads/master");
  462. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  463. config
  464. .addURI(new URIish(source.getRepository().getWorkTree()
  465. .getAbsolutePath()));
  466. config.addFetchRefSpec(new RefSpec(
  467. "+refs/heads/*:refs/remotes/origin/*"));
  468. config.update(targetConfig);
  469. targetConfig.save();
  470. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  471. // make sure we have the same content
  472. target.pull().call();
  473. assertFileContentsEqual(targetFile, "Hello world");
  474. }
  475. private static void writeToFile(File actFile, String string)
  476. throws IOException {
  477. FileOutputStream fos = null;
  478. try {
  479. fos = new FileOutputStream(actFile);
  480. fos.write(string.getBytes("UTF-8"));
  481. fos.close();
  482. } finally {
  483. if (fos != null)
  484. fos.close();
  485. }
  486. }
  487. private static void assertFileContentsEqual(File actFile, String string)
  488. throws IOException {
  489. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  490. FileInputStream fis = null;
  491. byte[] buffer = new byte[100];
  492. try {
  493. fis = new FileInputStream(actFile);
  494. int read = fis.read(buffer);
  495. while (read > 0) {
  496. bos.write(buffer, 0, read);
  497. read = fis.read(buffer);
  498. }
  499. String content = new String(bos.toByteArray(), "UTF-8");
  500. assertEquals(string, content);
  501. } finally {
  502. if (fis != null)
  503. fis.close();
  504. }
  505. }
  506. }