Skip to content
This repository has been archived by the owner on Oct 31, 2021. It is now read-only.

Commit

Permalink
getting ready to test using testng
Browse files Browse the repository at this point in the history
  • Loading branch information
tenorviol committed Dec 31, 2011
1 parent 91397ab commit e307c58
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/build
*.class
/test/report
2 changes: 1 addition & 1 deletion bin/git-search
Expand Up @@ -27,7 +27,7 @@ COMMAND=$1
if [ -z $COMMAND ]; then
help
elif [ $COMMAND == '--index' ]; then
java com.github.tenorviol.gitsearch.IndexFiles -index $INDEX -docs $REPO_BASE
java com.github.tenorviol.gitsearch.IndexFiles --index $INDEX index $REPO_BASE
else
java com.github.tenorviol.gitsearch.SearchFiles -index $INDEX -query $COMMAND
fi
28 changes: 16 additions & 12 deletions build.xml
Expand Up @@ -5,17 +5,25 @@
<property name="build.dir" value="build"/>
<property name="lib.dir" value="lib"/>
<property name="test.dir" value="test"/>
<property name="junit.jar" value="/usr/local/junit/junit.jar"/>
<property name="testng.jar" value="/usr/local/lib/java/testng.jar"/>

<property name="main-class" value="com.github.tenorviol.gitsearch.GitSearch"/>

<taskdef resource="testngtasks" classpath="${testng.jar}"/>

<path id="classpath">
<fileset dir="${lib.dir}" includes="*.jar"/>
<pathelement location="${junit.jar}" />
</path>

<path id="test-classpath">
<path refid="classpath"/>
<pathelement location="${test.dir}"/>
<pathelement location="${testng.jar}"/>
</path>

<target name="clean">
<delete dir="build"/>
<delete dir="{$test.dir}/report"/>
<delete>
<fileset dir="." includes="**/*.class"/>
<fileset dir="${lib.dir}" includes="gitsearch-*.jar"/>
Expand All @@ -33,16 +41,12 @@
</target>

<target name="test" depends="compile">
<javac srcdir="${test.dir}" classpathref="classpath" includeantruntime="false"/>
<junit printsummary="yes">
<classpath>
<path refid="classpath"/>
</classpath>

<batchtest fork="yes">
<fileset dir="${test.dir}" includes="*Test.java"/>
</batchtest>
</junit>
<javac srcdir="${test.dir}" classpathref="test-classpath" includeantruntime="false"/>
<mkdir dir="${test.dir}/report"/>

<testng classpathref="test-classpath" outputDir="${test.dir}/report" haltOnFailure="true" verbose="2">
<classfileset dir="${test.dir}" includes="**/*Test*.class" />
</testng>
</target>

<target name="run" depends="compile">
Expand Down
Binary file modified lib/gitsearch-0.1.jar
Binary file not shown.
8 changes: 6 additions & 2 deletions makefile
@@ -1,3 +1,7 @@
ant:
ant compile

