View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.diff;
46  
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.assertSame;
50  import static org.junit.Assert.assertTrue;
51  
52  import org.junit.Test;
53  
54  public class EditTest {
55  	@Test
56  	public void testCreate() {
57  		final Edit e = new Edit(1, 2, 3, 4);
58  		assertEquals(1, e.getBeginA());
59  		assertEquals(2, e.getEndA());
60  		assertEquals(3, e.getBeginB());
61  		assertEquals(4, e.getEndB());
62  	}
63  
64  	@Test
65  	public void testCreateEmpty() {
66  		final Edit e = new Edit(1, 3);
67  		assertEquals(1, e.getBeginA());
68  		assertEquals(1, e.getEndA());
69  		assertEquals(3, e.getBeginB());
70  		assertEquals(3, e.getEndB());
71  		assertTrue("is empty", e.isEmpty());
72  		assertSame(Edit.Type.EMPTY, e.getType());
73  	}
74  
75  	@Test
76  	public void testSwap() {
77  		final Edit e = new Edit(1, 2, 3, 4);
78  		e.swap();
79  		assertEquals(3, e.getBeginA());
80  		assertEquals(4, e.getEndA());
81  		assertEquals(1, e.getBeginB());
82  		assertEquals(2, e.getEndB());
83  	}
84  
85  	@Test
86  	public void testType_Insert() {
87  		final Edit e = new Edit(1, 1, 1, 2);
88  		assertSame(Edit.Type.INSERT, e.getType());
89  		assertFalse("not empty", e.isEmpty());
90  		assertEquals(0, e.getLengthA());
91  		assertEquals(1, e.getLengthB());
92  	}
93  
94  	@Test
95  	public void testType_Delete() {
96  		final Edit e = new Edit(1, 2, 1, 1);
97  		assertSame(Edit.Type.DELETE, e.getType());
98  		assertFalse("not empty", e.isEmpty());
99  		assertEquals(1, e.getLengthA());
100 		assertEquals(0, e.getLengthB());
101 	}
102 
103 	@Test
104 	public void testType_Replace() {
105 		final Edit e = new Edit(1, 2, 1, 4);
106 		assertSame(Edit.Type.REPLACE, e.getType());
107 		assertFalse("not empty", e.isEmpty());
108 		assertEquals(1, e.getLengthA());
109 		assertEquals(3, e.getLengthB());
110 	}
111 
112 	@Test
113 	public void testType_Empty() {
114 		final Edit e = new Edit(1, 1, 2, 2);
115 		assertSame(Edit.Type.EMPTY, e.getType());
116 		assertSame(Edit.Type.EMPTY, new Edit(1, 2).getType());
117 		assertTrue("is empty", e.isEmpty());
118 		assertEquals(0, e.getLengthA());
119 		assertEquals(0, e.getLengthB());
120 	}
121 
122 	@Test
123 	public void testToString() {
124 		final Edit e = new Edit(1, 2, 1, 4);
125 		assertEquals("REPLACE(1-2,1-4)", e.toString());
126 	}
127 
128 	@SuppressWarnings("unlikely-arg-type")
129 	@Test
130 	public void testEquals1() {
131 		final Edit e1 = new Edit(1, 2, 3, 4);
132 		final Edit e2 = new Edit(1, 2, 3, 4);
133 
134 		assertEquals(e1, e1);
135 		assertEquals(e2, e1);
136 		assertEquals(e1, e2);
137 		assertEquals(e1.hashCode(), e2.hashCode());
138 		assertFalse(e1.equals(""));
139 	}
140 
141 	@Test
142 	public void testNotEquals1() {
143 		assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(0, 2, 3, 4)));
144 	}
145 
146 	@Test
147 	public void testNotEquals2() {
148 		assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 0, 3, 4)));
149 	}
150 
151 	@Test
152 	public void testNotEquals3() {
153 		assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 2, 0, 4)));
154 	}
155 
156 	@Test
157 	public void testNotEquals4() {
158 		assertFalse(new Edit(1, 2, 3, 4).equals(new Edit(1, 2, 3, 0)));
159 	}
160 
161 	@Test
162 	public void testExtendA() {
163 		final Edit e = new Edit(1, 2, 1, 1);
164 
165 		e.extendA();
166 		assertEquals(new Edit(1, 3, 1, 1), e);
167 
168 		e.extendA();
169 		assertEquals(new Edit(1, 4, 1, 1), e);
170 	}
171 
172 	@Test
173 	public void testExtendB() {
174 		final Edit e = new Edit(1, 2, 1, 1);
175 
176 		e.extendB();
177 		assertEquals(new Edit(1, 2, 1, 2), e);
178 
179 		e.extendB();
180 		assertEquals(new Edit(1, 2, 1, 3), e);
181 	}
182 
183 	@Test
184 	public void testBeforeAfterCuts() {
185 		final Edit whole = new Edit(1, 8, 2, 9);
186 		final Edit mid = new Edit(4, 5, 3, 6);
187 
188 		assertEquals(new Edit(1, 4, 2, 3), whole.before(mid));
189 		assertEquals(new Edit(5, 8, 6, 9), whole.after(mid));
190 	}
191 }