//==============================================================================
// Test.java
//==============================================================================
package tribble.search.disk;
// System imports
import java.io.PrintStream;
import java.lang.Exception;
import java.lang.String;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
// Local imports
import tribble.search.DocumentI;
import tribble.search.DocumentSearcherI;
import tribble.search.disk.FDirectory;
/*******************************************************************************
* Test driver program for this package.
*
* @version $Revision: 1.3 $ $Date: 2001/06/18 03:05:16 $
* @since 2001-05-11
* @author
* David R. Tribble
* (david@tribble.com).
*
* Copyright
* ©2001 by David R. Tribble, all rights reserved.
*
* @see FDirectory
*/
class Test
{
// Identification
/** Revision information. */
static final String REV =
"@(#)tribble/search/disk/Test.java $Revision: 1.3 $ $Date: 2001/06/18 03:05:16 $\n";
/** This package name. */
static final String PACKAGE =
"tribble.search.disk";
/** This class name. */
static final String CLASS =
"Test";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public constants
/** Series number. */
public static final int SERIES = 100;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Private constants
//----------------------------------
// Program termination codes.
private static final int RC_OKAY = 0;
private static final int RC_USAGE = 255;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Private variables
/** Directory file searcher. */
private DocumentSearcherI m_searcher;
/** Date formatter. */
private SimpleDateFormat m_sdf =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public static methods
/***************************************************************************
* Test driver.
*
* @param args
* Command line arguments.
*
* @since 1.1, 2001-05-11
*/
public static void main(String[] args)
{
// Execute this test driver
(new Test()).run(args);
System.exit(RC_OKAY);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Private constructors
/***************************************************************************
* Default constructor.
*
* @since 1.1, 2001-05-11
*/
private Test()
{
// Do nothing
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Private methods
/***************************************************************************
* Display a program usage message, then punt.
*
* @return
* Doesn't, but calls System.exit() instead.
*
* @since 1.1, 2001-05-11
*/
private void usage()
{
// Display a program usage message
System.out.println("Test driver for " + PACKAGE);
System.out.println();
System.out.println("usage: java " + PACKAGE + "." + CLASS
+ " dir...");
System.out.println();
// Punt
System.exit(RC_USAGE);
}
/***************************************************************************
* Test driver.
*
* @param args
* Command line arguments.
*
* @since 1.1, 2001-05-11
*/
private void run(String[] args)
{
int totalCount;
// Search each command line arg as a file directory
totalCount = 0;
for (int i = 0; i < args.length; i++)
{
int count;
String dirname;
Enumeration iter;
count = 0;
try
{
// Get the directory name to search
dirname = args[i];
if (i > 0)
System.out.println();
System.out.print(dirname);
System.out.println(":");
// Set up to search the next file directory
m_searcher = new FDirectory();
m_searcher.initialize(null);
m_searcher.open(dirname);
// Search the file directory
iter = m_searcher.find(null);
while (iter != null && iter.hasMoreElements())
{
DocumentI doc;
// Get the next file
doc = (DocumentI) iter.nextElement();
count++;
totalCount++;
// Display info about the file entry
showInfo(System.out, doc);
}
// Clean up
if (m_searcher != null)
{
m_searcher.close();
m_searcher = null;
}
}
catch (Exception ex)
{
// An exception occurred
System.out.println();
System.out.println("*** Exception: " + ex);
}
// Display a summary
System.out.println("Entries: " + count);
}
// Display a summary
if (args.length > 1)
{
System.out.println();
System.out.println("Total: " + totalCount);
}
}
/***************************************************************************
* Display information about a file.
*
* @param out
* An output stream to write to.
*
* @param ent
* A file entry.
*
* @since 1.1, 2001-05-11
*/
private void showInfo(PrintStream out, DocumentI ent)
throws Exception
{
long len;
Date time;
// Display info about the file
out.print(ent.isDirectory() ? 'd' : '-');
out.print(ent.canRead() ? 'r' : '-');
out.print(ent.canWrite() ? 'w' : '-');
out.print(' ');
len = ent.length();
for (long p = 1000000000; p > 1; p /= 10)
{
if (len < p)
out.print(' ');
else
break;
}
out.print(len);
out.print(" ");
time = ent.lastModified();
out.print(m_sdf.format(time));
out.print(" ");
out.print(ent.getName());
out.println();
}
}
// End Test.java