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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. try (RevWalk rw = new RevWalk(dbTarget)) {
  125. RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
  126. String message = "Merge branch 'master' of "
  127. + db.getWorkTree().getAbsolutePath();
  128. assertEquals(message, mergeCommit.getShortMessage());
  129. }
  130. }
  131. @Test
  132. public void testPullConflict() throws Exception {
  133. PullResult res = target.pull().call();
  134. // nothing to update since we don't have different data yet
  135. assertTrue(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  136. assertTrue(res.getMergeResult().getMergeStatus().equals(
  137. MergeStatus.ALREADY_UP_TO_DATE));
  138. assertFileContentsEqual(targetFile, "Hello world");
  139. // change the source file
  140. writeToFile(sourceFile, "Source change");
  141. source.add().addFilepattern("SomeFile.txt").call();
  142. source.commit().setMessage("Source change in remote").call();
  143. // change the target file
  144. writeToFile(targetFile, "Target change");
  145. target.add().addFilepattern("SomeFile.txt").call();
  146. target.commit().setMessage("Target change in local").call();
  147. res = target.pull().call();
  148. String sourceChangeString = "Source change\n>>>>>>> branch 'master' of "
  149. + target.getRepository().getConfig().getString("remote",
  150. "origin", "url");
  151. assertFalse(res.getFetchResult().getTrackingRefUpdates().isEmpty());
  152. assertEquals(res.getMergeResult().getMergeStatus(),
  153. MergeStatus.CONFLICTING);
  154. String result = "<<<<<<< HEAD\nTarget change\n=======\n"
  155. + sourceChangeString + "\n";
  156. assertFileContentsEqual(targetFile, result);
  157. assertEquals(RepositoryState.MERGING, target.getRepository()
  158. .getRepositoryState());
  159. }
  160. @Test
  161. public void testPullLocalConflict() throws Exception {
  162. target.branchCreate().setName("basedOnMaster").setStartPoint(
  163. "refs/heads/master").setUpstreamMode(SetupUpstreamMode.TRACK)
  164. .call();
  165. target.getRepository().updateRef(Constants.HEAD).link(
  166. "refs/heads/basedOnMaster");
  167. PullResult res = target.pull().call();
  168. // nothing to update since we don't have different data yet
  169. assertNull(res.getFetchResult());
  170. assertTrue(res.getMergeResult().getMergeStatus().equals(
  171. MergeStatus.ALREADY_UP_TO_DATE));
  172. assertFileContentsEqual(targetFile, "Hello world");
  173. // change the file in master
  174. target.getRepository().updateRef(Constants.HEAD).link(
  175. "refs/heads/master");
  176. writeToFile(targetFile, "Master change");
  177. target.add().addFilepattern("SomeFile.txt").call();
  178. target.commit().setMessage("Source change in master").call();
  179. // change the file in slave
  180. target.getRepository().updateRef(Constants.HEAD).link(
  181. "refs/heads/basedOnMaster");
  182. writeToFile(targetFile, "Slave change");
  183. target.add().addFilepattern("SomeFile.txt").call();
  184. target.commit().setMessage("Source change in based on master").call();
  185. res = target.pull().call();
  186. String sourceChangeString = "Master change\n>>>>>>> branch 'master' of local repository";
  187. assertNull(res.getFetchResult());
  188. assertEquals(res.getMergeResult().getMergeStatus(),
  189. MergeStatus.CONFLICTING);
  190. String result = "<<<<<<< HEAD\nSlave change\n=======\n"
  191. + sourceChangeString + "\n";
  192. assertFileContentsEqual(targetFile, result);
  193. assertEquals(RepositoryState.MERGING, target.getRepository()
  194. .getRepositoryState());
  195. }
  196. @Test(expected = NoHeadException.class)
  197. public void testPullEmptyRepository() throws Exception {
  198. Repository empty = createWorkRepository();
  199. RefUpdate delete = empty.updateRef(Constants.HEAD, true);
  200. delete.setForceUpdate(true);
  201. delete.delete();
  202. Git.wrap(empty).pull().call();
  203. }
  204. @Test
  205. public void testPullMergeProgrammaticConfiguration() throws Exception {
  206. // create another commit on another branch in source
  207. source.checkout().setCreateBranch(true).setName("other").call();
  208. sourceFile = new File(db.getWorkTree(), "file2.txt");
  209. writeToFile(sourceFile, "content");
  210. source.add().addFilepattern("file2.txt").call();
  211. RevCommit sourceCommit = source.commit()
  212. .setMessage("source commit on branch other").call();
  213. File targetFile2 = new File(dbTarget.getWorkTree(), "OtherFile.txt");
  214. writeToFile(targetFile2, "Unconflicting change");
  215. target.add().addFilepattern("OtherFile.txt").call();
  216. RevCommit targetCommit = target.commit()
  217. .setMessage("Unconflicting change in local").call();
  218. PullResult res = target.pull().setRemote("origin")
  219. .setRemoteBranchName("other")
  220. .setRebase(false).call();
  221. MergeResult mergeResult = res.getMergeResult();
  222. ObjectId[] mergedCommits = mergeResult.getMergedCommits();
  223. assertEquals(targetCommit.getId(), mergedCommits[0]);
  224. assertEquals(sourceCommit.getId(), mergedCommits[1]);
  225. try (RevWalk rw = new RevWalk(dbTarget)) {
  226. RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
  227. String message = "Merge branch 'other' of "
  228. + db.getWorkTree().getAbsolutePath();
  229. assertEquals(message, mergeCommit.getShortMessage());
  230. }
  231. }
  232. @Test
  233. public void testPullMergeProgrammaticConfigurationImpliedTargetBranch()
  234. throws Exception {
  235. // create another commit on another branch in source
  236. source.checkout().setCreateBranch(true).setName("other").call();
  237. sourceFile = new File(db.getWorkTree(), "file2.txt");
  238. writeToFile(sourceFile, "content");
  239. source.add().addFilepattern("file2.txt").call();
  240. RevCommit sourceCommit = source.commit()
  241. .setMessage("source commit on branch other").call();
  242. target.checkout().setCreateBranch(true).setName("other").call();
  243. File targetFile2 = new File(dbTarget.getWorkTree(), "OtherFile.txt");
  244. writeToFile(targetFile2, "Unconflicting change");
  245. target.add().addFilepattern("OtherFile.txt").call();
  246. RevCommit targetCommit = target.commit()
  247. .setMessage("Unconflicting change in local").call();
  248. // the source branch "other" matching the target branch should be
  249. // implied
  250. PullResult res = target.pull().setRemote("origin").setRebase(false)
  251. .call();
  252. MergeResult mergeResult = res.getMergeResult();
  253. ObjectId[] mergedCommits = mergeResult.getMergedCommits();
  254. assertEquals(targetCommit.getId(), mergedCommits[0]);
  255. assertEquals(sourceCommit.getId(), mergedCommits[1]);
  256. try (RevWalk rw = new RevWalk(dbTarget)) {
  257. RevCommit mergeCommit = rw.parseCommit(mergeResult.getNewHead());
  258. String message = "Merge branch 'other' of "
  259. + db.getWorkTree().getAbsolutePath() + " into other";
  260. assertEquals(message, mergeCommit.getShortMessage());
  261. }
  262. }
  263. private enum TestPullMode {
  264. MERGE, REBASE, REBASE_PREASERVE
  265. }
  266. @Test
  267. /** global rebase config should be respected */
  268. public void testPullWithRebasePreserve1Config() throws Exception {
  269. Callable<PullResult> setup = new Callable<PullResult>() {
  270. public PullResult call() throws Exception {
  271. StoredConfig config = dbTarget.getConfig();
  272. config.setString("pull", null, "rebase", "preserve");
  273. config.save();
  274. return target.pull().call();
  275. }
  276. };
  277. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  278. }
  279. @Test
  280. /** the branch-local config should win over the global config */
  281. public void testPullWithRebasePreserveConfig2() throws Exception {
  282. Callable<PullResult> setup = new Callable<PullResult>() {
  283. public PullResult call() throws Exception {
  284. StoredConfig config = dbTarget.getConfig();
  285. config.setString("pull", null, "rebase", "false");
  286. config.setString("branch", "master", "rebase", "preserve");
  287. config.save();
  288. return target.pull().call();
  289. }
  290. };
  291. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  292. }
  293. @Test
  294. /** the branch-local config should be respected */
  295. public void testPullWithRebasePreserveConfig3() throws Exception {
  296. Callable<PullResult> setup = new Callable<PullResult>() {
  297. public PullResult call() throws Exception {
  298. StoredConfig config = dbTarget.getConfig();
  299. config.setString("branch", "master", "rebase", "preserve");
  300. config.save();
  301. return target.pull().call();
  302. }
  303. };
  304. doTestPullWithRebase(setup, TestPullMode.REBASE_PREASERVE);
  305. }
  306. @Test
  307. /** global rebase config should be respected */
  308. public void testPullWithRebaseConfig1() throws Exception {
  309. Callable<PullResult> setup = new Callable<PullResult>() {
  310. public PullResult call() throws Exception {
  311. StoredConfig config = dbTarget.getConfig();
  312. config.setString("pull", null, "rebase", "true");
  313. config.save();
  314. return target.pull().call();
  315. }
  316. };
  317. doTestPullWithRebase(setup, TestPullMode.REBASE);
  318. }
  319. @Test
  320. /** the branch-local config should win over the global config */
  321. public void testPullWithRebaseConfig2() throws Exception {
  322. Callable<PullResult> setup = new Callable<PullResult>() {
  323. public PullResult call() throws Exception {
  324. StoredConfig config = dbTarget.getConfig();
  325. config.setString("pull", null, "rebase", "preserve");
  326. config.setString("branch", "master", "rebase", "true");
  327. config.save();
  328. return target.pull().call();
  329. }
  330. };
  331. doTestPullWithRebase(setup, TestPullMode.REBASE);
  332. }
  333. @Test
  334. /** the branch-local config should be respected */
  335. public void testPullWithRebaseConfig3() throws Exception {
  336. Callable<PullResult> setup = new Callable<PullResult>() {
  337. public PullResult call() throws Exception {
  338. StoredConfig config = dbTarget.getConfig();
  339. config.setString("branch", "master", "rebase", "true");
  340. config.save();
  341. return target.pull().call();
  342. }
  343. };
  344. doTestPullWithRebase(setup, TestPullMode.REBASE);
  345. }
  346. @Test
  347. /** without config it should merge */
  348. public void testPullWithoutConfig() throws Exception {
  349. Callable<PullResult> setup = new Callable<PullResult>() {
  350. public PullResult call() throws Exception {
  351. return target.pull().call();
  352. }
  353. };
  354. doTestPullWithRebase(setup, TestPullMode.MERGE);
  355. }
  356. @Test
  357. /** the branch local config should win over the global config */
  358. public void testPullWithMergeConfig() throws Exception {
  359. Callable<PullResult> setup = new Callable<PullResult>() {
  360. public PullResult call() throws Exception {
  361. StoredConfig config = dbTarget.getConfig();
  362. config.setString("pull", null, "rebase", "true");
  363. config.setString("branch", "master", "rebase", "false");
  364. config.save();
  365. return target.pull().call();
  366. }
  367. };
  368. doTestPullWithRebase(setup, TestPullMode.MERGE);
  369. }
  370. @Test
  371. /** the branch local config should win over the global config */
  372. public void testPullWithMergeConfig2() throws Exception {
  373. Callable<PullResult> setup = new Callable<PullResult>() {
  374. public PullResult call() throws Exception {
  375. StoredConfig config = dbTarget.getConfig();
  376. config.setString("pull", null, "rebase", "false");
  377. config.save();
  378. return target.pull().call();
  379. }
  380. };
  381. doTestPullWithRebase(setup, TestPullMode.MERGE);
  382. }
  383. private void doTestPullWithRebase(Callable<PullResult> pullSetup,
  384. TestPullMode expectedPullMode) throws Exception {
  385. // simple upstream change
  386. writeToFile(sourceFile, "content");
  387. source.add().addFilepattern(sourceFile.getName()).call();
  388. RevCommit sourceCommit = source.commit().setMessage("source commit")
  389. .call();
  390. // create a merge commit in target
  391. File loxalFile = new File(dbTarget.getWorkTree(), "local.txt");
  392. writeToFile(loxalFile, "initial\n");
  393. target.add().addFilepattern("local.txt").call();
  394. RevCommit t1 = target.commit().setMessage("target commit 1").call();
  395. target.checkout().setCreateBranch(true).setName("side").call();
  396. String newContent = "initial\n" + "and more\n";
  397. writeToFile(loxalFile, newContent);
  398. target.add().addFilepattern("local.txt").call();
  399. RevCommit t2 = target.commit().setMessage("target commit 2").call();
  400. target.checkout().setName("master").call();
  401. MergeResult mergeResult = target.merge()
  402. .setFastForward(MergeCommand.FastForwardMode.NO_FF).include(t2)
  403. .call();
  404. assertEquals(MergeStatus.MERGED, mergeResult.getMergeStatus());
  405. assertFileContentsEqual(loxalFile, newContent);
  406. ObjectId merge = mergeResult.getNewHead();
  407. // pull
  408. PullResult res = pullSetup.call();
  409. assertNotNull(res.getFetchResult());
  410. if (expectedPullMode == TestPullMode.MERGE) {
  411. assertEquals(MergeStatus.MERGED, res.getMergeResult()
  412. .getMergeStatus());
  413. assertNull(res.getRebaseResult());
  414. } else {
  415. assertNull(res.getMergeResult());
  416. assertEquals(RebaseResult.OK_RESULT, res.getRebaseResult());
  417. }
  418. assertFileContentsEqual(sourceFile, "content");
  419. try (RevWalk rw = new RevWalk(dbTarget)) {
  420. rw.sort(RevSort.TOPO);
  421. rw.markStart(rw.parseCommit(dbTarget.resolve("refs/heads/master")));
  422. RevCommit next;
  423. if (expectedPullMode == TestPullMode.MERGE) {
  424. next = rw.next();
  425. assertEquals(2, next.getParentCount());
  426. assertEquals(merge, next.getParent(0));
  427. assertEquals(sourceCommit, next.getParent(1));
  428. // since both parents are known do no further checks here
  429. } else {
  430. if (expectedPullMode == TestPullMode.REBASE_PREASERVE) {
  431. next = rw.next();
  432. assertEquals(2, next.getParentCount());
  433. }
  434. next = rw.next();
  435. assertEquals(t2.getShortMessage(), next.getShortMessage());
  436. next = rw.next();
  437. assertEquals(t1.getShortMessage(), next.getShortMessage());
  438. next = rw.next();
  439. assertEquals(sourceCommit, next);
  440. next = rw.next();
  441. assertEquals("Initial commit for source",
  442. next.getShortMessage());
  443. next = rw.next();
  444. assertNull(next);
  445. }
  446. }
  447. }
  448. @Override
  449. @Before
  450. public void setUp() throws Exception {
  451. super.setUp();
  452. dbTarget = createWorkRepository();
  453. source = new Git(db);
  454. target = new Git(dbTarget);
  455. // put some file in the source repo
  456. sourceFile = new File(db.getWorkTree(), "SomeFile.txt");
  457. writeToFile(sourceFile, "Hello world");
  458. // and commit it
  459. source.add().addFilepattern("SomeFile.txt").call();
  460. source.commit().setMessage("Initial commit for source").call();
  461. // configure the target repo to connect to the source via "origin"
  462. StoredConfig targetConfig = dbTarget.getConfig();
  463. targetConfig.setString("branch", "master", "remote", "origin");
  464. targetConfig
  465. .setString("branch", "master", "merge", "refs/heads/master");
  466. RemoteConfig config = new RemoteConfig(targetConfig, "origin");
  467. config
  468. .addURI(new URIish(source.getRepository().getWorkTree()
  469. .getAbsolutePath()));
  470. config.addFetchRefSpec(new RefSpec(
  471. "+refs/heads/*:refs/remotes/origin/*"));
  472. config.update(targetConfig);
  473. targetConfig.save();
  474. targetFile = new File(dbTarget.getWorkTree(), "SomeFile.txt");
  475. // make sure we have the same content
  476. target.pull().call();
  477. assertFileContentsEqual(targetFile, "Hello world");
  478. }
  479. private static void writeToFile(File actFile, String string)
  480. throws IOException {
  481. FileOutputStream fos = null;
  482. try {
  483. fos = new FileOutputStream(actFile);
  484. fos.write(string.getBytes("UTF-8"));
  485. fos.close();
  486. } finally {
  487. if (fos != null)
  488. fos.close();
  489. }
  490. }
  491. private static void assertFileContentsEqual(File actFile, String string)
  492. throws IOException {
  493. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  494. FileInputStream fis = null;
  495. byte[] buffer = new byte[100];
  496. try {
  497. fis = new FileInputStream(actFile);
  498. int read = fis.read(buffer);
  499. while (read > 0) {
  500. bos.write(buffer, 0, read);
  501. read = fis.read(buffer);
  502. }
  503. String content = new String(bos.toByteArray(), "UTF-8");
  504. assertEquals(string, content);
  505. } finally {
  506. if (fis != null)
  507. fis.close();
  508. }
  509. }
  510. }