Last active
December 10, 2016 02:40
-
-
Save noxi515/2a0d5a56a01479a03aa9df6f6ec77214 to your computer and use it in GitHub Desktop.
PitaliumのWriteOnlyな連番Persister
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.noxi.pitalium.io; | |
import com.htmlhifive.pitalium.core.config.PtlTestConfig; | |
import com.htmlhifive.pitalium.core.io.PersistMetadata; | |
import com.htmlhifive.pitalium.core.io.Persister; | |
import com.htmlhifive.pitalium.core.io.ResourceUnavailableException; | |
import com.htmlhifive.pitalium.core.model.TargetResult; | |
import com.htmlhifive.pitalium.core.model.TestResult; | |
import javax.imageio.ImageIO; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.UncheckedIOException; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.atomic.AtomicInteger; | |
/** | |
* 画像データを連番で保存するPersister | |
*/ | |
public class LinerWriteOnlyPersister implements Persister { | |
private final ConcurrentHashMap<String, AtomicInteger> counts = new ConcurrentHashMap<>(); | |
private final File rootDirectory; | |
public LinerWriteOnlyPersister() throws IOException { | |
// 標準の出力先を取得 | |
String resultDirectory = PtlTestConfig.getInstance().getPersisterConfig().getFile().getResultDirectory(); | |
rootDirectory = new File(resultDirectory); | |
if (!rootDirectory.exists() && !rootDirectory.mkdirs()) | |
throw new IOException("Cannot create RootDirectory"); | |
} | |
@Override | |
public void saveDiffImage(PersistMetadata metadata, BufferedImage image) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public BufferedImage loadDiffImage(PersistMetadata metadata) throws ResourceUnavailableException { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void saveScreenshot(PersistMetadata metadata, BufferedImage image) { | |
// 撮影の指定がある場合、何もしない | |
if (metadata.getSelector() != null || metadata.getRectangle() != null) | |
return; | |
// 最初に全体を取った画像のみ保存する | |
String browserName = metadata.getCapabilities().getBrowserName(); | |
int i = counts.computeIfAbsent(browserName, key -> new AtomicInteger()).incrementAndGet(); | |
String fileName = String.format("%s_%04d.png", browserName, i); | |
File file = new File(rootDirectory, fileName); | |
try { | |
ImageIO.write(image, "png", file); | |
} catch (IOException e) { | |
throw new UncheckedIOException(e); | |
} | |
} | |
@Override | |
public InputStream getImageStream(PersistMetadata metadata) throws ResourceUnavailableException { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public BufferedImage loadScreenshot(PersistMetadata metadata) throws ResourceUnavailableException { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void saveTargetResults(PersistMetadata metadata, List<TargetResult> results) { | |
// do nothing | |
} | |
@Override | |
public List<TargetResult> loadTargetResults(PersistMetadata metadata) throws ResourceUnavailableException { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void saveTestResult(PersistMetadata metadata, TestResult result) { | |
// do nothing | |
} | |
@Override | |
public TestResult loadTestResult(PersistMetadata metadata) throws ResourceUnavailableException { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void saveExpectedIds(Map<String, Map<String, String>> expectedIds) { | |
// do nothing | |
} | |
@Override | |
public Map<String, Map<String, String>> loadExpectedIds() throws ResourceUnavailableException { | |
return Collections.emptyMap(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment