Skip to content

Commit

Permalink
return optional ofNullable, as the boundary condition may be undefine…
Browse files Browse the repository at this point in the history
…d during initialization.
  • Loading branch information
heySourabh committed Aug 17, 2023
1 parent 8f0b970 commit 611afab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/mesh/Boundary.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class Boundary {
private BoundaryCondition bc;

public Boundary(String name, List<Face> faces, BoundaryCondition bc) {
this.name = Objects.requireNonNull(name);
this.faces = Objects.requireNonNull(faces);
this.name = Objects.requireNonNull(name, "Boundary 'name' cannot be null.");
this.faces = Objects.requireNonNull(faces, "Boundary 'faces' cannot be null.");
this.bc = bc;
}

Expand All @@ -22,6 +22,6 @@ public void setBC(BoundaryCondition bc) {
}

public Optional<BoundaryCondition> bc() {
return Optional.of(bc);
return Optional.ofNullable(bc);
}
}
31 changes: 31 additions & 0 deletions test/main/mesh/BoundaryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main.mesh;

import org.junit.Test;

import java.util.List;
import java.util.Optional;

import static org.junit.Assert.*;

public class BoundaryTest {

@Test
public void constructor_nullability_test() {
// null name
assertThrows(NullPointerException.class, () -> new Boundary(null, List.of(), null));
// null faces
assertThrows(NullPointerException.class, () -> new Boundary("name", null, null));
}

@Test
public void bc_optionality_test() {
Boundary bnd = new Boundary("bnd name", List.of(), null);
assertEquals(Optional.empty(), bnd.bc());
}

@Test
public void trying_to_set_null_bc() {
Boundary bnd = new Boundary("bnd name", List.of(), null);
assertThrows(NullPointerException.class, () -> bnd.setBC(null));
}
}

0 comments on commit 611afab

Please sign in to comment.