View Javadoc
1   /*
2    * Copyright (C) 2019, Thomas Wolf <thomas.wolf@paranor.ch>
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  
44  package org.eclipse.jgit.lib;
45  
46  import static org.junit.Assert.assertEquals;
47  import static org.junit.Assert.fail;
48  
49  import java.io.IOException;
50  import java.nio.file.Files;
51  import java.nio.file.Path;
52  import java.nio.file.Paths;
53  import java.util.Arrays;
54  import java.util.List;
55  
56  import org.eclipse.jgit.junit.RepositoryTestCase;
57  import org.junit.Test;
58  
59  public class RebaseTodoFileTest extends RepositoryTestCase {
60  
61  	private static final String TEST_TODO = "test.todo";
62  
63  	private void createTodoList(String... lines) throws IOException {
64  		Path p = Paths.get(db.getDirectory().getAbsolutePath(), TEST_TODO);
65  		Files.write(p, Arrays.asList(lines));
66  	}
67  
68  	@Test
69  	public void testReadTodoFile() throws Exception {
70  		String[] expected = { "reword " + ObjectId.zeroId().name() + " Foo",
71  				"# A comment in the todo list",
72  				"pick " + ObjectId.zeroId().name() + " Foo fie",
73  				"squash " + ObjectId.zeroId().name() + " F",
74  				"fixup " + ObjectId.zeroId().name(),
75  				"edit " + ObjectId.zeroId().name() + " f",
76  				"edit " + ObjectId.zeroId().name() + ' ' };
77  		createTodoList(expected);
78  		RebaseTodoFile todo = new RebaseTodoFile(db);
79  		List<RebaseTodoLine> lines = todo.readRebaseTodo(TEST_TODO, true);
80  		assertEquals("Expected 7 lines", 7, lines.size());
81  		int i = 0;
82  		for (RebaseTodoLine line : lines) {
83  			switch (i) {
84  			case 0:
85  				assertEquals("Expected REWORD", RebaseTodoLine.Action.REWORD,
86  						line.getAction());
87  				assertEquals("Unexpected ID", ObjectId.zeroId().abbreviate(40),
88  						line.getCommit());
89  				assertEquals("Unexpected Message", "Foo",
90  						line.getShortMessage());
91  				break;
92  			case 1:
93  				assertEquals("Expected COMMENT", RebaseTodoLine.Action.COMMENT,
94  						line.getAction());
95  				assertEquals("Unexpected Message",
96  						"# A comment in the todo list",
97  						line.getComment());
98  				break;
99  			case 2:
100 				assertEquals("Expected PICK", RebaseTodoLine.Action.PICK,
101 						line.getAction());
102 				assertEquals("Unexpected ID", ObjectId.zeroId().abbreviate(40),
103 						line.getCommit());
104 				assertEquals("Unexpected Message", "Foo fie",
105 						line.getShortMessage());
106 				break;
107 			case 3:
108 				assertEquals("Expected SQUASH", RebaseTodoLine.Action.SQUASH,
109 						line.getAction());
110 				assertEquals("Unexpected ID", ObjectId.zeroId().abbreviate(40),
111 						line.getCommit());
112 				assertEquals("Unexpected Message", "F", line.getShortMessage());
113 				break;
114 			case 4:
115 				assertEquals("Expected FIXUP", RebaseTodoLine.Action.FIXUP,
116 						line.getAction());
117 				assertEquals("Unexpected ID", ObjectId.zeroId().abbreviate(40),
118 						line.getCommit());
119 				assertEquals("Unexpected Message", "", line.getShortMessage());
120 				break;
121 			case 5:
122 				assertEquals("Expected EDIT", RebaseTodoLine.Action.EDIT,
123 						line.getAction());
124 				assertEquals("Unexpected ID", ObjectId.zeroId().abbreviate(40),
125 						line.getCommit());
126 				assertEquals("Unexpected Message", "f", line.getShortMessage());
127 				break;
128 			case 6:
129 				assertEquals("Expected EDIT", RebaseTodoLine.Action.EDIT,
130 						line.getAction());
131 				assertEquals("Unexpected ID", ObjectId.zeroId().abbreviate(40),
132 						line.getCommit());
133 				assertEquals("Unexpected Message", "", line.getShortMessage());
134 				break;
135 			default:
136 				fail("Too many lines");
137 				return;
138 			}
139 			i++;
140 		}
141 	}
142 }