comparison test/org/tmatesoft/hg/test/TestFileFlags.java @ 477:9c9d09111aee

Tests for file flags(exec, link, regular)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 12 Jul 2012 18:07:51 +0200
parents
children 59b7c817bc4d
comparison
equal deleted inserted replaced
476:c02b5710d9ac 477:9c9d09111aee
1 /*
2 * Copyright (c) 2012 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.test;
18
19 import static org.junit.Assert.*;
20 import static org.tmatesoft.hg.repo.HgRepository.TIP;
21
22 import java.io.File;
23
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.tmatesoft.hg.internal.Internals;
29 import org.tmatesoft.hg.internal.RelativePathRewrite;
30 import org.tmatesoft.hg.repo.HgDataFile;
31 import org.tmatesoft.hg.repo.HgInternals;
32 import org.tmatesoft.hg.repo.HgManifest.Flags;
33 import org.tmatesoft.hg.repo.HgRepository;
34 import org.tmatesoft.hg.util.FileInfo;
35 import org.tmatesoft.hg.util.FileWalker;
36 import org.tmatesoft.hg.util.Path;
37 import org.tmatesoft.hg.util.PathRewrite;
38
39 /**
40 * {junit-test-repos}/test-flags/
41 *
42 * <p>Node, JAR can't keep symlinks. Solution would be to keep
43 * repo without WC and perform an `hg co -C` before use if we
44 * need files from working copy.
45 *
46 * @author Artem Tikhomirov
47 * @author TMate Software Ltd.
48 */
49 public class TestFileFlags {
50 @Rule
51 public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
52
53 private HgRepository repo;
54
55 @Test
56 public void testFlagsInManifest() {
57 HgDataFile link = repo.getFileNode("file-link");
58 HgDataFile exec = repo.getFileNode("file-exec");
59 HgDataFile file = repo.getFileNode("regular-file");
60 assertEquals(Flags.Link, link.getFlags(TIP));
61 assertEquals(Flags.Exec, exec.getFlags(TIP));
62 assertEquals(Flags.RegularFile, file.getFlags(TIP));
63 }
64
65 @Test
66 public void testFlagsInWorkingCopy() throws Exception {
67 File repoRoot = repo.getWorkingDir();
68 Path.Source pathSrc = new Path.SimpleSource(new PathRewrite.Composite(new RelativePathRewrite(repoRoot), repo.getToRepoPathHelper()));
69 FileWalker fw = new FileWalker(HgInternals.getContext(repo), repoRoot, pathSrc);
70
71 if (Internals.runningOnWindows()) {
72 System.out.println("Executing tests on Windows, no actual file flags in working area are checked");
73 assertFalse(fw.supportsExecFlag());
74 assertFalse(fw.supportsLinkFlag());
75 return;
76 } else {
77 assertTrue(fw.supportsExecFlag());
78 assertTrue(fw.supportsLinkFlag());
79 }
80 ExecHelper eh = new ExecHelper(new OutputParser.Stub(true), repo.getWorkingDir());
81 eh.run("hg", "checkout", "-C");
82
83 boolean exec, link, file;
84 exec = link = file = false;
85 while (fw.hasNext()) {
86 fw.next();
87 FileInfo fi = fw.file();
88 String fn = fw.name().toString();
89 if (fn.equals("file-link")) {
90 link = true;
91 errorCollector.assertTrue("Symlink shall exist despite the fact it points to nowhere", fi.exists());
92 errorCollector.assertFalse(fi.isExecutable());
93 errorCollector.assertTrue(fi.isSymlink());
94 } else if (fn.equals("file-exec")) {
95 exec = true;
96 errorCollector.assertTrue(fi.isExecutable());
97 errorCollector.assertFalse(fi.isSymlink());
98 } else if (fn.equals("regular-file")) {
99 file = true;
100 errorCollector.assertFalse(fi.isExecutable());
101 errorCollector.assertFalse(fi.isSymlink());
102 }
103 }
104 errorCollector.assertTrue("Missing executable file in WC", exec);
105 errorCollector.assertTrue("Missing symlink in WC", link);
106 errorCollector.assertTrue("Missing regular file in WC", file);
107 }
108
109 @Before
110 public void assignRepo() throws Exception {
111 repo = Configuration.get().find("test-flags");
112 }
113
114 @After
115 public void cleanFiles() {
116 File link = new File(repo.getWorkingDir(), "file-link");
117 File exec = new File(repo.getWorkingDir(), "file-exec");
118 File file = new File(repo.getWorkingDir(), "regular-file");
119 if (link.exists()) {
120 link.deleteOnExit();
121 }
122 if (exec.exists()) {
123 exec.deleteOnExit();
124 }
125 if (file.exists()) {
126 file.deleteOnExit();
127 }
128 }
129 }