Test revamp in progress.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2009 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2008-08-25 09:25:58 +00:00
parent 65a48e78a1
commit a29e4450da
35 changed files with 101 additions and 64 deletions
@@ -0,0 +1,43 @@
//
// $Id$
package com.samskivert.jdbc.tests;
import java.sql.Date;
import java.sql.Timestamp;
import com.samskivert.jdbc.depot.annotation.Column;
import com.samskivert.jdbc.depot.annotation.Entity;
import com.samskivert.jdbc.depot.annotation.Id;
import com.samskivert.jdbc.depot.annotation.Index;
import com.samskivert.util.StringUtil;
/**
* A test persistent object.
*/
@Entity(indices={ @Index(name="createdIndex", columns={"created"}) })
public class TestRecord
{
public static final int SCHEMA_VERSION = 1;
@Id
public int recordId;
@Column(nullable=false)
public String name;
@Column(nullable=false)
public int age;
@Column(nullable=false)
public Date created;
@Column(nullable=false)
public Timestamp lastModified;
public String toString ()
{
return StringUtil.fieldsToString(this);
}
}
@@ -0,0 +1,45 @@
//
// $Id$
package com.samskivert.jdbc.tests;
import java.sql.Date;
import java.sql.Timestamp;
import com.samskivert.jdbc.ConnectionProvider;
import com.samskivert.jdbc.StaticConnectionProvider;
import com.samskivert.jdbc.depot.DepotRepository;
/**
* A test tool for the Depot repository services.
*/
public class TestRepository extends DepotRepository
{
public static void main (String[] args)
throws Exception
{
TestRepository repo = new TestRepository(
new StaticConnectionProvider("depot.properties"));
repo.delete(TestRecord.class, 0);
TestRecord record = new TestRecord();
record.name = "Elvis";
record.age = 99;
record.created = new Date(System.currentTimeMillis());
record.lastModified = new Timestamp(System.currentTimeMillis());
repo.insert(record);
System.out.println(repo.load(TestRecord.class, record.recordId));
record.age = 25;
record.name = "Bob";
repo.update(record, "age");
System.out.println(repo.load(TestRecord.class, record.recordId));
}
public TestRepository (ConnectionProvider conprov)
{
super(conprov);
}
}
@@ -0,0 +1,156 @@
//
// $Id: SiteResourceLoaderTest.java,v 1.7 2004/02/25 13:21:41 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.servlet.tests;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import junit.framework.Test;
import junit.framework.TestCase;
import org.apache.commons.io.IOUtils;
import com.samskivert.servlet.Site;
import com.samskivert.servlet.SiteIdentifier;
import com.samskivert.servlet.SiteResourceLoader;
import com.samskivert.test.TestUtil;
public class SiteResourceLoaderTest extends TestCase
{
public SiteResourceLoaderTest ()
{
super(SiteResourceLoaderTest.class.getName());
}
public void runTest ()
{
// we need to fake a couple of things to get the test to work
TestSiteIdentifier ident = new TestSiteIdentifier();
// now create a resource loader and load up some resources
SiteResourceLoader loader = new SiteResourceLoader(
ident, TestUtil.getResourcePath("rsrc/servlet/srl"));
try {
testResourceLoader(SiteIdentifier.DEFAULT_SITE_ID,
loader, "defaultout.txt");
testResourceLoader(SITE1_ID, loader, "site1out.txt");
testResourceLoader(SITE2_ID, loader, "site2out.txt");
} catch (IOException ioe) {
ioe.printStackTrace();
fail("Caught exception while testing resource loader.");
}
}
protected void testResourceLoader (
int siteId, SiteResourceLoader loader, String compareFile)
throws IOException
{
StringBuffer gen = new StringBuffer();
appendResource(siteId, gen, loader, "/header.txt");
appendResource(siteId, gen, loader, "/body.txt");
appendResource(siteId, gen, loader, "/footer.txt");
StringBuffer cmp = new StringBuffer();
compareFile = "rsrc/servlet/srl/" + compareFile;
InputStream cin = TestUtil.getResourceAsStream(compareFile);
if (cin == null) {
throw new IOException("Unable to load " + compareFile);
}
cmp.append(IOUtils.toString(cin));
// Log.info("Loaded resources [cmp=" + compareFile + "]: " + gen);
// now make sure the strings match
assertEquals("Testing " + compareFile,
cmp.toString(), gen.toString());
}
protected void appendResource (int siteId, StringBuffer buffer,
SiteResourceLoader loader, String path)
throws IOException
{
InputStream rin = null;
// load up the site-specific resource
try {
rin = loader.getResourceAsStream(siteId, path);
} catch (FileNotFoundException fnfe) {
// fall through
}
if (rin == null) {
// fall back to the "default" resource if we couldn't load a
// site-specific version
String rpath = "rsrc/servlet/srl/default/" + path;
rpath = TestUtil.getResourcePath(rpath);
rin = new FileInputStream(rpath);
}
buffer.append(IOUtils.toString(rin));
}
public static Test suite ()
{
return new SiteResourceLoaderTest();
}
public static class TestSiteIdentifier implements SiteIdentifier
{
public int identifySite (HttpServletRequest req)
{
return DEFAULT_SITE_ID;
}
public String getSiteString (int siteId)
{
switch (siteId) {
case SITE1_ID: return "site1";
case SITE2_ID: return "site2";
default: return DEFAULT_SITE_STRING;
}
}
public int getSiteId (String siteString)
{
if ("site1".equals(siteString)) {
return SITE1_ID;
} else if ("site2".equals(siteString)) {
return SITE2_ID;
} else {
return DEFAULT_SITE_ID;
}
}
public Iterator<Site> enumerateSites ()
{
return null; // not used
}
}
protected static final int SITE1_ID = 1;
protected static final int SITE2_ID = 2;
}
@@ -0,0 +1,50 @@
//
// $Id: AdjustTestApp.java,v 1.2 2003/01/15 03:24:53 mdb Exp $
package com.samskivert.swing.tests;
import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import com.samskivert.swing.RuntimeAdjust;
import com.samskivert.util.Config;
/**
* Does something extraordinary.
*/
public class AdjustTestApp
{
public static void main (String[] args)
{
Config config = new Config("test");
new RuntimeAdjust.IntAdjust(
"This is a test adjustment. It is nice.",
"samskivert.test.int_adjust1", config, 5);
new RuntimeAdjust.IntAdjust(
"This is another test adjustment. It is nice.",
"samskivert.thwack.int_adjust2", config, 15);
new RuntimeAdjust.BooleanAdjust(
"This is a test adjustment. It is nice.",
"samskivert.thwack.boolean_adjust1", config, true);
new RuntimeAdjust.IntAdjust(
"This is an other test adjustment. It is nice.",
"otherpackage.test.int_adjust2", config, 15);
new RuntimeAdjust.BooleanAdjust(
"This is a an other test adjustment. It is nice.",
"otherpackage.test.boolean_adjust1", config, false);
new RuntimeAdjust.EnumAdjust(
"This is yet an other test adjustment.",
"otherpackage.test.enum_adjust1", config,
new String[] { "debug", "info", "warning" }, "info");
JFrame frame = new JFrame();
((JComponent)frame.getContentPane()).setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
frame.getContentPane().add(RuntimeAdjust.createAdjustEditor(), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
}
@@ -0,0 +1,133 @@
//
// $Id: LabelDemo.java,v 1.10 2004/02/25 13:22:25 mdb Exp $
package com.samskivert.swing.tests;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import com.samskivert.swing.Label;
public class LabelDemo extends JPanel
{
public LabelDemo ()
{
// create our labels
String text = "The quick brown fox jumped over the lazy dog. " +
"He then popped into the butcher's and picked up some mutton.";
Font font = new Font("Courier", Font.PLAIN, 10);
int idx = 0;
_labels[idx] = new Label("\u307e\u305b\u3002Amores\u30d1\u30a4\u30e9");
// _labels[idx].setStyle(Label.OUTLINE);
_labels[idx].setAlternateColor(Color.green);
_labels[idx].setFont(new Font("Dialog", Font.PLAIN, 10));
_labels[idx++].setAlignment(Label.LEFT);
_labels[idx] = new Label(text);
// _labels[idx].setFont(font);
_labels[idx].setTargetWidth(110);
_labels[idx].setAlignment(Label.RIGHT);
_labels[idx].setAlternateColor(Color.lightGray);
_labels[idx].setStyle(Label.SHADOW);
_labels[idx++].setFont(new Font("Dialog", Font.PLAIN, 12));
text = "\u306e\u6d77\u306b\u884c\u3063\u3089\u3057\u3083\u3044\u307e" +
"\u305b\u3002Periwinkle\u30d1\u30a4\u30e9\u30c8\u3092\u9078" +
"\u3073\u51fa\u3057\u3066\u4e0b\u3055\u3044\u3002";
_labels[idx] = new Label(text);
// _labels[idx].setFont(font);
_labels[idx].setTargetHeight(30);
_labels[idx].setAlignment(Label.CENTER);
_labels[idx].setAlternateColor(Color.lightGray);
_labels[idx].setStyle(Label.BOLD);
_labels[idx++].setFont(new Font("Dialog", Font.PLAIN, 12));
_labels[idx] = new Label(text);
// _labels[idx].setFont(font);
_labels[idx].setFont(new Font("Dialog", Font.PLAIN, 11));
_labels[idx++].setGoldenLayout();
try {
File file = new File("delarobb.TTF");
if (file.exists()) {
InputStream in = new FileInputStream(file);
Font sfont = Font.createFont(Font.TRUETYPE_FONT, in);
in.close();
_labels[idx++] = new Label(String.valueOf(10), Label.OUTLINE,
Color.pink, Color.black,
sfont.deriveFont(Font.PLAIN, 24));
_labels[idx++] = new Label(String.valueOf(30), Label.OUTLINE,
Color.pink, Color.black,
sfont.deriveFont(Font.PLAIN, 24));
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
public void doLayout ()
{
super.doLayout();
// layout our labels
Graphics2D g = (Graphics2D)getGraphics();
for (int ii = 0; ii < _labels.length; ii++) {
if (_labels[ii] != null) {
_labels[ii].layout(g);
System.out.println("l" + ii + ": " + _labels[ii].getSize());
}
}
}
public void paintComponent (Graphics g)
{
super.paintComponent(g);
// render our labels
Graphics2D g2 = (Graphics2D)g;
Dimension size;
int x = 10, y = 10;
for (int ii = 0; ii < _labels.length; ii++) {
if (_labels[ii] == null) {
continue;
}
size = _labels[ii].getSize();
g2.setColor(Color.white);
switch (ii) {
case 0: break;
case 1: g2.fillRect(x, y, 110, size.height); break;
case 2: g2.fillRect(x, y, size.width, 30); break;
case 3: break;
}
g2.setColor(Color.gray);
g2.fillRect(x, y, size.width, size.height);
g2.setColor(Color.black);
_labels[ii].render(g2, x, y);
y += size.height + 10;
}
}
public Dimension getPreferredSize ()
{
return new Dimension(400, 300);
}
public static void main (String[] args)
{
JFrame frame = new JFrame("Label Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
LabelDemo demo = new LabelDemo();
frame.getContentPane().add(demo);
frame.pack();
frame.show();
}
protected Label[] _labels = new Label[10];
}
@@ -0,0 +1,47 @@
//
// $Id: TestComboButtonBox.java,v 1.1 2002/03/10 05:10:37 mdb Exp $
package com.samskivert.swing.tests;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import com.samskivert.swing.ComboButtonBox;
/**
* Tests the image button box.
*/
public class TestComboButtonBox
{
protected static Image createImage (Color color)
{
BufferedImage img = new BufferedImage(24, 24, BufferedImage.TYPE_INT_ARGB);
Graphics g = img.getGraphics();
g.setColor(color);
g.fillRect(0, 0, 24, 24);
g.dispose();
return img;
}
public static void main (String[] args)
{
JFrame frame = new JFrame("Test ComboButtonBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(createImage(Color.blue));
model.addElement(createImage(Color.green));
model.addElement(createImage(Color.red));
model.addElement(createImage(Color.yellow));
ComboButtonBox box = new ComboButtonBox(ComboButtonBox.HORIZONTAL, model);
frame.getContentPane().add(box);
frame.pack();
frame.show();
}
}
@@ -0,0 +1,104 @@
//
// $Id: ProximityTrackerTest.java,v 1.3 2004/02/25 13:21:41 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.swing.tests;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.swing.util.ProximityTracker;
public class ProximityTrackerTest extends TestCase
{
public ProximityTrackerTest ()
{
super(ProximityTrackerTest.class.getName());
}
public void runTest ()
{
Random rand = new Random();
ProximityTracker tracker = new ProximityTracker();
ArrayList<Point> points = new ArrayList<Point>();
// create 100 random points and add them to the tracker and our
// comparison list
for (int i = 0; i < 100; i++) {
int x = rand.nextInt(MAX_X);
int y = rand.nextInt(MAX_Y);
Point p = new Point(x, y);
tracker.addObject(x, y, p);
points.add(p);
}
// now choose 100 new random points and confirm that the tracker
// reports the same closest point that our brute force check
// reports
for (int i = 0; i < 100; i++) {
int x = rand.nextInt(MAX_X);
int y = rand.nextInt(MAX_Y);
// get the closest point via the tracker
Point tp = (Point)tracker.findClosestObject(x, y, null);
// get the closest point via brute force
Point cp = null;
int mindist = Integer.MAX_VALUE;
for (int p = 0; p < points.size(); p++) {
Point hp = points.get(p);
int dist = ProximityTracker.distance(hp.x, hp.y, x, y);
if (dist < mindist) {
mindist = dist;
cp = hp;
}
}
// the points might actually be different, but in that case,
// the distances should be equal
int tdist = ProximityTracker.distance(x, y, tp.x, tp.y);
int cdist = ProximityTracker.distance(x, y, cp.x, cp.y);
String tps = tp.x + "," + tp.y;
String cps = cp.x + "," + cp.y;
String ps = x + "," + y;
assertTrue(ps + " => " + cps + " (" + cdist + ") ! " +
tps + " (" + tdist + ")",
tp.equals(cp) || (tdist == cdist));
}
}
public static Test suite ()
{
return new ProximityTrackerTest();
}
public static void main (String[] args)
{
ProximityTrackerTest test = new ProximityTrackerTest();
test.runTest();
}
protected static final int MAX_X = 1000;
protected static final int MAX_Y = 1000;
}
@@ -0,0 +1,104 @@
//
// $Id: ArrayIntSetTest.java,v 1.5 2004/02/25 13:21:08 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.util.tests;
import java.util.Arrays;
import java.util.Random;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.ArrayIntSet;
public class ArrayIntSetTest extends TestCase
{
public ArrayIntSetTest ()
{
super(ArrayIntSetTest.class.getName());
}
public void runTest ()
{
ArrayIntSet set = new ArrayIntSet();
set.add(new int[] { 3, 5, 5, 9, 5, 7, 1 });
int[] values = { 1, 3, 5, 7, 9 };
int[] setvals = set.toIntArray();
assertTrue("values equal", Arrays.equals(values, setvals));
ArrayIntSet set1 = new ArrayIntSet();
set1.add(new int[] { 1, 2, 3, 5, 7, 12, 19, 35 });
ArrayIntSet set2 = new ArrayIntSet();
set2.add(new int[] { 3, 4, 5, 11, 13, 17, 19, 25 });
ArrayIntSet set3 = new ArrayIntSet();
set3.add(new int[] { 3, 5, 19 });
// intersect sets 1 and 2; making sure that retainAll() returns
// true to indicate that set 1 was modified
assertTrue("retain modifies", set1.retainAll(set2));
// make sure the intersections were correct
assertTrue("intersection", set1.equals(set3));
// make sure that retainAll returns false if we do something that
// doesn't modify the set
assertTrue("retain didn't modify", !set1.retainAll(set3));
Random rando = new Random();
for (int i = 0; i < 1000; i++) {
ArrayIntSet s1 = new ArrayIntSet();
ArrayIntSet s2 = new ArrayIntSet();
ArrayIntSet s3 = new ArrayIntSet();
// add some odd numbers to all three sets
for (int c = 0; c < 100; c++) {
int r = rando.nextInt(5000) * 2 + 1;
s1.add(r);
s2.add(r);
s3.add(r);
}
// now add some even numbers to each of the first two sets in
// non-overlapping ranges
for (int c = 0; c < 100; c++) {
s1.add(rando.nextInt(5000)*2);
s2.add(rando.nextInt(5000)*2 + 15000);
}
// now ensure that s1.retainAll(s2) equals s3
s1.retainAll(s2);
assertTrue("random intersection", s1.equals(s3));
}
}
public static Test suite ()
{
return new ArrayIntSetTest();
}
public static void main (String[] args)
{
ArrayIntSetTest test = new ArrayIntSetTest();
test.runTest();
}
}
@@ -0,0 +1,155 @@
//
// $Id: ArrayUtilTest.java,v 1.2 2002/09/06 02:12:26 shaper Exp $
package com.samskivert.util.tests;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.Log;
import com.samskivert.util.ArrayUtil;
import com.samskivert.util.StringUtil;
/**
* Tests the {@link ArrayUtil} class.
*/
public class ArrayUtilTest extends TestCase
{
public ArrayUtilTest ()
{
super(ArrayUtilTest.class.getName());
}
public void runTest ()
{
// test reversing an array
int[] values = new int[] { 0 };
int[] work = (int[])values.clone();
ArrayUtil.reverse(work);
Log.info("reverse: " + StringUtil.toString(work));
values = new int[] { 0, 1, 2 };
work = (int[])values.clone();
ArrayUtil.reverse(work);
Log.info("reverse: " + StringUtil.toString(work));
work = (int[])values.clone();
ArrayUtil.reverse(work, 0, 2);
Log.info("reverse first-half: " + StringUtil.toString(work));
work = (int[])values.clone();
ArrayUtil.reverse(work, 1, 2);
Log.info("reverse second-half: " + StringUtil.toString(work));
values = new int[] { 0, 1, 2, 3, 4 };
work = (int[])values.clone();
ArrayUtil.reverse(work, 1, 3);
Log.info("reverse middle: " + StringUtil.toString(work));
values = new int[] { 0, 1, 2, 3 };
work = (int[])values.clone();
ArrayUtil.reverse(work);
Log.info("reverse even: " + StringUtil.toString(work));
// test shuffling two elements
values = new int[] { 0, 1 };
work = (int[])values.clone();
ArrayUtil.shuffle(work, 0, 1);
Log.info("first-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
ArrayUtil.shuffle(work, 1, 1);
Log.info("second-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
ArrayUtil.shuffle(work);
Log.info("full shuffle: " + StringUtil.toString(work));
// test shuffling three elements
values = new int[] { 0, 1, 2 };
work = (int[])values.clone();
ArrayUtil.shuffle(work, 0, 2);
Log.info("first-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
ArrayUtil.shuffle(work, 1, 2);
Log.info("second-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
ArrayUtil.shuffle(work);
Log.info("full shuffle: " + StringUtil.toString(work));
// test shuffling ten elements
values = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
work = (int[])values.clone();
ArrayUtil.shuffle(work, 0, 5);
Log.info("first-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
ArrayUtil.shuffle(work, 5, 5);
Log.info("second-half shuffle: " + StringUtil.toString(work));
work = (int[])values.clone();
ArrayUtil.shuffle(work);
Log.info("full shuffle: " + StringUtil.toString(work));
// test splicing with simple truncate beyond offset
values = new int[] { 0, 1, 2 };
work = (int[])values.clone();
work = ArrayUtil.splice(work, 0);
Log.info("splice truncate 0: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 1);
Log.info("splice truncate 1: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 2);
Log.info("splice truncate 2: " + StringUtil.toString(work));
values = new int[] { 0 };
work = (int[])values.clone();
work = ArrayUtil.splice(work, 0);
Log.info("single element splice truncate 0: " +
StringUtil.toString(work));
// test splicing out a single element
values = new int[] { 0, 1, 2 };
work = (int[])values.clone();
work = ArrayUtil.splice(work, 0, 1);
Log.info("splice concat 0, 1: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 1, 1);
Log.info("splice concat 1, 1: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 2, 1);
Log.info("splice concat 2, 1: " + StringUtil.toString(work));
// test splicing out two elements
values = new int[] { 0, 1, 2, 3 };
work = (int[])values.clone();
work = ArrayUtil.splice(work, 0, 2);
Log.info("splice concat 0, 2: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 1, 2);
Log.info("splice concat 1, 2: " + StringUtil.toString(work));
work = (int[])values.clone();
work = ArrayUtil.splice(work, 2, 2);
Log.info("splice concat 2, 2: " + StringUtil.toString(work));
}
public static Test suite ()
{
return new ArrayUtilTest();
}
public static void main (String[] args)
{
ArrayUtilTest test = new ArrayUtilTest();
test.runTest();
}
}
@@ -0,0 +1,66 @@
//
// $Id: CheapIntMapTest.java,v 1.1 2003/02/06 19:57:29 mdb Exp $
package com.samskivert.util.tests;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.CheapIntMap;
/**
* Tests the {@link CheapIntMap} class.
*/
public class CheapIntMapTest extends TestCase
{
public CheapIntMapTest ()
{
super(CheapIntMapTest.class.getName());
}
public void runTest ()
{
CheapIntMap map = new CheapIntMap(10);
for (int ii = 0; ii < 100; ii += 20) {
map.put(ii, new Integer(ii));
}
for (int ii = 0; ii < 5; ii++) {
map.put(ii, new Integer(ii));
}
for (int ii = 0; ii < 100; ii++) {
Object val = map.get(ii);
if (val != null) {
System.out.println(ii + " => " + val);
}
}
for (int ii = 0; ii < 100; ii += 20) {
System.out.println("Removing " + map.remove(ii));
}
for (int ii = 10; ii > 0; ii--) {
map.put(ii, new Integer(ii));
}
for (int ii = 0; ii < 100; ii++) {
Object val = map.get(ii);
if (val != null) {
System.out.println(ii + " => " + val);
}
}
}
public static Test suite ()
{
return new CheapIntMapTest();
}
public static void main (String[] args)
{
CheapIntMapTest test = new CheapIntMapTest();
test.runTest();
}
}
@@ -0,0 +1,60 @@
//
// $Id: CollectionUtilTest.java,v 1.2 2002/09/23 01:45:47 mdb Exp $
package com.samskivert.util.tests;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.CollectionUtil;
import com.samskivert.util.ComparableArrayList;
import com.samskivert.util.StringUtil;
/**
* Tests the {@link CollectionUtil} class.
*/
public class CollectionUtilTest extends TestCase
{
public CollectionUtilTest ()
{
super(CollectionUtil.class.getName());
}
public void runTest ()
{
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 100; i++) {
list.add(new Integer(i));
}
for (int i = 0; i < 10; i++) {
List<Integer> subset = CollectionUtil.selectRandomSubset(list, 10);
// System.out.println(StringUtil.toString(subset));
assertTrue("length == 10", subset.size() == 10);
}
// test comparable array list insertion
Random rand = new Random();
ComparableArrayList<Integer> slist = new ComparableArrayList<Integer>();
for (int ii = 0; ii < 25; ii++) {
Integer value = new Integer(rand.nextInt(100));
slist.insertSorted(value);
}
System.out.println(StringUtil.toString(slist));
}
public static Test suite ()
{
return new CollectionUtilTest();
}
public static void main (String[] args)
{
CollectionUtilTest test = new CollectionUtilTest();
test.runTest();
}
}
@@ -0,0 +1,68 @@
//
// $Id: ConfigTest.java,v 1.3 2002/03/28 22:21:06 mdb Exp $
package com.samskivert.util.tests;
import java.util.Iterator;
import java.util.Properties;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.Config;
import com.samskivert.util.StringUtil;
/**
* Tests the {@link Config} class.
*/
public class ConfigTest extends TestCase
{
public ConfigTest ()
{
super(ConfigTest.class.getName());
}
public void runTest ()
{
Config config = new Config("rsrc/util/test");
System.out.println("prop1: " + config.getValue("prop1", 1));
System.out.println("prop2: " + config.getValue("prop2", "two"));
int[] ival = new int[] { 1, 2, 3 };
ival = config.getValue("prop3", ival);
System.out.println("prop3: " + StringUtil.toString(ival));
String[] sval = new String[] { "one", "two", "three" };
sval = config.getValue("prop4", sval);
System.out.println("prop4: " + StringUtil.toString(sval));
System.out.println("prop5: " + config.getValue("prop5", "undefined"));
// now set some properties
config.setValue("prop1", 15);
System.out.println("prop1: " + config.getValue("prop1", 1));
config.setValue("prop2", "three");
System.out.println("prop2: " + config.getValue("prop2", "two"));
Iterator iter = config.keys();
System.out.println("Keys: " + StringUtil.toString(iter));
config.setValue("sub.sub3", "three");
Properties subprops = config.getSubProperties("sub");
System.out.println("Sub: " +
StringUtil.toString(subprops.propertyNames()));
}
public static Test suite ()
{
return new ConfigTest();
}
public static void main (String[] args)
{
ConfigTest test = new ConfigTest();
test.runTest();
}
}
@@ -0,0 +1,110 @@
//
// $Id: ConfigUtilTest.java,v 1.5 2004/02/25 13:21:08 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.util.tests;
import java.util.Properties;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.ConfigUtil;
/**
* Our test properties files:
*
* <pre>
* lib/test-c.jar:
* _package = testC
* _overrides = testAL, testBR
*
* three = testC - three
*
* lib/test-br.jar:
* _package = testBR
* _overrides = testAR
*
* two = testBR - two
* four = testBR - four
*
* lib/test-ar.jar:
* _package = testAR
* _overrides = test
*
* one = testAR - one
* two = testAR - two
* four = testAR - four
*
* lib/test-al.jar:
* _package = testAL
* _overrides = test
*
* one = testAL - one
*
* lib/test.jar:
* _package = test
*
* one = test - one
* two = test - two
* three = test - three
* </pre>
*/
public class ConfigUtilTest extends TestCase
{
public ConfigUtilTest ()
{
super(ConfigUtilTest.class.getName());
}
public void runTest ()
{
try {
String path = "/rsrc/util/test.properties";
Properties props = ConfigUtil.loadInheritedProperties(path);
assertTrue("props valid", props.toString().equals(DUMP));
path = "test/test.properties";
props = ConfigUtil.loadInheritedProperties(path);
assertTrue("props valid", props.toString().equals(IDUMP));
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
public static Test suite ()
{
return new ConfigUtilTest();
}
public static void main (String[] args)
{
ConfigUtilTest test = new ConfigUtilTest();
test.runTest();
}
protected static final String DUMP =
"{prop4=one, two, three,, and a half, four, " +
"prop3=9, 8, 7, 6, prop2=twenty five, prop1=25, " +
"sub.sub2=whee!, sub.sub1=5}";
protected static final String IDUMP = "{two=testBR - two, " +
"one=testAR - one, three=testC - three, four=testBR - four}";
}
@@ -0,0 +1,147 @@
//
// $Id: HashIntMapTest.java,v 1.5 2004/02/25 13:21:08 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.util.tests;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.HashIntMap;
import com.samskivert.util.StringUtil;
public class HashIntMapTest extends TestCase
{
public HashIntMapTest ()
{
super(HashIntMapTest.class.getName());
}
public void runTest ()
{
HashIntMap<Integer> table = new HashIntMap<Integer>();
populateTable(table);
// check the table contents
for (int i = 10; i < 20; i++) {
Integer val = table.get(i);
assertTrue("get(" + i + ") == " + i, val.intValue() == i);
}
checkContents(table, TEST1, TEST1);
// remove some entries and attempt to remove some non-entries
for (int i = 12; i < 22; i++) {
table.remove(i);
}
checkContents(table, TEST2, TEST2);
// now try some serialization
populateTable(table);
try {
File tmpfile = new File("/tmp/himt.dat");
FileOutputStream fout = new FileOutputStream(tmpfile);
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(table);
out.close();
FileInputStream fin = new FileInputStream(tmpfile);
ObjectInputStream in = new ObjectInputStream(fin);
@SuppressWarnings("unchecked") HashIntMap<Integer> map =
(HashIntMap<Integer>)in.readObject();
// check the table contents
for (int i = 10; i < 20; i++) {
Integer val = table.get(i);
assertTrue("get(" + i + ") == " + i, val.intValue() == i);
}
tmpfile.delete();
} catch (Exception e) {
e.printStackTrace();
fail("serialization failure");
}
table.clear();
// now try putting lots and lots of values in the table
// so that it grows a bunch
for (int ii=1; ii < 12345; ii += 3) {
table.put(ii, new Integer(ii));
}
// now check by removing most and seeing if everything's equal
for (int ii=1; ii < 12345; ii += 3) {
Integer val;
// let's keep the ones that are a multiple of 16
if ((ii & 15) == 0) {
val = table.get(ii);
} else {
val = table.remove(ii);
}
assertTrue("get(" + ii + ") == " + val, val.intValue() == ii);
}
// and then let's also remove the multiples of 16
for (int ii = 1; ii < 12345; ii += 3) {
if ((ii & 15) == 0) {
Integer val = table.remove(ii);
assertTrue("get(" + ii + ") == " + val, val.intValue() == ii);
}
}
}
protected void populateTable (HashIntMap<Integer> table)
{
for (int i = 10; i < 20; i++) {
Integer value = new Integer(i);
table.put(i, value);
}
}
protected void checkContents (HashIntMap<Integer> table, String exkeys, String exvals)
{
ArrayList<Integer> keys = new ArrayList<Integer>();
keys.addAll(table.keySet());
Collections.sort(keys);
String keystr = StringUtil.toString(keys);
assertTrue(keystr + ".equals(" + exkeys + ")", keystr.equals(exkeys));
ArrayList<Integer> values = new ArrayList<Integer>();
values.addAll(table.values());
Collections.sort(values);
String valuestr = StringUtil.toString(values);
assertTrue(valuestr + ".equals(" + exvals + ")", valuestr.equals(exvals));
}
public static Test suite ()
{
return new HashIntMapTest();
}
protected static final String TEST1 = "(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)";
protected static final String TEST2 = "(10, 11)";
}
@@ -0,0 +1,119 @@
//
// $Id: IntListUtilTest.java,v 1.5 2004/02/25 13:21:08 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.util.tests;
import java.util.Arrays;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.IntListUtil;
public class IntListUtilTest extends TestCase
{
public IntListUtilTest ()
{
super(IntListUtilTest.class.getName());
}
public void runTest ()
{
int[] list = null;
list = IntListUtil.add(list, 2);
// System.out.println("add(2): " + StringUtil.toString(list));
assertTrue("add(2)", Arrays.equals(list, new int[] { 2, 0, 0, 0 }));
list = IntListUtil.add(list, 5);
// System.out.println("add(5): " + StringUtil.toString(list));
assertTrue("add(5)", Arrays.equals(list, new int[] { 2, 5, 0, 0 }));
IntListUtil.clear(list, 2);
// System.out.println("clear(2): " + StringUtil.toString(list));
assertTrue("clear(2)", Arrays.equals(list, new int[] { 0, 5, 0, 0 }));
boolean contains5 = IntListUtil.contains(list, 5);
// System.out.println("contains(newBar): " + contains5);
assertTrue("contains(5)", contains5);
IntListUtil.removeAt(list, 1);
// System.out.println("removeAt(1): " + StringUtil.toString(list));
assertTrue("removeAt(1)",
Arrays.equals(list, new int[] { 0, 0, 0, 0 }));
list = IntListUtil.add(list, 0, 2);
list = IntListUtil.add(list, 1, 5);
// System.out.println("add(0, 2) + add(1, 5): " +
// StringUtil.toString(list));
assertTrue("add(0, 2) + add(1, 5))",
Arrays.equals(list, new int[] { 2, 5, 0, 0 }));
IntListUtil.remove(list, 2);
// System.out.println("remove(2): " + StringUtil.toString(list));
assertTrue("removeAt(2)",
Arrays.equals(list, new int[] { 5, 0, 0, 0 }));
list = IntListUtil.add(list, 0, 2);
list = IntListUtil.add(list, 1, 5);
list = IntListUtil.add(list, 2, 6);
// System.out.println("add(0, 2) + add(1, 5) + add(2, 6): " +
// StringUtil.toString(list));
assertTrue("add(0, 2) + add(1, 5) + add(2, 6)",
Arrays.equals(list, new int[] { 5, 2, 5, 6 }));
IntListUtil.removeAt(list, 0);
// System.out.println("removeAt(0): " + StringUtil.toString(list));
assertTrue("removeAt(0)",
Arrays.equals(list, new int[] { 2, 5, 6, 0 }));
int[] tl = IntListUtil.testAndAdd(list, 5);
// if (tl == null) {
// System.out.println("testAndAdd(5): failed: " +
// StringUtil.toString(list));
// } else {
// list = tl;
// System.out.println("testAndAdd(5): added: " +
// StringUtil.toString(list));
// }
assertTrue("testAndAdd(5)", tl == null);
tl = IntListUtil.testAndAdd(list, 7);
// if (tl == null) {
// System.out.println("testAndAdd(7): failed: " +
// StringUtil.toString(list));
// } else {
// list = tl;
// System.out.println("testAndAdd(7): added: " +
// StringUtil.toString(list));
// }
assertTrue("testAndAdd(7)", tl != null);
IntListUtil.removeAt(list, 0);
// System.out.println("removeAt(0): " + StringUtil.toString(list));
assertTrue("removeAt(0)",
Arrays.equals(list, new int[] { 5, 6, 7, 0 }));
}
public static Test suite ()
{
return new IntListUtilTest();
}
}
@@ -0,0 +1,60 @@
//
// $Id: LRUHashMapTest.java,v 1.1 2003/01/17 00:40:45 mdb Exp $
package com.samskivert.util.tests;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.LRUHashMap;
/**
* Tests the {@link LRUHashMap} class.
*/
public class LRUHashMapTest extends TestCase
{
public LRUHashMapTest ()
{
super(LRUHashMapTest.class.getName());
}
public void runTest ()
{
LRUHashMap<String,Integer> map =
new LRUHashMap<String,Integer>(10, new LRUHashMap.ItemSizer<Integer>() {
public int computeSize (Integer item) {
return item.intValue();
}
});
map.put("one.1", new Integer(1));
assertTrue("size == 1", map.size() == 1);
map.put("one.2", new Integer(1));
assertTrue("size == 2", map.size() == 2);
map.put("one.3", new Integer(1));
assertTrue("size == 3", map.size() == 3);
map.put("one.4", new Integer(1));
assertTrue("size == 4", map.size() == 4);
map.put("one.5", new Integer(1));
assertTrue("size == 5", map.size() == 5);
map.put("three.1", new Integer(3));
assertTrue("size == 6", map.size() == 6);
map.put("five.1", new Integer(5));
assertTrue("size == 4", map.size() == 4);
map.put("three.2", new Integer(3));
assertTrue("size == 2", map.size() == 2);
map.put("three.3", new Integer(3));
assertTrue("size == 2", map.size() == 2);
}
public static Test suite ()
{
return new LRUHashMapTest();
}
public static void main (String[] args)
{
LRUHashMapTest test = new LRUHashMapTest();
test.runTest();
}
}
@@ -0,0 +1,101 @@
//
// $Id: ObserverListTest.java,v 1.2 2004/02/25 13:21:08 mdb Exp $
package com.samskivert.util.tests;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.ObserverList;
/**
* Tests the {@link ObserverList} class.
*/
public class ObserverListTest extends TestCase
{
public ObserverListTest ()
{
super(ObserverListTest.class.getName());
}
public void runTest ()
{
// Log.info("Testing safe list.");
testList(new ObserverList<TestObserver>(ObserverList.SAFE_IN_ORDER_NOTIFY));
// Log.info("Testing unsafe list.");
testList(new ObserverList<TestObserver>(ObserverList.FAST_UNSAFE_NOTIFY));
}
public void testList (final ObserverList<TestObserver> list)
{
final int[] notifies = new int[1];
for (int i = 0; i < 1000; i++) {
// add some test observers
list.add(new TestObserver(1));
list.add(new TestObserver(2));
list.add(new TestObserver(3));
list.add(new TestObserver(4));
int ocount = list.size();
notifies[0] = 0;
list.apply(new ObserverList.ObserverOp<TestObserver>() {
public boolean apply (TestObserver obs) {
notifies[0]++;
obs.foozle();
// 1/3 of the time, remove the observer; 1/3 of the
// time append a new observer; 1/3 of the time do
// nothing
double rando = Math.random();
if (rando < 0.33) {
return false;
} else if (rando > 0.66) {
list.add(new TestObserver(5));
}
return true;
}
});
// Log.info("had " + ocount + "; notified " + notifies[0] +
// "; size " + list.size() + ".");
assertTrue("had " + ocount + "; notified " + notifies[0],
ocount == notifies[0]);
list.clear();
}
}
public static Test suite ()
{
return new ObserverListTest();
}
public static void main (String[] args)
{
ObserverListTest test = new ObserverListTest();
test.runTest();
}
protected static class TestObserver
{
public TestObserver (int index)
{
_index = index;
}
public void foozle ()
{
// Log.info("foozle! " + _index);
}
public String toString ()
{
return Integer.toString(_index);
}
protected int _index;
}
}
@@ -0,0 +1,104 @@
//
// $Id: QuickSortTest.java,v 1.2 2002/04/11 04:07:42 mdb Exp $
package com.samskivert.util.tests;
import java.util.Comparator;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.QuickSort;
/**
* Tests the {@link QuickSort} class.
*/
public class QuickSortTest extends TestCase
{
public QuickSortTest ()
{
super(QuickSortTest.class.getName());
}
public void runTest ()
{
Integer[] a = new Integer[100];
Comparator<Integer> comp = new Comparator<Integer>() {
public int compare (Integer x, Integer y) {
return x.intValue() - y.intValue();
}
};
for (int d = 1; d <= 100; d++) {
for (int n = 0; n < 100; n++) {
a[n] = new Integer(n / d);
QuickSort.sort(a, 0, n, comp);
for (int i = 0; i <= n; i++) {
assertTrue("Failure for up " + n + "/" + d,
a[i].intValue() == i / d);
}
}
}
// System.out.println("up test ok");
for (int d = 1; d <= 100; d++) {
for (int n = 0; n < 100; n++) {
for (int i = 0; i <= n; i++) {
a[i] = new Integer((n - i) / d);
}
QuickSort.sort(a, 0, n, comp);
for (int i = 0; i <= n; i++) {
assertTrue("Failure for down " + n + "/" + d,
a[i].intValue() == i / d);
}
}
}
// System.out.println("down test ok");
int tests = 1000;
for (int sorts = 0; sorts < tests; sorts++) {
int n = rand(100);
for (int i = 0; i <= n; i++) {
a[i] = new Integer(rand(30000));
}
QuickSort.sort(a, 0, n, comp);
for (int i = 0; i < n; i++) {
assertTrue("Failure for random " + n,
a[i].intValue() <= a[i+1].intValue());
}
QuickSort.sort(a, 0, n, comp);
for (int i = 0; i < n; i++) {
assertTrue("Failure for random " + n + " (resort)",
a[i].intValue() <= a[i+1].intValue());
}
a[rand(n+1)] = new Integer(rand(30000));
QuickSort.sort(a, 0, n, comp);
for (int i = 0; i < n; i++) {
assertTrue("Failure for random " + n + " (resort 2)",
a[i].intValue() <= a[i+1].intValue());
}
}
// System.out.println("successfully sorted " + tests +
// " random arrays");
}
public static Test suite ()
{
return new QuickSortTest();
}
public static void main (String[] args)
{
QuickSortTest test = new QuickSortTest();
test.runTest();
}
private static int rand (int n)
{
return (int)(Math.random() * n);
}
}
@@ -0,0 +1,145 @@
//
// $Id$
package com.samskivert.util.tests;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.Queue;
import com.samskivert.util.RunQueue;
import com.samskivert.util.SerialExecutor;
/**
* Tests the {@link SerialExecutor} class.
*/
public class SerialExecutorTest extends TestCase
implements RunQueue
{
public SerialExecutorTest ()
{
super(SerialExecutorTest.class.getName());
_main = Thread.currentThread();
}
public void runTest ()
{
SerialExecutor executor = new SerialExecutor(this);
int added = 0;
// _sleeps++, _exits++, _results++
executor.addTask(new Sleeper(500L, false));
added++;
// _interrupts++, _exits++, _timeouts++
executor.addTask(new Sleeper(1500L, false));
added++;
// _interrupts++, _timeouts++
executor.addTask(new Sleeper(1500L, true));
added++;
// _sleeps++, _exits++, _results++
executor.addTask(new Sleeper(500L, false));
added++;
// process the results posted on our run queue
for (int ii = 0; ii < added; ii++) {
Runnable r = _queue.get();
r.run();
}
// give the executor threads a second to run to completion
try {
Thread.sleep(1000L);
} catch (InterruptedException ie) {
}
// make sure we got the expected number of sleeps, interrupts, etc.
assertCount("_sleeps", _sleeps, 2);
assertCount("_interrupts", _interrupts, 2);
assertCount("_exits", _exits, 3);
assertCount("_results", _results, 2);
assertCount("_timeouts", _timeouts, 2);
assertCount("_doubleints", _doubleints, 0);
}
protected void assertCount (String field, int value, int expected)
{
assertTrue(field + " != " + expected + " (" + value + ")",
value == expected);
}
public void postRunnable (Runnable r)
{
_queue.append(r);
}
public boolean isDispatchThread ()
{
return Thread.currentThread() == _main;
}
public static Test suite ()
{
return new SerialExecutorTest();
}
public static void main (String[] args)
{
SerialExecutorTest test = new SerialExecutorTest();
test.runTest();
}
protected class Sleeper implements SerialExecutor.ExecutorTask
{
public Sleeper (long sleepFor, boolean hang) {
_sleepFor = sleepFor;
_hang = hang;
}
public boolean merge (SerialExecutor.ExecutorTask task) {
return false;
}
public long getTimeout () {
return 1000L;
}
public void executeTask () {
try {
Thread.sleep(_sleepFor);
_sleeps++;
} catch (InterruptedException ie) {
_interrupts++;
}
if (_hang) {
try {
Thread.sleep(100000L);
} catch (InterruptedException ie) {
_doubleints++;
}
}
_exits++;
}
public void resultReceived () {
_results++;
}
public void timedOut () {
_timeouts++;
}
protected long _sleepFor;
protected boolean _hang;
}
protected Thread _main;
protected Queue<Runnable> _queue = new Queue<Runnable>();
protected int _sleeps, _interrupts, _doubleints, _exits;
protected int _results, _timeouts;
}
@@ -0,0 +1,57 @@
//
// $Id: StringUtilTest.java,v 1.4 2004/02/25 13:21:08 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.util.tests;
import junit.framework.Test;
import junit.framework.TestCase;
import com.samskivert.util.StringUtil;
public class StringUtilTest extends TestCase
{
public StringUtilTest ()
{
super(StringUtilTest.class.getName());
}
public void runTest ()
{
String source = "mary, had, a,, little, lamb, and, a, comma,,";
// split the source string into tokens
String[] tokens = StringUtil.parseStringArray(source);
assertTrue("tokens.length == 7", tokens.length == 7);
// now join them back together
String joined = StringUtil.joinEscaped(tokens);
assertTrue("joined.equals(source)", joined.equals(source));
// make sure null to empty string works
tokens = new String[] { "this", null, "is", null, "a", null, "test" };
joined = StringUtil.joinEscaped(tokens);
assertTrue("null elements work", joined.equals("this, , is, , a, , test"));
}
public static Test suite ()
{
return new StringUtilTest();
}
}
@@ -0,0 +1,45 @@
//
// $Id: SystemInfoDemo.java,v 1.1 2003/01/14 22:07:06 shaper Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2003 Walter Korman
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.util.tests;
import com.samskivert.util.SystemInfo;
public class SystemInfoDemo
{
public static void main (String[] args)
{
// output initial system information
SystemInfo info = new SystemInfo();
System.out.println("Initial system info:");
System.out.println(info.toString());
// allocate a bit of data
System.out.println("\nAllocating test data.");
for (int ii = 0; ii < 10000; ii++) {
int[] data = new int[100];
}
// update and output the latest system information
info.update();
System.out.println("\nUpdated system info:");
System.out.println(info.toString());
}
}
@@ -0,0 +1,103 @@
//
// $Id: SetFieldRuleTest.java,v 1.4 2004/02/25 13:21:41 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.samskivert.xml.tests;
import java.io.InputStream;
import java.io.FileInputStream;
import junit.framework.Test;
import junit.framework.TestCase;
import org.apache.commons.digester.Digester;
import com.samskivert.test.TestUtil;
import com.samskivert.util.StringUtil;
import com.samskivert.xml.SetFieldRule;
public class SetFieldRuleTest extends TestCase
{
public SetFieldRuleTest ()
{
super(SetFieldRuleTest.class.getName());
}
public static class TestObject
{
public int intField;
public String stringField;
public Integer integerField;
public int[] intArrayField;
public String[] stringArrayField;
public String toString ()
{
return "[intField=" + intField + ", stringField=" + stringField +
", integerField=" + integerField +
", intArrayField=" + StringUtil.toString(intArrayField) +
", stringArrayField=" + StringUtil.toString(stringArrayField) +
"]";
}
}
public void runTest ()
{
Digester digester = new Digester();
// create our object and push it onto the digester
TestObject object = new TestObject();
digester.push(object);
// set up some rules
digester.addRule("object/intField", new SetFieldRule("intField"));
digester.addRule("object/stringField", new SetFieldRule("stringField"));
digester.addRule("object/integerField", new SetFieldRule("integerField"));
digester.addRule("object/intArrayField", new SetFieldRule("intArrayField"));
digester.addRule("object/stringArrayField", new SetFieldRule("stringArrayField"));
try {
String xmlpath =
TestUtil.getResourcePath("rsrc/xml/setfieldtest.xml");
InputStream input = new FileInputStream(xmlpath);
digester.parse(input);
input.close();
} catch (Exception e) {
fail("Parsing failed: " + e);
}
assertTrue(EXPECTED.equals(object.toString()));
}
public static Test suite ()
{
return new SetFieldRuleTest();
}
public static void main (String[] args)
{
SetFieldRuleTest test = new SetFieldRuleTest();
test.runTest();
}
protected static final String EXPECTED =
"[intField=5, stringField=howdy partner!, integerField=15, " +
"intArrayField=(1, 2, 3, 4, 5), " +
"stringArrayField=(one, two, three, four, five)]";
}