view src/test/java/jungle/core/table/AbstractRecordTestTemplate.java @ 3:9eb9fabd9f29

added Table framework
author shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
date Wed, 13 Jun 2012 01:29:02 +0900
parents
children 761d04aecfcb
line wrap: on
line source

package jungle.core.table;

import java.util.HashSet;
import java.util.Iterator;
import jungle.util.Pair;
import junit.framework.Assert;
import junit.framework.TestCase;

public abstract class AbstractRecordTestTemplate extends TestCase
{
	public abstract Record newInstance();
	
	public static final String ONE = "one";
	public static final String TWO = "two";
	public static final String THREE = "three";
	
	public Record prepare()
	{
		Record r = newInstance();
		
		r.setProperty(ONE,ONE);
		r.setProperty(TWO,TWO);
		r.setProperty(THREE,THREE);
		
		r.createSequence(ONE);
		r.createSequence(TWO);
		r.createSequence(THREE);
		
		return r;
	}
	
	public void testSetProperty()
	{
		Record r = prepare();
		r.setProperty("TEST","TEST");
		
		Assert.assertEquals(r.getProperty("TEST"),"TEST");
	}
	
	public void testGetProperty()
	{
		Record r = prepare();
		Assert.assertEquals(r.getProperty(ONE),ONE);
		Assert.assertEquals(r.getProperty(TWO),TWO);
		Assert.assertEquals(r.getProperty(THREE),THREE);
	}
	
	public void testRemoveProperty()
	{
		Record r = prepare();
		String removed = r.removeProperty(TWO);
		Assert.assertEquals(TWO,removed);
		Assert.assertNull(r.getProperty(TWO));
	}
	
	public void testCreateSequence()
	{
		Record r = prepare();
		PropertySequence seq = r.createSequence("TEST");
		
		Assert.assertNotNull(seq);
		Assert.assertEquals(seq,r.getSequence("TEST"));
	}
	
	public void testGetSequence()
	{
		Record r = newInstance();
		PropertySequence one = r.createSequence(ONE);
		PropertySequence two = r.createSequence(TWO);
		PropertySequence three = r.createSequence(THREE);
		
		Assert.assertEquals(r.getSequence(ONE),one);
		Assert.assertEquals(r.getSequence(TWO),two);
		Assert.assertEquals(r.getSequence(THREE),three);
	}
	
	public void testRemoveSequence()
	{
		Record r = prepare();
		PropertySequence removed = r.removeSequence(TWO);
		
		Assert.assertNotNull(removed);
		Assert.assertNull(r.removeSequence(TWO));
	}
	
	public void testProperties()
	{
		Record r = prepare();
		
		HashSet<String> props = new HashSet<String>();
		props.add(ONE);
		props.add(TWO);
		props.add(THREE);
		
		Iterator<Pair<String,String>> itr = r.properties();
		while(itr.hasNext()){
			Pair<String,String> p = itr.next();
			Assert.assertEquals(p.left(),p.right());
			Assert.assertTrue(props.contains(p.left()));
			props.remove(p.left());
		}
		
		Assert.assertEquals(0,props.size());
	}
	
	public void testSequences()
	{
		Record r = newInstance();
		PropertySequence one = r.createSequence(ONE);
		PropertySequence two = r.createSequence(TWO);
		PropertySequence three = r.createSequence(THREE);
		
		HashSet<PropertySequence> props = new HashSet<PropertySequence>();
		props.add(one);
		props.add(two);
		props.add(three);
		
		Iterator<Pair<String,PropertySequence>> itr = r.sequences();
		while(itr.hasNext()){
			Pair<String,PropertySequence> p = itr.next();
			PropertySequence seq = p.right();
			Assert.assertTrue(props.contains(seq));
			props.remove(seq);
		}
		
		Assert.assertEquals(0,props.size());
	}
}