package study.hard.lambdaj;

import static ch.lambdaj.Lambda.*;
import static org.hamcrest.Matchers.*;

import java.util.List;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import study.hard.commons.User;
import ch.lambdaj.group.Group;

import com.google.common.collect.Lists;

/**
 * @author SeomGi, Han(iandmyhand@gmail.com)
 */
public class LambdajTester {

	private List<User> userList;

	@Before
	public void setUp() {
		userList = Lists.newArrayList();

		// Add the 10 users.
		for (int i = 1; i <= 10; i++) {
			User user = new User("Tester" + (i % 2), 20 + (i % 3));
			userList.add(user);
		}

		println("Sample list:", userList);
	}

	@Test
	public void testSelectWithOneCondition() {
		String name = "Tester1";
		List<User> selectedList = select
			(
				userList,
				having(
					on(User.class).getName(),
					equalTo(name)
				)
			);

		println("test to select with one condition:", selectedList);
	}

	@Test
	public void testOrderingComparison() {
		String name = "Tester1";
		List<User> selectedList = select
			(
				userList,
				allOf(
					having(
						on(User.class).getName(),
						equalTo(name)),
					having(
						on(User.class).getAge(),
						allOf(
							greaterThanOrEqualTo(21),
							lessThanOrEqualTo(23)
						)
					)
				)
			);

		println("Test ordering comparison:", selectedList);
	}

	@Test
	public void testGrouping() {
		Group<User> groupAgeOfUser = group
			(
				userList,
				by(on(User.class).getAge())
			);
		Set<String> groupAgeKeys = groupAgeOfUser.keySet();

		println("group keys:", groupAgeKeys);

		for (String ageKey : groupAgeKeys) {
			println("age key:", ageKey);
			println("size of group by age key[" + ageKey + "]:", groupAgeOfUser.find(ageKey).size());
			for (User user : groupAgeOfUser.find(ageKey)) {
				println("user:", user);
			}
		}
	}

	private void println(String title, Object object) {
		System.out.println("==================================================");
		System.out.println(title);
		System.out.println("--------------------------------------------------");
		System.out.println("result: " + object);
		System.out.println("==================================================");
	}
}

@@ -96,16 +96,104 @@
 hers.*;%0A
+import static org.junit.Assert.*;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.HashSet;
 %0Aimport 
@@ -743,24 +743,62 @@
 , userList);
+%0A%0A%09%09assertTrue(10 == userList.size());
 %0A%09%7D%0A%0A%09@Test%0A
@@ -1051,32 +1051,73 @@
 , selectedList);
+%0A%0A%09%09assertTrue(5 == selectedList.size());
 %0A%09%7D%0A%0A%09@Test%0A%09pub
@@ -1511,16 +1511,57 @@
 edList);
+%0A%0A%09%09assertTrue(3 == selectedList.size());
 %0A%09%7D%0A%0A%09@T
@@ -1742,17 +1742,16 @@
 ySet();%0A
-%0A
 %09%09printl
@@ -1781,16 +1781,56 @@
 geKeys);
+%0A%09%09assertTrue(3 == groupAgeKeys.size());
 %0A%0A%09%09for 
@@ -2079,16 +2079,249 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09@Test%0A%09public void testSelectDistinct() %7B%0A%09%09List%3CUser%3E distinctedUserList = new ArrayList%3CUser%3E(new HashSet%3CUser%3E(userList));%0A%09%09println(%22distinctedUserList: %22, distinctedUserList);%0A%0A%09%09assertTrue(6 == distinctedUserList.size());%0A%09%7D%0A%0A
 %09private

Add test case to distinct list.