package com.github.davidmoten.fjpa;

import static com.github.davidmoten.fjpa.Document.toId;
import static com.github.davidmoten.fjpa.EntityManagers.emf;
import static com.google.common.collect.Lists.newArrayList;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.List;

import org.junit.Test;

import com.google.common.collect.ImmutableList;

public class RichEntityManagerFactoryTest {

	@Test
	public void testRichEmf() {
		RichEntityManager em = emf("test").em();
		ImmutableList<String> list = em.begin().persist(new Document("a"))
				.persist(new Document("b")).persist(new Document("c")).commit()
				.createQuery("from Document order by id", Document.class)
				.fluent().transform(toId()).toList();
		assertEquals(newArrayList("a", "b", "c"), list);
		em.closeAll();
	}

	@Test
	public void testRichEmfRun() {
		RichEntityManagerFactory emf = EntityManagers.emf("test");
		emf.run(new TaskVoid() {
			@Override
			public void run(RichEntityManager em) {
				ImmutableList<String> list = em
						.persist(new Document("a"))
						.persist(new Document("b"))
						.persist(new Document("c"))
						.createQuery("from Document order by id",
								Document.class).fluent().transform(toId())
						.toList();
				assertEquals(newArrayList("a", "b", "c"), list);
			}
		}).close();
	}

	@Test
	public void testRichEmfRunReturningList() {
		RichEntityManagerFactory emf = EntityManagers.emf("test");
		List<String> list = emf.run(new Task<List<String>>() {
			@Override
			public List<String> run(RichEntityManager em) {
				return em
						.persist(new Document("a"))
						.persist(new Document("b"))
						.persist(new Document("c"))
						.createQuery("from Document order by id",
								Document.class).fluent().transform(toId())
						.toList();
			}
		}).result();
		assertEquals(newArrayList("a", "b", "c"), list);
		emf.close();
	}

	@Test
	public void testRichEmfRunReturningListAndUsingProcessor() {
		RichEntityManagerFactory emf = EntityManagers.emf("test");
		emf.run(new Task<List<String>>() {
			@Override
			public List<String> run(RichEntityManager em) {
				return em
						.persist(new Document("a"))
						.persist(new Document("b"))
						.persist(new Document("c"))
						.createQuery("from Document order by id",
								Document.class).fluent().transform(toId())
						.toList();
			}
		}).process(new Processor<List<String>>() {
			@Override
			public void process(List<String> list) {
				assertEquals(newArrayList("a", "b", "c"), list);
			}
		}).emf().close();
	}
	
	@Test
	public void testRunScript() {
		StringWriter commands = new StringWriter();
		commands.write("insert into document(id) values('a');\n");
		commands.write("insert into document(id) values('b');\n");
		InputStream is = new ByteArrayInputStream(commands.toString().getBytes());
		RichEntityManagerFactory emf = emf("test");
		long count = emf.runScript(is).em().count(Document.class);
		emf.close();
		assertEquals(2,count);
	}

}

@@ -251,16 +251,149 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertFalse;%0Aimport static org.junit.Assert.assertNotNull;%0Aimport static org.junit.Assert.assertTrue;
 %0A%0Aimport
@@ -575,16 +575,55 @@
 bleList;
+%0Aimport com.google.common.collect.Maps;
 %0A%0Apublic
@@ -2777,10 +2777,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09@Te
@@ -3043,16 +3043,21 @@
 String()
+%0A%09%09%09%09
 .getByte
@@ -3201,16 +3201,17 @@
 quals(2,
+ 
 count);%0A
@@ -3214,10 +3214,603 @@
 t);%0A%09%7D%0A%0A
+%09@Test%0A%09public void testCreateEntityManager() %7B%0A%09%09emf(%22test%22).createEntityManager(Maps.%3CString, Object%3E newHashMap())%0A%09%09%09%09.begin().persist(new Document(%22a%22)).commit().close();%0A%09%7D%0A%09%0A%09@Test %0A%09public void testCreateCriteriaBuilder() %7B%0A%09%09RichEntityManagerFactory emf = emf(%22test%22);%0A%09%09assertNotNull(emf.getCriteriaBuilder());%0A%09%09assertNotNull(emf.getMetamodel());%0A%09%09assertTrue(emf.isOpen());%0A%09%09assertNotNull(emf.getProperties());%0A%09%09assertNotNull(emf.getCache());%0A%09%09assertNotNull(emf.getPersistenceUnitUtil());%0A%09%09assertNotNull(emf.getProperties());%0A%09%09emf.close();%0A%09%09assertFalse(emf.isOpen());%0A%09%09%0A%09%7D%0A%0A
 %7D%0A

add tests