View Javadoc
1   /*
2    * Copyright (C) 2012 Google Inc. 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  package org.eclipse.jgit.pgm;
11  
12  import static org.junit.Assert.assertArrayEquals;
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.fail;
16  
17  import org.eclipse.jgit.api.Git;
18  import org.eclipse.jgit.dircache.DirCache;
19  import org.eclipse.jgit.lib.CLIRepositoryTestCase;
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  public class AddTest extends CLIRepositoryTestCase {
24  	private Git git;
25  
26  	@Override
27  	@Before
28  	public void setUp() throws Exception {
29  		super.setUp();
30  		git = new Git(db);
31  	}
32  
33  	@Test
34  	public void testAddNothing() throws Exception {
35  		try {
36  			execute("git add");
37  			fail("Must die");
38  		} catch (Die e) {
39  			// expected, requires argument
40  		}
41  	}
42  
43  	@Test
44  	public void testAddUsage() throws Exception {
45  		execute("git add --help");
46  	}
47  
48  	@Test
49  	public void testAddAFile() throws Exception {
50  		writeTrashFile("greeting", "Hello, world!");
51  		assertArrayEquals(new String[] { "" }, //
52  				execute("git add greeting"));
53  
54  		DirCache cache = db.readDirCache();
55  		assertNotNull(cache.getEntry("greeting"));
56  		assertEquals(1, cache.getEntryCount());
57  	}
58  
59  	@Test
60  	public void testAddFileTwice() throws Exception {
61  		writeTrashFile("greeting", "Hello, world!");
62  		assertArrayEquals(new String[] { "" }, //
63  				execute("git add greeting greeting"));
64  
65  		DirCache cache = db.readDirCache();
66  		assertNotNull(cache.getEntry("greeting"));
67  		assertEquals(1, cache.getEntryCount());
68  	}
69  
70  	@Test
71  	public void testAddAlreadyAdded() throws Exception {
72  		writeTrashFile("greeting", "Hello, world!");
73  		git.add().addFilepattern("greeting").call();
74  		assertArrayEquals(new String[] { "" }, //
75  				execute("git add greeting"));
76  
77  		DirCache cache = db.readDirCache();
78  		assertNotNull(cache.getEntry("greeting"));
79  		assertEquals(1, cache.getEntryCount());
80  	}
81  }