Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b565c593fb6672d8685be350431a4496bc21301d (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
/*******************************************************************************
 * Copyright (c) 2006, 2010 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
 *     Andrew Gvozdev
 *******************************************************************************/

package org.eclipse.cdt.errorparsers.xlc.tests;

import junit.framework.Assert;

import org.eclipse.cdt.core.ErrorParserManager;
import org.eclipse.cdt.core.IErrorParserNamed;
import org.eclipse.cdt.core.IMarkerGenerator;
import org.eclipse.cdt.core.ProblemMarkerInfo;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;

/*
 * Helper tester class to be able to test XlcErrorParser which extends AbstractErrorParser.
 */

public class XlcErrorParserTester {
	public static final String XLC_ERROR_PARSER_ID = "org.eclipse.cdt.errorparsers.xlc.XlcErrorParser";

	static private int counter=0;
	IProject fTempProject = ResourcesPlugin.getWorkspace().getRoot().getProject("XlcErrorParserTester.temp." + counter++);

	XlcErrorParserTester() {
		try {
			fTempProject.create(null);
		} catch (CoreException e) {
			e.printStackTrace();
			Assert.fail("Exception creating temporary project "+fTempProject.getName()+": "+e);
		}
	}

	private String fileName;
	private int lineNumber;
	private int severity;
	private String message;

	/*
	 * Dummy class implementing IMarkerGenerator lets get through testing
	 * without NPE.
	 */
	private class MockMarkerGenerator implements IMarkerGenerator {

		public void addMarker(IResource file, int lineNumber, String errorDesc,
				int severity, String errorVar) {
			// dummy
		}

		public void addMarker(ProblemMarkerInfo problemMarkerInfo) {
			// dummy
		}

	}

	/*
	 * Class MockErrorParserManager replaces ErrorParserManager
	 * with the purpose to be able to inquire how the line was parsed.
	 * fileName, lineNumber, message and severity are populated
	 * to be accessed from the test cases.
	 * Relying on internal implementation of ErrorPattern.RecordError()
	 * to provide necessary data via generateExternalMarker() call
	 */
	private class MockErrorParserManager extends ErrorParserManager {

		private MockErrorParserManager() {
			super(fTempProject, new MockMarkerGenerator());
		}

		/*
		 * A stub function just to return non-null IFile.
		 * Necessary to trick ErrorPattern.RecordError() to generate markers.
		 */
		@Override
		public IFile findFileName(String fileName) {
			if (fileName!=null && fileName.trim().length()>0)
				return fTempProject.getFile(fileName);
			return null;
		}

		/**
		 * Called by ErrorPattern.RecordError() for external problem markers
		 */
		@Override
		public void generateExternalMarker(IResource rc, int lineNumb, String desc, int sev, String varName, IPath externalPath) {
			// if rc is this project it means that file was not found
			if (rc!=null && rc!=fTempProject) {
				fileName = rc.getName();
			} else {
				fileName="";
			}
			lineNumber = lineNumb;
			message = desc;
			severity = sev;
		}
	}

	/**
	 * Main method called by individual error parser tests.
	 * @param line one xlC error message
	 * @return
	 */
	boolean parseLine(String line) {
		IErrorParserNamed errorParser = ErrorParserManager.getErrorParserCopy(XLC_ERROR_PARSER_ID);
		Assert.assertNotNull(errorParser);

		MockErrorParserManager epManager = new MockErrorParserManager();
		return errorParser.processLine(line, epManager);
	}

	String getFileName() {
		return fileName;
	}

	int getLineNumber() {
		return lineNumber;
	}

	int getSeverity() {
		return severity;
	}

	String getMessage() {
		return message;
	}
}

Back to the top