/**
*
*/
package test.object;
import java.util.List;
import junit.framework.TestCase;
import ncsa.hdf.hdf5lib.H5;
import ncsa.hdf.hdf5lib.HDF5Constants;
import ncsa.hdf.object.Attribute;
import ncsa.hdf.object.DataFormat;
import ncsa.hdf.object.Datatype;
import ncsa.hdf.object.FileFormat;
import ncsa.hdf.object.h5.H5Datatype;
import ncsa.hdf.object.h5.H5File;
/**
* @author rsinha
*
*/
public class DataFormatTest extends TestCase {
private static final H5File H5FILE = new H5File();
private H5File testFile = null;
private DataFormat testGroup = null;
/**
* @param arg0
*/
public DataFormatTest(String arg0) {
super(arg0);
}
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#setUp()
*/
@Override
protected void setUp() throws Exception {
super.setUp();
testFile = (H5File) H5FILE.open(H5TestFile.NAME_FILE_H5,
FileFormat.WRITE);
assertNotNull(testFile);
testGroup = testFile.get(H5TestFile.NAME_GROUP_ATTR);
assertNotNull(testGroup);
}
/*
* (non-Javadoc)
*
* @see junit.framework.TestCase#tearDown()
*/
@Override
protected void tearDown() throws Exception {
super.tearDown();
if (testFile != null) {
try {
testFile.close();
}
catch (final Exception ex) {
}
testFile = null;
}
}
/**
* Test method for {@link ncsa.hdf.object.DataFormat#getFile()}.
*
* - Test if the file name is correct
*
*/
public final void testGetFile() {
if (!testGroup.getFile().equals(H5TestFile.NAME_FILE_H5)) {
fail("getFile() fails.");
}
int nObjs = 0;
try {
nObjs = H5.H5Fget_obj_count(testFile.getFID(),
HDF5Constants.H5F_OBJ_ALL);
}
catch (final Exception ex) {
fail("H5.H5Fget_obj_count() failed. " + ex);
}
assertEquals(1, nObjs); // file id should be the only one left open
}
/**
* Test method for {@link ncsa.hdf.object.DataFormat#getMetadata()}.
*
* - Reading the attributes
*
- Checking the values of attributes
*
*/
public final void testGetMetadata() {
Attribute strAttr = null;
Attribute arrayIntAttr = null;
List mdataList = null;
try {
mdataList = testGroup.getMetadata();
}
catch (final Exception ex) {
fail("getMetadata() failed. " + ex);
}
strAttr = (Attribute) mdataList.get(0);
arrayIntAttr = (Attribute) mdataList.get(1);
String[] value = (String[]) strAttr.getValue();
if (!value[0].equals("String attribute.")) {
fail("getMdata() failed.");
}
int[] intValue = (int[]) arrayIntAttr.getValue();
long[] dims = arrayIntAttr.getDataDims();
for (int i = 0; i < dims[0]; i++) {
if (intValue[i] != i + 1) {
fail("getValue() failed");
}
}
int nObjs = 0;
try {
nObjs = H5.H5Fget_obj_count(testFile.getFID(),
HDF5Constants.H5F_OBJ_ALL);
}
catch (final Exception ex) {
fail("H5.H5Fget_obj_count() failed. " + ex);
}
assertEquals(1, nObjs); // file id should be the only one left open
}
/**
* Test method for
* {@link ncsa.hdf.object.DataFormat#writeMetadata(java.lang.Object)}.
*
* - Writing new attributes
*
- Checking that the new attributes are written in file
*
*/
public final void testWriteMetadata() {
long[] attrDims = { 1 };
String attrName = "CLASS";
String[] classValue = { "IMAGE" };
Datatype attrType = new H5Datatype(Datatype.CLASS_STRING, classValue[0]
.length() + 1, -1, -1);
Attribute attr = new Attribute(attrName, attrType, attrDims);
assertNotNull(testGroup);
assertNotNull(attr);
attr.setValue(classValue);
try {
testGroup.writeMetadata(attr);
}
catch (Exception ex) {
fail("writeMetadata() failed " + ex.getMessage());
}
List mdataList = null;
try {
mdataList = testGroup.getMetadata();
}
catch (final Exception ex) {
fail("getMetadata() failed. " + ex);
}
assertEquals(3, mdataList.size());
Attribute strAttr = null;
Attribute arrayIntAttr = null;
strAttr = (Attribute) mdataList.get(0);
arrayIntAttr = (Attribute) mdataList.get(1);
String[] value = (String[]) strAttr.getValue();
if (!value[0].equals("String attribute.")) {
fail("writeMdata() failed.");
}
int[] intValue = (int[]) arrayIntAttr.getValue();
long[] dims = arrayIntAttr.getDataDims();
for (int i = 0; i < dims[0]; i++) {
if (intValue[i] != i + 1) {
fail("writeValue() failed");
}
}
strAttr = (Attribute) mdataList.get(2);
value = (String[]) strAttr.getValue();
if (!value[0].equals("IMAGE")) {
fail("writeMetadata() failed.");
}
int nObjs = 0;
try {
nObjs = H5.H5Fget_obj_count(testFile.getFID(),
HDF5Constants.H5F_OBJ_ALL);
}
catch (final Exception ex) {
fail("H5.H5Fget_obj_count() failed. " + ex);
}
assertEquals(1, nObjs); // file id should be the only one left open
}
/**
* Test method for
* {@link ncsa.hdf.object.DataFormat#removeMetadata(java.lang.Object)}.
*
*/
public final void testRemoveMetadata() {
List mdataList = null;
try {
mdataList = testGroup.getMetadata();
}
catch (final Exception ex) {
fail("getMetadata() failed. " + ex.getMessage());
}
Attribute strAttr = (Attribute) mdataList.get(2);
try {
testGroup.removeMetadata(strAttr);
}
catch (Exception e) {
fail("removeMetadata() failed " + e.getMessage());
}
assertEquals(2, mdataList.size());
int nObjs = 0;
try {
nObjs = H5.H5Fget_obj_count(testFile.getFID(),
HDF5Constants.H5F_OBJ_ALL);
}
catch (final Exception ex) {
fail("H5.H5Fget_obj_count() failed. " + ex);
}
assertEquals(1, nObjs); // file id should be the only one left open
}
}