-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIssue417Test.java
145 lines (132 loc) · 6.84 KB
/
Issue417Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package issues;
import com.kazurayam.materialstore.zest.TestOutputOrganizerFactory;
import com.kazurayam.materialstore.base.reduce.MaterialProductGroup;
import com.kazurayam.materialstore.base.reduce.differ.ImageDiffStuffer;
import com.kazurayam.materialstore.base.reduce.zipper.MaterialProduct;
import com.kazurayam.materialstore.core.FileType;
import com.kazurayam.materialstore.core.JobName;
import com.kazurayam.materialstore.core.JobTimestamp;
import com.kazurayam.materialstore.core.MaterialList;
import com.kazurayam.materialstore.core.MaterialstoreException;
import com.kazurayam.materialstore.core.Metadata;
import com.kazurayam.materialstore.core.QueryOnMetadata;
import com.kazurayam.materialstore.core.Store;
import com.kazurayam.materialstore.core.Stores;
import com.kazurayam.unittest.TestOutputOrganizer;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import ru.yandex.qatools.ashot.comparison.ImageDiff;
import ru.yandex.qatools.ashot.comparison.ImageDiffer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* Reproducing the issue https://github.com/kazurayam/materialstore/issues/417
*
* com.kazurayam.materialstore.base.reduce.differ.ImageDifferToPNG.stuffDiff(ImageDifferToPNG.java:41
* caused OutOfMemoryError
*/
public class Issue417Test {
private static final TestOutputOrganizer too =
TestOutputOrganizerFactory.create(Issue417Test.class);
private static final Path fixtureDir =
too.getProjectDirectory().resolve("src/test/fixtures/issue#417");
private static Store store;
@BeforeAll
public static void beforeAll() throws MaterialstoreException, IOException {
Path rootDir = too.cleanClassOutputDirectory().resolve("store");
store = Stores.newInstance(rootDir);
}
/**
* When both of the left and right Material are given,
* no Error will be raised
*/
@Test
public void test_normal_case() throws MaterialstoreException {
// prepare the fixture files
JobName jobName = new JobName("test_normal_case");
Metadata metadata =
Metadata.builder().put("description","fixture").build();
Path pngFile = fixtureDir.resolve("3a98c4ba471f11462d06a4c94ef4daa4010a466a.png");
JobTimestamp jtLeft = JobTimestamp.now();
store.write(jobName, jtLeft, FileType.PNG, metadata, pngFile);
JobTimestamp jtRight = JobTimestamp.laterThan(jtLeft);
store.write(jobName, jtRight, FileType.PNG, metadata, pngFile);
//
MaterialList mlLeft = store.select(jobName, jtLeft,
FileType.PNG, QueryOnMetadata.ANY);
MaterialList mlRight = store.select(jobName, jtRight,
FileType.PNG, QueryOnMetadata.ANY);
MaterialProductGroup mpg = MaterialProductGroup.builder(mlLeft, mlRight).build();
MaterialProduct stuffed = new ImageDiffStuffer(store).stuffDiff(mpg.get(0));
assertNotNull(stuffed);
}
/**
* When either of the left or right Material is a NULL_OBJECT,
* ImageDifferToPNG.stuffDiff() throws the OutOfMemoryError
*/
@Test
public void test_reproduce_OutOfMemoryError() throws MaterialstoreException {
// prepare the fixture files
JobName jobName = new JobName("test_reproduce_OutOfMemoryError");
JobTimestamp jtLeft = JobTimestamp.now();
JobTimestamp jtRight = JobTimestamp.laterThan(jtLeft);
Metadata metadata =
Metadata.builder().put("description", "fixture").build();
Path pngFile = fixtureDir.resolve("3a98c4ba471f11462d06a4c94ef4daa4010a466a.png");
store.write(jobName, jtRight, FileType.PNG, metadata, pngFile);
// invoke ImageDifferToPNG.stuffDiff() to reproduce the Error
MaterialList left = store.select(jobName, jtLeft, FileType.PNG, QueryOnMetadata.ANY);
MaterialList right = store.select(jobName, jtRight, FileType.PNG, QueryOnMetadata.ANY);
MaterialProductGroup mpg = MaterialProductGroup.builder(left, right).build();
MaterialProduct stuffed = new ImageDiffStuffer(store).stuffDiff(mpg.get(0));
assertNotNull(stuffed);
// OutOfMemoryError will occur
}
@Disabled
// this test is no longer necessary as I modified the ImageDifferToPng#stuffDiff()
/**
* Try to execute ru.yandex.qatools.ashot.comparison.ImageDiff#makeDiff(leftImage, rightImage)
* with the leftImage being loaded from the main/resources/com/kazurayam/materialstore/core/NoCounterpartFound.png
* Interested in how much memory the method requires
* as it possibly causes the OutOfMemoryError reported at
* https://github.com/kazurayam/materialstore/issues/417 .
*/
@Test
public void test_AShot_imageDiff_how_much_memory_it_requires() throws MaterialstoreException, IOException {
Path noMaterialFoundPNG = too.getProjectDirectory()
.resolve("src/main/resources/com/kazurayam/materialstore/core/NoCounterpartFound.png");
BufferedImage leftImage = ImageIO.read(noMaterialFoundPNG.toFile());
Path fixturePNG = too.getProjectDirectory()
.resolve("src/test/fixtures/issue#417/3a98c4ba471f11462d06a4c94ef4daa4010a466a.png");
BufferedImage rightImage = ImageIO.read(fixturePNG.toFile());
ImageDiffer imageDiffer = new ImageDiffer();
ImageDiff imageDiff = imageDiffer.makeDiff(leftImage, rightImage);
// this causes OutOfMemoryError
assertNotNull(imageDiff);
}
@Disabled
// this test is no longer necessary as I modified the ImageDifferToPng#stuffDiff()
@Test
public void test_AShot_imageDiff_of_the_same_size_3445x3872() throws MaterialstoreException, IOException {
Path noMaterialFoundPNG = too.getProjectDirectory()
.resolve("src/test/fixtures/issue#417/NoMaterialFound_3445x4872.png");
BufferedImage leftImage = ImageIO.read(noMaterialFoundPNG.toFile());
Path fixturePNG = too.getProjectDirectory()
.resolve("src/test/fixtures/issue#417/3a98c4ba471f11462d06a4c94ef4daa4010a466a.png");
BufferedImage rightImage = ImageIO.read(fixturePNG.toFile());
ImageDiffer imageDiffer = new ImageDiffer();
ImageDiff imageDiff = imageDiffer.makeDiff(leftImage, rightImage);
// this does NOT raise OutOfMemoryError
assertNotNull(imageDiff);
// save the image into the store to have a look
BufferedImage bi = imageDiff.getDiffImage();
JobName jobName = new JobName("test_AShot_imageDiff_of_the_same_size_3445x3872");
JobTimestamp jobTimestamp = JobTimestamp.now();
Metadata metadata = Metadata.builder().put("description", "diff").build();
store.write(jobName, jobTimestamp, FileType.PNG, metadata, bi);
}
}