Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 644c53f65d4eb2ac1d89a06141b4cba5f16a1e8b (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*******************************************************************************
 * Copyright (c) 2005, 2007 Wind River Systems, Inc.
 * 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: 
 * Markus Schorn - initial API and implementation 
 ******************************************************************************/ 

package org.eclipse.cdt.ui.tests.refactoring.rename;

import java.io.StringWriter;

import org.eclipse.cdt.core.tests.BaseTestFramework;
import org.eclipse.cdt.internal.core.dom.SavedCodeReaderFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
import org.eclipse.ltk.core.refactoring.TextEditChangeGroup;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.search.internal.core.text.FileCharSequenceProvider;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.text.edits.TextEditGroup;


/**
 * @author markus.schorn@windriver.com
 */
public class RefactoringTests extends BaseTestFramework {
    private int fBufferSize;

    public RefactoringTests() {
    }

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

    protected void setUp() throws Exception {
        super.setUp();
        fBufferSize= FileCharSequenceProvider.BUFFER_SIZE;
        FileCharSequenceProvider.BUFFER_SIZE= 1024*4;
    }

    protected void tearDown() throws Exception {
        super.tearDown();
        SavedCodeReaderFactory.getInstance().getCodeReaderCache().flush();
        FileCharSequenceProvider.BUFFER_SIZE= fBufferSize;
    }

    protected void assertTotalChanges(int numChanges, Change changes) throws Exception {
        assertTotalChanges(numChanges, 0, 0, changes);
    }

    protected void assertTotalChanges(int numChanges, int potChanges, int commentCh, 
            Change changes) throws Exception {
        int count[]= {0,0,0};
        if( changes != null ) {
            countChanges( changes, count);
        }
        assertEquals( numChanges, count[0] );
        assertEquals("potential changes: ", potChanges, count[1]); //$NON-NLS-1$
        assertEquals("comment changes: ", commentCh, count[2]); //$NON-NLS-1$
    }

    private void countChanges(Change change, int[] count) {
        if( change instanceof CompositeChange ){
            Change [] children = ((CompositeChange) change).getChildren();
            for( int i = 0; i < children.length; i++ ){
                countChanges( children[i], count);
            }
        } else if( change instanceof TextFileChange ){
            TextFileChange tfc= (TextFileChange) change;
            TextEditChangeGroup[] tecgs= tfc.getTextEditChangeGroups();
            for (int i = 0; i < tecgs.length; i++) {
                TextEditChangeGroup group = tecgs[i];
                countChanges(group, count);
            }
        }
    }

    private void countChanges(TextEditChangeGroup edit, int[] count) {
        String name= edit.getName();
        if (name.indexOf("potential") != -1) { //$NON-NLS-1$
            count[1]++;
        }
        else if (name.indexOf("comment") != -1) { //$NON-NLS-1$
            count[2]++;
        }
        else {
            count[0]++;
        }
    }

    protected void assertChange(Change changes, IFile file, int startOffset, int numChars, String newText) throws Exception {
        assertChange(changes, file, startOffset, numChars, newText, false);
    }

    protected void assertChange(Change changes, IFile file, int startOffset, int numChars, String newText, boolean potential) throws Exception {
        boolean found = false;
        if( changes != null && changes instanceof CompositeChange ){
            found = checkCompositeChange( (CompositeChange) changes, file, startOffset, numChars, newText, potential);
        }
        
        if( !found ) {
            fail ("Rename at offset " + startOffset + " in \"" + file.getLocation() + "\" not found.");  //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
            assertFalse( true );
        }
    }

    private boolean checkCompositeChange(CompositeChange composite, IFile file, int startOffset, int numChars, String newText, boolean potential) {
        boolean found = false;
        Change [] children = composite.getChildren();
        for( int i = 0; i < children.length; i++ ){
            if( children[i] instanceof CompositeChange )
                found = checkCompositeChange( (CompositeChange) children[i], file, startOffset, numChars, newText, potential);
            else if( children[i] instanceof TextFileChange ){
                TextFileChange tuChange = (TextFileChange) children[i];
                if( tuChange.getFile().toString().equals( file.toString() ) ){
                    found = checkTranslationUnitChange( tuChange, startOffset, numChars, newText, potential );
                }
            }
            if( found )
                return found;
        }
        return found;
    }

    private boolean checkTranslationUnitChange(TextFileChange change, int startOffset, int numChars, String newText, boolean potential) {
        TextEditChangeGroup[] groups= change.getTextEditChangeGroups();
        for (int i = 0; i < groups.length; i++) {
            TextEditGroup group = groups[i].getTextEditGroup();
            if ((group.getName().indexOf("potential") != -1) == potential) {  //$NON-NLS-1$
                TextEdit[] edits= group.getTextEdits();
                if (checkTextEdits(edits, startOffset, numChars, newText)) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean checkTextEdit(TextEdit edit, int startOffset, int numChars, String newText) {
        if (edit instanceof MultiTextEdit) {
            if (checkTextEdits(((MultiTextEdit) edit).getChildren(), 
                    startOffset, numChars, newText)) {
                return true;
            }
        }
        else if (edit instanceof ReplaceEdit) {
            if (checkReplaceEdit((ReplaceEdit) edit, startOffset, numChars, newText ) ) {
                return true;
            }
        }
        return false;
    }

    private boolean checkTextEdits(TextEdit[] edits, int startOffset, int numChars, String newText) {
        for (int i = 0; i < edits.length; i++) {
            TextEdit edit = edits[i];
            if (checkTextEdit(edit, startOffset, numChars, newText)) {
                return true;
            }
        }
        return false;
    }

    private boolean checkReplaceEdit(ReplaceEdit edit, int startOffset, int numChars, String newText) {
        return ( edit.getOffset() == startOffset && edit.getLength() == numChars && edit.getText().equals( newText ) );
    }
    
    protected IFile createCppFwdDecls(String fileName) throws Exception {
        StringWriter writer = new StringWriter();
        writer.write( "class class_fwd;         \n"); //$NON-NLS-1$
        writer.write( "struct struct_fwd;       \n"); //$NON-NLS-1$
        writer.write( "union union_fwd;         \n"); //$NON-NLS-1$
        writer.write( "int func_proto();        \n"); //$NON-NLS-1$
        writer.write( "int func_proto_ov();     \n"); //$NON-NLS-1$
        writer.write( "int func_proto_ov(int);  \n"); //$NON-NLS-1$
        writer.write( "int func_proto_ov(int*); \n"); //$NON-NLS-1$
        writer.write( "extern int extern_var;   \n"); //$NON-NLS-1$
        String contents = writer.toString();
        return importFile(fileName, contents ); 
    }

    protected IFile createCFwdDecls(String fileName) throws Exception {
        StringWriter writer = new StringWriter();
        writer.write( "struct struct_fwd;       \n"); //$NON-NLS-1$
        writer.write( "union union_fwd;         \n"); //$NON-NLS-1$
        writer.write( "int func_proto();        \n"); //$NON-NLS-1$
        writer.write( "extern int extern_var;   \n"); //$NON-NLS-1$
        String contents = writer.toString();
        return importFile(fileName, contents ); 
    }

    protected IFile createCppDefs(String fileName) throws Exception {
        StringWriter writer = new StringWriter();
        writer.write( "class class_def {               \n"); //$NON-NLS-1$
        writer.write( "public:                         \n"); //$NON-NLS-1$
        writer.write( "   int member;                  \n"); //$NON-NLS-1$
        writer.write( "   static int static_member;    \n"); //$NON-NLS-1$
        writer.write( "   void method(int par);        \n"); //$NON-NLS-1$
        writer.write( "   void static_method(int par); \n"); //$NON-NLS-1$
        writer.write( "   int method_ov();             \n"); //$NON-NLS-1$
        writer.write( "   int method_ov(int);          \n"); //$NON-NLS-1$
        writer.write( "   int method_ov(int*);         \n"); //$NON-NLS-1$
        writer.write( "};                              \n"); //$NON-NLS-1$
        writer.write( "struct struct_def {        \n"); //$NON-NLS-1$
        writer.write( "   int st_member;          \n"); //$NON-NLS-1$
        writer.write( "};                         \n"); //$NON-NLS-1$
        writer.write( "union union_def {          \n"); //$NON-NLS-1$
        writer.write( "   int un_member;          \n"); //$NON-NLS-1$
        writer.write( "};                         \n"); //$NON-NLS-1$
        writer.write( "typedef int typedef_def;   \n"); //$NON-NLS-1$
        writer.write( "namespace namespace_def{}; \n"); //$NON-NLS-1$
        writer.write( "enum enum_def {            \n"); //$NON-NLS-1$
        writer.write( "   enum_item };            \n"); //$NON-NLS-1$
        writer.write( "int func_def() {}          \n"); //$NON-NLS-1$
        writer.write( "int func_def_ov() {}       \n"); //$NON-NLS-1$
        writer.write( "int func_def_ov(int){}     \n"); //$NON-NLS-1$
        writer.write( "int func_def_ov(int*){}    \n"); //$NON-NLS-1$
        writer.write( "int var_def;               \n"); //$NON-NLS-1$
        String contents = writer.toString();
        return importFile(fileName, contents ); 
    }

    protected IFile createCDefs(String fileName) throws Exception {
        StringWriter writer = new StringWriter();
        writer.write( "struct struct_def {        \n"); //$NON-NLS-1$
        writer.write( "   int st_member;          \n"); //$NON-NLS-1$
        writer.write( "};                         \n"); //$NON-NLS-1$
        writer.write( "union union_def {          \n"); //$NON-NLS-1$
        writer.write( "   int un_member;          \n"); //$NON-NLS-1$
        writer.write( "};                         \n"); //$NON-NLS-1$
        writer.write( "typedef int typedef_def;   \n"); //$NON-NLS-1$
        writer.write( "enum enum_def {            \n"); //$NON-NLS-1$
        writer.write( "   enum_item };            \n"); //$NON-NLS-1$
        writer.write( "int func_def() {}          \n"); //$NON-NLS-1$
        writer.write( "int var_def;               \n"); //$NON-NLS-1$
        String contents = writer.toString();
        return importFile(fileName, contents ); 
    }

    protected void assertRefactoringError(RefactoringStatus status, String msg) {
        RefactoringStatusEntry e= status.getEntryMatchingSeverity(RefactoringStatus.ERROR);
        assertNotNull("Expected refactoring error!", e); //$NON-NLS-1$
        assertEquals(msg, e.getMessage());
    }

    protected void assertRefactoringWarning(RefactoringStatus status, String msg) {
        RefactoringStatusEntry e= status.getEntryMatchingSeverity(RefactoringStatus.WARNING);
        assertNotNull("Expected refactoring warning!", e); //$NON-NLS-1$
        assertEquals(msg, e.getMessage());
    }

    protected void assertRefactoringOk(RefactoringStatus status) {
        assertTrue("Expected refactoring status ok: " + //$NON-NLS-1$
                status.getMessageMatchingSeverity(status.getSeverity()), 
                status.getSeverity()==RefactoringStatus.OK); 
    }

}

Back to the top