Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.libs.git.jgit.commands; |
44 |
|
45 |
import java.io.File; |
46 |
import java.io.IOException; |
47 |
import java.util.Arrays; |
48 |
import java.util.Map; |
49 |
import org.eclipse.jgit.lib.Repository; |
50 |
import org.eclipse.jgit.transport.RemoteConfig; |
51 |
import org.eclipse.jgit.transport.URIish; |
52 |
import org.netbeans.libs.git.GitClient; |
53 |
import org.netbeans.libs.git.GitSubmoduleStatus; |
54 |
import org.netbeans.libs.git.jgit.AbstractGitTestCase; |
55 |
import org.netbeans.libs.git.jgit.Utils; |
56 |
|
57 |
/** |
58 |
* |
59 |
* @author Ondrej Vrabec |
60 |
*/ |
61 |
public class SubmoduleTest extends AbstractGitTestCase { |
62 |
|
63 |
private Repository repository; |
64 |
private Repository repositorySM1; |
65 |
private Repository repositorySM2; |
66 |
private File workDir; |
67 |
private File moduleRepo; |
68 |
private File submoduleRepo1; |
69 |
private File submoduleRepo2; |
70 |
private File submoduleFolder1; |
71 |
private File submoduleFolder2; |
72 |
private File f1; |
73 |
private File f2; |
74 |
private File f; |
75 |
|
76 |
public SubmoduleTest (String testName) throws IOException { |
77 |
super(testName); |
78 |
} |
79 |
|
80 |
@Override |
81 |
protected void setUp() throws Exception { |
82 |
super.setUp(); |
83 |
workDir = getWorkingDirectory(); |
84 |
repository = getRepository(getLocalGitRepository()); |
85 |
|
86 |
moduleRepo = new File(workDir.getParentFile(), "module"); |
87 |
GitClient client = getClient(moduleRepo); |
88 |
client.init(NULL_PROGRESS_MONITOR); |
89 |
submoduleRepo1 = new File(workDir.getParentFile(), "submodule1"); |
90 |
client = getClient(submoduleRepo1); |
91 |
client.init(NULL_PROGRESS_MONITOR); |
92 |
submoduleRepo2 = new File(workDir.getParentFile(), "submodule2"); |
93 |
client = getClient(submoduleRepo2); |
94 |
client.init(NULL_PROGRESS_MONITOR); |
95 |
|
96 |
client = getClient(workDir); |
97 |
f = new File(workDir, "f"); |
98 |
write(f, "init"); |
99 |
client.add(new File[] { f }, NULL_PROGRESS_MONITOR); |
100 |
client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR); |
101 |
RemoteConfig cfg = new RemoteConfig(repository.getConfig(), "origin"); |
102 |
cfg.addURI(new URIish(moduleRepo.toURI().toURL().toString())); |
103 |
cfg.update(repository.getConfig()); |
104 |
repository.getConfig().save(); |
105 |
client.push("origin", Arrays.asList("refs/heads/master:refs/heads/master"), |
106 |
Arrays.asList("+refs/heads/master:refs/remotes/origin/master"), NULL_PROGRESS_MONITOR); |
107 |
|
108 |
File submodules = new File(workDir, "submodules"); |
109 |
|
110 |
submoduleFolder1 = new File(submodules, "submodule1"); |
111 |
submoduleFolder1.mkdirs(); |
112 |
getClient(submoduleFolder1).init(NULL_PROGRESS_MONITOR); |
113 |
f1 = new File(submoduleFolder1, "file"); |
114 |
write(f1, "init"); |
115 |
getClient(submoduleFolder1).add(new File[] { f1 }, NULL_PROGRESS_MONITOR); |
116 |
getClient(submoduleFolder1).commit(new File[] { f1 }, "init SM1 commit", null, null, NULL_PROGRESS_MONITOR); |
117 |
repositorySM1 = getRepository(getClient(submoduleFolder1)); |
118 |
cfg = new RemoteConfig(repositorySM1.getConfig(), "origin"); |
119 |
cfg.addURI(new URIish(submoduleRepo1.toURI().toURL().toString())); |
120 |
cfg.update(repositorySM1.getConfig()); |
121 |
repositorySM1.getConfig().save(); |
122 |
getClient(submoduleFolder1).push("origin", Arrays.asList("refs/heads/master:refs/heads/master"), |
123 |
Arrays.asList("+refs/heads/master:refs/remotes/origin/master"), NULL_PROGRESS_MONITOR); |
124 |
|
125 |
|
126 |
submoduleFolder2 = new File(submodules, "submodule2"); |
127 |
submoduleFolder2.mkdirs(); |
128 |
getClient(submoduleFolder2).init(NULL_PROGRESS_MONITOR); |
129 |
f2 = new File(submoduleFolder2, "file"); |
130 |
write(f2, "init"); |
131 |
getClient(submoduleFolder2).add(new File[] { f2 }, NULL_PROGRESS_MONITOR); |
132 |
getClient(submoduleFolder2).commit(new File[] { f2 }, "init SM1 commit", null, null, NULL_PROGRESS_MONITOR); |
133 |
repositorySM2 = getRepository(getClient(submoduleFolder2)); |
134 |
cfg = new RemoteConfig(repositorySM2.getConfig(), "origin"); |
135 |
cfg.addURI(new URIish(submoduleRepo2.toURI().toURL().toString())); |
136 |
cfg.update(repositorySM2.getConfig()); |
137 |
repositorySM2.getConfig().save(); |
138 |
getClient(submoduleFolder2).push("origin", Arrays.asList("refs/heads/master:refs/heads/master"), |
139 |
Arrays.asList("+refs/heads/master:refs/remotes/origin/master"), NULL_PROGRESS_MONITOR); |
140 |
} |
141 |
|
142 |
public void testStatusEmpty () throws Exception { |
143 |
GitClient client = getClient(workDir); |
144 |
assertEquals(0, client.getSubmoduleStatus(new File[0], NULL_PROGRESS_MONITOR).size()); |
145 |
} |
146 |
|
147 |
public void testStatusUninitialized () throws Exception { |
148 |
prepareUninitializedWorkdir(); |
149 |
|
150 |
GitClient client = getClient(workDir); |
151 |
Map<File, GitSubmoduleStatus> status = client.getSubmoduleStatus(new File[] { f } , NULL_PROGRESS_MONITOR); |
152 |
assertEquals(0, status.size()); |
153 |
|
154 |
status = client.getSubmoduleStatus(new File[] { submoduleFolder1 } , NULL_PROGRESS_MONITOR); |
155 |
assertStatus(status.get(submoduleFolder1), GitSubmoduleStatus.StatusType.UNINITIALIZED, |
156 |
submoduleFolder1); |
157 |
|
158 |
status = client.getSubmoduleStatus(new File[] { submoduleFolder2 } , NULL_PROGRESS_MONITOR); |
159 |
assertStatus(status.get(submoduleFolder2), GitSubmoduleStatus.StatusType.UNINITIALIZED, |
160 |
submoduleFolder2); |
161 |
|
162 |
status = client.getSubmoduleStatus(new File[0], NULL_PROGRESS_MONITOR); |
163 |
assertStatus(status.get(submoduleFolder1), GitSubmoduleStatus.StatusType.UNINITIALIZED, |
164 |
submoduleFolder1); |
165 |
assertStatus(status.get(submoduleFolder2), GitSubmoduleStatus.StatusType.UNINITIALIZED, |
166 |
submoduleFolder2); |
167 |
} |
168 |
|
169 |
public void testInitialize () throws Exception { |
170 |
prepareUninitializedWorkdir(); |
171 |
|
172 |
GitClient client = getClient(workDir); |
173 |
Map<File, GitSubmoduleStatus> status = client.getSubmoduleStatus(new File[] { f } , NULL_PROGRESS_MONITOR); |
174 |
assertEquals(0, status.size()); |
175 |
|
176 |
status = client.getSubmoduleStatus(new File[] { submoduleFolder1 }, NULL_PROGRESS_MONITOR); |
177 |
assertStatus(status.get(submoduleFolder1), GitSubmoduleStatus.StatusType.UNINITIALIZED, |
178 |
submoduleFolder1); |
179 |
|
180 |
status = client.initializeSubmodules(new File[] { submoduleFolder1 }, NULL_PROGRESS_MONITOR); |
181 |
assertStatus(status.get(submoduleFolder1), GitSubmoduleStatus.StatusType.UNINITIALIZED, |
182 |
submoduleFolder1); |
183 |
} |
184 |
|
185 |
public void testUpdate () throws Exception { |
186 |
prepareUninitializedWorkdir(); |
187 |
|
188 |
GitClient client = getClient(workDir); |
189 |
Map<File, GitSubmoduleStatus> status = client.getSubmoduleStatus(new File[] { f } , NULL_PROGRESS_MONITOR); |
190 |
assertEquals(0, status.size()); |
191 |
|
192 |
status = client.initializeSubmodules(new File[] { submoduleFolder1 }, NULL_PROGRESS_MONITOR); |
193 |
assertStatus(status.get(submoduleFolder1), GitSubmoduleStatus.StatusType.UNINITIALIZED, |
194 |
submoduleFolder1); |
195 |
status = client.updateSubmodules(new File[] { submoduleFolder1 }, NULL_PROGRESS_MONITOR); |
196 |
assertStatus(status.get(submoduleFolder1), GitSubmoduleStatus.StatusType.INITIALIZED, |
197 |
submoduleFolder1); |
198 |
} |
199 |
|
200 |
private void prepareUninitializedWorkdir () throws Exception { |
201 |
File gmFile = new File(workDir, ".gitmodules"); |
202 |
gmFile.createNewFile(); |
203 |
write(gmFile, "[submodule \"submodules/submodule1\"]\n" + |
204 |
" path = submodules/submodule1\n" + |
205 |
" url = " + submoduleRepo1.toURI().toURL().toString() + "\n" + |
206 |
"[submodule \"submodules/submodule2\"]\n" + |
207 |
" path = submodules/submodule2\n" + |
208 |
" url = " + submoduleRepo2.toURI().toURL().toString() + "\n"); |
209 |
|
210 |
GitClient client = getClient(workDir); |
211 |
client.add(new File[] { gmFile, submoduleFolder1, submoduleFolder2 }, NULL_PROGRESS_MONITOR); |
212 |
client.commit(new File[0], "adding modules", null, null, NULL_PROGRESS_MONITOR); |
213 |
client.push("origin", Arrays.asList("refs/heads/master:refs/heads/master"), |
214 |
Arrays.asList("+refs/heads/master:refs/remotes/origin/master"), NULL_PROGRESS_MONITOR); |
215 |
|
216 |
Utils.deleteRecursively(submoduleFolder1); |
217 |
Utils.deleteRecursively(submoduleFolder2); |
218 |
assertFalse(submoduleFolder1.exists()); |
219 |
assertFalse(submoduleFolder2.exists()); |
220 |
client.reset("master", GitClient.ResetType.HARD, NULL_PROGRESS_MONITOR); |
221 |
assertTrue(submoduleFolder1.exists()); |
222 |
assertTrue(submoduleFolder2.exists()); |
223 |
} |
224 |
|
225 |
private void assertStatus (GitSubmoduleStatus status, GitSubmoduleStatus.StatusType statusKind, |
226 |
File submoduleRoot) { |
227 |
assertEquals(statusKind, status.getStatus()); |
228 |
assertEquals(submoduleRoot, status.getSubmoduleFolder()); |
229 |
} |
230 |
} |