Skip to content

Commit dd9a6e3

Browse files
committed
added a test for possible problematic code.
1 parent 2fa189a commit dd9a6e3

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

snap-gpf/src/main/java/org/esa/snap/core/gpf/common/resample/ResamplingOp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ private void resampleBands() {
428428
}
429429
}
430430

431-
private static MultiLevelImage createMaskedImage(final RasterDataNode node, Number maskValue) {
431+
static MultiLevelImage createMaskedImage(final RasterDataNode node, Number maskValue) {
432432
MultiLevelImage varImage = node.getSourceImage();
433433
if (node.getValidPixelExpression() != null) {
434434
varImage = replaceInvalidValuesByNaN(node, varImage, node.getValidMaskImage(), maskValue);

snap-gpf/src/test/java/org/esa/snap/core/gpf/common/resample/ResamplingOpTest.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88
import org.esa.snap.core.gpf.GPF;
99
import org.esa.snap.core.gpf.OperatorException;
1010
import org.esa.snap.core.transform.MathTransform2D;
11+
import org.esa.snap.core.util.DummyProductBuilder;
1112
import org.junit.Test;
1213

1314
import java.util.Date;
1415
import java.util.HashMap;
1516
import java.util.Map;
1617

17-
import static junit.framework.Assert.assertEquals;
18-
import static junit.framework.Assert.assertFalse;
19-
import static junit.framework.Assert.fail;
20-
import static org.junit.Assert.assertNotNull;
21-
import static org.junit.Assert.assertTrue;
18+
import static org.junit.Assert.*;
2219

2320
/**
2421
* @author Tonio Fincke
@@ -44,7 +41,7 @@ public void testTimeInformationIsPreserved() {
4441
assertEquals(startTime.getAsDate().getTime(), resampledProduct.getStartTime().getAsDate().getTime());
4542
assertEquals(endTime.getAsDate().getTime(), resampledProduct.getEndTime().getAsDate().getTime());
4643
assertNotNull(resampledProduct.getSceneTimeCoding());
47-
assertEquals(endTime.getMJD(), resampledProduct.getSceneTimeCoding().getMJD(new PixelPos(0, 1)));
44+
assertEquals(endTime.getMJD(), resampledProduct.getSceneTimeCoding().getMJD(new PixelPos(0, 1)), 1.0e-6);
4845
}
4946

5047
@Test
@@ -141,4 +138,46 @@ public void testBothTargetWidthAndHeightAreSet() {
141138
assertEquals("If targetHeight is set, targetWidth must be set, too.", oe.getMessage());
142139
}
143140
}
141+
142+
@Test
143+
public void testCreateMaskedImage() {
144+
// It was reported that createMaskedImage can produce a NullPointerException.
145+
// Could not reproduce with this test.
146+
// Exception stack trace is:
147+
// java.lang.NullPointerException
148+
// at org.esa.snap.core.gpf.common.resample.ResamplingOp$1.createImage(ResamplingOp.java:450)
149+
// at com.bc.ceres.glevel.support.AbstractMultiLevelSource.getImage(AbstractMultiLevelSource.java:65)
150+
// at com.bc.ceres.glevel.support.DefaultMultiLevelImage.<init>(DefaultMultiLevelImage.java:45)
151+
// at org.esa.snap.core.gpf.common.resample.ResamplingOp.replaceInvalidValuesByNaN(ResamplingOp.java:446)
152+
// at org.esa.snap.core.gpf.common.resample.ResamplingOp.createMaskedImage(ResamplingOp.java:434)
153+
// at org.esa.snap.core.gpf.common.resample.ResamplingOp.resampleBands(ResamplingOp.java:380)
154+
DummyProductBuilder builder = new DummyProductBuilder();
155+
builder.size(DummyProductBuilder.Size.MEDIUM);
156+
Product product = builder.create();
157+
158+
Band noValidationBand = product.addBand("noValidationBand", "X", ProductData.TYPE_INT16);
159+
ResamplingOp.createMaskedImage(noValidationBand, Float.NaN);
160+
161+
Band noDataBand = product.addBand("noDataBand", "X", ProductData.TYPE_INT16);
162+
noDataBand.setNoDataValue(5);
163+
noDataBand.setNoDataValueUsed(true);
164+
ResamplingOp.createMaskedImage(noDataBand, Float.NaN);
165+
166+
Band expressionBand = product.addBand("expressionBand", "X", ProductData.TYPE_INT16);
167+
expressionBand.setValidPixelExpression("expressionBand == 6");
168+
ResamplingOp.createMaskedImage(expressionBand, Float.NaN);
169+
170+
Band noDataExpressionBand = product.addBand("noDataExpressionBand", "X", ProductData.TYPE_INT16);
171+
noDataExpressionBand.setNoDataValue(5);
172+
noDataExpressionBand.setNoDataValueUsed(true);
173+
noDataExpressionBand.setValidPixelExpression("expressionBand == 6");
174+
ResamplingOp.createMaskedImage(noDataExpressionBand, Float.NaN);
175+
176+
Band badExpressionBand = product.addBand("badExpressionBand", "X", ProductData.TYPE_INT16);
177+
Band tempNotExisting = product.addBand("notExisting", "Y", ProductData.TYPE_INT16);
178+
badExpressionBand.setValidPixelExpression("notExisting == 6");
179+
badExpressionBand.getValidMaskImage();
180+
product.removeBand(tempNotExisting);
181+
ResamplingOp.createMaskedImage(badExpressionBand, Float.NaN);
182+
}
144183
}

0 commit comments

Comments
 (0)