Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 375406f971f9f92150ea223c2cb10b60fa092cec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.tests;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import junit.framework.TestCase;

import org.eclipse.compare.IStreamMerger;
import org.eclipse.compare.internal.merge.TextStreamMerger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;

public class StreamMergerTest extends TestCase {

	private static final String ABC= "abc"; //$NON-NLS-1$
	private static final String DEF= "def"; //$NON-NLS-1$
	private static final String BAR= "bar"; //$NON-NLS-1$
	private static final String FOO= "foo"; //$NON-NLS-1$
	private static final String XYZ= "xyz"; //$NON-NLS-1$
	private static final String _123= "123"; //$NON-NLS-1$
	private static final String _456= "456"; //$NON-NLS-1$

	String encoding= "UTF-8"; //$NON-NLS-1$
	static final String SEPARATOR= System.getProperty("line.separator"); //$NON-NLS-1$

	public StreamMergerTest(String name) {
		super(name);
	}

	public void testIncomingAddition() throws UnsupportedEncodingException {

		String a= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String t= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String o= ABC + SEPARATOR + DEF + SEPARATOR + _123 + SEPARATOR + XYZ;

		StringBuffer output= new StringBuffer();

		IStatus status= merge(output, a, t, o);

		assertEquals(status.getSeverity(), IStatus.OK);
		assertEquals(status.getCode(), IStatus.OK);
		assertEquals(output.toString(), ABC + SEPARATOR + DEF + SEPARATOR + _123 + SEPARATOR + XYZ + SEPARATOR);
	}

	public void testIncomingDeletion() throws UnsupportedEncodingException {

		String a= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String t= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String o= ABC + SEPARATOR + XYZ;

		StringBuffer output= new StringBuffer();

		IStatus status= merge(output, a, t, o);

		assertEquals(status.getSeverity(), IStatus.OK);
		assertEquals(status.getCode(), IStatus.OK);
		assertEquals(output.toString(), ABC + SEPARATOR + XYZ + SEPARATOR);
	}

	public void testIncomingReplacement() throws UnsupportedEncodingException {

		String a= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String t= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String o= ABC + SEPARATOR + _123 + SEPARATOR + XYZ;

		StringBuffer output= new StringBuffer();

		IStatus status= merge(output, a, t, o);

		assertEquals(status.getSeverity(), IStatus.OK);
		assertEquals(status.getCode(), IStatus.OK);
		assertEquals(output.toString(), ABC + SEPARATOR + _123 + SEPARATOR + XYZ + SEPARATOR);
	}

	public void testNonConflictingMerge() throws UnsupportedEncodingException {

		String a= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String t= ABC + SEPARATOR + DEF + SEPARATOR + XYZ + SEPARATOR + FOO;
		String o= ABC + SEPARATOR + _123 + SEPARATOR + _456 + SEPARATOR + XYZ;

		StringBuffer output= new StringBuffer();

		IStatus status= merge(output, a, t, o);

		assertEquals(status.getSeverity(), IStatus.OK);
		assertEquals(status.getCode(), IStatus.OK);
		assertEquals(output.toString(), ABC + SEPARATOR + _123 + SEPARATOR + _456 + SEPARATOR + XYZ + SEPARATOR + FOO + SEPARATOR);
	}

	public void testConflictingReplacement() throws UnsupportedEncodingException {

		String a= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String t= ABC + SEPARATOR + FOO + SEPARATOR + XYZ;
		String o= ABC + SEPARATOR + BAR + SEPARATOR + XYZ;

		StringBuffer output= new StringBuffer();

		IStatus status= merge(output, a, t, o);

		assertEquals(status.getSeverity(), IStatus.ERROR);
		assertEquals(status.getCode(), IStreamMerger.CONFLICT);
	}

	public void testConflictingAddition() throws UnsupportedEncodingException {

		String a= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String t= ABC + SEPARATOR + DEF + SEPARATOR + _123 + SEPARATOR + XYZ;
		String o= ABC + SEPARATOR + DEF + SEPARATOR + _123 + SEPARATOR + XYZ;

		StringBuffer output= new StringBuffer();

		IStatus status= merge(output, a, t, o);

		assertEquals(status.getSeverity(), IStatus.OK);
		assertEquals(status.getCode(), IStatus.OK);
		assertEquals(output.toString(), ABC + SEPARATOR + DEF + SEPARATOR + _123 + SEPARATOR + XYZ + SEPARATOR);
	}

	public void testConflictingDeletion() throws UnsupportedEncodingException {

		String a= ABC + SEPARATOR + DEF + SEPARATOR + XYZ;
		String t= ABC + SEPARATOR + XYZ;
		String o= ABC + SEPARATOR + XYZ;

		StringBuffer output= new StringBuffer();

		IStatus status= merge(output, a, t, o);

		assertEquals(status.getSeverity(), IStatus.OK);
		assertEquals(status.getCode(), IStatus.OK);
		assertEquals(output.toString(), ABC + SEPARATOR + XYZ + SEPARATOR);
	}

	private IStatus merge(StringBuffer output, String a, String m, String y) throws UnsupportedEncodingException {
		InputStream ancestor= new ByteArrayInputStream(a.getBytes(StandardCharsets.UTF_8));
		InputStream target= new ByteArrayInputStream(m.getBytes(StandardCharsets.UTF_8));
		InputStream other= new ByteArrayInputStream(y.getBytes(StandardCharsets.UTF_8));

		return merge(output, ancestor, target, other);
	}

	private IStatus merge(StringBuffer output, InputStream ancestor,
			InputStream target, InputStream other)
			throws UnsupportedEncodingException {
		ByteArrayOutputStream os= new ByteArrayOutputStream();

		IStreamMerger merger= new TextStreamMerger();
		IStatus status= merger.merge(os, encoding, ancestor, encoding, target, encoding, other, encoding, (IProgressMonitor) null);

		output.append(new String(os.toByteArray(), StandardCharsets.UTF_8));

		return status;
	}
}

Back to the top