Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3e3884e09824b11d5e92c99f07ee8a16a3cf5371 (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
/*
 * Created on Apr 12, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.eclipse.compare.tests;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

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 {

    String encoding= "UTF-8"; //$NON-NLS-1$

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

	public void testIncomingAddition() throws UnsupportedEncodingException {
	    
        String a= "abc\ndef\nxyz"; //$NON-NLS-1$
        String t= "abc\ndef\nxyz"; //$NON-NLS-1$
        String o= "abc\ndef\n123\nxyz"; //$NON-NLS-1$
        
        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\ndef\n123\nxyz\n"); //$NON-NLS-1$
	}
	
	public void testIncomingDeletion() throws UnsupportedEncodingException {
	    
        String a= "abc\ndef\nxyz"; //$NON-NLS-1$
        String t= "abc\ndef\nxyz"; //$NON-NLS-1$
        String o= "abc\nxyz"; //$NON-NLS-1$
        
        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\nxyz\n"); //$NON-NLS-1$
	}
	
	public void testIncomingReplacement() throws UnsupportedEncodingException {
	    
        String a= "abc\ndef\nxyz"; //$NON-NLS-1$
        String t= "abc\ndef\nxyz"; //$NON-NLS-1$
        String o= "abc\n123\nxyz"; //$NON-NLS-1$
        
        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\n123\nxyz\n"); //$NON-NLS-1$
	}
	
	public void testNonConflictingMerge() throws UnsupportedEncodingException {
	    
        String a= "abc\ndef\nxyz"; //$NON-NLS-1$
        String t= "abc\ndef\nxyz\nfoo"; //$NON-NLS-1$
        String o= "abc\n123\n456\nxyz"; //$NON-NLS-1$
        
        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\n123\n456\nxyz\nfoo\n"); //$NON-NLS-1$
	}
	
	public void testConflictingReplacement() throws UnsupportedEncodingException {
	    
        String a= "abc\ndef\nxyz"; //$NON-NLS-1$
        String t= "abc\nfoo\nxyz"; //$NON-NLS-1$
        String o= "abc\nbar\nxyz"; //$NON-NLS-1$

        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\ndef\nxyz"; //$NON-NLS-1$
        String t= "abc\ndef\n123\nxyz"; //$NON-NLS-1$
        String o= "abc\ndef\n123\nxyz"; //$NON-NLS-1$

        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\ndef\n123\nxyz\n"); //$NON-NLS-1$
	}
	
	public void testConflictingDeletion() throws UnsupportedEncodingException {
	    
        String a= "abc\ndef\nxyz"; //$NON-NLS-1$
        String t= "abc\nxyz"; //$NON-NLS-1$
        String o= "abc\nxyz"; //$NON-NLS-1$

        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\nxyz\n"); //$NON-NLS-1$
	}
	
	private IStatus merge(StringBuffer output, String a, String m, String y) throws UnsupportedEncodingException {
        InputStream ancestor= new ByteArrayInputStream(a.getBytes(encoding));
        InputStream target= new ByteArrayInputStream(m.getBytes(encoding));
        InputStream other= new ByteArrayInputStream(y.getBytes(encoding));
        
        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(), encoding));
 
        return status;
	}
}

Back to the top