Skip to content

Commit

Permalink
Rewrite groovy test as a junit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tara Drwenski committed Dec 11, 2023
1 parent 9e64be4 commit 1705c65
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 64 deletions.
64 changes: 0 additions & 64 deletions cdm/core/src/test/groovy/ucar/nc2/iosp/netcdf3/N3iospSpec.groovy

This file was deleted.

56 changes: 56 additions & 0 deletions cdm/core/src/test/java/ucar/nc2/iosp/netcdf3/TestN3iosp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ucar.nc2.iosp.netcdf3;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import org.junit.Test;

public class TestN3iosp {

@Test
public void shouldReturnInvalidForNullOrEmptyNames() {
assertThat(N3iosp.isValidNetcdfObjectName(null)).isFalse();
assertThrows(NullPointerException.class, () -> N3iosp.makeValidNetcdfObjectName(null));

assertThat(N3iosp.isValidNetcdfObjectName("")).isFalse();
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> N3iosp.makeValidNetcdfObjectName(""));
assertThat(e.getMessage()).isEqualTo("Illegal NetCDF object name: ''");
}

@Test
public void shouldReturnInvalidForBadFirstCharacters() {
// names with first chars not in ([a-zA-Z0-9_]|{UTF8}) are invalid
assertThat(N3iosp.isValidNetcdfObjectName(" blah")).isFalse();
assertThat(N3iosp.makeValidNetcdfObjectName(" blah")).isEqualTo("blah");

assertThat(N3iosp.isValidNetcdfObjectName("\n/blah")).isFalse();
assertThat(N3iosp.makeValidNetcdfObjectName("\n/blah")).isEqualTo("blah");

// Unit separator and DEL
assertThat(N3iosp.isValidNetcdfObjectName("\u001F\u007F blah")).isFalse();
assertThat(N3iosp.makeValidNetcdfObjectName("\u001F\u007F blah")).isEqualTo("blah");
}

@Test
public void shouldReturnInvalidForBadCharacters() {
// names with remaining chars not in ([^\x00-\x1F\x7F/]|{UTF8})* are invalid
assertThat(N3iosp.isValidNetcdfObjectName("1\u000F2\u007F3/4")).isFalse();
assertThat(N3iosp.makeValidNetcdfObjectName("1\u000F2\u007F3/4")).isEqualTo("1234");

// names may not have trailing spaces
assertThat(N3iosp.isValidNetcdfObjectName("foo ")).isFalse();
assertThat(N3iosp.makeValidNetcdfObjectName("foo ")).isEqualTo("foo");
}

@Test
public void shouldAcceptValidNames() {
// valid names have syntax: ([a-zA-Z0-9_]|{UTF8})([^\x00-\x1F\x7F/]|{UTF8})*
assertThat(N3iosp.isValidNetcdfObjectName("_KfS9Jn_s9__")).isTrue();
assertThat(N3iosp.makeValidNetcdfObjectName("_KfS9Jn_s9__")).isEqualTo("_KfS9Jn_s9__");

// unicode characters greater than 0x7F can appear anywhere
assertThat(N3iosp.isValidNetcdfObjectName("\u0123\u1234\u2345\u3456")).isTrue();
assertThat(N3iosp.makeValidNetcdfObjectName("\u0123\u1234\u2345\u3456")).isEqualTo("\u0123\u1234\u2345\u3456");
}
}

0 comments on commit 1705c65

Please sign in to comment.