From: <rit...@us...> - 2010-06-23 02:55:23
|
Revision: 23 http://netcdftools.svn.sourceforge.net/netcdftools/?rev=23&view=rev Author: ritacsiro Date: 2010-06-23 02:55:17 +0000 (Wed, 23 Jun 2010) Log Message: ----------- ANDSWRON-682 - Update test cases for defineAttribute. Modified Paths: -------------- trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java Modified: trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java =================================================================== --- trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java 2010-06-23 02:23:03 UTC (rev 22) +++ trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java 2010-06-23 02:55:17 UTC (rev 23) @@ -29,8 +29,12 @@ import org.apache.commons.cli.Options; +import ucar.ma2.DataType; import ucar.nc2.Attribute; +import ucar.nc2.Dimension; import ucar.nc2.NetcdfFile; +import ucar.nc2.NetcdfFileWriteable; +import ucar.nc2.Variable; import au.csiro.netcdf.cli.Command; import au.csiro.netcdf.util.NetCDFUtils; @@ -58,6 +62,19 @@ * The name of the command line option used for specifying the global attributes of a netCDF file. */ private static final String ATTRIBUTES = "attributes"; + + /** + * The name of the command line option used for specifying the name of the variable. + */ + private static final String VARIABLE_NAME = "variable"; + + /** Data for the variable */ + private static final String VARIABLE_NAME_VALUE = "myVar"; + + /** + * The testing value for the dimension option. + */ + private static final String DIM_NAME = "myDimension"; /** * The name of the netCDF file to write to. @@ -600,4 +617,234 @@ } } } + + /** + * Test delete a global attribute + * @throws Exception + */ + public final void testDeleteGlobalAttribute() throws Exception + { + String[] args = new String[] { ncDefineAttr.getCommandName(), + "-" + OUTPUT_FILE, NC_FILE_NAME, + "-" + ATTRIBUTES, ATTR3_NAME + "=" + ATTR3_DATA, + "-t", "float"}; + + File ncFile = new File(NC_FILE_NAME); + if (ncFile.exists()) + { + ncFile.delete(); + } + + ncDefineAttr.execute(args); + assertTrue("The nc file was not created: " + NC_FILE_NAME, ncFile.exists()); + + NetcdfFile netcdfFile = NetcdfFile.open(NC_FILE_NAME); + Attribute attr = netcdfFile.findGlobalAttribute(ATTR3_NAME); + assertTrue("The Attribute was not defined: " + ATTR3_NAME, (attr != null)); + assertEquals("The Attrbute " + ATTR3_NAME + " had a string value. ", + null, attr.getStringValue()); + assertEquals("The Attrbute had the wrong data: " + ATTR3_NAME, + Float.parseFloat(ATTR3_DATA), (Float)attr.getNumericValue(), 0.01f); + + netcdfFile.close(); + + // now delete the attribute + args[4] = ATTR3_NAME+"="+"null"; + ncDefineAttr.execute(args); + netcdfFile = NetcdfFile.open(NC_FILE_NAME); + + attr = netcdfFile.findGlobalAttribute(ATTR3_NAME); + assertTrue("The Attribute was not deleted: " + ATTR3_NAME, (attr == null)); + netcdfFile.close(); + ncFile.delete(); + } + + /** + * Test delete a variable attribute + * @throws IOException + */ + public final void testDeleteVariableAttribute() throws IOException + { + String[] args = new String[] { ncDefineAttr.getCommandName(), + "-" + OUTPUT_FILE, NC_FILE_NAME, + "-" + ATTRIBUTES, + ATTR1_NAME+"="+ATTR1_DATA, + "-"+VARIABLE_NAME, + VARIABLE_NAME_VALUE}; + NetcdfFile netcdfFile = null; + File ncFile = null; + + try + { + ncFile = new File(NC_FILE_NAME); + if (ncFile.exists()) + { + ncFile.delete(); + } + + // create dummy netCDF file with content. + createDummyNCFile(NC_FILE_NAME, DataType.CHAR); + assertTrue("The nc file was not created: " + NC_FILE_NAME, ncFile.exists()); + + ncDefineAttr.execute(args); + netcdfFile = NetcdfFile.open(NC_FILE_NAME); + + Variable variable = netcdfFile.findVariable(VARIABLE_NAME_VALUE); + assertTrue("The Variable was not defined: " + VARIABLE_NAME_VALUE, (variable != null)); + + Attribute attr = variable.findAttribute(ATTR1_NAME); + assertTrue("The Attribute was not defined: " + ATTR1_NAME, (attr != null)); + assertTrue("The Attrbute had the wrong data: " + ATTR1_NAME, attr.getStringValue().contains( + ATTR1_DATA)); + + netcdfFile.close(); + + // now delete the attribute + args[4] = ATTR1_NAME+"="+"null"; + ncDefineAttr.execute(args); + netcdfFile = NetcdfFile.open(NC_FILE_NAME); + variable = netcdfFile.findVariable(VARIABLE_NAME_VALUE); + assertTrue("The Variable was not defined: " + VARIABLE_NAME_VALUE, (variable != null)); + + attr = variable.findAttribute(ATTR1_NAME); + assertTrue("The Attribute was not deleted: " + ATTR1_NAME, (attr == null)); + + }catch (Exception e) + { + e.printStackTrace(); + fail(e.getMessage()); + } + finally + { + if(netcdfFile != null && ncFile != null) + { + netcdfFile.close(); + ncFile.delete(); + } + } + } + + /** + * Test adding attributes to an existing variable + * + * @throws IOException + */ + public final void testVariableAttribute() throws IOException + { + String[] args = new String[] { ncDefineAttr.getCommandName(), + "-" + OUTPUT_FILE, NC_FILE_NAME, + "-" + ATTRIBUTES, + ATTR1_NAME+"="+ATTR1_DATA, + "-"+VARIABLE_NAME, + VARIABLE_NAME_VALUE}; + NetcdfFile netcdfFile = null; + File ncFile = null; + + try + { + ncFile = new File(NC_FILE_NAME); + if (ncFile.exists()) + { + ncFile.delete(); + } + + // create dummy netCDF file with content. + createDummyNCFile(NC_FILE_NAME, DataType.CHAR); + assertTrue("The nc file was not created: " + NC_FILE_NAME, ncFile.exists()); + + ncDefineAttr.execute(args); + netcdfFile = NetcdfFile.open(NC_FILE_NAME); + + Variable variable = netcdfFile.findVariable(VARIABLE_NAME_VALUE); + assertTrue("The Variable was not defined: " + VARIABLE_NAME_VALUE, (variable != null)); + + Attribute attr = variable.findAttribute(ATTR1_NAME); + assertTrue("The Attribute was not defined: " + ATTR1_NAME, (attr != null)); + assertTrue("The Attrbute had the wrong data: " + ATTR1_NAME, attr.getStringValue().contains( + ATTR1_DATA)); + + }catch (Exception e) + { + e.printStackTrace(); + fail(e.getMessage()); + } + finally + { + if(netcdfFile != null && ncFile != null) + { + netcdfFile.close(); + ncFile.delete(); + } + } + } + + /** + * Test adding attributes to a non-existing variable + * + * @throws Exception + */ + public final void testVariableAttributeNoVar() throws Exception + { + String[] args = new String[] { ncDefineAttr.getCommandName(), + "-" + OUTPUT_FILE, NC_FILE_NAME }; + try + { + File ncFile = new File(NC_FILE_NAME); + if (ncFile.exists()) + { + ncFile.delete(); + } + + ncDefineAttr.execute(args); + assertTrue("The nc file was not created: " + NC_FILE_NAME, ncFile.exists()); + + // Now run a second time on the same file + args = new String[] { ncDefineAttr.getCommandName(), + "-" + OUTPUT_FILE, NC_FILE_NAME, + ATTR1_NAME+"="+ATTR1_DATA, + "-"+VARIABLE_NAME, + VARIABLE_NAME_VALUE}; + + ncDefineAttr.execute(args); + fail("adding attribute to non-existing variable should have failed."); + + } + catch (IllegalArgumentException expected) + { + // expected exception + } + catch (Exception e) + { + throw e; + } + finally + { + File ncFile = new File(NC_FILE_NAME); + if (ncFile.exists()) + { + ncFile.delete(); + } + } + } + + /** + * Makes a dummy netCDF file with known contents. + */ + private void createDummyNCFile(String outputFilename, DataType dType) throws IOException + { + NetcdfFileWriteable ncFile = null; + + ncFile = NetcdfFileWriteable.createNew(outputFilename, true); + try + { + Dimension dimension = new Dimension(DIM_NAME, 10, true /* isShared */, false /* isUnlimited */, false /* isVariableLength */); + ncFile.addDimension(null, dimension); + ncFile.addVariable(VARIABLE_NAME_VALUE, dType, DIM_NAME); + ncFile.create(); + } + finally + { + ncFile.close(); + } + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |