Update README with note on official Oracle plugin
[nbgit.git] / test / unit / src / org / nbgit / client / IndexBuilderTest.java
blob789f5663486534b1b09ab0c00d9f8a5416403ad5
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2009 Jonas Fonseca <fonseca@diku.dk>
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html. See the License for the
12 * specific language governing permissions and limitations under the
13 * License. When distributing the software, include this License Header
14 * Notice in each file.
16 * This particular file is subject to the "Classpath" exception as provided
17 * by Sun in the GPL Version 2 section of the License file that
18 * accompanied this code. If applicable, add the following below the
19 * License Header, with the fields enclosed by brackets [] replaced by
20 * your own identifying information:
21 * "Portions Copyrighted [year] [name of copyright owner]"
23 * Contributor(s):
25 * If you wish your version of this file to be governed by only the CDDL
26 * or only the GPL Version 2, indicate your decision by adding
27 * "[Contributor] elects to include this software in this distribution
28 * under the [CDDL or GPL Version 2] license." If you do not indicate a
29 * single choice of license, a recipient has the option to distribute
30 * your version of this file under either the CDDL, the GPL Version 2 or
31 * to extend the choice of license to its licensees as provided above.
32 * However, if you add GPL Version 2 code and therefore, elected the GPL
33 * Version 2 license, then the option applies only if the new code is
34 * made subject to such option by the copyright holder.
36 package org.nbgit.client;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import org.nbgit.junit.RepositoryTestCase;
42 public class IndexBuilderTest extends RepositoryTestCase {
44 public IndexBuilderTest() {
45 super(IndexBuilderTest.class.getSimpleName());
48 public void testCreate() throws Exception {
49 assertNotNull(IndexBuilder.create(repository));
50 assertNotNull(IndexBuilder.create(workDir));
53 public void testWriteUnmodified() throws Exception {
54 copyFile(toGitDirFile("index"), toGitDirFile("index.orig"));
55 IndexBuilder.create(repository).write();
56 assertFile(toGitDirFile("index.orig"), toGitDirFile("index"));
59 public void testAdd() throws Exception {
60 IndexBuilder.create(repository).
61 add(toWorkDirFile("d")).
62 write();
63 compareIndexFiles();
66 public void testAddModified() throws Exception {
67 IndexBuilder.create(repository).
68 add(toWorkDirFile("d")).
69 write();
70 compareIndexFiles();
73 public void testAddSupportsExecutable() throws Exception {
74 IndexBuilder.create(repository).
75 add(toWorkDirFile("d.exe")).
76 write();
77 compareIndexFiles();
80 public void testAddModifiedSupportsExecutable() throws Exception {
81 IndexBuilder.create(repository).
82 add(toWorkDirFile("d.exe")).
83 write();
84 compareIndexFiles();
87 public void testAddAll() throws Exception {
88 IndexBuilder.create(repository).
89 addAll(toFiles(workDir, "b/e/f", "d", "g/h")).
90 write();
91 compareIndexFiles();
94 public void testAddAllUnordered() throws Exception {
95 IndexBuilder.create(repository).
96 addAll(toFiles(workDir, "2", "3", "1")).
97 write();
98 compareIndexFiles();
101 public void testDelete() throws Exception {
102 IndexBuilder.create(repository).
103 delete(toWorkDirFile("b/e/f")).
104 write();
105 compareIndexFiles();
108 public void testDeleteAll() throws Exception {
109 IndexBuilder.create(repository).
110 deleteAll(toFiles(workDir, "a", "b/e/f", "d")).
111 write();
112 compareIndexFiles();
115 public void testDeleteAllUnordered() throws Exception {
116 IndexBuilder.create(repository).
117 deleteAll(toFiles(workDir, "d", "b/e/f", "b/c")).
118 write();
119 compareIndexFiles();
122 public void testMove() throws Exception {
123 toWorkDirFile("b/e/f").renameTo(toWorkDirFile("f"));
124 IndexBuilder.create(repository).
125 move(toWorkDirFile("b/e/f"), toWorkDirFile("f")).
126 write();
127 compareIndexFiles();
130 public void testMoveSupportsExecutable() throws Exception {
131 toWorkDirFile("b/e/f").renameTo(toWorkDirFile("f.exe"));
132 setExecutable(toWorkDirFile("f.exe"), true);
133 IndexBuilder.create(repository).
134 move(toWorkDirFile("b/e/f"), toWorkDirFile("f.exe")).
135 write();
136 compareIndexFiles();
139 public void testLog() throws Exception {
140 String[] expectedMessages = {
141 "A add",
142 "M modified",
143 "D delete",
144 "R from -> to"
146 toWorkDirFile("add").createNewFile();
147 toWorkDirFile("to").createNewFile();
148 IndexBuilder.create(repository).
149 log(logger).
150 add(toWorkDirFile("add")).
151 add(toWorkDirFile("modified")).
152 delete(toWorkDirFile("delete")).
153 move(toWorkDirFile("from"), toWorkDirFile("to"));
154 assertEquals(expectedMessages.length, loggerMessages.size());
155 for (int i = 0; i < expectedMessages.length; i++)
156 assertEquals(expectedMessages[i], loggerMessages.get(i));
159 public void testAddNonExisting() throws Exception {
160 try {
161 IndexBuilder.create(repository).
162 add(toWorkDirFile("fail"));
163 fail("No exception thrown");
164 } catch (IOException error) {
165 assertErrorPath(error, "fail");
169 public void testAddAllNonExisting() throws Exception {
170 try {
171 IndexBuilder.create(repository).
172 addAll(toFiles(workDir, "fail/1", "fail/2"));
173 fail("No exception thrown");
174 } catch (IOException error) {
175 assertErrorPath(error, "fail/1");
179 public void testMoveDestinationNonExisting() throws Exception {
180 try {
181 IndexBuilder.create(repository).
182 move(toWorkDirFile("a"), toWorkDirFile("fail"));
183 fail("No exception thrown");
184 } catch (IOException error) {
185 assertErrorPath(error, "fail");
189 private void assertErrorPath(IOException error, String path) {
190 assertTrue(error.getMessage().startsWith(toWorkDirFile(path).getPath()));