package com.alvazan.test;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.alvazan.orm.api.base.NoSqlEntityManager;
import com.alvazan.orm.api.base.NoSqlEntityManagerFactory;
import com.alvazan.test.db.ExpiringEntity;

public class TestExpire {

	private static NoSqlEntityManagerFactory factory;
	private NoSqlEntityManager mgr;

	@BeforeClass
	public static void setup() {
		factory = FactorySingleton.createFactoryOnce();
	}

	@Before
	public void createEntityManager() {
		mgr = factory.createEntityManager();
	}

	@After
	public void clearDatabase() {
		NoSqlEntityManager other = factory.createEntityManager();
		other.clearDatabase(true);
	}

	@Test
	public void testPersistWithExpire() throws InterruptedException {
		ExpiringEntity entity = new ExpiringEntity();
		entity.setId(50);
		entity.setName("test1");
		entity.setTtl(5);
		mgr.put(entity);
		mgr.flush();

		// read entity back
		mgr.clear();
		entity = mgr.find(ExpiringEntity.class, 50L);
		assertNotNull("Entity not persisted", entity);
	}

	@Test
	public void testExpireEntity() throws InterruptedException {
		ExpiringEntity entity = new ExpiringEntity();
		entity.setId(50);
		entity.setName("test1");
		entity.setTtl(5);
		mgr.put(entity);
		mgr.flush();

		// wait for entity to expire
		Thread.sleep(6000);
		mgr.clear();
		entity = mgr.find(ExpiringEntity.class, 50L);
		assertNull("Entity not expired", entity);
	}
}

@@ -1102,24 +1102,370 @@
 ntity);%0A%09%7D%0A%0A
+%09@Test%0A%09public void testTTLReadBack() %7B%0A%09%09ExpiringEntity entity = new ExpiringEntity();%0A%09%09entity.setId(50);%0A%09%09entity.setName(%22test1%22);%0A%09%09entity.setTtl(5);%0A%09%09mgr.put(entity);%0A%09%09mgr.flush();%0A%0A%09%09// read entity back%0A%09%09mgr.clear();%0A%09%09entity = mgr.find(ExpiringEntity.class, 50L);%0A%09%09assertEquals(%22TTL not read back correctly%22, 5, entity.getTtl());%0A%09%7D%0A%0A
 %09@Test%0A%09publ

test if TTL is read back from database correctly