/*
 * Copyright (C) 2004-2005  exedio GmbH (www.exedio.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.exedio.cope;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DataTest extends AbstractLibTest
{
	public DataTest()
	{
		super(Main.dataModel);
	}
	
	private DataItem item;
	private final byte[] data = new byte[]{-86,122,-8,23};
	private final byte[] data2 = new byte[]{-97,35,-126,86,19,-8};
	private final byte[] dataFile = new byte[]{-54,104,-63,23,19,-45,71,-23};
	private final byte[] dataEmpty = new byte[]{};
	
	public void setUp() throws Exception
	{
		super.setUp();
		deleteOnTearDown(item = new DataItem());
	}
	
	public void testData() throws IOException
	{
		assertTrue(item.isDataNull());
		assertEquals(null, item.getData());
		assertEquals(-1, item.getDataLength());
		assertEquals(-1, item.getDataLastModified());

		{
			sleepForFileLastModified();
			final Date beforeData = new Date();
			item.setData(stream(data));
			final Date afterData = new Date();
			assertTrue(!item.isDataNull());
			assertData(data, item.getData());
			assertEquals(data.length, item.getDataLength());
			assertWithinFileLastModified(beforeData, afterData, new Date(item.getDataLastModified()));
		}
		{
			sleepForFileLastModified();
			final Date beforeData2 = new Date();
			item.setData(stream(data2));
			final Date afterData2 = new Date();
			assertTrue(!item.isDataNull());
			assertData(data2, item.getData());
			assertEquals(data2.length, item.getDataLength());
			assertWithinFileLastModified(beforeData2, afterData2, new Date(item.getDataLastModified()));
		}
		{
			sleepForFileLastModified();
			final Date beforeDataEmpty = new Date();
			item.setData(stream(dataEmpty));
			final Date afterDataEmpty = new Date();
			assertTrue(!item.isDataNull());
			assertData(dataEmpty, item.getData());
			assertEquals(0, item.getDataLength());
			assertWithinFileLastModified(beforeDataEmpty, afterDataEmpty, new Date(item.getDataLastModified()));
		}
		item.setData((InputStream)null);
		assertTrue(item.isDataNull());
		assertEquals(-1, item.getDataLength());
		assertEquals(-1, item.getDataLastModified());
		assertEquals(null, item.getData());
		{
			sleepForFileLastModified();
			final Date before = new Date();
			item.setData(file(dataFile));
			final Date after = new Date();
			assertTrue(!item.isDataNull());
			assertData(dataFile, item.getData());
			assertEquals(dataFile.length, item.getDataLength());
			assertWithinFileLastModified(before, after, new Date(item.getDataLastModified()));
		}
		item.setData((File)null);
		assertTrue(item.isDataNull());
		assertEquals(-1, item.getDataLength());
		assertEquals(-1, item.getDataLastModified());
		assertEquals(null, item.getData());
	}

}

@@ -1662,20 +1662,16 @@
 e before
-Data
  = new D
@@ -1727,20 +1727,16 @@
 te after
-Data
  = new D
@@ -1908,23 +1908,15 @@
 fore
-Data
 , after
-Data
 , ne
@@ -2011,21 +2011,16 @@
 e before
-Data2
  = new D
@@ -2077,21 +2077,16 @@
 te after
-Data2
  = new D
@@ -2260,25 +2260,15 @@
 fore
-Data2
 , after
-Data2
 , ne
@@ -2363,25 +2363,16 @@
 e before
-DataEmpty
  = new D
@@ -2433,25 +2433,16 @@
 te after
-DataEmpty
  = new D
@@ -2609,33 +2609,15 @@
 fore
-DataEmpty, afterDataEmpty
+, after
 , ne

simplify naming