View Javadoc
1   /*
2    * Copyright (C) 2011, Christian Halstrick <christian.halstrick@sap.com>
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.api;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  import java.io.IOException;
51  
52  import org.eclipse.jgit.api.errors.GitAPIException;
53  import org.eclipse.jgit.api.errors.NoFilepatternException;
54  import org.eclipse.jgit.errors.NoWorkTreeException;
55  import org.eclipse.jgit.junit.RepositoryTestCase;
56  import org.eclipse.jgit.lib.Sets;
57  import org.junit.Test;
58  
59  public class StatusCommandTest extends RepositoryTestCase {
60  
61  	@Test
62  	public void testEmptyStatus() throws NoWorkTreeException,
63  			GitAPIException {
64  		Git git = new Git(db);
65  
66  		Status stat = git.status().call();
67  		assertEquals(0, stat.getAdded().size());
68  		assertEquals(0, stat.getChanged().size());
69  		assertEquals(0, stat.getMissing().size());
70  		assertEquals(0, stat.getModified().size());
71  		assertEquals(0, stat.getRemoved().size());
72  		assertEquals(0, stat.getUntracked().size());
73  	}
74  
75  	@Test
76  	public void testDifferentStates() throws IOException,
77  			NoFilepatternException, GitAPIException {
78  		Git git = new Git(db);
79  		writeTrashFile("a", "content of a");
80  		writeTrashFile("b", "content of b");
81  		writeTrashFile("c", "content of c");
82  		git.add().addFilepattern("a").addFilepattern("b").call();
83  		Status stat = git.status().call();
84  		assertEquals(Sets.of("a", "b"), stat.getAdded());
85  		assertEquals(0, stat.getChanged().size());
86  		assertEquals(0, stat.getMissing().size());
87  		assertEquals(0, stat.getModified().size());
88  		assertEquals(0, stat.getRemoved().size());
89  		assertEquals(Sets.of("c"), stat.getUntracked());
90  		git.commit().setMessage("initial").call();
91  
92  		writeTrashFile("a", "modified content of a");
93  		writeTrashFile("b", "modified content of b");
94  		writeTrashFile("d", "content of d");
95  		git.add().addFilepattern("a").addFilepattern("d").call();
96  		writeTrashFile("a", "again modified content of a");
97  		stat = git.status().call();
98  		assertEquals(Sets.of("d"), stat.getAdded());
99  		assertEquals(Sets.of("a"), stat.getChanged());
100 		assertEquals(0, stat.getMissing().size());
101 		assertEquals(Sets.of("b", "a"), stat.getModified());
102 		assertEquals(0, stat.getRemoved().size());
103 		assertEquals(Sets.of("c"), stat.getUntracked());
104 		git.add().addFilepattern(".").call();
105 		git.commit().setMessage("second").call();
106 
107 		stat = git.status().call();
108 		assertEquals(0, stat.getAdded().size());
109 		assertEquals(0, stat.getChanged().size());
110 		assertEquals(0, stat.getMissing().size());
111 		assertEquals(0, stat.getModified().size());
112 		assertEquals(0, stat.getRemoved().size());
113 		assertEquals(0, stat.getUntracked().size());
114 
115 		deleteTrashFile("a");
116 		assertFalse(new File(git.getRepository().getWorkTree(), "a").exists());
117 		git.add().addFilepattern("a").setUpdate(true).call();
118 		writeTrashFile("a", "recreated content of a");
119 		stat = git.status().call();
120 		assertEquals(0, stat.getAdded().size());
121 		assertEquals(0, stat.getChanged().size());
122 		assertEquals(0, stat.getMissing().size());
123 		assertEquals(0, stat.getModified().size());
124 		assertEquals(Sets.of("a"), stat.getRemoved());
125 		assertEquals(Sets.of("a"), stat.getUntracked());
126 		git.commit().setMessage("t").call();
127 
128 		writeTrashFile("sub/a", "sub-file");
129 		stat = git.status().call();
130 		assertEquals(1, stat.getUntrackedFolders().size());
131 		assertTrue(stat.getUntrackedFolders().contains("sub"));
132 	}
133 
134 	@Test
135 	public void testDifferentStatesWithPaths() throws IOException,
136 			NoFilepatternException, GitAPIException {
137 		Git git = new Git(db);
138 		writeTrashFile("a", "content of a");
139 		writeTrashFile("D/b", "content of b");
140 		writeTrashFile("D/c", "content of c");
141 		writeTrashFile("D/D/d", "content of d");
142 		git.add().addFilepattern(".").call();
143 
144 		writeTrashFile("a", "new content of a");
145 		writeTrashFile("D/b", "new content of b");
146 		writeTrashFile("D/D/d", "new content of d");
147 
148 
149 		// filter on an not existing path
150 		Status stat = git.status().addPath("x").call();
151 		assertEquals(0, stat.getModified().size());
152 
153 		// filter on an existing file
154 		stat = git.status().addPath("a").call();
155 		assertEquals(Sets.of("a"), stat.getModified());
156 
157 		// filter on an existing folder
158 		stat = git.status().addPath("D").call();
159 		assertEquals(Sets.of("D/b", "D/D/d"), stat.getModified());
160 
161 		// filter on an existing folder and file
162 		stat = git.status().addPath("D/D").addPath("a").call();
163 		assertEquals(Sets.of("a", "D/D/d"), stat.getModified());
164 
165 		// do not filter at all
166 		stat = git.status().call();
167 		assertEquals(Sets.of("a", "D/b", "D/D/d"), stat.getModified());
168 	}
169 }