From e5f107fb3262869b99f2ab39a997117ac8b5eba3 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Wed, 31 Jul 2002 14:52:20 +0000 Subject: [PATCH] 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 --- .../test/net/sourceforge/pmd/cpd/CPDTest.java | 52 +++++++++++++++++++ pmd/src/net/sourceforge/pmd/cpd/CPD.java | 38 +++++++++----- 2 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 pmd/regress/test/net/sourceforge/pmd/cpd/CPDTest.java diff --git a/pmd/regress/test/net/sourceforge/pmd/cpd/CPDTest.java b/pmd/regress/test/net/sourceforge/pmd/cpd/CPDTest.java new file mode 100644 index 0000000000..f830829053 --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/cpd/CPDTest.java @@ -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; + } + +} diff --git a/pmd/src/net/sourceforge/pmd/cpd/CPD.java b/pmd/src/net/sourceforge/pmd/cpd/CPD.java index 259a22deef..b92839d99e 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/CPD.java +++ b/pmd/src/net/sourceforge/pmd/cpd/CPD.java @@ -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) {