diff --git a/jhdf/src/main/java/io/jhdf/Constants.java b/jhdf/src/main/java/io/jhdf/Constants.java index e9d1d055..ad043500 100644 --- a/jhdf/src/main/java/io/jhdf/Constants.java +++ b/jhdf/src/main/java/io/jhdf/Constants.java @@ -16,6 +16,10 @@ */ public final class Constants { + private Constants() { + throw new AssertionError("No instances of Constants"); + } + public static final byte NULL = '\0'; public static final byte SPACE = ' '; @@ -25,6 +29,4 @@ public final class Constants { public static final String PATH_SEPARATOR = "/"; - private Constants() { - } // No instances } diff --git a/jhdf/src/main/java/io/jhdf/Utils.java b/jhdf/src/main/java/io/jhdf/Utils.java index f323223d..58909353 100644 --- a/jhdf/src/main/java/io/jhdf/Utils.java +++ b/jhdf/src/main/java/io/jhdf/Utils.java @@ -25,7 +25,7 @@ public final class Utils { private static final CharsetEncoder ASCII = StandardCharsets.US_ASCII.newEncoder(); private Utils() { - // No instances + throw new AssertionError("No instances of Utils"); } /** diff --git a/jhdf/src/main/java/io/jhdf/dataset/CompoundDatasetReader.java b/jhdf/src/main/java/io/jhdf/dataset/CompoundDatasetReader.java index 5bf76a54..29a59521 100644 --- a/jhdf/src/main/java/io/jhdf/dataset/CompoundDatasetReader.java +++ b/jhdf/src/main/java/io/jhdf/dataset/CompoundDatasetReader.java @@ -21,8 +21,8 @@ public final class CompoundDatasetReader { - /** No instances */ private CompoundDatasetReader() { + throw new AssertionError("No instances of CompoundDatasetReader"); } public static Map readDataset(CompoundDataType type, ByteBuffer buffer, int[] dimensions, HdfFileChannel hdfFc) { diff --git a/jhdf/src/main/java/io/jhdf/dataset/DatasetLoader.java b/jhdf/src/main/java/io/jhdf/dataset/DatasetLoader.java index 7bf0d9ec..24f8d3a2 100644 --- a/jhdf/src/main/java/io/jhdf/dataset/DatasetLoader.java +++ b/jhdf/src/main/java/io/jhdf/dataset/DatasetLoader.java @@ -25,7 +25,7 @@ public final class DatasetLoader { private DatasetLoader() { - // No instances + throw new AssertionError("No instances of DatasetLoader"); } public static Dataset createDataset(HdfFileChannel hdfFc, ObjectHeader oh, String name, diff --git a/jhdf/src/main/java/io/jhdf/dataset/DatasetReader.java b/jhdf/src/main/java/io/jhdf/dataset/DatasetReader.java index fd14fcdb..251ca0ef 100644 --- a/jhdf/src/main/java/io/jhdf/dataset/DatasetReader.java +++ b/jhdf/src/main/java/io/jhdf/dataset/DatasetReader.java @@ -36,7 +36,6 @@ */ public final class DatasetReader { - /** No instances */ private DatasetReader() { throw new AssertionError("No instances of DatasetReader"); } diff --git a/jhdf/src/main/java/io/jhdf/dataset/VariableLengthDatasetReader.java b/jhdf/src/main/java/io/jhdf/dataset/VariableLengthDatasetReader.java index 1033f08d..e2f6632d 100644 --- a/jhdf/src/main/java/io/jhdf/dataset/VariableLengthDatasetReader.java +++ b/jhdf/src/main/java/io/jhdf/dataset/VariableLengthDatasetReader.java @@ -29,8 +29,8 @@ public final class VariableLengthDatasetReader { - /** No instances */ private VariableLengthDatasetReader() { + throw new AssertionError("No instances of VariableLengthDatasetReader"); } public static Object readDataset(VariableLength type, ByteBuffer buffer, int[] dimensions, HdfFileChannel hdfFc) { diff --git a/jhdf/src/test/java/io/jhdf/NoInstancesTest.java b/jhdf/src/test/java/io/jhdf/NoInstancesTest.java new file mode 100644 index 00000000..fc135cac --- /dev/null +++ b/jhdf/src/test/java/io/jhdf/NoInstancesTest.java @@ -0,0 +1,46 @@ +/* + * This file is part of jHDF. A pure Java library for accessing HDF5 files. + * + * http://jhdf.io + * + * Copyright (c) 2020 James Mudd + * + * MIT License see 'LICENSE' file + */ +package io.jhdf; + +import io.jhdf.dataset.CompoundDatasetReader; +import io.jhdf.dataset.DatasetLoader; +import io.jhdf.dataset.DatasetReader; +import io.jhdf.dataset.EnumDatasetReader; +import io.jhdf.dataset.VariableLengthDatasetReader; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.powermock.reflect.Whitebox.invokeConstructor; + +class NoInstancesTest { + + static Stream noInstances() { + return Stream.of( + Arguments.of(DatasetLoader.class), + Arguments.of(DatasetReader.class), + Arguments.of(CompoundDatasetReader.class), + Arguments.of(EnumDatasetReader.class), + Arguments.of(VariableLengthDatasetReader.class), + Arguments.of(Constants.class), + Arguments.of(Utils.class) + ); + } + + @ParameterizedTest + @MethodSource + void noInstances(Class clazz) { + assertThrows(AssertionError.class, () -> invokeConstructor(clazz)); + } + +} diff --git a/jhdf/src/test/java/io/jhdf/dataset/DatasetLoaderTest.java b/jhdf/src/test/java/io/jhdf/dataset/DatasetLoaderTest.java new file mode 100644 index 00000000..b1b9a25d --- /dev/null +++ b/jhdf/src/test/java/io/jhdf/dataset/DatasetLoaderTest.java @@ -0,0 +1,67 @@ +/* + * This file is part of jHDF. A pure Java library for accessing HDF5 files. + * + * http://jhdf.io + * + * Copyright (c) 2020 James Mudd + * + * MIT License see 'LICENSE' file + */ +package io.jhdf.dataset; + +import io.jhdf.HdfFileChannel; +import io.jhdf.ObjectHeader; +import io.jhdf.api.Group; +import io.jhdf.exceptions.HdfException; +import io.jhdf.object.message.DataLayout; +import io.jhdf.object.message.DataLayoutMessage; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.BitSet; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.powermock.reflect.Whitebox.invokeConstructor; + +class DatasetLoaderTest { + + @Test + void testUnrecognisedDataLayoutMessage() { + HdfFileChannel hdfFileChannel = mock(HdfFileChannel.class); + ObjectHeader objectHeader = mock(ObjectHeader.class); + String name = "test"; + Group group = mock(Group.class); + + when(objectHeader.getMessageOfType(DataLayoutMessage.class)) + .thenReturn(new UnknownDataLayoutMessage(new BitSet())); + + assertThrows(HdfException.class, () -> DatasetLoader.createDataset(hdfFileChannel, objectHeader, name, group)); + } + + @Test + void testFailureToReadDataset() { + HdfFileChannel hdfFileChannel = mock(HdfFileChannel.class); + ObjectHeader objectHeader = mock(ObjectHeader.class); + String name = "test"; + Group group = mock(Group.class); + + when(objectHeader.getMessageOfType(DataLayoutMessage.class)) + .thenThrow(RuntimeException.class); + + assertThrows(HdfException.class, () -> DatasetLoader.createDataset(hdfFileChannel, objectHeader, name, group)); + } + + private static class UnknownDataLayoutMessage extends DataLayoutMessage { + + public UnknownDataLayoutMessage(BitSet flags) { + super(flags); + } + + @Override + public DataLayout getDataLayout() { + return null; + } + } +}