View | Details | Raw Unified | Return to bug 237051 | Differences between
and this patch

Collapse All | Expand All

(-)formatter/org/eclipse/jdt/internal/formatter/FormatterCommentParser.java (+11 lines)
Lines 179-184 Link Here
179
	this.tagSourceStart = previousPosition;
179
	this.tagSourceStart = previousPosition;
180
	this.tagSourceEnd = this.scanner.getCurrentTokenEndPosition();
180
	this.tagSourceEnd = this.scanner.getCurrentTokenEndPosition();
181
	char[] tagName = this.scanner.getCurrentIdentifierSource();
181
	char[] tagName = this.scanner.getCurrentIdentifierSource();
182
	int length = tagName.length;
183
	if (length == 0 || !ScannerHelper.isJavaIdentifierStart(tagName[0])) {
184
		this.invalidTagName = true;
185
		return null;
186
	}
187
	for (int i=1; i<length; i++) {
188
		if (!ScannerHelper.isJavaIdentifierPart(tagName[i])) {
189
			this.invalidTagName = true;
190
			return null;
191
		}
192
	}
182
    return tagName;
193
    return tagName;
183
}
194
}
184
195
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsMassiveTests.java (-480 / +230 lines)
Lines 15-26 Link Here
15
import java.io.IOException;
15
import java.io.IOException;
16
import java.text.SimpleDateFormat;
16
import java.text.SimpleDateFormat;
17
import java.util.ArrayList;
17
import java.util.ArrayList;
18
import java.util.Arrays;
19
import java.util.Date;
18
import java.util.Date;
20
import java.util.HashMap;
19
import java.util.HashMap;
21
import java.util.List;
20
import java.util.List;
22
import java.util.Map;
21
import java.util.Map;
23
import java.util.StringTokenizer;
24
22
25
import junit.framework.AssertionFailedError;
23
import junit.framework.AssertionFailedError;
26
import junit.framework.ComparisonFailure;
24
import junit.framework.ComparisonFailure;
Lines 30-45 Link Here
30
import org.eclipse.core.runtime.IPath;
28
import org.eclipse.core.runtime.IPath;
31
import org.eclipse.core.runtime.Path;
29
import org.eclipse.core.runtime.Path;
32
import org.eclipse.jdt.core.formatter.CodeFormatter;
30
import org.eclipse.jdt.core.formatter.CodeFormatter;
33
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants;
34
import org.eclipse.jdt.core.tests.model.ModelTestsUtil;
31
import org.eclipse.jdt.core.tests.model.ModelTestsUtil;
35
import org.eclipse.jdt.core.tests.util.Util;
32
import org.eclipse.jdt.core.tests.util.Util;
36
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
37
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
38
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
39
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
33
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
40
import org.eclipse.jdt.internal.compiler.parser.Scanner;
41
import org.eclipse.jdt.internal.core.util.CodeSnippetParsingUtil;
42
import org.eclipse.jdt.internal.core.util.SimpleDocument;
43
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
34
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter;
44
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
35
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions;
45
import org.eclipse.text.edits.TextEdit;
36
import org.eclipse.text.edits.TextEdit;
Lines 102-178 Link Here
102
 * <code>linesLeading</code> (e.g. ignore white spaces at the beginning of the
93
 * <code>linesLeading</code> (e.g. ignore white spaces at the beginning of the
103
 * lines, including the star inside javadoc or block comments):
94
 * lines, including the star inside javadoc or block comments):
104
 * <ul>
95
 * <ul>
105
 * 	<li>Eclipse 3.0 performance workspace (9951 units):<ul>
96
 * 	<li>JUnit 3.8.2 workspace (71 units):
97
 * 	<ul>
106
 * 		<li>0 error</li>
98
 * 		<li>0 error</li>
107
 * 		<li>0 failures</li>
99
 * 		<li>0 failures</li>
108
 * 		<li>8 failures due to old formatter</li>
100
 * 		<li>0 failures due to old formatter</li>
109
 * 		<li>723 files have different lines leading spaces</li>
101
 * 		<li>8 files have different lines leading spaces</li>
110
 * 		<li>9 files have different spaces</li>
102
 * 		<li>0 files have different spaces</li>
111
 *		</ul></li>
103
 *		</ul></li>
112
 * 	<li>Eclipse 3.4 workspace (16592 units):<ul>
104
 * 	<li>Eclipse 3.0 performance workspace (9951 units):
105
 * 	<ul>
113
 * 		<li>0 error</li>
106
 * 		<li>0 error</li>
114
 * 		<li>11 failures</li>
107
 * 		<li>1 failures</li>
115
 * 		<li>17 failures due to old formatter</li>
108
 * 		<li>8 failures due to old formatter</li>
116
 * 		<li>1244 files have different lines leading spaces</li>
109
 * 		<li>722 files have different lines leading spaces</li>
117
 * 		<li>11 files have different spaces</li>
110
 * 		<li>9 files have different spaces</li>
118
 *		</ul></li>
111
 *		</ul></li>
119
 *		<li>ganymede M5 workspace (25819 units):<ul>
112
 * 	<li>Eclipse 3.4 workspace (17890 units):
113
 * 	<ul>
120
 * 		<li>0 error</li>
114
 * 		<li>0 error</li>
121
 * 		<li>12 failures due to different output while reformatting!</li>
115
 * 		<li>17 failures</li>
122
 * 		<li>15 failures due to old formatter</li>
116
 * 		<li>21 failures due to old formatter</li>
123
 * 		<li>1371 files have different line leading spaces when reformatting!</li>
117
 * 		<li>1372 files have different lines leading spaces</li>
124
 * 		<li>14 files have different spaces when reformatting!</li>
118
 * 		<li>12 files have different spaces</li>
125
 *		</ul></li>
119
 *		</ul></li>
126
 *		<li>ganymede M6a workspace (26336 units):<ul>
120
 *		<li>ganymede workspace (33190 units):
127
 * 		<li>0 error</li>
121
 *		<ul>
128
 * 		<li>16 failures due to different output while reformatting!</li>
122
 * 		<li>1 error</li>
129
 * 		<li>17 failures due to old formatter</li>
123
 * 		<li>21 failures due to different output while reformatting!</li>
130
 * 		<li>1469 files have different line leading spaces when reformatting!</li>
124
 * 		<li>21 failures due to old formatter</li>
131
 * 		<li>14 files have different spaces when reformatting!</li>
