View Javadoc
1   /*
2    * Copyright (C) 2011, Robin Rosenberg
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.dircache;
44  
45  import static org.junit.Assert.assertEquals;
46  
47  import java.util.ArrayList;
48  import java.util.List;
49  
50  import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
51  import org.eclipse.jgit.lib.FileMode;
52  import org.eclipse.jgit.lib.ObjectId;
53  import org.junit.Test;
54  
55  public class DirCachePathEditTest {
56  
57  	static final class AddEdit extends PathEdit {
58  
59  		public AddEdit(String entryPath) {
60  			super(entryPath);
61  		}
62  
63  		@Override
64  		public void apply(DirCacheEntry ent) {
65  			ent.setFileMode(FileMode.REGULAR_FILE);
66  			ent.setLength(1);
67  			ent.setObjectId(ObjectId.zeroId());
68  		}
69  
70  	}
71  
72  	private static final class RecordingEdit extends PathEdit {
73  		final List<DirCacheEntry> entries = new ArrayList<DirCacheEntry>();
74  
75  		public RecordingEdit(String entryPath) {
76  			super(entryPath);
77  		}
78  
79  		@Override
80  		public void apply(DirCacheEntry ent) {
81  			entries.add(ent);
82  		}
83  	}
84  
85  	@Test
86  	public void testAddDeletePathAndTreeNormalNames() {
87  		DirCache dc = DirCache.newInCore();
88  		DirCacheEditor editor = dc.editor();
89  		editor.add(new AddEdit("a"));
90  		editor.add(new AddEdit("b/c"));
91  		editor.add(new AddEdit("c/d"));
92  		editor.finish();
93  		assertEquals(3, dc.getEntryCount());
94  		assertEquals("a", dc.getEntry(0).getPathString());
95  		assertEquals("b/c", dc.getEntry(1).getPathString());
96  		assertEquals("c/d", dc.getEntry(2).getPathString());
97  
98  		editor = dc.editor();
99  		editor.add(new DirCacheEditor.DeletePath("b/c"));
100 		editor.finish();
101 		assertEquals(2, dc.getEntryCount());
102 		assertEquals("a", dc.getEntry(0).getPathString());
103 		assertEquals("c/d", dc.getEntry(1).getPathString());
104 
105 		editor = dc.editor();
106 		editor.add(new DirCacheEditor.DeleteTree(""));
107 		editor.finish();
108 		assertEquals(0, dc.getEntryCount());
109 	}
110 
111 	@Test
112 	public void testAddDeleteTrickyNames() {
113 		DirCache dc = DirCache.newInCore();
114 		DirCacheEditor editor = dc.editor();
115 		editor.add(new AddEdit("a/b"));
116 		editor.add(new AddEdit("a-"));
117 		editor.add(new AddEdit("ab"));
118 		editor.finish();
119 		assertEquals(3, dc.getEntryCount());
120 
121 		// Validate sort order
122 		assertEquals("a-", dc.getEntry(0).getPathString());
123 		assertEquals("a/b", dc.getEntry(1).getPathString());
124 		assertEquals("ab", dc.getEntry(2).getPathString());
125 
126 		editor = dc.editor();
127 
128 		// Sort order should not confuse DeleteTree
129 		editor.add(new DirCacheEditor.DeleteTree("a"));
130 		editor.finish();
131 		assertEquals(2, dc.getEntryCount());
132 		assertEquals("a-", dc.getEntry(0).getPathString());
133 		assertEquals("ab", dc.getEntry(1).getPathString());
134 	}
135 
136 	@Test
137 	public void testPathEditShouldBeCalledForEachStage() throws Exception {
138 		DirCache dc = DirCache.newInCore();
139 		DirCacheBuilder builder = new DirCacheBuilder(dc, 3);
140 		builder.add(createEntry("a", DirCacheEntry.STAGE_1));
141 		builder.add(createEntry("a", DirCacheEntry.STAGE_2));
142 		builder.add(createEntry("a", DirCacheEntry.STAGE_3));
143 		builder.finish();
144 
145 		DirCacheEditor editor = dc.editor();
146 		RecordingEdit recorder = new RecordingEdit("a");
147 		editor.add(recorder);
148 		editor.finish();
149 
150 		List<DirCacheEntry> entries = recorder.entries;
151 		assertEquals(3, entries.size());
152 		assertEquals(DirCacheEntry.STAGE_1, entries.get(0).getStage());
153 		assertEquals(DirCacheEntry.STAGE_2, entries.get(1).getStage());
154 		assertEquals(DirCacheEntry.STAGE_3, entries.get(2).getStage());
155 	}
156 
157 	private static DirCacheEntry createEntry(String path, int stage) {
158 		DirCacheEntry entry = new DirCacheEntry(path, stage);
159 		entry.setFileMode(FileMode.REGULAR_FILE);
160 		return entry;
161 	}
162 }