added new method to CPD to fetch results

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@566 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-07-31 14:52:20 +00:00
parent 4d0b843d65
commit e5f107fb32
2 changed files with 78 additions and 12 deletions

View File

@ -0,0 +1,52 @@
/*
* User: tom
* Date: Jul 31, 2002
* Time: 10:41:25 AM
*/
package test.net.sourceforge.pmd.cpd;
import junit.framework.TestCase;
import net.sourceforge.pmd.cpd.*;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
public class CPDTest extends TestCase{
public CPDTest(String name) {
super(name);
}
public void testBasic() throws Throwable {
CPD cpd = new CPD();
cpd.add("1", "helloworld");
cpd.add("2", "hellothere");
cpd.go(5);
Occurrences occs = cpd.getResults();
Iterator i = occs.getOccurrences(new Tile(getHelloTokens()));
assertTrue(i.hasNext());
Occurrence occ = (Occurrence)i.next();
if (occ.getTokenSetID().equals("1")) {
assertEquals(0, occ.getIndex());
} else {
assertEquals("2", occ.getTokenSetID());
assertEquals(0, occ.getIndex());
}
}
private List getHelloTokens() {
List tokens = new ArrayList();
Token tok = new Token('h', 0, "1");
tokens.add(tok);
Token tok1 = new Token('e', 1, "1");
tokens.add(tok1);
Token tok3 = new Token('l', 2, "1");
tokens.add(tok3);
Token tok4 = new Token('l', 3, "1");
tokens.add(tok4);
Token tok5 = new Token('o', 4, "1");
tokens.add(tok5);
return tokens;
}
}

View File

@ -20,11 +20,16 @@ public class CPD {
private TokenSets tokenSets = new TokenSets();
private Occurrences occ = new Occurrences();
private Occurrences results;
public void add(File file) throws IOException {
System.out.println("Adding file " + file);
Tokenizer t = new Tokenizer();
TokenSet ts = new TokenSet(file.getAbsolutePath());
t.tokenize(ts, new FileReader(file));
FileReader fr = new FileReader(file);
t.tokenize(ts, fr);
fr.close();
tokenSets.add(ts);
}
@ -39,15 +44,11 @@ public class CPD {
generateInitialOccurrenceTable();
GST gst = new GST(this.tokenSets, this.occ, minimumTileSize);
gst.crunch();
Occurrences occ = gst.getResults();
for (Iterator i = occ.getTiles(); i.hasNext();) {
Tile tile = (Tile)i.next();
System.out.println(tile.getImage());
System.out.println("[Source,Location]");
for (Iterator j = occ.getOccurrences(tile); j.hasNext();) {
System.out.println(j.next());
}
}
results = gst.getResults();
}
public Occurrences getResults() {
return results;
}
public String toString() {
@ -72,7 +73,12 @@ public class CPD {
cpd.add(new File("c:\\data\\pmd\\pmd\\test-data\\Unused2.java"));
cpd.add(new File("c:\\data\\pmd\\pmd\\test-data\\Unused3.java"));
*/
List files = findFilesRecursively("c:\\data\\pmd\\pmd\\test-data\\");
/*
List files = findFilesRecursively("c:\\data\\cougaar\\core\\src\\org\\cougaar\\core\\adaptivity");
files.addAll(findFilesRecursively("c:\\data\\cougaar\\core\\src\\org\\cougaar\\core\\agent"));
files.addAll(findFilesRecursively("c:\\data\\cougaar\\core\\src\\org\\cougaar\\core\\blackboard"));
*/
List files = findFilesRecursively("c:\\data\\cougaar\\core\\src\\org\\");
for (Iterator i = files.iterator(); i.hasNext();) {
cpd.add((File)i.next());
}
@ -80,7 +86,15 @@ public class CPD {
ioe.printStackTrace();
return;
}
cpd.go(40);
cpd.go(150);
for (Iterator i = cpd.getResults().getTiles(); i.hasNext();) {
Tile tile = (Tile)i.next();
System.out.println(tile.getImage());
System.out.println("[Source,Location]");
for (Iterator j = cpd.getResults().getOccurrences(tile); j.hasNext();) {
System.out.println(j.next());
}
}
}
private static List findFilesRecursively(String dir) {