125
 * 		<li>1780 files have different line leading spaces when reformatting!</li>
126
 * 		<li>20 files have different spaces when reformatting!</li>
132
 *		</ul></li>
127
 *		</ul></li>
133
 * </ul>
128
 * </ul>
134
 */
129
 */
135
public class FormatterCommentsMassiveTests extends FormatterRegressionTests {
130
public class FormatterCommentsMassiveTests extends FormatterRegressionTests {
136
131
137
	private static final String LINE_SEPARATOR = org.eclipse.jdt.internal.compiler.util.Util.LINE_SEPARATOR;
138
	final File file;
132
	final File file;
139
	final IPath path;
133
	final IPath path;
140
	List failures = new ArrayList();
141
	List expectedFailures = new ArrayList();
142
	List leadingWhitespacesFailures = new ArrayList();
143
	List whitespacesFailures= new ArrayList();
144
	boolean hasSpaceFailure;
134
	boolean hasSpaceFailure;
145
	private int changedHeaderFooter;
135
	private DefaultCodeFormatterOptions preferences;
146
	private int changedPreTags;
136
	private final static File INPUT_DIR = new File(System.getProperty("inputDir"));
147
	private int changedCodeTags;
137
	private final static File OUTPUT_DIR;
148
	private final static boolean DEBUG_TESTS = "true".equals(System.getProperty("debugTests"));
138
	private final static boolean COMPARE;
149
	private final static String DIR = System.getProperty("dir"); //$NON-NLS-1$
150
	private final static String COMPARE = System.getProperty("compare"); //$NON-NLS-1$
151
	private final static int IGNORE_SPACES;
152
	private final static int ALL_SPACES = 1;	// ignore all spaces
153
	private final static int LINES_LEADING_SPACES = 2;	// ignore all spaces at the beginning of all lines
154
	private final static int ALL_COMMENTS_SPACES = 3;	// ignore all spaces inside all comments
155
	private final static int ALL_COMMENTS_LINES_LEADING_SPACES = 4;	// ignore all spaces at the beginning of all comments lines
156
	static {
139
	static {
157
		String ignoreSpaces = System.getProperty("ignoreSpaces"); //$NON-NLS-1$
140
		String dir = System.getProperty("outputDir"); //$NON-NLS-1$
158
		int filterValue;
141
		File outputDir = null;
159
		if ("all".equals(ignoreSpaces)) {
142
		boolean compare = true;
160
			filterValue = ALL_SPACES;
143
		if (dir != null) {
161
		} else if ("linesLeading".equals(ignoreSpaces)) {
144
			outputDir = new File(dir);
162
			filterValue = LINES_LEADING_SPACES;
145
			if (!outputDir.exists()) {
163
		} else if ("comments".equals(ignoreSpaces)) {
146
				compare = false;
164
			filterValue = ALL_COMMENTS_SPACES;
147
				System.err.println("WARNING: The output directory "+dir+" does not exist...");
165
		} else if ("commentsLinesLeading".equals(ignoreSpaces)) {
148
				System.err.println("=> NO comparison will be done! The formatted files will be written there instead.");
166
			filterValue = ALL_COMMENTS_LINES_LEADING_SPACES;
149
				try {
167
		} else {
150
	                Thread.sleep(1000);
168
			filterValue = 0; // no filter
151
                } catch (InterruptedException e) {
152
	                // skip
153
                }
154
			}
169
		}
155
		}
170
		IGNORE_SPACES = filterValue;
156
		OUTPUT_DIR = outputDir;
157
		COMPARE = compare;
171
	}
158
	}
172
	private final static int FORMAT_REPEAT  = Integer.parseInt(System.getProperty("repeat", "2")); //$NON-NLS-1$
159
	private final static int FORMAT_REPEAT  = Integer.parseInt(System.getProperty("repeat", "2")); //$NON-NLS-1$
160
	
161
	// Failures management
162
	final static int OTHER_FAILURE = 0;
163
	final static int COMPARISON_FAILURE = 1;
164
	final static int REFORMATTING_FAILURE = 2;
165
	final static int REFORMATTING_LEADING_FAILURE = 4;
166
	final static int REFORMATTING_WHITESPACES_FAILURE = 5;
167
	final static int REFORMATTING_EXPECTED_FAILURE = 3;
168
	class FormattingFailure {
169
		String title;
170
		String label;
171
		List failures = new ArrayList();
172
		public FormattingFailure(String label) {
173
	        this(label, label);
174
        }
175
		public FormattingFailure(String label, String title) {
176
	        super();
177
	        this.label = label;
178
	        this.title = title;
179
        }
180
	}
181
	final static FormattingFailure[] FAILURES = new FormattingFailure[6];
182
	{
183
		FAILURES[OTHER_FAILURE] = new FormattingFailure("");
184
		FAILURES[COMPARISON_FAILURE] = new FormattingFailure("comparing with previous version");
185
		FAILURES[REFORMATTING_FAILURE] = new FormattingFailure("reformatting twice");
186
		FAILURES[REFORMATTING_LEADING_FAILURE] = new FormattingFailure("reformatting twice but only by leading whitespaces");
187
		FAILURES[REFORMATTING_WHITESPACES_FAILURE] = new FormattingFailure("reformatting twice but only by whitespaces");
188
		FAILURES[REFORMATTING_EXPECTED_FAILURE] = new FormattingFailure("reformatting twice but was expected");
189
	}
173
	private static final int MAX_FAILURES = Integer.parseInt(System.getProperty("maxFailures", "100")); // Max failures using string comparison
190
	private static final int MAX_FAILURES = Integer.parseInt(System.getProperty("maxFailures", "100")); // Max failures using string comparison
174
	private static boolean ASSERT_EQUALS_STRINGS = MAX_FAILURES > 0;
191
	private static boolean ASSERT_EQUALS_STRINGS = MAX_FAILURES > 0;
175
	private final static IPath[] EXPECTED_FAILURES = DIR.indexOf("v34") < 0
192
	private final static IPath[] EXPECTED_FAILURES = INPUT_DIR.getPath().indexOf("v34") < 0
176
		? new IPath[] {
193
		? new IPath[] {
177
			new Path("org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java"),
194
			new Path("org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java"),
178
			new Path("org/eclipse/jdt/internal/eval/CodeSnippetSingleNameReference.java"),
195
			new Path("org/eclipse/jdt/internal/eval/CodeSnippetSingleNameReference.java"),
Lines 184-189 Link Here
184
			new Path("org/eclipse/team/internal/ccvs/ui/wizards/UpdateWizard.java"),
201
			new Path("org/eclipse/team/internal/ccvs/ui/wizards/UpdateWizard.java"),
185
		}
202
		}
186
		:	new IPath[] {
203
		:	new IPath[] {
204
			// Eclipse
187
			new Path("org/eclipse/equinox/internal/p2/director/NewDependencyExpander.java"),
205
			new Path("org/eclipse/equinox/internal/p2/director/NewDependencyExpander.java"),
188
			new Path("org/eclipse/jdt/core/JavaCore.java"),
206
			new Path("org/eclipse/jdt/core/JavaCore.java"),
189
			new Path("org/eclipse/jdt/internal/codeassist/CompletionEngine.java"),
207
			new Path("org/eclipse/jdt/internal/codeassist/CompletionEngine.java"),
Lines 203-223 Link Here
203
			new Path("org/eclipse/jdt/internal/core/search/JavaSearchScope.java"),
221
			new Path("org/eclipse/jdt/internal/core/search/JavaSearchScope.java"),
204
			new Path("org/eclipse/jdt/internal/eval/EvaluationContext.java"),
222
			new Path("org/eclipse/jdt/internal/eval/EvaluationContext.java"),
205
			new Path("org/eclipse/jdt/internal/ui/text/javadoc/JavadocContentAccess2.java"),
223
			new Path("org/eclipse/jdt/internal/ui/text/javadoc/JavadocContentAccess2.java"),
224
			new Path("org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeJavaSourceOutputStream.java"),
206
			new Path("org/eclipse/team/internal/ccvs/ui/mappings/WorkspaceSubscriberContext.java"),
225
			new Path("org/eclipse/team/internal/ccvs/ui/mappings/WorkspaceSubscriberContext.java"),
226
			// Ganymede
227
			new Path("com/ibm/icu/text/Collator.java"),
228
			new Path("org/apache/lucene/analysis/ISOLatin1AccentFilter.java"),
207
	};
229
	};
208
	static {
209
		// Sort expected failures to allow binary search
210
		Arrays.sort(EXPECTED_FAILURES);
211
	}
212
	
230
	
213
public static Test suite() {
231
public static Test suite() {
214
	TestSuite suite = new Suite(FormatterCommentsMassiveTests.class.getName());
232
	TestSuite suite = new Suite(FormatterCommentsMassiveTests.class.getName());
215
	try {
233
	try {
216
		File testDir = ModelTestsUtil.getWorkspaceRoot().getLocation().toFile();
217
		if (DIR != null) {
218
			File dir = new File(DIR);
219
			if (dir.exists()) testDir = dir;
220
		}
221
		FileFilter filter = new FileFilter() {
234
		FileFilter filter = new FileFilter() {
222
			public boolean accept(File pathname) {
235
			public boolean accept(File pathname) {
223
	            return pathname.isDirectory() || pathname.getPath().endsWith(".java");
236
	            return pathname.isDirectory() || pathname.getPath().endsWith(".java");
Lines 227-240 Link Here
227
		SimpleDateFormat format = new SimpleDateFormat();
240
		SimpleDateFormat format = new SimpleDateFormat();
228
		Date now = new Date(start);
241
		Date now = new Date(start);
229
		System.out.println("Date of test: "+format.format(now));
242
		System.out.println("Date of test: "+format.format(now));
230
		System.out.print("Get all Java files located in "+testDir+"...");
243
		System.out.print("Get all Java files located in "+INPUT_DIR+"...");
231
		File[] allFiles = ModelTestsUtil.getAllFiles(testDir, filter);
244
		File[] allFiles = ModelTestsUtil.getAllFiles(INPUT_DIR, filter);
232
		int length = allFiles.length;
245
		int length = allFiles.length;
233
		System.out.println(length+" found in " + (System.currentTimeMillis() - start) + "ms");
246
		System.out.println(length+" found in " + (System.currentTimeMillis() - start) + "ms");
234
		for (int i=0; i<length; i++) {
247
		for (int i=0; i<length; i++) {
235
			suite.addTest(new FormatterCommentsMassiveTests(allFiles[i]));
248
			suite.addTest(new FormatterCommentsMassiveTests(allFiles[i]));
236
		}
249
		}
237
//		ASSERT_EQUALS_STRINGS = length < 15000; 
238
    } catch (Exception e) {
250
    } catch (Exception e) {
239
    	// skip
251
    	// skip
240
    }
252
    }
Lines 244-250 Link Here
244
public FormatterCommentsMassiveTests(File file) {
256
public FormatterCommentsMassiveTests(File file) {
245
	super("testCompare");
257
	super("testCompare");
246
	this.file = file;
258
	this.file = file;
247
	this.path = new Path(file.getPath().substring(DIR.length()+1));
259
	this.path = new Path(file.getPath().substring(INPUT_DIR.getPath().length()+1));
248
}
260
}
249
261
250
/* (non-Javadoc)
262
/* (non-Javadoc)
Lines 260-265 Link Here
260
public void setUp() throws Exception {
272
public void setUp() throws Exception {
261
	super.setUp();
273
	super.setUp();
262
	this.hasSpaceFailure = false;
274
	this.hasSpaceFailure = false;
275
	this.preferences = DefaultCodeFormatterOptions.getEclipseDefaultSettings();
263
}
276
}
264
277
265
/* (non-Javadoc)
278
/* (non-Javadoc)
Lines 280-335 Link Here
280
 * @see org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests#tearDownSuite()
293
 * @see org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests#tearDownSuite()
281
 */
294
 */
282
public void tearDownSuite() throws Exception {
295
public void tearDownSuite() throws Exception {
283
	// skip standard model suite tear down
296
	if (OUTPUT_DIR != null) {
284
	int sFailures = this.failures.size();
297
		if (COMPARE) {
285
	int seFailures = this.expectedFailures.size();
298
			System.out.println("Comparison done with output files located in "+OUTPUT_DIR);
286
	int swFailures = this.whitespacesFailures.size();
287
	int slwFailures = this.leadingWhitespacesFailures.size();
288
	String failuresType = COMPARE != null ? "than old formatter" : "when reformatting";
289
	System.out.println();
290
	if (sFailures > 0) {
291
		System.out.println(sFailures+" files has still different output while reformatting!");
292
	}
293
	if (seFailures > 0) {
294
		System.out.println(seFailures+" files has still different output while reformatting due to old formatter bugs!");
295
	}
296
	if (slwFailures == 0) {
297
		System.out.println("No file has different line leading spaces "+failuresType+" :-)");
298
	} else {
299
		System.out.println(slwFailures+" files have different line leading spaces "+failuresType+"!");
300
	}
301
	if (swFailures > 0) {
302
		System.out.println(swFailures+" files have different spaces "+failuresType+"!");
303
	}
304
	if (this.changedHeaderFooter >0) {
305
		System.out.println(this.changedHeaderFooter+" differences in header/footer have been found");
306
	}
307
	if (this.changedPreTags >0) {
308
		System.out.println(this.changedPreTags+" differences in <pre> tags (blank lines) have been found");
309
	}
310
	System.out.println();
311
	if (sFailures > 0) {
312
		System.out.println("List of files with different output "+failuresType+":");
313
		for (int i=0; i<sFailures; i++) {
314
			System.out.println("	- "+this.failures.get(i));
315
		}
299
		}
316
	}
300
	}
317
	if (seFailures > 0) {
301
	// skip standard model suite tear down
318
		System.out.println("List of files with different output "+failuresType+" (due to old formatter bugs):");
302
	System.out.println();
319
		for (int i=0; i<seFailures; i++) {
303
	int max = FAILURES.length;
320
			System.out.println("	- "+this.expectedFailures.get(i));
304
	for (int i=0; i<max; i++) {
321
		}
305
		List failures = FAILURES[i].failures;
322
	}
306
		int size = failures.size();
323
	if (slwFailures > 0) {
307
		if (size > 0) {
324
		System.out.println("List of files with different line leading spaces "+failuresType+":");
308
			System.out.print(size);
325
		for (int i=0; i<slwFailures; i++) {
309
			System.out.print(" file");
326
			System.out.println("	- "+this.leadingWhitespacesFailures.get(i));
310
			if (size > 1) System.out.print('s');
311
			System.out.print(" has different ouput while ");
312
			System.out.print(FAILURES[i].title);
313
			System.out.println('!');
327
		}
314
		}
328
	}
315
	}
329
	if (swFailures > 0) {
316
	System.out.println();
330
		System.out.println("List of files with different spaces "+failuresType+":");
317
	for (int i=0; i<max; i++) {
331
		for (int i=0; i<swFailures; i++) {
318
		List failures = FAILURES[i].failures;
332
			System.out.println("	- "+this.whitespacesFailures.get(i));
319
		int size = failures.size();
320
		if (size > 0) {
321
			System.out.println("List of files with different output while "+FAILURES[i].label+":");
322
			for (int j=0; j<size; j++) {
323
				System.out.println("	- "+failures.get(j));
324
			}
333
		}
325
		}
334
	}
326
	}
335
}
327
}
Lines 340-542 Link Here
340
 * The line separators in 'actual' are converted to '\n' before the comparison.
332
 * The line separators in 'actual' are converted to '\n' before the comparison.
341
 */
333
 */
342
protected void assertSourceEquals(String message, String expected, String actual) {
334
protected void assertSourceEquals(String message, String expected, String actual) {
335
	if (expected == null) {
336
		assertNull(message, actual);
337
		return;
338
	}
343
	if (actual == null) {
339
	if (actual == null) {
344
		assertEquals(message, expected, null);
340
		assertEquals(message, expected, null);
345
		return;
341
		return;
346
	}
342
	}
343
	expected = Util.convertToIndependantLineDelimiter(expected);
347
	actual = Util.convertToIndependantLineDelimiter(actual);
344
	actual = Util.convertToIndependantLineDelimiter(actual);
348
	try {
345
	if (ASSERT_EQUALS_STRINGS) {
349
		if (ASSERT_EQUALS_STRINGS) {
346
		assertEquals(message, expected, actual);
350
			assertEquals(message, expected, actual);
347
	} else {
351
		} else {
348
		assertTrue(message, actual.equals(expected));
352
			assertTrue(message, actual.equals(expected));
353
		}
354
	}
355
	catch (ComparisonFailure cf) {
356
		if ("true".equals(COMPARE)) {
357
			String trimmedExpected = expected;
358
			String trimmedActual = actual;
359
			switch (IGNORE_SPACES) {
360
				case ALL_SPACES:
361
					trimmedExpected = ModelTestsUtil.removeWhiteSpace(expected);
362
					trimmedActual= ModelTestsUtil.removeWhiteSpace(actual);
363
					if (trimmedExpected.equals(trimmedActual)) {
364
						this.whitespacesFailures.add(this.path);
365
						return;
366
					}
367
					break;
368
				case LINES_LEADING_SPACES:
369
					trimmedExpected = ModelTestsUtil.trimLinesLeadingWhitespaces(expected);
370
					trimmedActual= ModelTestsUtil.trimLinesLeadingWhitespaces(actual);
371
					if (trimmedExpected.equals(trimmedActual)) {
372
						this.leadingWhitespacesFailures.add(this.path);
373
						return;
374
					}
375
					trimmedExpected = ModelTestsUtil.removeWhiteSpace(expected);
376
					trimmedActual= ModelTestsUtil.removeWhiteSpace(actual);
377
					if (trimmedExpected.equals(trimmedActual)) {
378
						this.whitespacesFailures.add(this.path);
379
						return;
380
					}
381
					break;
382
			}
383
			if (DEBUG_TESTS && ASSERT_EQUALS_STRINGS) {
384
				assertEquals(message, trimmedExpected, trimmedActual);
385
			}
386
		}
387
		this.failures.add(this.path);
388
		ASSERT_EQUALS_STRINGS = this.failures.size() < MAX_FAILURES;
389
		throw cf;
390
	}
391
	catch (AssertionFailedError afe) {
392
		this.failures.add(this.path);
393
		throw afe;
394
	}
395
}
396
397
private String cleanAllKnownDifferences(String comment) {
398
	int kind = comment.charAt(1) == '/' ? 1 : comment.charAt(2) == '*' ? 3 : 2;
399
	String cleanedComment = comment;
400
	switch (kind) {
401
		case 1: // line comment
402
			cleanedComment = cleanBlankLinesAfterLineComment(comment);
403
			break;
404
		case 3: // javadoc comment
405
			cleanedComment = cleanHeaderAndFooter(comment);
406
			String newComment = cleanPreTags(cleanedComment);
407
			if (cleanedComment == newComment) {
408
				cleanedComment = cleanCodeTags(cleanedComment);
409
			} else {
410
				cleanedComment = newComment;
411
			}
412
			break;
413
	}
414
	return cleanedComment;
415
}
416
private String cleanHeaderAndFooter(String comment) {
417
	int start = 1; // skip starting '/'
418
	int length = comment.length();
419
	int end = length - 1; // skip ending '/'
420
	while (comment.charAt(start) == '*') {
421
		// remove all contiguous '*' in header
422
		start++;
423
	}
424
	while (comment.charAt(--end) == '*') {
425
		// remove all contiguous '*' in header
426
	}
427
	if (start > 3 || end < (length - 2)) {
428
		this.changedHeaderFooter++;
429
		return comment.substring(start, end);
430
	}
431
	return comment;
432
}
433
434
private String cleanBlankLinesAfterLineComment(String comment) {
435
	int length = comment.length();
436
	if (comment.charAt(length-1) == '\n') {
437
		length--;
438
		if (comment.charAt(length-1) == '\r') {
439
			length--;
440
		}
441
		return comment.substring(0, length);
442
	}
443
	return comment;
444
}
445
446
private String cleanCodeTags(String comment) {
447
	if (comment.indexOf("<code>") < 0) return comment;
448
	StringTokenizer tokenizer = new StringTokenizer(comment, "\r\n\f");
449
	StringBuffer buffer = new StringBuffer();
450
	while (tokenizer.hasMoreTokens()) {
451
		String line = tokenizer.nextToken();
452
		if (line.indexOf("<pre>") >= 0) {
453
			while (line.indexOf("</pre>") < 0) {
454
				line = tokenizer.nextToken();
455
			}
456
		} else {
457
			buffer.append(line);
458
			buffer.append("\n");
459
		}
460
	}
461
	this.changedCodeTags++;
462
	return buffer.toString();
463
}
464
465
private String cleanPreTags(String comment) {
466
	if (comment.indexOf("<pre>") < 0) return comment;
467
	StringTokenizer tokenizer = new StringTokenizer(comment, "\r\n\f");
468
	StringBuffer buffer = new StringBuffer();
469
	StringBuffer emptyLines = new StringBuffer();
470
	String previousLine = null;
471
	while (tokenizer.hasMoreTokens()) {
472
		String line = tokenizer.nextToken();
473
		if (line.trim() == "*") {
474
			if (previousLine == null || previousLine.indexOf("<pre>") < 0) {
475
				buffer.append(line);
476
				buffer.append("\n");
477
				continue;
478
			} else {
479
				emptyLines.append(line);
480
				emptyLines.append("\n");
481
			}
482
		} else if (line.indexOf("<code>") >= 0) {
483
			while (line.indexOf("</code>") < 0) {
484
				line = tokenizer.nextToken();
485
			}
486
		} else if (emptyLines.length() != 0) {
487
			if (line.indexOf("</pre>") < 0) {
488
				buffer.append(emptyLines);
489
				emptyLines.setLength(0);
490
			}
491
			buffer.append(line);
492
			buffer.append("\n");
493
		} else {
494
			buffer.append(line);
495
			buffer.append("\n");
496
		}
497
		previousLine = line;
498
	}
349
	}
499
	this.changedPreTags++;
500
	return buffer.toString();
501
}
350
}
502
351
503
DefaultCodeFormatter codeFormatter() {
352
DefaultCodeFormatter codeFormatter() {
504
	DefaultCodeFormatterOptions preferences = DefaultCodeFormatterOptions.getEclipseDefaultSettings();
353
	DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.preferences, getDefaultCompilerOptions());
505
	DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences);
506
	return codeFormatter;
354
	return codeFormatter;
507
}
355
}
508
356
509
void compareFormattedSource() throws IOException, Exception {
357
void compareFormattedSource() throws IOException, Exception {
510
	DefaultCodeFormatter codeFormatter = codeFormatter();
511
	String source = new String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(this.file, null));
358
	String source = new String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(this.file, null));
512
	try {
359
	try {
513
		if ("comments".equals(COMPARE)) {
360
		// Format the source
514
			String[] oldFormattedComments = formattedComments(source, true);
361
		String actualResult = runFormatter(codeFormatter(), source, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, 0, source.length(), null);
515
			String[] newFormattedComments = formattedComments(source, false);
362
516
			int length = oldFormattedComments == null ? 0 : oldFormattedComments.length;
363
		// Look for output to compare with
517
			this.abortOnFailure = false;
364
		File outputFile = new Path(OUTPUT_DIR.getPath()).append(this.path).toFile();
518
			assertEquals("Unexpected number of comments!", length, newFormattedComments == null ? 0 : newFormattedComments.length);
365
		if (COMPARE) {
519
			for (int i=0; i<length; i++) {
366
			String expectedResult = new String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(outputFile, null));
520
				String oldComment = oldFormattedComments[i];
367
			try {
521
				String newComment = newFormattedComments[i];
368
				assertSourceEquals("Unexpected format output!", expectedResult, actualResult);
522
				if (oldComment == null) {
369
			}
523
					assertNull("Unexpected non-null new comment", newComment);
370
			catch (ComparisonFailure cf) {
524
				} else {
371
				FAILURES[COMPARISON_FAILURE].failures.add(this.path);
525
					String expected = cleanAllKnownDifferences(oldComment);
372
				throw cf;
526
					String actual = cleanAllKnownDifferences(newComment);
373
			}
527
					if (!expected.equals(actual)) {
374
			catch (AssertionFailedError afe) {
528
						String actualResult = runFormatter(codeFormatter, source, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, 0, source.length(), null);
375
				FAILURES[COMPARISON_FAILURE].failures.add(this.path);
529
						String expectedResult = expectedFormattedSource(source);
376
				throw afe;
530
						assertEquals("Unexpected difference with formatted comment "+(i+1), Util.convertToIndependantLineDelimiter(expectedResult), Util.convertToIndependantLineDelimiter(actualResult));
531
					}
532
				}
533
			}
377
			}
534
		} else {
378
		} else {
535
			String actualResult = runFormatter(codeFormatter, source, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, 0, source.length(), null);
379
			outputFile.getParentFile().mkdirs();
536
			if (!this.hasSpaceFailure && "true".equals(COMPARE)) {
380
			Util.writeToFile(actualResult, outputFile.getAbsolutePath());
537
				String expectedResult = expectedFormattedSource(source);
538
				assertLineEquals(actualResult, source, expectedResult, false);
539
			}
540
		}
381
		}
541
	}
382
	}
542
	catch (Exception e) {
383
	catch (Exception e) {
Lines 566-707 Link Here
566
	return buffer.toString();
407
	return buffer.toString();
567
}
408
}
568
409
569
private String expectedFormattedSource(String source) {
570
	boolean enableNewCommentFormatter = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT;
571
	try {
572
		DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT = false;
573
		DefaultCodeFormatter codeFormatter = codeFormatter();
574
		Scanner scanner = new Scanner(true, true, false/*nls*/, ClassFileConstants.JDK1_4/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/);
575
		CodeSnippetParsingUtil codeSnippetParsingUtil = new CodeSnippetParsingUtil();
576
		CompilationUnitDeclaration compilationUnitDeclaration = codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true);
577
		final TypeDeclaration[] types = compilationUnitDeclaration.types;
578
		int headerEndPosition = types == null ? compilationUnitDeclaration.sourceEnd : types[0].declarationSourceStart;
579
		scanner.setSource(source.toCharArray());
580
		scanner.lineEnds = codeSnippetParsingUtil.recordedParsingInformation.lineEnds;
581
		int[][] commentsPositions = compilationUnitDeclaration.comments;
582
		int length = commentsPositions == null ? 0 : commentsPositions.length;
583
		String[] formattedComments = new String[length];
584
		for (int i=0; i<length; i++) {
585
			int[] positions = commentsPositions[i];
586
			int commentKind = CodeFormatter.K_JAVA_DOC;
587
			int commentStart = positions [0];
588
			int commentEnd = positions [1];
589
			if (commentEnd < 0) { // line or block comments have negative end position
590
				commentEnd = -commentEnd;
591
				if (commentStart > 0) { // block comments have positive start position
592
					commentKind = CodeFormatter.K_MULTI_LINE_COMMENT;
593
				} else {
594
					commentStart = -commentStart;
595
					commentKind = CodeFormatter.K_SINGLE_LINE_COMMENT;
596
				}
597
			}
598
			if (commentStart >= headerEndPosition) {
599
				int indentationLevel = getIndentationLevel(scanner, commentStart);
600
				formattedComments[i] = runFormatter(codeFormatter, source.substring(commentStart, commentEnd), commentKind, indentationLevel, 0, commentEnd - commentStart, LINE_SEPARATOR);
601
			}
602
		}
603
		SimpleDocument document = new SimpleDocument(source);
604
		for (int i=length-1; i>=0; i--) {
605
			if (formattedComments[i] != null) {
606
				int[] positions = commentsPositions[i];
607
				int commentStart = positions [0];
608
				int commentEnd = positions [1];
609
				if (commentEnd < 0) { // line or block comments have negative end position
610
					commentEnd = -commentEnd;
611
					if (commentStart < 0) { // line comments have negative start position
612
						commentStart = -commentStart;
613
					}
614
				}
615
				document.replace(commentStart, commentEnd - commentStart, formattedComments[i]);
616
			}
617
		}
618
		String newSource = document.get();
619
		String oldResult = runFormatter(codeFormatter, newSource, CodeFormatter.K_COMPILATION_UNIT, 0, 0, newSource.length(), null);
620
		return oldResult == null ? newSource : oldResult;
621
	}
622
	finally {
623
		DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT = enableNewCommentFormatter;
624
	}
625
}
626
private String[] formattedComments(String source, boolean old) {
627
	boolean enableNewCommentFormatter = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT;
628
	try {
629
		DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT = !old;
630
		DefaultCodeFormatter codeFormatter = codeFormatter();
631
		Scanner scanner = new Scanner(true, true, false/*nls*/, ClassFileConstants.JDK1_4/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/);
632
		CodeSnippetParsingUtil codeSnippetParsingUtil = new CodeSnippetParsingUtil();
633
		CompilationUnitDeclaration compilationUnitDeclaration = codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true);
634
		final TypeDeclaration[] types = compilationUnitDeclaration.types;
635
		int headerEndPosition = types == null ? compilationUnitDeclaration.sourceEnd : types[0].declarationSourceStart;
636
		scanner.setSource(source.toCharArray());
637
		scanner.lineEnds = codeSnippetParsingUtil.recordedParsingInformation.lineEnds;
638
		int[][] commentsPositions = compilationUnitDeclaration.comments;
639
		int length = commentsPositions == null ? 0 : commentsPositions.length;
640
		String[] formattedComments = new String[length];
641
		for (int i=0; i<length; i++) {
642
			int[] positions = commentsPositions[i];
643
			int commentKind = CodeFormatter.K_JAVA_DOC;
644
			int commentStart = positions [0];
645
			int commentEnd = positions [1];
646
			if (commentEnd < 0) { // line or block comments have negative end position
647
				commentEnd = -commentEnd;
648
				if (commentStart > 0) { // block comments have positive start position
649
					commentKind = CodeFormatter.K_MULTI_LINE_COMMENT;
650
				} else {
651
					commentStart = -commentStart;
652
					commentKind = CodeFormatter.K_SINGLE_LINE_COMMENT;
653
				}
654
			}
655
			if (commentStart >= headerEndPosition) {
656
				int indentationLevel = getIndentationLevel(scanner, commentStart);
657
				formattedComments[i] = runFormatter(codeFormatter, source.substring(commentStart, commentEnd), commentKind, indentationLevel, 0, commentEnd - commentStart, LINE_SEPARATOR);
658
			}
659
		}
660
		return formattedComments;
661
	}
662
	finally {
663
		DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT = enableNewCommentFormatter;
664
	}
665
}
666
667
private int getIndentationLevel(Scanner scanner, int position) {
668
	int indentationLevel = 0;
669
	int numberOfIndentations = 0;
670
	int indentationSize;
671
	try {
672
		indentationSize = Integer.parseInt(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE);
673
	} catch (NumberFormatException nfe) {
674
		indentationSize = 4;
675
	}
676
	int lineNumber = scanner.getLineNumber(position);
677
	int lineStart = scanner.getLineStart(lineNumber);
678
	scanner.resetTo(lineStart, position-1);
679
	while (!scanner.atEnd()) {
680
		int ch = scanner.getNextChar();
681
		switch (ch) {
682
			case '\n':
683
				indentationLevel = 0;
684
				numberOfIndentations = 0;
685
				break;
686
			case '\t':
687
				numberOfIndentations++;
688
				indentationLevel = numberOfIndentations * indentationSize;
689
				break;
690
			default:
691
				indentationLevel++;
692
				if ((indentationLevel%indentationSize) == 0) {
693
					numberOfIndentations++;
694
				}
695
				break;
696
		}
697
	}
698
	if ((indentationLevel%indentationSize) != 0) {
699
		numberOfIndentations++;
700
		indentationLevel = numberOfIndentations * indentationSize;
701
	}
702
	return numberOfIndentations;
703
}
704
705
private Map getDefaultCompilerOptions() {
410
private Map getDefaultCompilerOptions() {
706
	Map optionsMap = new HashMap(30);
411
	Map optionsMap = new HashMap(30);
707
	optionsMap.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE); 
412
	optionsMap.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE); 
Lines 751-758 Link Here
751
	optionsMap.put(CompilerOptions.OPTION_ReportUnusedDeclaredThrownException, CompilerOptions.IGNORE);
456
	optionsMap.put(CompilerOptions.OPTION_ReportUnusedDeclaredThrownException, CompilerOptions.IGNORE);
752
	optionsMap.put(CompilerOptions.OPTION_ReportUnusedDeclaredThrownExceptionWhenOverriding, CompilerOptions.DISABLED); 
457
	optionsMap.put(CompilerOptions.OPTION_ReportUnusedDeclaredThrownExceptionWhenOverriding, CompilerOptions.DISABLED); 
753
	optionsMap.put(CompilerOptions.OPTION_ReportUnqualifiedFieldAccess, CompilerOptions.IGNORE);
458
	optionsMap.put(CompilerOptions.OPTION_ReportUnqualifiedFieldAccess, CompilerOptions.IGNORE);
754
	optionsMap.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_4);
459
	optionsMap.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6);
755
	optionsMap.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_2); 
