View Javadoc
1   /*
2    * Copyright (C) 2010, Robin Stocker <robin@nibor.org>
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.merge;
44  
45  import static org.junit.Assert.assertEquals;
46  
47  import java.io.IOException;
48  import java.util.Arrays;
49  
50  import org.eclipse.jgit.lib.ObjectId;
51  import org.eclipse.jgit.lib.ObjectIdRef;
52  import org.eclipse.jgit.lib.Ref;
53  import org.eclipse.jgit.lib.Ref.Storage;
54  import org.eclipse.jgit.lib.RefUpdate;
55  import org.eclipse.jgit.lib.SymbolicRef;
56  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
57  import org.junit.Before;
58  import org.junit.Test;
59  
60  /**
61   * Test construction of merge message by {@link MergeMessageFormatter}.
62   */
63  public class MergeMessageFormatterTest extends SampleDataRepositoryTestCase {
64  
65  	private MergeMessageFormatter formatter;
66  
67  	@Override
68  	@Before
69  	public void setUp() throws Exception {
70  		super.setUp();
71  
72  		RefUpdate createRemoteRefA = db
73  				.updateRef("refs/remotes/origin/remote-a");
74  		createRemoteRefA.setNewObjectId(db.resolve("refs/heads/a"));
75  		createRemoteRefA.update();
76  
77  		RefUpdate createRemoteRefB = db
78  				.updateRef("refs/remotes/origin/remote-b");
79  		createRemoteRefB.setNewObjectId(db.resolve("refs/heads/b"));
80  		createRemoteRefB.update();
81  
82  		formatter = new MergeMessageFormatter();
83  	}
84  
85  	@Test
86  	public void testOneBranch() throws IOException {
87  		Ref a = db.exactRef("refs/heads/a");
88  		Ref master = db.exactRef("refs/heads/master");
89  		String message = formatter.format(Arrays.asList(a), master);
90  		assertEquals("Merge branch 'a'", message);
91  	}
92  
93  	@Test
94  	public void testTwoBranches() throws IOException {
95  		Ref a = db.exactRef("refs/heads/a");
96  		Ref b = db.exactRef("refs/heads/b");
97  		Ref master = db.exactRef("refs/heads/master");
98  		String message = formatter.format(Arrays.asList(a, b), master);
99  		assertEquals("Merge branches 'a' and 'b'", message);
100 	}
101 
102 	@Test
103 	public void testThreeBranches() throws IOException {
104 		Ref c = db.exactRef("refs/heads/c");
105 		Ref b = db.exactRef("refs/heads/b");
106 		Ref a = db.exactRef("refs/heads/a");
107 		Ref master = db.exactRef("refs/heads/master");
108 		String message = formatter.format(Arrays.asList(c, b, a), master);
109 		assertEquals("Merge branches 'c', 'b' and 'a'", message);
110 	}
111 
112 	@Test
113 	public void testRemoteBranch() throws Exception {
114 		Ref remoteA = db.exactRef("refs/remotes/origin/remote-a");
115 		Ref master = db.exactRef("refs/heads/master");
116 		String message = formatter.format(Arrays.asList(remoteA), master);
117 		assertEquals("Merge remote-tracking branch 'origin/remote-a'", message);
118 	}
119 
120 	@Test
121 	public void testMixed() throws IOException {
122 		Ref c = db.exactRef("refs/heads/c");
123 		Ref remoteA = db.exactRef("refs/remotes/origin/remote-a");
124 		Ref master = db.exactRef("refs/heads/master");
125 		String message = formatter.format(Arrays.asList(c, remoteA), master);
126 		assertEquals("Merge branch 'c', remote-tracking branch 'origin/remote-a'",
127 				message);
128 	}
129 
130 	@Test
131 	public void testTag() throws IOException {
132 		Ref tagA = db.exactRef("refs/tags/A");
133 		Ref master = db.exactRef("refs/heads/master");
134 		String message = formatter.format(Arrays.asList(tagA), master);
135 		assertEquals("Merge tag 'A'", message);
136 	}
137 
138 	@Test
139 	public void testCommit() throws IOException {
140 		ObjectId objectId = ObjectId
141 				.fromString("6db9c2ebf75590eef973081736730a9ea169a0c4");
142 		Ref commit = new ObjectIdRef.Unpeeled(Storage.LOOSE,
143 				objectId.getName(), objectId);
144 		Ref master = db.exactRef("refs/heads/master");
145 		String message = formatter.format(Arrays.asList(commit), master);
146 		assertEquals("Merge commit '6db9c2ebf75590eef973081736730a9ea169a0c4'",
147 				message);
148 	}
149 
150 	@Test
151 	public void testPullWithUri() throws IOException {
152 		String name = "branch 'test' of http://egit.eclipse.org/jgit.git";
153 		ObjectId objectId = ObjectId
154 				.fromString("6db9c2ebf75590eef973081736730a9ea169a0c4");
155 		Ref remoteBranch = new ObjectIdRef.Unpeeled(Storage.LOOSE, name,
156 				objectId);
157 		Ref master = db.exactRef("refs/heads/master");
158 		String message = formatter.format(Arrays.asList(remoteBranch), master);
159 		assertEquals("Merge branch 'test' of http://egit.eclipse.org/jgit.git",
160 				message);
161 	}
162 
163 	@Test
164 	public void testIntoOtherThanMaster() throws IOException {
165 		Ref a = db.exactRef("refs/heads/a");
166 		Ref b = db.exactRef("refs/heads/b");
167 		String message = formatter.format(Arrays.asList(a), b);
168 		assertEquals("Merge branch 'a' into b", message);
169 	}
170 
171 	@Test
172 	public void testIntoHeadOtherThanMaster() throws IOException {
173 		Ref a = db.exactRef("refs/heads/a");
174 		Ref b = db.exactRef("refs/heads/b");
175 		SymbolicRef head = new SymbolicRef("HEAD", b);
176 		String message = formatter.format(Arrays.asList(a), head);
177 		assertEquals("Merge branch 'a' into b", message);
178 	}
179 
180 	@Test
181 	public void testIntoSymbolicRefHeadPointingToMaster() throws IOException {
182 		Ref a = db.exactRef("refs/heads/a");
183 		Ref master = db.exactRef("refs/heads/master");
184 		SymbolicRef head = new SymbolicRef("HEAD", master);
185 		String message = formatter.format(Arrays.asList(a), head);
186 		assertEquals("Merge branch 'a'", message);
187 	}
188 
189 	@Test
190 	public void testFormatWithConflictsNoFooter() {
191 		String originalMessage = "Header Line\n\nCommit body\n";
192 		String message = formatter.formatWithConflicts(originalMessage,
193 				Arrays.asList(new String[] { "path1" }));
194 		assertEquals("Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n",
195 				message);
196 	}
197 
198 	@Test
199 	public void testFormatWithConflictsNoFooterNoLineBreak() {
200 		String originalMessage = "Header Line\n\nCommit body";
201 		String message = formatter.formatWithConflicts(originalMessage,
202 				Arrays.asList(new String[] { "path1" }));
203 		assertEquals("Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n",
204 				message);
205 	}
206 
207 	@Test
208 	public void testFormatWithConflictsWithFooters() {
209 		String originalMessage = "Header Line\n\nCommit body\n\nChangeId:"
210 				+ " I123456789123456789123456789123456789\nBug:1234567\n";
211 		String message = formatter.formatWithConflicts(originalMessage,
212 				Arrays.asList(new String[] { "path1" }));
213 		assertEquals(
214 				"Header Line\n\nCommit body\n\nConflicts:\n\tpath1\n\n"
215 						+ "ChangeId: I123456789123456789123456789123456789\nBug:1234567\n",
216 				message);
217 	}
218 
219 	@Test
220 	public void testFormatWithConflictsWithFooterlikeLineInBody() {
221 		String originalMessage = "Header Line\n\nCommit body\nBug:1234567\nMore Body\n\nChangeId:"
222 				+ " I123456789123456789123456789123456789\nBug:1234567\n";
223 		String message = formatter.formatWithConflicts(originalMessage,
224 				Arrays.asList(new String[] { "path1" }));
225 		assertEquals(
226 				"Header Line\n\nCommit body\nBug:1234567\nMore Body\n\nConflicts:\n\tpath1\n\n"
227 						+ "ChangeId: I123456789123456789123456789123456789\nBug:1234567\n",
228 				message);
229 	}
230 }