View Javadoc
1   /*
2    * Copyright (C) 2013, Robin Stocker <robin@nibor.org> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
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 /* not used */);
35  				existing.put("refs/heads/q", null /* not used */);
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  			// Not needed
76  		}
77  
78  		@Override
79  		public void close() {
80  			// Not needed
81  		}
82  	};
83  
84  	@Test
85  	public void testGetConflictingNames() throws IOException {
86  		// new references cannot replace an existing container
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  		// existing reference is not conflicting
92  		assertNoConflictingNames("refs/heads/a/b");
93  
94  		// new references are not conflicting
95  		assertNoConflictingNames("refs/heads/a/d");
96  		assertNoConflictingNames("refs/heads/master");
97  
98  		// existing reference must not be used as a container
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 }