460
	optionsMap.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6); 
756
	optionsMap.put(CompilerOptions.OPTION_TaskTags, ""); //$NON-NLS-1$
461
	optionsMap.put(CompilerOptions.OPTION_TaskTags, ""); //$NON-NLS-1$
757
	optionsMap.put(CompilerOptions.OPTION_TaskPriorities, ""); //$NON-NLS-1$
462
	optionsMap.put(CompilerOptions.OPTION_TaskPriorities, ""); //$NON-NLS-1$
758
	optionsMap.put(CompilerOptions.OPTION_TaskCaseSensitive, CompilerOptions.DISABLED);
463
	optionsMap.put(CompilerOptions.OPTION_TaskCaseSensitive, CompilerOptions.DISABLED);
Lines 761-767 Link Here
761
	optionsMap.put(CompilerOptions.OPTION_ReportSpecialParameterHidingField, CompilerOptions.DISABLED); 
466
	optionsMap.put(CompilerOptions.OPTION_ReportSpecialParameterHidingField, CompilerOptions.DISABLED); 
762
	optionsMap.put(CompilerOptions.OPTION_MaxProblemPerUnit, String.valueOf(100));
467
	optionsMap.put(CompilerOptions.OPTION_MaxProblemPerUnit, String.valueOf(100));
