/*
 * 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.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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());
	}
	
	private void assertIt(final byte[] expectedData) throws IOException
	{
		assertTrue(!item.isDataNull());
		assertData(expectedData, item.getData());
		assertDataFile(expectedData);
		assertEquals(expectedData.length, item.getDataLength());
	}
	
	public void testData() throws IOException
	{
		assertTrue(item.isDataNull());
		assertEquals(null, item.getData());
		assertDataFile(null);
		assertEquals(-1, item.getDataLength());

		item.setData(data);
		assertIt(data);

		item.setData(data2);
		assertIt(data2);

		item.setData(dataEmpty);
		assertIt(dataEmpty);

		item.setData((byte[])null);
		assertTrue(item.isDataNull());
		assertEquals(-1, item.getDataLength());
		assertEquals(null, item.getData());
		assertDataFile(null);

		item.setData(stream(data));
		assertIt(data);

		item.setData(stream(data2));
		assertIt(data2);

		item.setData(stream(dataEmpty));
		assertIt(dataEmpty);

		item.setData((InputStream)null);
		assertTrue(item.isDataNull());
		assertEquals(-1, item.getDataLength());
		assertEquals(null, item.getData());
		assertDataFile(null);

		item.setData(file(dataFile));
		assertIt(dataFile);

		item.setData(file(dataEmpty));
		assertIt(dataEmpty);

		item.setData((File)null);
		assertTrue(item.isDataNull());
		assertEquals(-1, item.getDataLength());
		assertEquals(null, item.getData());
		assertDataFile(null);
		
		try
		{
			item.getData((OutputStream)null);
			fail();
		}
		catch(NullPointerException e)
		{
			assertEquals(null, e.getMessage());
		}
		try
		{
			item.getData((File)null);
			fail();
		}
		catch(NullPointerException e)
		{
			assertEquals(null, e.getMessage());
		}
	}
	
	// TODO rename to assertData
	private final void assertDataFile(final byte[] expectedData) throws IOException
	{
		final File tempFile = File.createTempFile("cope-DataTest.", ".tmp");
		assertTrue(tempFile.delete());
		assertFalse(tempFile.exists());
		
		item.getData(tempFile);
		assertEqualContent(expectedData, tempFile);

		final ByteArrayOutputStream out = new ByteArrayOutputStream();
		item.getData(out);
		final byte[] actualData = out.toByteArray();
		if(expectedData!=null)
			assertData(expectedData, actualData);
		else
			assertEquals(0, actualData.length);
	}
	
}

@@ -1507,32 +1507,62 @@
  IOException%0A%09%7B%0A
+%09%09if(expectedData!=null)%0A%09%09%7B%0A%09
 %09%09assertTrue(!it
@@ -1571,32 +1571,33 @@
 .isDataNull());%0A
+%09
 %09%09assertData(exp
@@ -1616,32 +1616,33 @@
 tem.getData());%0A
+%09
 %09%09assertDataFile
@@ -1655,24 +1655,25 @@
 tedData);%0A%09%09
+%09
 assertEquals
@@ -1721,59 +1721,24 @@
 ));%0A
+%09
 %09%7D%0A%09
-%0A%09public void testData() throws IOException%0A
+%09else%0A%09
 %09%7B%0A
+%09
 %09%09as
@@ -1758,32 +1758,33 @@
 .isDataNull());%0A
+%09
 %09%09assertEquals(n
@@ -1799,32 +1799,33 @@
 m.getData());%0A%09%09
+%09
 assertDataFile(n
@@ -1826,24 +1826,25 @@
 File(null);%0A
+%09
 %09%09assertEqua
@@ -1873,16 +1873,105 @@
 gth());%0A
+%09%09%7D%0A%09%7D%0A%09%0A%09public void testData() throws IOException%0A%09%7B%0A%09%09assertIt(null);%0A%0A%09%09// set byte%5B%5D
 %0A%09%09item.
@@ -2140,137 +2140,40 @@
 sert
-True(item.isDataN
+It(n
 ull
-()
 );%0A
-%09%09assertEquals(-1, item.getDataLength());%0A%09%09assertEquals(null, item.getData());%0A%09%09assertDataFile(null);%0A
+%0A%0A%09%09// set InputStream
 %0A%09%09i
@@ -2375,137 +2375,35 @@
 sert
-True(item.isDataN
+It(n
 ull
-()
 );%0A
-%09%09assertEquals(-1, item.getDataLength());%0A%09%09assertEquals(null, item.getData());%0A%09%09assertDataFile(null);%0A
+%0A%09%09%0A%09%09// set File
 %0A%09%09i
@@ -2551,136 +2551,20 @@
 sert
-True(item.isDataNull());%0A%09%09assertEquals(-1, item.getDataLength());%0A%09%09assertEquals(null, item.getData());%0A%09%09assertDataFile(null);
+It(null);%0A%09%09
 %0A%09%09%0A

use assertIt for null