1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-13 19:25:38 +02:00

Fixed test infrastructure (use common code C test utils to read files from java comments)

This commit is contained in:
Alena Laskavaia 2010-05-11 02:08:44 +00:00
parent fdb501da55
commit 5f256a6889
7 changed files with 341 additions and 356 deletions

View file

@ -148,11 +148,17 @@ public class ControlFlowGraphTest extends CodanTestCase {
return false; return false;
} }
protected void buildAndCheck(String code) {
buildAndCheck(code, false);
}
protected void buildAndCheck_cpp(String code) {
buildAndCheck(code, true);
}
/** /**
* @param file * @param file
*/ */
protected void buildAndCheck(String file) { protected void buildAndCheck(String code, boolean cpp) {
load(file); loadcode(code, cpp);
buildCfg(); buildCfg();
checkCfg(); checkCfg();
} }
@ -195,30 +201,27 @@ public class ControlFlowGraphTest extends CodanTestCase {
return null; return null;
} }
/*- @Override
<code file="test1.c"> public boolean isCpp() {
main() { return true;
int a;
a=1;
}
</code>
*/
public void test_basic() {
buildAndCheck("test1.c");
} }
/*- // main() {
<code file="test2.c"> // int a;
main() { // a=1;
int a=10; // }
while (a--) { public void test_basic() {
a=a-2; buildAndCheck(getAboveComment());
} }
}
</code> // main() {
*/ // int a=10;
// while (a--) {
// a=a-2;
// }
// }
public void test_while() { public void test_while() {
buildAndCheck("test2.c"); buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode(); IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing(); IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IConnectorNode conn = (IConnectorNode) decl.getOutgoing(); IConnectorNode conn = (IConnectorNode) decl.getOutgoing();
@ -233,18 +236,14 @@ public class ControlFlowGraphTest extends CodanTestCase {
IExitNode ret = (IExitNode) ((IConnectorNode) m1).getOutgoing(); IExitNode ret = (IExitNode) ((IConnectorNode) m1).getOutgoing();
} }
/*- // main() {
<code file="test3.c"> // int a=10;
main() { // if (a--) {
int a=10; // a=a-2;
if (a--) { // }
a=a-2; // }
}
}
</code>
*/
public void test_if() { public void test_if() {
buildAndCheck("test3.c"); buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode(); IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing(); IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing(); IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@ -257,18 +256,14 @@ public class ControlFlowGraphTest extends CodanTestCase {
assertSame(m1, m2); assertSame(m1, m2);
} }
/*- // main() {
<code file="test4.c"> // int a=10;
main() { // if (a--) {
int a=10; // return;
if (a--) { // }
return; // }
}
}
</code>
*/
public void test_if_ret() { public void test_if_ret() {
buildAndCheck("test4.c"); buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode(); IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing(); IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing(); IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@ -277,19 +272,16 @@ public class ControlFlowGraphTest extends CodanTestCase {
IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE); IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE);
IBasicBlock m1 = jumpEnd(bElse); IBasicBlock m1 = jumpEnd(bElse);
} }
/*-
<code file="test5.c"> // main() {
main() { // int a=10;
int a=10; // if (a--) {
if (a--) { // return;
return; // a++;
a++; // }
} // }
}
</code>
*/
public void test_if_dead() { public void test_if_dead() {
buildAndCheck("test5.c"); buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode(); IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing(); IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing(); IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@ -297,21 +289,18 @@ public class ControlFlowGraphTest extends CodanTestCase {
IExitNode bThen = (IExitNode) branchEnd(des, IBranchNode.THEN); IExitNode bThen = (IExitNode) branchEnd(des, IBranchNode.THEN);
IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE); IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE);
IBasicBlock m1 = jumpEnd(bElse); IBasicBlock m1 = jumpEnd(bElse);
assertEquals(1,graph.getUnconnectedNodeSize()); assertEquals(1, graph.getUnconnectedNodeSize());
} }
/*-
<code file="test_ifif.c"> // foo() {
foo() { // int a=10, x=5;
int a=10, x=5; // if (a--) {
if (a--) { // if (x<0)
if (x<0) // a++;
a++; // }
} // }
}
</code>
*/
public void test_ifif() { public void test_ifif() {
buildAndCheck("test_ifif.c"); buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode(); IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing(); IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing(); IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@ -319,41 +308,33 @@ public class ControlFlowGraphTest extends CodanTestCase {
IDecisionNode bThen = (IDecisionNode) branchEnd(des, IBranchNode.THEN); IDecisionNode bThen = (IDecisionNode) branchEnd(des, IBranchNode.THEN);
assertEquals("x<0", data(bThen)); assertEquals("x<0", data(bThen));
IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE); IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE);
IBasicBlock m2 = jumpEnd(branchEnd(bThen,IBranchNode.THEN)); IBasicBlock m2 = jumpEnd(branchEnd(bThen, IBranchNode.THEN));
IBasicBlock m1 = jumpEnd(bElse); IBasicBlock m1 = jumpEnd(bElse);
IBasicBlock m3 = jumpEnd(m2); IBasicBlock m3 = jumpEnd(m2);
assertSame(m1, m3); assertSame(m1, m3);
} }
/*-
<code file="test_throw.cc"> // int foo() {
int foo() { // throw 5;
throw 5; // }
}
</code>
*/
public void test_throw() { public void test_throw() {
buildAndCheck("test_throw.cc"); buildAndCheck_cpp(getAboveComment());
IStartNode startNode = graph.getStartNode(); IStartNode startNode = graph.getStartNode();
assertEquals(1, graph.getExitNodeSize()); assertEquals(1, graph.getExitNodeSize());
Iterator<IExitNode> exitNodeIterator = graph.getExitNodeIterator(); Iterator<IExitNode> exitNodeIterator = graph.getExitNodeIterator();
IExitNode exit = exitNodeIterator.next(); IExitNode exit = exitNodeIterator.next();
assertEquals("throw 5;", data(exit)); assertEquals("throw 5;", data(exit));
} }
/*-
<code file="test_exit.c"> // int foo() {
int foo() { // exit(0);
exit(0); // }
}
</code>
*/
public void test_exit() { public void test_exit() {
buildAndCheck("test_exit.c"); buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode(); IStartNode startNode = graph.getStartNode();
assertEquals(1, graph.getExitNodeSize()); assertEquals(1, graph.getExitNodeSize());
Iterator<IExitNode> exitNodeIterator = graph.getExitNodeIterator(); Iterator<IExitNode> exitNodeIterator = graph.getExitNodeIterator();
IExitNode exit = exitNodeIterator.next(); IExitNode exit = exitNodeIterator.next();
assertEquals("exit(0);", data(exit)); assertEquals("exit(0);", data(exit));
} }
} }