763
	optionsMap.put(CompilerOptions.OPTION_InlineJsr, CompilerOptions.DISABLED); 
468
	optionsMap.put(CompilerOptions.OPTION_InlineJsr, CompilerOptions.DISABLED); 
764
	optionsMap.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
469
	optionsMap.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
765
	return optionsMap;
470
	return optionsMap;
766
}
471
}
767
472
Lines 769-830 Link Here
769
	int length = EXPECTED_FAILURES.length;
474
	int length = EXPECTED_FAILURES.length;
770
	for (int i=0; i<length; i++) {
475
	for (int i=0; i<length; i++) {
771
		IPath expectedFailure= EXPECTED_FAILURES[i];
476
		IPath expectedFailure= EXPECTED_FAILURES[i];
772
		if (this.path.matchingFirstSegments(expectedFailure) == expectedFailure.segmentCount()) {
477
		if (this.path.toString().indexOf(expectedFailure.toString()) >= 0) {
773
			this.expectedFailures.add(this.path);
478
			FAILURES[REFORMATTING_EXPECTED_FAILURE].failures.add(this.path);
774
			return true;
479
			return true;
775
		}
480
		}
776
	}
481
	}
777
	return false;
482
	return false;
778
}
483
}
779
484
485
/*
486
private boolean runFormatterWithoutComments(CodeFormatter codeFormatter, String source, int kind, int indentationLevel, int offset, int length, String lineSeparator) {
487
	DefaultCodeFormatterOptions preferencesWithoutComment = DefaultCodeFormatterOptions.getEclipseDefaultSettings();
488
	preferencesWithoutComment.comment_format_line_comment = false;
489
	preferencesWithoutComment.comment_format_block_comment = false;
490
	preferencesWithoutComment.comment_format_javadoc_comment = false;
491
	DefaultCodeFormatter codeFormatterWithoutComment = new DefaultCodeFormatter(preferencesWithoutComment);
492
493
	TextEdit edit = codeFormatterWithoutComment.format(kind, source, offset, length, indentationLevel, lineSeparator);//$NON-NLS-1$
494
	if (edit == null) return false;
495
	String initialResult = org.eclipse.jdt.internal.core.util.Util.editedString(source, edit);
496
497
	int count = 1;
498
	String result = initialResult;
499
	String previousResult = result;
500
	while (count++ < FORMAT_REPEAT) {
501
		edit = codeFormatterWithoutComment.format(kind, result, 0, result.length(), indentationLevel, lineSeparator);//$NON-NLS-1$
502
		if (edit == null) return false;
503
		previousResult = result;
504
		result = org.eclipse.jdt.internal.core.util.Util.editedString(result, edit);
505
	}
506
	return previousResult.equals(result);
507
}
508
*/
509
780
String runFormatter(CodeFormatter codeFormatter, String source, int kind, int indentationLevel, int offset, int length, String lineSeparator) {
510
String runFormatter(CodeFormatter codeFormatter, String source, int kind, int indentationLevel, int offset, int length, String lineSeparator) {
781
	TextEdit edit = codeFormatter.format(kind, source, offset, length, indentationLevel, lineSeparator);//$NON-NLS-1$
511
	TextEdit edit = codeFormatter.format(kind, source, offset, length, indentationLevel, lineSeparator);//$NON-NLS-1$
782
	if (edit == null) return null;
512
	try {
783
	String result = org.eclipse.jdt.internal.core.util.Util.editedString(source, edit);
513
		assertNotNull("Formatted source should not be null!", edit);
514
	}
515
	catch (ComparisonFailure cf) {
516
		FAILURES[OTHER_FAILURE].failures.add(this.path);
517
		throw cf;
518
	}
519
	catch (AssertionFailedError afe) {
520
		FAILURES[OTHER_FAILURE].failures.add(this.path);
521
		throw afe;
522
	}
523
	String initialResult = org.eclipse.jdt.internal.core.util.Util.editedString(source, edit);
784
524
785
	int count = 1;
525
	int count = 1;
786
	if (COMPARE == null && length == source.length()) {
526
	String result = initialResult;
787
		String previousResult = result;
527
	String previousResult = result;
788
		while (count++ < FORMAT_REPEAT) {
528
	while (count++ < FORMAT_REPEAT) {
789
			edit = codeFormatter.format(kind, result, 0, result.length(), indentationLevel, lineSeparator);//$NON-NLS-1$
529
		edit = codeFormatter.format(kind, result, 0, result.length(), indentationLevel, lineSeparator);//$NON-NLS-1$
790
			if (edit == null) return null;
530
		if (edit == null) return null;
791
			previousResult = result;
531
		previousResult = result;
792
			result = org.eclipse.jdt.internal.core.util.Util.editedString(result, edit);
532
		result = org.eclipse.jdt.internal.core.util.Util.editedString(result, edit);
793
		}
533
	}
794
		if (!previousResult.equals(result)) {
534
	if (!previousResult.equals(result)) {
795
			switch (IGNORE_SPACES) {
535
796
				case ALL_SPACES:
536
		// Try to compare without leading spaces
797
					String trimmedExpected = ModelTestsUtil.removeWhiteSpace(previousResult);
537
		String trimmedExpected = ModelTestsUtil.trimLinesLeadingWhitespaces(previousResult);
798
					String trimmedActual= ModelTestsUtil.removeWhiteSpace(result);
538
		String trimmedActual= ModelTestsUtil.trimLinesLeadingWhitespaces(result);
799
					if (trimmedExpected.equals(trimmedActual)) {
539
		if (trimmedExpected.equals(trimmedActual)) {
800
						this.whitespacesFailures.add(this.path);
540
			FAILURES[REFORMATTING_LEADING_FAILURE].failures.add(this.path);
801
						this.hasSpaceFailure = true;
541
			this.hasSpaceFailure = true;
802
						return previousResult;
542
			return initialResult;
803
					}
543
		}
804
					break;
544
		
805
				case LINES_LEADING_SPACES:
545
		// Try to compare without spaces at all
806
					trimmedExpected = ModelTestsUtil.trimLinesLeadingWhitespaces(previousResult);
546
		if (ModelTestsUtil.removeWhiteSpace(previousResult).equals(ModelTestsUtil.removeWhiteSpace(result))) {
807
					trimmedActual= ModelTestsUtil.trimLinesLeadingWhitespaces(result);
547
			FAILURES[REFORMATTING_WHITESPACES_FAILURE].failures.add(this.path);
808
					if (trimmedExpected.equals(trimmedActual)) {
548
			this.hasSpaceFailure = true;
809
						this.leadingWhitespacesFailures.add(this.path);
549
			return initialResult;
810
						this.hasSpaceFailure = true;
550
		}
811
						return previousResult;
551
812
					}
552
		/*
813
					if (ModelTestsUtil.removeWhiteSpace(previousResult).equals(ModelTestsUtil.removeWhiteSpace(result))) {
553
		// Try to see if the formatting also fails without comments
814
						this.whitespacesFailures.add(this.path);
554
		if (!runFormatterWithoutComments(null, source, kind, indentationLevel, offset, length, lineSeparator)) {
815
						this.hasSpaceFailure = true;
555
			return initialResult;
816
						return previousResult;
556
		}
817
					}
557
818
					break;
558
		// format without comments is OK => there's a problem with comment formatting
819
			}
559
		String counterString = counterToString(count-1);
820
			if (!isExpectedFailure()) {
560
		assertSourceEquals(counterString+" formatting is different from first one!", previousResult, result);
821
				String counterString = counterToString(count-1);
561
		*/
822
				assertSourceEquals(counterString+" formatting is different from first one!", Util.convertToIndependantLineDelimiter(previousResult), Util.convertToIndependantLineDelimiter(result));
562
		if (!isExpectedFailure()) {
563
			String counterString = counterToString(count-1);
564
			try {
565
				assertSourceEquals(counterString+" formatting is different from first one!", previousResult, result);
566
			}
567
			catch (ComparisonFailure cf) {
568
				FAILURES[REFORMATTING_FAILURE].failures.add(this.path);
569
				throw cf;
570
			}
571
			catch (AssertionFailedError afe) {
572
				FAILURES[REFORMATTING_FAILURE].failures.add(this.path);
573
				throw afe;
823
			}
574
			}
824
			result = previousResult;
825
		}
575
		}
826
	}
576
	}
827
	return result;
577
	return initialResult;
828
}
578
}
829
579
830
public void testCompare() throws IOException, Exception {
580
public void testCompare() throws IOException, Exception {
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterCommentsBugsTest.java (+28 lines)
Lines 977-980 Link Here
977
		1 /* indentation level */
977
		1 /* indentation level */
978
	);
978
	);
979
}
979
}
980
981
/**
982
 * @bug 237051: [formatter] Formatter insert blank lines after javadoc if javadoc contains Commons Attributes @@ annotations
983
 * @test Ensure that Commons Attributes @@ annotations do not screw up the comment formatter
984
 * @see "https://bugs.eclipse.org/bugs/show_bug.cgi?id=237051"
985
 */
986
public void testBug237051() throws JavaModelException {
987
	String source = 
988
		"public interface Test {\n" + 
989
		"/**\n" + 
990
		" * foo\n" + 
991
		" * \n" + 
992
		" * @@Foo(\"foo\")\n" + 
993
		" */\n" + 
994
		"Object doSomething(Object object) throws Exception;\n" + 
995
		"}\n" + 
996
		"\n";
997
	formatSource(source,
998
		"public interface Test {\n" + 
999
		"	/**\n" + 
1000
		"	 * foo\n" + 
1001
		"	 * \n" + 
1002
		"	 * @@Foo(\"foo\")\n" + 
1003
		"	 */\n" + 
1004
		"	Object doSomething(Object object) throws Exception;\n" + 
1005
		"}\n"
1006
	);
1007
}
980
}
1008
}

Return to bug 237051