/*
 * Created on Mar 5, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.codehaus.activemq.util;

import junit.framework.TestCase;

import java.util.Hashtable;

/**
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class IdGeneratorTest extends TestCase {

    public static void main(String[] args) {
        junit.textui.TestRunner.run(IdGeneratorTest.class);
    }

    /*
     * @see TestCase#setUp()
     */
    protected void setUp() throws Exception {
        super.setUp();
    }

    /*
     * @see TestCase#tearDown()
     */
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /**
     * Constructor for IdGeneratorTest.
     *
     * @param arg0
     */
    public IdGeneratorTest(String arg0) {
        super(arg0);
    }

    /**
     * This is not a good test!
     */

    public void testGenerateId() {
        IdGenerator idGen = new IdGenerator();
        int count = 100000;
        Hashtable table = new Hashtable();
        String str = "";
        for (int i = 0; i < count; i++) {
            str = idGen.generateId();
            assertTrue(str != null);
            assertTrue(table.get(str) == null);
            table.put(str, str);
        }
    }

    public void testGetSeedFromId() {
        IdGenerator idGen = new IdGenerator();
        String seed = idGen.getSeed();
        String id = idGen.generateId();
        String generatorId = IdGenerator.getSeedFromId(id);
        for (int i = 0; i < 1000; i++) {
            assertTrue(generatorId.equals(seed));
        }
    }

    public void testGetCountFromId() {
        IdGenerator idGen = new IdGenerator();
        String seed = idGen.getSeed();
        for (int i = 0; i < 100; i++) {
            String id = idGen.generateId();
            long count = IdGenerator.getCountFromId(id);
            assertTrue(count == i);
        }
    }

    public void testCompare() {
        IdGenerator idGen = new IdGenerator();
        String seed = idGen.getSeed();
        String lastId = null;
        for (int i = 0; i < 100; i++) {
            String id = idGen.generateId();
            if (lastId != null) {
                assertTrue(IdGenerator.compare(lastId, id) == -1);
            }
            lastId = id;
        }
    }

}