View file

@ -18,95 +18,71 @@ import org.eclipse.cdt.codan.core.test.CheckerTestCase;
* *
*/ */
public class ExpressionRequiredInReturnCheckerTest extends CheckerTestCase { public class ExpressionRequiredInReturnCheckerTest extends CheckerTestCase {
/*-
<code file="test1.c">
dummy() { // dummy() {
return; // error here on line 2 // return; // error here on line 2
} // }
</code>
*/
public void testDummyFunction() { public void testDummyFunction() {
load("test1.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkNoErrors(); // because return type if not defined, usually people don't care checkNoErrors(); // because return type if not defined, usually people don't care
} }
/*-
<code file="test2.c"> // void void_function(void) {
void void_function(void) { // return; // no error here
return; // no error here // }
}
</code>
*/
public void testVoidFunction() { public void testVoidFunction() {
load("test2.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkNoErrors(); checkNoErrors();
} }
/*-
<code file="test3.c"> // int integer_return_function(void) {
int integer_return_function(void) { // if (global) {
if (global) { // if (global == 100) {
if (global == 100) { // return; // error here on line 4
return; // error here on line 4 // }
} // }
} // }
}
</code>
*/
public void testBasicTypeFunction() { public void testBasicTypeFunction() {
load("test3.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkErrorLine(4); checkErrorLine(4);
} }
/*- //
<code file="test4.c"> // struct My_Struct {
struct My_Struct { // int a;
int a; // };
}; //
// struct My_Struct struct_return_function(void) {
struct My_Struct struct_return_function(void) { // return; // error here on line 6
return; // error here on line 6 // }
}
</code>
*/
public void testUserDefinedFunction() { public void testUserDefinedFunction() {
load("test4.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkErrorLine(6); checkErrorLine(6);
} }
/*-
<code file="test5.c"> // typedef unsigned int uint8_t;
typedef unsigned int uint8_t; //
// uint8_t return_typedef(void) {
uint8_t return_typedef(void) { // return; // error here on line 4
return; // error here on line 4 // }
}
</code>
*/
public void testTypedefReturnFunction() { public void testTypedefReturnFunction() {
load("test5.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkErrorLine(4); checkErrorLine(4);
} }
/*-
<code file="test6.c"> // typedef unsigned int uint8_t;
typedef unsigned int uint8_t; //
// uint8_t (*return_fp_no_typedef(void))(void)
uint8_t (*return_fp_no_typedef(void))(void) // {
{ // return; // error here on line 5
return; // error here on line 5 // }
}
</code>
*/
public void testFunctionPointerReturnFunction() { public void testFunctionPointerReturnFunction() {
load("test6.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkErrorLine(5); checkErrorLine(5);
} }

View file

@ -10,6 +10,9 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.core.internal.checkers; package org.eclipse.cdt.codan.core.internal.checkers;
import java.io.File;
import java.io.IOException;
import org.eclipse.cdt.codan.core.test.CheckerTestCase; import org.eclipse.cdt.codan.core.test.CheckerTestCase;
/** /**
@ -17,84 +20,89 @@ import org.eclipse.cdt.codan.core.test.CheckerTestCase;
* *
*/ */
public class StatementHasNoEffectCheckerTest extends CheckerTestCase { public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
/*- // main() {
<code file="test1.c"> // int a;
main() { // +a; // error here on line 3
int a; // }
+a; // error here on line 3 public void testUnaryExpression() throws IOException {
} loadCodeAndRun(getAboveComment());
</code>
*/
public void testUnaryExpression() {
load("test1.c");
runOnFile();
checkErrorLine(3); checkErrorLine(3);
} }
/*- // main() {
<code file="test2.c"> // int a,b;
main() { //
int a,b; // b+a; // error here on line 4
// }
b+a; // error here on line 4
}
</code>
*/
public void testBinaryExpression() { public void testBinaryExpression() {
load("test2.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkErrorLine(4); checkErrorLine(4);
} }
/*- // main() {
<code file="test3.c"> // int a,b;
main() { //
int a,b; // a=b+a; // no error here
// }
a=b+a; // no error here
}
</code>
*/
public void testNormalAssignment() { public void testNormalAssignment() {
load("test3.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkNoErrors(); checkNoErrors();
} }
/*-
<code file="test4.c"> // main() {
main() { // int a,b;
int a,b; //
// (a=b); // no errors here
(a=b); // no errors here // a+=b;
a+=b; // a<<=b;
a<<=b; // a-=b;
a-=b; // a++;
a++; // b--;
b--; // --a;
--a; // ++b;
++b; // a%=2;
a%=2; // a>>=2;
a>>=2; // }
}
</code>
*/
public void testFalsePositives() { public void testFalsePositives() {
load("test4.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkNoErrors(); checkNoErrors();
} }
/*- // main() {
<code file="test5.c"> // int a;
main() { // a; // error here on line 3
int a; // }
a; // error here on line 3
}
</code>
*/
public void testIdExpression() { public void testIdExpression() {
load("test5.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkErrorLine(3); checkErrorLine(3);
} }
// main() {
// int a=({foo();a;}); // no error here on line 2
// }
public void testGNUExpressionCompoundStmt() {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
/* first file */
// main() {
// int a;
// +a; // error here on line 3
// }
/* second file */
// foo() {
// int a;
//
// +a; // error here on line 4
// }
/* this test is using two files */
public void test2FilesUnaryExpression() throws IOException {
StringBuffer[] code = getContents(2);
File f1 = loadcode(code[0].toString());
File f2 = loadcode(code[1].toString());
runOnProject();
checkErrorLine(f1, 3);
checkErrorLine(f2, 4);
}
} }

View file

@ -17,87 +17,68 @@ import org.eclipse.cdt.codan.core.test.CheckerTestCase;
* *
*/ */
public class SuggestedParenthesisCheckerTest extends CheckerTestCase { public class SuggestedParenthesisCheckerTest extends CheckerTestCase {
/*-
<code file="test1.c"> // main() {
main() { // int a=1,b=3;
int a=1,b=3; // if (!a<10) b=4; // error here on line 3
if (!a<10) b=4; // error here on line 3 // }
}
</code>
*/
public void test1() { public void test1() {
load("test1.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkErrorLine(3); checkErrorLine(3);
} }
/*-
<code file="test2.c"> // main() {
main() { // int a=1,b=3;
int a=1,b=3; //
// if (b+a && a>b || b-a) b--; // error here on line 4
if (b+a && a>b || b-a) b--; // error here on line 4 // }
}
</code>
*/
public void test2() { public void test2() {
load("test2.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkErrorLine(4); checkErrorLine(4);
} }
/*-
<code file="test3.c"> // main() {
main() { // int a=1,b=3;
int a=1,b=3; // if (!(a<10)) b=4; // no error here on line 3
if (!(a<10)) b=4; // no error here on line 3 // }
}
</code>
*/
public void test3() { public void test3() {
load("test3.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkNoErrors(); checkNoErrors();
} }
/*-
<code file="test4.c"> // main() {
main() { // int a=1,b=3;
int a=1,b=3; // if (a && !b) b=4; // no error here on line 3
if (a && !b) b=4; // no error here on line 3 // }
}
</code>
*/
public void test_lastnot() { public void test_lastnot() {
load("test4.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkNoErrors(); checkNoErrors();
} }
/*-
<code file="test5.c"> // main() {
main() { // int a=1,b=3;
int a=1,b=3; // if ((!a) && 10) b=4; // no error here on line 3
if ((!a) && 10) b=4; // no error here on line 3 // }
}
</code>
*/
public void test_fixed() { public void test_fixed() {
load("test5.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkNoErrors(); checkNoErrors();
} }
/*-
<code file="test6.c"> // main() {
main() { // int a=1,b=3;
int a=1,b=3; // if (a && b & a) b=4; // error here on line 3
if (a && b & a) b=4; // error here on line 3 // }
}
</code>
*/
public void test_mixedbin() { public void test_mixedbin() {
load("test6.c"); loadCodeAndRun(getAboveComment());
runOnFile();
checkErrorLine(3); checkErrorLine(3);
} }
} }

View file

@ -10,6 +10,7 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.core.test; package org.eclipse.cdt.codan.core.test;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.eclipse.cdt.codan.core.CodanRuntime; import org.eclipse.cdt.codan.core.CodanRuntime;
@ -24,12 +25,14 @@ import org.eclipse.core.runtime.CoreException;
public class CheckerTestCase extends CodanTestCase { public class CheckerTestCase extends CodanTestCase {
private IMarker[] markers; private IMarker[] markers;
public void checkErrorLine(int i) {
checkErrorLine(currentFile, i);
}
/** /**
* @param i * @param i
* - line * - line
*/ */
public void checkErrorLine(int i) { public void checkErrorLine(File file, int i) {
assertTrue(markers != null); assertTrue(markers != null);
assertTrue(markers.length > 0); assertTrue(markers.length > 0);
boolean found = false; boolean found = false;
@ -48,8 +51,13 @@ public class CheckerTestCase extends CodanTestCase {
} catch (IOException e) { } catch (IOException e) {
fail(e.getMessage()); fail(e.getMessage());
} }
String mfile = m.getResource().getName();
if (line.equals(i)) { if (line.equals(i)) {
found = true; found = true;
if (file!=null && !file.getName().equals(mfile))
found = false;
else
break;
} }
} }
assertTrue("Error on line " + i + " not found ", found); assertTrue("Error on line " + i + " not found ", found);
@ -63,12 +71,27 @@ public class CheckerTestCase extends CodanTestCase {
/** /**
* *
*/ */
public void runOnFile() { public void runOnProject() {
try { try {
loadFiles(); indexFiles();
} catch (CoreException e) { } catch (CoreException e) {
fail(e.getMessage()); fail(e.getMessage());
} }
runCodan();
}
public void loadCodeAndRun(String code) {
loadcode(code);
runOnProject();
}
public void loadCodeAndRunCpp(String code) {
loadcode(code, true);
runOnProject();
}
/**
*
*/
protected void runCodan() {
CodanRuntime.getInstance().getBuilder().processResource( CodanRuntime.getInstance().getBuilder().processResource(
cproject.getProject(), NPM); cproject.getProject(), NPM);
try { try {

View file

@ -10,17 +10,20 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.core.test; package org.eclipse.cdt.codan.core.test;
import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import junit.framework.AssertionFailedError;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager; import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper; import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase; import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspace;
@ -54,7 +57,7 @@ public class CodanTestCase extends BaseTestCase {
} }
/** /**
* Override for c++ * Override for c++ (i.e. at least one c++ test)
* *
* @return is c++ tests * @return is c++ tests
*/ */
@ -69,32 +72,6 @@ public class CodanTestCase extends BaseTestCase {
tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile(); tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile();
} }
public File load(String file) {
Class clazz = getClass();
InputStream st = null;
try {
st = TestUtils.getJavaFileText(clazz);
} catch (IOException e) {
fail("Cannot find java file: " + clazz + ": " + e.getMessage());
}
try {
File testFile = new File(tmpDir, file);
tempFiles.add(testFile);
TestUtils.saveFile(st, testFile);
st.close();
currentFile = testFile;
try {
cproject.getProject().refreshLocal(1, null);
} catch (CoreException e) {
// hmm
}
return testFile;
} catch (IOException e) {
fail("Cannot save test: " + file + ": " + e.getMessage());
return null;
}
}
public void tearDown() throws CoreException { public void tearDown() throws CoreException {
if (cproject != null) { if (cproject != null) {
try { try {
@ -102,7 +79,6 @@ public class CodanTestCase extends BaseTestCase {
IResource.FORCE IResource.FORCE
| IResource.ALWAYS_DELETE_PROJECT_CONTENT, | IResource.ALWAYS_DELETE_PROJECT_CONTENT,
new NullProgressMonitor()); new NullProgressMonitor());
} catch (CoreException e) { } catch (CoreException e) {
throw e; throw e;
} }
@ -156,7 +132,7 @@ public class CodanTestCase extends BaseTestCase {
return cprojects[0]; return cprojects[0];
} }
protected void loadFiles() throws CoreException { protected void indexFiles() throws CoreException {
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 {
@ -167,7 +143,7 @@ public class CodanTestCase extends BaseTestCase {
CCorePlugin.getIndexManager().setIndexerId(cproject, CCorePlugin.getIndexManager().setIndexerId(cproject,
IPDOMManager.ID_FAST_INDEXER); IPDOMManager.ID_FAST_INDEXER);
// wait until the indexer is done // wait until the indexer is done
assertTrue(CCorePlugin.getIndexManager().joinIndexer(1000*60, // 1 min assertTrue(CCorePlugin.getIndexManager().joinIndexer(1000 * 60, // 1 min
new NullProgressMonitor())); new NullProgressMonitor()));
return; return;
} }
@ -190,10 +166,75 @@ public class CodanTestCase extends BaseTestCase {
return line; return line;
cur++; cur++;
} }
;
} finally { } finally {
st.close(); st.close();
} }
return 0; return 0;
} }
protected String getAboveComment() {
return getContents(1)[0].toString();
}
protected StringBuffer[] getContents(int sections) {
try {
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
if (plugin == null)
throw new AssertionFailedError(
"This test must be run as a JUnit plugin test");
return TestSourceReader.getContentsForTest(plugin.getBundle(),
"src", getClass(), getName(), sections);
} catch (IOException e) {
fail(e.getMessage());
return null;
}
}
public File loadcode(String code, boolean cpp) {
String ext = cpp ? ".cc" : ".c"; //$NON-NLS-1$ //$NON-NLS-2$
File testFile = null;
try {
testFile = File.createTempFile("test", ext, tmpDir); //$NON-NLS-1$
} catch (IOException e1) {
fail(e1.getMessage());
return null;
}
return loadcode(code, testFile);
}
public File loadcode(String code, String filename) {
File testFile = new File(tmpDir, filename);
return loadcode(code, testFile);
}
private File loadcode(String code, File testFile) {
try {
tempFiles.add(testFile);
TestUtils.saveFile(
new ByteArrayInputStream(code.trim().getBytes()), testFile);
currentFile = testFile;
try {
cproject.getProject().refreshLocal(1, null);
} catch (CoreException e) {
// hmm
fail(e.getMessage());
}
return testFile;
} catch (IOException e) {
fail("Cannot save test: " + testFile + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
}
public File loadcode_c(String code) {
return loadcode(code, true);
}
public File loadcode_cpp(String code) {
return loadcode(code, false);
}
public File loadcode(String code) {
return loadcode(code, isCpp());
}
} }

View file

@ -7,8 +7,7 @@
* *
* Contributors: * Contributors:
* Alena Laskavaia - initial API and implementation * Alena Laskavaia - initial API and implementation
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.codan.core.test; package org.eclipse.cdt.codan.core.test;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -19,42 +18,19 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintStream; import java.io.PrintStream;
import java.net.URL; import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* TODO: add description * TODO: add description
*/ */
public class TestUtils { public class TestUtils {
static final Pattern filePattern = Pattern.compile("file=\"(.*)\"");
/**
* @param st
* @param testFile
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static File saveFile(InputStream st, File testFile) public static File saveFile(InputStream st, File testFile)
throws FileNotFoundException, IOException { throws FileNotFoundException, IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(st)); BufferedReader r = new BufferedReader(new InputStreamReader(st));
String line; String line;
PrintStream wr = new PrintStream(testFile); PrintStream wr = new PrintStream(testFile);
try { try {
boolean print = false;
while ((line = r.readLine()) != null) { while ((line = r.readLine()) != null) {
if (line.contains("<code ")) { wr.println(line);
Matcher m = filePattern.matcher(line);
if (m.find()) {
String userFile = m.group(1);
if (userFile.equals(testFile.getName())) {
print = true;
}
}
} else if (line.contains("</code>")) {
print = false;
} else if (print) {
wr.println(line);
}
} }
} finally { } finally {
wr.close(); wr.close();
@ -67,20 +43,19 @@ public class TestUtils {
* @return * @return
* @throws IOException * @throws IOException
*/ */
public static InputStream getJavaFileText(Class clazz) throws IOException { public static InputStream getJavaFileText(String srcRoot, Class clazz)
throws IOException {
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault(); CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
String classFile = clazz.getName().replaceAll("\\.", "/"); String classFile = clazz.getName().replaceAll("\\.", "/");
classFile += ".java"; classFile += ".java";
InputStream st = null; InputStream st = null;
if (plugin != null) {
if (plugin != null) { URL resource = plugin.getBundle().getResource(
URL resource = plugin.getBundle().getResource( srcRoot + "/" + classFile);
"src/" + classFile); st = resource.openStream();
st = resource.openStream(); } else {
} else { st = clazz.getResourceAsStream(classFile);
st = clazz.getResourceAsStream(classFile); }
}
return st; return st;
} }
} }