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