1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.lib;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.IOException;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.junit.Test;
26
27 public class RefDatabaseConflictingNamesTest {
28
29 private RefDatabase refDatabase = new RefDatabase() {
30 @Override
31 public Map<String, Ref> getRefs(String prefix) throws IOException {
32 if (ALL.equals(prefix)) {
33 Map<String, Ref> existing = new HashMap<>();
34 existing.put("refs/heads/a/b", null );
35 existing.put("refs/heads/q", null );
36 return existing;
37 }
38 return Collections.emptyMap();
39 }
40
41 @Override
42 public Ref peel(Ref ref) throws IOException {
43 return null;
44 }
45
46 @Override
47 public RefUpdate newUpdate(String name, boolean detach)
48 throws IOException {
49 return null;
50 }
51
52 @Override
53 public RefRename newRename(String fromName, String toName)
54 throws IOException {
55 return null;
56 }
57
58 @Override
59 public boolean isNameConflicting(String name) throws IOException {
60 return false;
61 }
62
63 @Override
64 public Ref exactRef(String name) throws IOException {
65 return null;
66 }
67
68 @Override
69 public List<Ref> getAdditionalRefs() throws IOException {
70 return null;
71 }
72
73 @Override
74 public void create() throws IOException {
75
76 }
77
78 @Override
79 public void close() {
80
81 }
82 };
83
84 @Test
85 public void testGetConflictingNames() throws IOException {
86
87 assertConflictingNames("refs", "refs/heads/a/b", "refs/heads/q");
88 assertConflictingNames("refs/heads", "refs/heads/a/b", "refs/heads/q");
89 assertConflictingNames("refs/heads/a", "refs/heads/a/b");
90
91
92 assertNoConflictingNames("refs/heads/a/b");
93
94
95 assertNoConflictingNames("refs/heads/a/d");
96 assertNoConflictingNames("refs/heads/master");
97
98
99 assertConflictingNames("refs/heads/a/b/c", "refs/heads/a/b");
100 assertConflictingNames("refs/heads/q/master", "refs/heads/q");
101 }
102
103 private void assertNoConflictingNames(String proposed) throws IOException {
104 assertTrue("expected conflicting names to be empty", refDatabase
105 .getConflictingNames(proposed).isEmpty());
106 }
107
108 private void assertConflictingNames(String proposed, String... conflicts)
109 throws IOException {
110 Set<String> expected = new HashSet<>(Arrays.asList(conflicts));
111 assertEquals(expected,
112 new HashSet<>(refDatabase.getConflictingNames(proposed)));
113 }
114 }