Skip to content

Commit 9634766

Browse files
committed
[SNAP-3479] Exception in decode qualification of ZnapProductReader
not only productRoot could have been null but also the return of productRoot.getFileName()
1 parent b66edeb commit 9634766

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

snap-znap/src/main/java/org/esa/snap/dataio/znap/ZnapProductReaderPlugIn.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public DecodeQualification getDecodeQualification(Object input) {
5454
} else {
5555
productRoot = inputPath.getParent();
5656
}
57-
final boolean isValidRootDirName = productRoot != null && productRoot.getFileName().toString().toLowerCase().endsWith(ZNAP_CONTAINER_EXTENSION);
57+
Path productRootName = productRoot != null ? productRoot.getFileName() : null;
58+
final boolean isValidRootDirName = productRootName != null && productRootName.toString().toLowerCase().endsWith(ZNAP_CONTAINER_EXTENSION);
5859
if (isValidRootDirName) {
5960
final boolean productRootIsDirectory = Files.isDirectory(productRoot);
6061
final Path productHeader = productRoot.resolve(FILENAME_DOT_ZGROUP);
@@ -74,7 +75,7 @@ public DecodeQualification getDecodeQualification(Object input) {
7475
}
7576
}
7677
}
77-
final boolean isValidZnapZipArchiveName = productRoot != null && productRoot.getFileName().toString().toLowerCase().endsWith(ZNAP_ZIP_CONTAINER_EXTENSION);
78+
final boolean isValidZnapZipArchiveName = productRootName != null && productRootName.toString().toLowerCase().endsWith(ZNAP_ZIP_CONTAINER_EXTENSION);
7879
if (isValidZnapZipArchiveName) {
7980
try (ZipStore zipStore = new ZipStore(productRoot)) {
8081
final InputStream productHeaderStream = zipStore.getInputStream(FILENAME_DOT_ZGROUP);

snap-znap/src/test/java/org/esa/snap/dataio/znap/ZnapProductReaderPlugInTest_getDecodeQualification.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,26 @@
1919
package org.esa.snap.dataio.znap;
2020

2121
import com.bc.zarr.ZarrConstants;
22+
import com.google.common.jimfs.Configuration;
2223
import com.google.common.jimfs.Jimfs;
2324
import org.esa.snap.core.dataio.DecodeQualification;
2425
import org.esa.snap.core.util.io.TreeDeleter;
2526
import org.junit.After;
2627
import org.junit.Before;
2728
import org.junit.Test;
29+
import org.junit.runner.RunWith;
2830

31+
import java.io.File;
2932
import java.io.IOException;
3033
import java.nio.file.FileSystem;
3134
import java.nio.file.Files;
3235
import java.nio.file.Path;
36+
import java.nio.file.Paths;
3337

3438
import static org.hamcrest.MatcherAssert.assertThat;
3539
import static org.hamcrest.Matchers.equalTo;
3640
import static org.hamcrest.Matchers.is;
41+
import static org.junit.Assume.assumeNotNull;
3742

3843
public class ZnapProductReaderPlugInTest_getDecodeQualification {
3944

@@ -95,7 +100,7 @@ public void decodeQualification_INTENDED_perfectMatch_inputStringObject() throws
95100

96101
@Test
97102
public void decodeQualification_INTENDED_perfectMatch_inputHasNoParent() throws IOException {
98-
try(FileSystem fileSystem = Jimfs.newFileSystem()) {
103+
try (FileSystem fileSystem = Jimfs.newFileSystem()) {
99104
Path path = fileSystem.getPath("snap_zarr_product_root_dir.znap");
100105

101106
Path dataDir = path.resolve("a_raster_data_dir");
@@ -127,17 +132,30 @@ public void decodeQualification_UNABLE_inputObjectIsNullOrCanNotBeConvertedToPat
127132
}
128133

129134
@Test
130-
public void decodeQualification_UNABLE_productRootDoesNotExist() {
131-
// No file or directory as expected
135+
public void decodeQualification_UNABLE_productRootDoesNotExist() throws IOException {
136+
try (FileSystem fileSystem = Jimfs.newFileSystem()) {
137+
Path path = fileSystem.getPath("any_not_supported_file.tar");
132138

133-
final DecodeQualification decodeQualification = plugIn.getDecodeQualification(productRoot);
139+
final DecodeQualification decodeQualification = plugIn.getDecodeQualification(path);
140+
assertThat(decodeQualification, is(equalTo(DecodeQualification.UNABLE)));
141+
}
142+
143+
}
144+
145+
@Test()
146+
public void decodeQualification_UNABLE_AnyFileAtRootLevel() {
147+
String systemDrive = System.getenv("SystemDrive");
148+
assumeNotNull(systemDrive); // only on windows not null
149+
Path root = Paths.get(systemDrive);
150+
Path notValid = root.resolve("any_not_supported_file.notznap");
134151

152+
final DecodeQualification decodeQualification = plugIn.getDecodeQualification(notValid);
135153
assertThat(decodeQualification, is(equalTo(DecodeQualification.UNABLE)));
136154
}
137155

138156
@Test
139157
public void decodeQualification_UNABLE_aZGroupFileWhichHasNoParentDir() throws IOException {
140-
try(FileSystem fileSystem = Jimfs.newFileSystem()) {
158+
try (FileSystem fileSystem = Jimfs.newFileSystem()) {
141159
Path path = fileSystem.getPath("any_zarr_group_file_without_parent.zgroup");
142160
final DecodeQualification decodeQualification = plugIn.getDecodeQualification(path);
143161

0 commit comments

Comments
 (0)