Skip to content

Commit

Permalink
Merge pull request #220 from jamesmudd/coverage-improvements
Browse files Browse the repository at this point in the history
Coverage improvements
  • Loading branch information
jamesmudd authored Sep 9, 2020
2 parents eedc7be + 1fcc568 commit afe45d7
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 7 deletions.
6 changes: 4 additions & 2 deletions jhdf/src/main/java/io/jhdf/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ' ';
Expand All @@ -25,6 +29,4 @@ public final class Constants {

public static final String PATH_SEPARATOR = "/";

private Constants() {
} // No instances
}
2 changes: 1 addition & 1 deletion jhdf/src/main/java/io/jhdf/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

public final class CompoundDatasetReader {

/** No instances */
private CompoundDatasetReader() {
throw new AssertionError("No instances of CompoundDatasetReader");
}

public static Map<String, Object> readDataset(CompoundDataType type, ByteBuffer buffer, int[] dimensions, HdfFileChannel hdfFc) {
Expand Down
2 changes: 1 addition & 1 deletion jhdf/src/main/java/io/jhdf/dataset/DatasetLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion jhdf/src/main/java/io/jhdf/dataset/DatasetReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
*/
public final class DatasetReader {

/** No instances */
private DatasetReader() {
throw new AssertionError("No instances of DatasetReader");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
46 changes: 46 additions & 0 deletions jhdf/src/test/java/io/jhdf/NoInstancesTest.java
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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));
}

}
67 changes: 67 additions & 0 deletions jhdf/src/test/java/io/jhdf/dataset/DatasetLoaderTest.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit afe45d7

Please sign in to comment.