org.sitoolkit.wt.domain.evidence.EvidenceDir Maven / Gradle / Ivy
package org.sitoolkit.wt.domain.evidence;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FalseFileFilter;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.commons.lang3.StringUtils;
import org.sitoolkit.wt.infra.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EvidenceDir {
private static final Logger LOG = LoggerFactory.getLogger(EvidenceDir.class);
private static final String BASE_EVIDENCE_ROOT = "base-evidence";
private static final String COMPARE_PREFIX = "comp_";
private static final String COMPARE_NG_PREFIX = "comp_ng_";
private static final String MASK_PREFIX = "mask_";
private static final String UNMATCH_PREFIX = "unmatch_";
private static final String FAILSAFE_REPORT_NAME = "failsafe-report.html";
private static final String PROJECT_REPORTS_NAME = "project-reports.html";
private static final String IMG_BASE_DIR = "base";
private static final String EVIDENCE_ROOT_DIR = "evidence";
private File dir;
private static String evidenceDirRegex = "^evidence_.*";
private EvidenceDir() {
}
public static EvidenceDir getInstance(File dir) {
EvidenceDir instance = new EvidenceDir();
instance.dir = dir;
return instance;
}
public static EvidenceDir getInstance(String dir) {
return getInstance(new File(dir));
}
public static EvidenceDir getBase(String browser) {
return getInstance(new File(BASE_EVIDENCE_ROOT, browser));
}
public static String getRoot() {
return EVIDENCE_ROOT_DIR;
}
public static EvidenceDir getLatest() {
return getInstance(getLatestEvidenceDir());
}
public static File getLatestEvidenceDir() {
File outputDir = new File(EVIDENCE_ROOT_DIR);
List evidenceDirs = new ArrayList(FileUtils.listFilesAndDirs(outputDir,
FalseFileFilter.INSTANCE, new RegexFileFilter(evidenceDirRegex)));
evidenceDirs.remove(outputDir);
Collections.sort(evidenceDirs, new FileNameComarator(false));
if (evidenceDirs.isEmpty()) {
LOG.info("エビデンスフォルダがありません {}", outputDir.getAbsolutePath());
return null;
}
return evidenceDirs.get(0);
}
public static class FileNameComarator implements Comparator {
private int signum = 1;
public FileNameComarator() {
super();
}
public FileNameComarator(boolean ascending) {
this();
signum = ascending ? 1 : -1;
}
@Override
public int compare(File o1, File o2) {
return signum * o1.compareTo(o2);
}
}
public static EvidenceDir targetEvidenceDir(String dir) {
return dir == null ? EvidenceDir.getLatest() : EvidenceDir.getInstance(dir);
}
public static EvidenceDir baseEvidenceDir(String dir, String browser) {
return dir == null ? EvidenceDir.getBase(browser) : EvidenceDir.getInstance(dir);
}
public List getEvidenceFiles() {
List evidenceFiles = new ArrayList<>();
for (File evidenceFile : FileUtils.listFiles(dir, new String[] { "html" }, true)) {
String htmlName = evidenceFile.getName();
if (isCompareEvidence(htmlName) || isCompareNgEvidence(htmlName)
|| isMaskEvidence(htmlName) || isFailsafeReport(htmlName)
|| isProjectReport(htmlName)) {
continue;
}
evidenceFiles.add(evidenceFile);
}
return evidenceFiles;
}
public Collection getScreenshots(String evidenceFileName) {
return getScreenshotFilesAsMap(evidenceFileName).values();
}
public Map getScreenshotFilesAsMap(String evidenceFileName) {
Map screenshotFiles = new HashMap<>();
String evidenceName = StringUtils.removeEnd(evidenceFileName, ".html");
for (File imgFile : FileUtils.listFiles(dir, new String[] { "png" }, true)) {
String imgName = imgFile.getName();
if (isEvidenceScreenshot(imgName, evidenceName)
|| isMaskEvidenceScreenshot(imgName, evidenceName)
|| isUnmatchEvidenceScreenshot(imgName, evidenceName)
|| isUnmatchMaskEvidenceScreenshot(imgName, evidenceName)) {
screenshotFiles.put(imgFile.getName(), imgFile);
}
}
return screenshotFiles;
}
public static String extractTable(File evidenceFile) throws IOException {
List lines = FileUtils.readLines(evidenceFile, "UTF-8");
int tableTagStart = lines.indexOf(" ");
int tableTagEnd = lines.indexOf("
");
if (tableTagStart == -1 || tableTagEnd == -1) {
return "";
}
StringBuilder sb = new StringBuilder();
for (String line : lines.subList(tableTagStart, tableTagEnd + 1)) {
sb.append(StringUtils.join(" ", line, "\n"));
}
return sb.toString();
}
public static String removeInputLine(String htmlString) throws IOException {
String[] lines = htmlString.split("\n");
StringBuilder sb = new StringBuilder();
for (String line : lines) {
if (line.trim().startsWith("
© 2015 - 2025 Weber Informatics LLC | Privacy Policy