src/com/github/tenorviol/gitsearch/*.class: src/com/github/tenorviol/gitsearch/*.java
javac -classpath lucene-core-3.5.0.jar src/com/github/tenorviol/gitsearch/*.java
test: ant_test

ant_test:
ant test
66 changes: 66 additions & 0 deletions src/CommandLine.java
@@ -0,0 +1,66 @@
package com.github.tenorviol.gitsearch;

import java.util.Arrays;
import java.util.List;

public class CommandLine {
public String indexPath;
public String command;
public List<String> commandArgs;

public CommandLine(String[] args) throws IllegalArgumentException {
List<String> list = Arrays.asList(args);

parseCommand(list);

if ("index".equals(command)) {
parseIndexArgs();
} else if ("search".equals(command)) {
parseSearchArgs();
} else {
throw new IllegalArgumentException("Unknown command, '" + command + "'");
}
}

private void parseCommand(List<String> args) throws IllegalArgumentException {
if (!"--index".equals(args.get(0)) || args.size() < 2) {
throw new IllegalArgumentException("--index <path> required");
}
indexPath = args.get(1);

if (args.size() < 3) {
throw new IllegalArgumentException("No command given");
}
command = args.get(2);
commandArgs = args.subList(3, args.size());
}

private void parseIndexArgs() throws IllegalArgumentException {
if (commandArgs.size() == 0) {
throw new IllegalArgumentException("No path(s) given");
}
}

private void parseSearchArgs() throws IllegalArgumentException {
if (commandArgs.size() == 0) {
throw new IllegalArgumentException("No search query given");
}
}

public static void help() {
System.out.println("java com.github.tenorviol.gitsearch.CommandLine --index <index path> index <path(s)>");
//String usage = "java org.apache.lucene.demo.IndexFiles"
// + " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
// + "This indexes the documents in DOCS_PATH, creating a Lucene index"
// + "in INDEX_PATH that can be searched with SearchFiles";
System.out.println("java com.github.tenorviol.gitsearch.CommandLine --index <index path> search <query term(s)>");
//String usage =
// "Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/java/4_0/demo.html for details.";
}

public static void main(String[] args) throws IllegalArgumentException {
help();
CommandLine cl = new CommandLine(args);
System.out.println(cl);
}
}
20 changes: 20 additions & 0 deletions src/GitSearch.java
@@ -0,0 +1,20 @@
package com.github.tenorviol.gitsearch;

class GitSearch {
public static void main(String[] args) {
try {
CommandLine cl = new CommandLine(args);

if ("index".equals(cl.command)) {
IndexFiles.index(cl);
} else {
SearchFiles.search(cl);
}

} catch (Exception e) {
System.out.println(e.getMessage());
CommandLine.help();
System.exit(1);
}
}
}
Expand Up @@ -49,42 +49,21 @@ public class IndexFiles {
private IndexFiles() {}

/** Index all text files under a directory. */
public static void main(String[] args) {
String usage = "java org.apache.lucene.demo.IndexFiles"
+ " [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\n"
+ "This indexes the documents in DOCS_PATH, creating a Lucene index"
+ "in INDEX_PATH that can be searched with SearchFiles";
String indexPath = "index";
String docsPath = null;
public static void index(CommandLine cl) {
boolean create = true;
for(int i=0;i<args.length;i++) {
if ("-index".equals(args[i])) {
indexPath = args[i+1];
i++;
} else if ("-docs".equals(args[i])) {
docsPath = args[i+1];
i++;
} else if ("-update".equals(args[i])) {
create = false;
}
}

if (docsPath == null) {
System.err.println("Usage: " + usage);
System.exit(1);
}

final File docDir = new File(docsPath);
// TODO: multi files
final File docDir = new File(cl.commandArgs.get(0));
if (!docDir.exists() || !docDir.canRead()) {
System.out.println("Document directory '" +docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path");
System.out.println("Document directory '" + docDir.getAbsolutePath() + "' does not exist or is not readable, please check the path");
System.exit(1);
}

Date start = new Date();
try {
System.out.println("Indexing to directory '" + indexPath + "'...");
System.out.println("Indexing to directory '" + cl.indexPath + "'...");

Directory dir = FSDirectory.open(new File(indexPath));
Directory dir = FSDirectory.open(new File(cl.indexPath));
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_31, analyzer);

Expand Down
Expand Up @@ -42,14 +42,7 @@ public class SearchFiles {
private SearchFiles() {}

/** Simple command-line based search demo. */
public static void main(String[] args) throws Exception {
String usage =
"Usage:\tjava org.apache.lucene.demo.SearchFiles [-index dir] [-field f] [-repeat n] [-queries file] [-query string] [-raw] [-paging hitsPerPage]\n\nSee http://lucene.apache.org/java/4_0/demo.html for details.";
if (args.length > 0 && ("-h".equals(args[0]) || "-help".equals(args[0]))) {
System.out.println(usage);
System.exit(0);
}

public static void search(CommandLine cl) throws Exception {
String index = "index";
String field = "contents";
String queries = null;
Expand All @@ -58,35 +51,7 @@ public static void main(String[] args) throws Exception {
String queryString = null;
int hitsPerPage = 10;

for(int i = 0;i < args.length;i++) {
if ("-index".equals(args[i])) {
index = args[i+1];
i++;
} else if ("-field".equals(args[i])) {
field = args[i+1];
i++;
} else if ("-queries".equals(args[i])) {
queries = args[i+1];
i++;
} else if ("-query".equals(args[i])) {
queryString = args[i+1];
i++;
} else if ("-repeat".equals(args[i])) {
repeat = Integer.parseInt(args[i+1]);
i++;
} else if ("-raw".equals(args[i])) {
raw = true;
} else if ("-paging".equals(args[i])) {
hitsPerPage = Integer.parseInt(args[i+1]);
if (hitsPerPage <= 0) {
System.err.println("There must be at least 1 hit per page.");
System.exit(1);
}
i++;
}
}

IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));
IndexReader reader = IndexReader.open(FSDirectory.open(new File(cl.indexPath)));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_31);

Expand Down
7 changes: 0 additions & 7 deletions src/com/github/tenorviol/gitsearch/GitSearch.java

This file was deleted.

18 changes: 18 additions & 0 deletions test/CommandLineTest.java
@@ -0,0 +1,18 @@
import org.testng.Assert;
import org.testng.annotations.*;

import com.github.tenorviol.gitsearch.CommandLine;

public class CommandLineTest {

@Test
public void testFoo() {
String[] args = {"--index", "./foo/index", "index"};
try {
CommandLine cl = new CommandLine(args);
} catch (Exception e) {
System.out.println(e);
}
Assert.assertEquals(1,1);
}
}
10 changes: 0 additions & 10 deletions test/HelloWorldTest.java

This file was deleted.

0 comments on commit e307c58

Please sign in to comment.