|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 University Corporation for Atmospheric Research/Unidata |
| 3 | + * See LICENSE for license information. |
| 4 | + */ |
| 5 | +package ucar.nc2.iosp.hdf5; |
| 6 | + |
| 7 | +import org.junit.AfterClass; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.junit.runners.Parameterized; |
| 11 | +import ucar.nc2.Attribute; |
| 12 | +import ucar.nc2.NetcdfFile; |
| 13 | +import ucar.nc2.NetcdfFiles; |
| 14 | +import ucar.nc2.Variable; |
| 15 | +import ucar.unidata.util.test.TestDir; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collection; |
| 20 | + |
| 21 | +import static com.google.common.truth.Truth.assertThat; |
| 22 | + |
| 23 | +/** Test String Attributes */ |
| 24 | +@RunWith(Parameterized.class) |
| 25 | +public class TestNullStringAttr { |
| 26 | + |
| 27 | + private static NetcdfFile ncfOld, ncfNew; |
| 28 | + private static final String testFile = TestDir.cdmLocalTestDataDir + "hdf5/string_attrs.nc4"; |
| 29 | + |
| 30 | + private final NetcdfFile ncf; |
| 31 | + |
| 32 | + public TestNullStringAttr(String apiType, NetcdfFile ncf) { |
| 33 | + this.ncf = ncf; |
| 34 | + } |
| 35 | + |
| 36 | + @Parameterized.Parameters(name = "{0}") |
| 37 | + public static Collection<?> netcdfFileObjects() throws IOException { |
| 38 | + ncfOld = NetcdfFile.open(testFile); |
| 39 | + ncfNew = NetcdfFiles.open(testFile); |
| 40 | + return Arrays.asList(new Object[][] {{"Old API", ncfOld}, {"New API", ncfNew},}); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testNullStringGlobalAttr() { |
| 45 | + Attribute attr = ncf.findGlobalAttribute("NULL_STR_GATTR"); |
| 46 | + assertThat(attr).isNotNull(); |
| 47 | + String value = attr.getStringValue(); |
| 48 | + assertThat(value).isNotNull(); |
| 49 | + assertThat(value).isNotEmpty(); |
| 50 | + assertThat(value).isNotEqualTo(""); |
| 51 | + assertThat(value).isEqualTo("NIL"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testEmptyStringGlobalAttr() { |
| 56 | + Attribute attr = ncf.findGlobalAttribute("EMPTY_STR_GATTR"); |
| 57 | + assertThat(attr).isNotNull(); |
| 58 | + String value = attr.getStringValue(); |
| 59 | + assertThat(value).isNotNull(); |
| 60 | + assertThat(value).isEmpty(); |
| 61 | + assertThat(value).isEqualTo(""); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testNullStringAttr() { |
| 66 | + Variable testVar = ncf.findVariable("/var"); |
| 67 | + assert null != testVar; |
| 68 | + Attribute attr = testVar.findAttribute("NULL_STR_ATTR"); |
| 69 | + assertThat(attr).isNotNull(); |
| 70 | + String value = attr.getStringValue(); |
| 71 | + assertThat(value).isNotNull(); |
| 72 | + assertThat(value).isNotEmpty(); |
| 73 | + assertThat(value).isNotEqualTo(""); |
| 74 | + assertThat(value).isEqualTo("NIL"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testEmptyStringAttr() { |
| 79 | + Variable testVar = ncf.findVariable("/var"); |
| 80 | + assert null != testVar; |
| 81 | + Attribute attr = testVar.findAttribute("EMPTY_STR_ATTR"); |
| 82 | + assertThat(attr).isNotNull(); |
| 83 | + String value = attr.getStringValue(); |
| 84 | + assertThat(value).isNotNull(); |
| 85 | + assertThat(value).isEmpty(); |
| 86 | + assertThat(value).isEqualTo(""); |
| 87 | + } |
| 88 | + |
| 89 | + @AfterClass |
| 90 | + public static void closeFile() throws IOException { |
| 91 | + ncfOld.close(); |
| 92 | + ncfNew.close(); |
| 93 | + } |
| 94 | +} |
0 commit comments