package org.unidal.cat.message.storage;

import java.io.File;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.unidal.helper.Files;
import org.unidal.lookup.ComponentTestCase;

import com.dianping.cat.message.internal.MessageId;

public class IndexTest extends ComponentTestCase {

	@Before
	public void before() throws Exception {
		StorageConfiguration config = lookup(StorageConfiguration.class);

		config.setBaseDataDir(new File("target"));
		File baseDir = new File("target");

		Files.forDir().delete(new File(baseDir, "dump"), true);
	}

	@Test
	public void testMapAndLookup() throws Exception {
		MessageId from = MessageId.parse("from-0a260014-403899-76543");
		MessageId expected = MessageId.parse("to-0a260015-403899-12345");
		IndexManager manager = lookup(IndexManager.class, "local");
		Index index = manager.getIndex(from.getDomain(), from.getHour(), true);

		index.map(from, expected);

		MessageId actual = index.lookup(from);

		try {
			Assert.assertEquals(expected, actual);
		} finally {
			index.close();
		}
	}

	@Test
	public void testMapAndLookups() throws Exception {
		IndexManager manager = lookup(IndexManager.class, "local");
		Index index = manager.getIndex("from", 403899, true);

		for (int i = 1; i < 150000; i++) {
			MessageId from = MessageId.parse("from-0a260014-403899-" + i);
			MessageId to = MessageId.parse("to-0a260015-403899-" + i);

			index.map(from, to);
		}

		index.close();
		index = manager.getIndex("from", 403899, true);
		for (int i = 1; i < 150000; i++) {
			MessageId from = MessageId.parse("from-0a260014-403899-" + i);
			MessageId expected = MessageId.parse("to-0a260015-403899-" + i);

			MessageId actual = index.lookup(from);

			Assert.assertEquals(expected, actual);
		}
	}
}

@@ -317,17 +317,16 @@
 tCase %7B%0A
-%0A
 %09@Before
@@ -585,499 +585,8 @@
 %09%7D%0A%0A
-%09@Test%0A%09public void testMapAndLookup() throws Exception %7B%0A%09%09MessageId from = MessageId.parse(%22from-0a260014-403899-76543%22);%0A%09%09MessageId expected = MessageId.parse(%22to-0a260015-403899-12345%22);%0A%09%09IndexManager manager = lookup(IndexManager.class, %22local%22);%0A%09%09Index index = manager.getIndex(from.getDomain(), from.getHour(), true);%0A%0A%09%09index.map(from, expected);%0A%0A%09%09MessageId actual = index.lookup(from);%0A%0A%09%09try %7B%0A%09%09%09Assert.assertEquals(expected, actual);%0A%09%09%7D finally %7B%0A%09%09%09index.close();%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Te
@@ -1285,10 +1285,853 @@
 %0A%09%09%7D%0A%09%7D%0A
+%0A%09@Test%0A%09public void testMapAndLookupManyIps() throws Exception %7B%0A%09%09IndexManager manager = lookup(IndexManager.class, %22local%22);%0A%09%09Index index = manager.getIndex(%22from%22, 403899, true);%0A%0A%09%09for (int i = 1; i %3C 150000; i++) %7B%0A%09%09%09for (int ip = 0; ip %3C 9; ip++) %7B%0A%09%09%09%09MessageId from = MessageId.parse(%22from-0a26000%22 + ip + %22-403899-%22 + i);%0A%09%09%09%09MessageId to = MessageId.parse(%22from-0a25000%22 + ip + %22-403899-%22 + i);%0A%0A%09%09%09%09index.map(from, to);%0A%09%09%09%7D%0A%0A%09%09%7D%0A%0A%09%09index.close();%0A%09%09index = manager.getIndex(%22from%22, 403899, true);%0A%09%09for (int i = 1; i %3C 150000; i++) %7B%0A%0A%09%09%09for (int ip = 0; ip %3C 9; ip++) %7B%0A%09%09%09%09MessageId from = MessageId.parse(%22from-0a26000%22 + ip + %22-403899-%22 + i);%0A%09%09%09%09MessageId expected = MessageId.parse(%22from-0a25000%22 + ip + %22-403899-%22 + i);%0A%0A%09%09%09%09MessageId actual = index.lookup(from);%0A%0A%09%09%09%09Assert.assertEquals(expected, actual);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

modify the index test