/*
 * jETeL/CloverETL - Java based ETL application framework.
 * Copyright (c) Javlin, a.s. (info@cloveretl.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 org.jetel.data;

import java.nio.ByteBuffer;

import org.jetel.metadata.DataFieldMetadata;
import org.jetel.metadata.DataRecordMetadata;
import org.jetel.test.CloverTestCase;

/**
 * @author Martin Janik, Javlin a.s. &lt;martin.janik@javlin.eu&gt;
 *
 * @version 15th December 2010
 * @since 15th December 2010
 */
public class DataRecordTest extends CloverTestCase {

	private static final String DELIMITER = ";";

	private DataRecord record;

	protected void setUp() {
		initEngine();
		Defaults.Record.USE_FIELDS_NULL_INDICATORS = true;

		DataRecordMetadata metadata = new DataRecordMetadata("record");
		metadata.addField(createDataField(DataFieldMetadata.STRING_FIELD, "string", false));
		metadata.addField(createDataField(DataFieldMetadata.STRING_FIELD, "nullString", true));
		metadata.addField(createDataField(DataFieldMetadata.INTEGER_FIELD, "integer", false));
		metadata.addField(createDataField(DataFieldMetadata.INTEGER_FIELD, "nullInteger", true));
		metadata.addField(createDataField(DataFieldMetadata.BOOLEAN_FIELD, "boolean", false));
		metadata.addField(createDataField(DataFieldMetadata.BOOLEAN_FIELD, "nullBoolean", true));

		record = new DataRecord(metadata);
		record.init();

		record.getField(0).setValue("String value");
		record.getField(2).setValue(73);
		record.getField(4).setValue(true);
	}

	private DataFieldMetadata createDataField(char dataType, String name, boolean nullable) {
		DataFieldMetadata field = new DataFieldMetadata(name, dataType, DELIMITER);
		field.setNullable(nullable);

		return field;
	}

	/**
	 * Test method for {@link org.jetel.data.DataRecord#serialize(java.nio.ByteBuffer)}.
	 */
	public void testSerializeByteBuffer() {
		DataRecord deserializedRecord = new DataRecord(record.getMetadata());
		deserializedRecord.init();

		ByteBuffer buffer = ByteBuffer.allocateDirect(Defaults.Record.MAX_RECORD_SIZE);
		record.serialize(buffer);
		buffer.flip();
		deserializedRecord.deserialize(buffer);

		assertEquals(record, deserializedRecord);
	}

}

@@ -1064,16 +1064,61 @@
 %0A%0D%0A/**%0D%0A
+ * Tests the DataRecord implementation.%0D%0A *%0D%0A
  * @auth
@@ -1331,30 +1331,82 @@
 nal 
-String DELIMITER = %22;%22
+int NUMBER_OF_FIELDS = 16;%0D%0A%09private static final int NULL_FIELDS = 0xDDBA
 ;%0D%0A%0D
@@ -1480,16 +1480,95 @@
 gine();%0D
+%0A%0D%0A%09%09// make sure that the optimized serialization of null values is turned on%0D
 %0A%09%09Defau
@@ -1687,189 +1687,61 @@
 );%0D%0A
-%09%09metadata.addField(createDataField(DataFieldMetadata.STRING_FIELD, %22string%22, false));%0D%0A%09%09metadata.addField(createDataField(DataFieldMetadata.STRING_FIELD, %22nullString%22, true));
+%0D%0A%09%09for (int i = 0; i %3C NUMBER_OF_FIELDS; i++) %7B
 %0D%0A%09%09
+%09
 meta
@@ -1754,32 +1754,20 @@
 ddField(
-createDataField(
+new 
 DataFiel
@@ -1779,319 +1779,62 @@
 data
-.INTEGER_FIELD, %22integer%22, false));%0D%0A%09%09metadata.addField(createDataField(DataFieldMetadata.INTEGER_FIELD, %22nullInteger%22, true));%0D%0A%09%09metadata.addField(createDataField(DataFieldMetadata.BOOLEAN_FIELD, %22boolean%22, false));%0D%0A%09%09metadata.addField(createDataField(DataFieldMetadata.BOOLEAN_FIELD, %22nullBoolean%22, true));
+(%22f%22 + i, DataFieldMetadata.INTEGER_FIELD, %22;%22));%0D%0A%09%09%7D
 %0D%0A%0D%0A
@@ -1897,54 +1897,55 @@
 %0D%0A%09%09
-record.getField(0).setValue(%22String value%22);
+for (int i = 0; i %3C NUMBER_OF_FIELDS; i++) %7B
 %0D%0A
+%09
 %09%09re
@@ -1958,17 +1958,17 @@
 etField(
-2
+i
 ).setVal
@@ -1974,278 +1974,57 @@
 lue(
-73);%0D%0A%09%09record.getField(4).setValue(true);%0D%0A%09%7D%0D%0A%0D%0A%09private DataFieldMetadata createDataField(char dataType, String name, boolean nullable) %7B%0D%0A%09%09DataFieldMetadata field = new DataFieldMetadata(name, dataType, DELIMITER);%0D%0A%09%09field.setNullable(nullable);%0D%0A%0D%0A%09%09return field;
+(((1 %3C%3C i) & NULL_FIELDS) != 0) ? null : i);%0D%0A%09%09%7D
 %0D%0A%09%7D
@@ -2152,26 +2152,16 @@
 erialize
-ByteBuffer
 () %7B%0D%0A%09%09
@@ -2444,20 +2444,207 @@
 %0D%0A%09%09
-assertEquals
+RecordKey comparator = new RecordKey(record.getMetadata().getFieldNamesArray(), record.getMetadata());%0D%0A%09%09comparator.setEqualNULLs(true);%0D%0A%09%09comparator.init();%0D%0A%0D%0A%09%09assertEquals(0, comparator.compare
 (rec
@@ -2667,16 +2667,17 @@
 dRecord)
+)
 ;%0D%0A%09%7D%0D%0A%0D

Test enhanced.