From: <rit...@us...> - 2010-06-22 23:14:29
|
Revision: 20 http://netcdftools.svn.sourceforge.net/netcdftools/?rev=20&view=rev Author: ritacsiro Date: 2010-06-22 23:14:22 +0000 (Tue, 22 Jun 2010) Log Message: ----------- ANDSWRON-678 - Add variable parameter to defineAttributes. 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-22 07:16:36 UTC (rev 19) +++ trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java 2010-06-22 23:14:22 UTC (rev 20) @@ -56,7 +56,7 @@ /** * The name of the command line option used for specifying the global attributes of a netCDF file. */ - private static final String GLOBAL_ATTRIBUTES = "globalAttributes"; + private static final String ATTRIBUTES = "attributes"; /** * The name of the netCDF file to write to. @@ -167,8 +167,8 @@ assertTrue("The following option is not recognised by the command: " + OUTPUT_FILE, options.hasOption(OUTPUT_FILE)); - assertTrue("The following option is not recognised by the command: " + GLOBAL_ATTRIBUTES, - options.hasOption(GLOBAL_ATTRIBUTES)); + assertTrue("The following option is not recognised by the command: " + ATTRIBUTES, + options.hasOption(ATTRIBUTES)); } @@ -212,7 +212,7 @@ { String[] args = new String[] { ncDefineAttr.getCommandName(), "-" + OUTPUT_FILE, NC_FILE_NAME, - "-" + GLOBAL_ATTRIBUTES, ATTR1_NAME+"="+ATTR1_DATA }; + "-" + ATTRIBUTES, ATTR1_NAME+"="+ATTR1_DATA }; try { @@ -265,7 +265,7 @@ { String[] args = new String[] { ncDefineAttr.getCommandName(), "-" + OUTPUT_FILE, NC_FILE_NAME, - "-" + GLOBAL_ATTRIBUTES, ATTR1_NAME + "=" + ATTR1_DATA + "," + ATTR2_NAME + "=" + ATTR2_DATA }; + "-" + ATTRIBUTES, ATTR1_NAME + "=" + ATTR1_DATA + "," + ATTR2_NAME + "=" + ATTR2_DATA }; try { @@ -306,7 +306,7 @@ public final void testMissingRequiredOption() { String[] args = new String[] { ncDefineAttr.getCommandName(), - "-" + GLOBAL_ATTRIBUTES, ATTR1_NAME+"="+ATTR1_DATA }; + "-" + ATTRIBUTES, ATTR1_NAME+"="+ATTR1_DATA }; String errors = ncDefineAttr.validCommand(args); assertTrue("Command without output file option should return an error", !errors.isEmpty()); @@ -320,12 +320,12 @@ { String[] args = new String[] { ncDefineAttr.getCommandName(), "-" + OUTPUT_FILE, NC_FILE_NAME, - "-" + GLOBAL_ATTRIBUTES, ATTR1_NAME}; + "-" + ATTRIBUTES, ATTR1_NAME}; String errors = ncDefineAttr.validCommand(args); assertTrue("Command with invalid attribute string should return an error", !errors.isEmpty()); assertEquals("Error string was not correct", - "\nglobalAttributes value is not a comma separated String of attribute-value pairs: Attr1", errors); + "\nattributes value is not a comma separated String of attribute-value pairs: Attr1", errors); } /** @@ -337,7 +337,7 @@ "-" + OUTPUT_FILE, NC_FILE_NAME}; String errors = ncDefineAttr.validCommand(args); - assertEquals("Command with missing " + GLOBAL_ATTRIBUTES + " attribute should not return an error", "", errors); + assertEquals("Command with missing " + ATTRIBUTES + " attribute should not return an error", "", errors); } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <rit...@us...> - 2010-06-24 01:17:18
|
Revision: 30 http://netcdftools.svn.sourceforge.net/netcdftools/?rev=30&view=rev Author: ritacsiro Date: 2010-06-24 01:17:12 +0000 (Thu, 24 Jun 2010) Log Message: ----------- ANDSWRON-682 - updated code based on review comment. 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-24 00:29:08 UTC (rev 29) +++ trunk/src/test/java/au/csiro/netcdf/TestNcDefineAttributes.java 2010-06-24 01:17:12 UTC (rev 30) @@ -628,35 +628,46 @@ "-" + OUTPUT_FILE, NC_FILE_NAME, "-" + ATTRIBUTES, ATTR3_NAME + "=" + ATTR3_DATA, "-t", "float"}; + NetcdfFile netcdfFile = null; + File ncFile = null; - File ncFile = new File(NC_FILE_NAME); - if (ncFile.exists()) + try { - ncFile.delete(); + 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.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)); } - - 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(); + finally + { + if(netcdfFile != null && ncFile != null) + { + netcdfFile.close(); + ncFile.delete(); + } + } } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |