1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 18:26:01 +02:00

- fixed checking line number for tests

This commit is contained in:
Alena Laskavaia 2009-11-21 03:03:58 +00:00
parent da6cc58f76
commit 95b1911c1d

View file

@ -12,6 +12,9 @@ package org.eclipse.cdt.codan.core.test;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -47,6 +50,7 @@ public class CheckerTestCase extends BaseTestCase {
protected File tmpDir; protected File tmpDir;
private ICProject cproject; private ICProject cproject;
private IMarker[] markers; private IMarker[] markers;
private File currentFile;
/** /**
* Override for c++ * Override for c++
@ -64,12 +68,12 @@ public class CheckerTestCase extends BaseTestCase {
tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile(); tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile();
} }
public File load(String file) { public File load(String file) {
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault(); CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
String name = getClass().getName(); String name = getClass().getName();
String classFile = name.replaceAll("\\.", "/"); String classFile = name.replaceAll("\\.", "/");
classFile += ".java"; classFile += ".java";
InputStream st=null; InputStream st = null;
File f = null; File f = null;
try { try {
if (plugin != null) { if (plugin != null) {
@ -79,18 +83,17 @@ public class CheckerTestCase extends BaseTestCase {
} else { } else {
st = getClass().getResourceAsStream(classFile); st = getClass().getResourceAsStream(classFile);
} }
} catch (IOException e) { } catch (IOException e) {
fail("Cannot find java file: " + classFile); fail("Cannot find java file: " + classFile);
} }
try { try {
f = saveFile(st, file); f = saveFile(st, file);
st.close(); st.close();
currentFile = f;
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
return f; return f;
} }
static final Pattern filePattern = Pattern.compile("file=\"(.*)\""); static final Pattern filePattern = Pattern.compile("file=\"(.*)\"");
@ -141,7 +144,6 @@ public class CheckerTestCase extends BaseTestCase {
throw e; throw e;
} }
} }
} }
/** /**
@ -153,12 +155,10 @@ public class CheckerTestCase extends BaseTestCase {
for (int i = 0; i < projects.length; i++) { for (int i = 0; i < projects.length; i++) {
IProject p = projects[i]; IProject p = projects[i];
if (p.getName().startsWith("Codan")) { if (p.getName().startsWith("Codan")) {
p.delete( p.delete(IResource.FORCE
IResource.FORCE | IResource.ALWAYS_DELETE_PROJECT_CONTENT,
| IResource.ALWAYS_DELETE_PROJECT_CONTENT,
new NullProgressMonitor()); new NullProgressMonitor());
} }
} }
} }
@ -167,7 +167,8 @@ public class CheckerTestCase extends BaseTestCase {
ModelJoiner mj = new ModelJoiner(); ModelJoiner mj = new ModelJoiner();
try { try {
// Create the cproject // Create the cproject
final String projectName = "CodanProjTest_" + System.currentTimeMillis(); final String projectName = "CodanProjTest_"
+ System.currentTimeMillis();
final IWorkspace workspace = ResourcesPlugin.getWorkspace(); final IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace.run(new IWorkspaceRunnable() { workspace.run(new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException { public void run(IProgressMonitor monitor) throws CoreException {
@ -214,30 +215,64 @@ public class CheckerTestCase extends BaseTestCase {
return; return;
} }
/** /**
* @param i - line * @param i
* - line
*/ */
public void checkErrorLine(int i) { public void checkErrorLine(int i) {
assertTrue(markers!=null); assertTrue(markers != null);
assertTrue(markers.length>0); assertTrue(markers.length > 0);
boolean found=false; boolean found = false;
for (int j = 0; j < markers.length; j++) { for (int j = 0; j < markers.length; j++) {
IMarker m = markers[j]; IMarker m = markers[j];
Object line=null; Object line = null;
Object pos;
try { try {
line = m.getAttribute(IMarker.LINE_NUMBER); line = m.getAttribute(IMarker.LINE_NUMBER);
if (line == null || line.equals(-1)) {
pos = m.getAttribute(IMarker.CHAR_START);
line = new Integer(pos2line(((Integer) pos).intValue()));
}
} catch (CoreException e) { } catch (CoreException e) {
fail(e.getMessage()); fail(e.getMessage());
} catch (IOException e) {
fail(e.getMessage());
} }
if (line.equals(i)) { if (line.equals(i)) {
found=true; found = true;
} }
} }
assertTrue("Error on line "+i+" not found ",found); assertTrue("Error on line " + i + " not found ", found);
} }
/**
* @param pos
* @return
* @throws IOException
*/
private int pos2line(int pos) throws IOException {
FileInputStream st = new FileInputStream(currentFile);
try {
int c;
int line = 1;
int cur = 0;
while ((c = st.read()) != -1) {
if (c == '\n')
line++;
if (cur >= pos)
return line;
cur++;
}
;
} finally {
st.close();
}
return 0;
}
public void checkNoErrors() { public void checkNoErrors() {
assertTrue("Found errors but should not",markers==null || markers.length==0); assertTrue("Found errors but should not", markers == null
|| markers.length == 0);
} }
/** /**
@ -249,9 +284,13 @@ public class CheckerTestCase extends BaseTestCase {
} catch (CoreException e) { } catch (CoreException e) {
fail(e.getMessage()); fail(e.getMessage());
} }
CodanRuntime.getInstance().getBuilder().processResource(cproject.getProject(), NPM); CodanRuntime.getInstance().getBuilder().processResource(
cproject.getProject(), NPM);
try { try {
markers = cproject.getProject().findMarkers(IProblemReporter.GENERIC_CODE_ANALYSIS_MARKER_TYPE, true, 1); markers = cproject.getProject()
.findMarkers(
IProblemReporter.GENERIC_CODE_ANALYSIS_MARKER_TYPE,
true, 1);
} catch (CoreException e) { } catch (CoreException e) {
fail(e.getMessage()); fail(e.getMessage());
} }