diff --git a/src/main/java/com/lazerycode/selenium/download/DownloadHandler.java b/src/main/java/com/lazerycode/selenium/download/DownloadHandler.java index c271255..0bcdaf8 100644 --- a/src/main/java/com/lazerycode/selenium/download/DownloadHandler.java +++ b/src/main/java/com/lazerycode/selenium/download/DownloadHandler.java @@ -1,6 +1,6 @@ package com.lazerycode.selenium.download; -import com.lazerycode.selenium.extract.BinaryFileNames; +import com.lazerycode.selenium.repository.BinaryType; import com.lazerycode.selenium.extract.ExtractFilesFromArchive; import com.lazerycode.selenium.repository.FileDetails; import org.apache.commons.io.FilenameUtils; @@ -67,7 +67,7 @@ public void ensureStandaloneExecutableFilesExist() throws MojoFailureException, String extractedFileLocation = this.rootStandaloneServerDirectory.getAbsolutePath() + File.separator + fileToDownload.getKey(); String binaryForOperatingSystem = fileToDownload.getKey().replace("\\", "/").split("/")[1].toUpperCase(); //TODO should really store the OSType we have extracted somewhere rather than doing this hack! LOG.debug("Detected a binary for OSType: " + binaryForOperatingSystem); - if (ExtractFilesFromArchive.extractFileFromArchive(fileToUnzip, extractedFileLocation, this.overwriteFilesThatExist, BinaryFileNames.valueOf(binaryForOperatingSystem))) { + if (ExtractFilesFromArchive.extractFileFromArchive(fileToUnzip, extractedFileLocation, this.overwriteFilesThatExist, BinaryType.valueOf(binaryForOperatingSystem))) { LOG.info("File(s) copied to " + extractedFileLocation); } } diff --git a/src/main/java/com/lazerycode/selenium/extract/ExtractFilesFromArchive.java b/src/main/java/com/lazerycode/selenium/extract/ExtractFilesFromArchive.java index 5e897ce..9c8e77a 100644 --- a/src/main/java/com/lazerycode/selenium/extract/ExtractFilesFromArchive.java +++ b/src/main/java/com/lazerycode/selenium/extract/ExtractFilesFromArchive.java @@ -1,5 +1,6 @@ package com.lazerycode.selenium.extract; +import com.lazerycode.selenium.repository.BinaryType; import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveInputStream; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; @@ -34,7 +35,7 @@ public class ExtractFilesFromArchive { * @return boolean * @throws IOException */ - public static boolean extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryFileNames possibleFilenames) throws IOException, IllegalArgumentException, MojoFailureException { + public static boolean extractFileFromArchive(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryType possibleFilenames) throws IOException, IllegalArgumentException, MojoFailureException { String fileType = FilenameUtils.getExtension(downloadedCompressedFile.getAbsolutePath()); LOG.debug("Determined archive type: " + fileType); if (fileType.equals("zip")) { @@ -56,7 +57,7 @@ public static boolean extractFileFromArchive(File downloadedCompressedFile, Stri * @throws IOException */ - static boolean unzipFile(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryFileNames possibleFilenames) throws IOException { + static boolean unzipFile(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryType possibleFilenames) throws IOException { Boolean fileExtracted = false; LOG.debug("Extracting binary from .zip file"); ZipFile zip = new ZipFile(downloadedCompressedFile); @@ -104,7 +105,7 @@ static boolean unzipFile(File downloadedCompressedFile, String extractedToFilePa * @throws IOException */ - static boolean untarFile(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryFileNames possibleFilenames) throws IOException, MojoFailureException { + static boolean untarFile(File downloadedCompressedFile, String extractedToFilePath, boolean overwriteFilesThatExist, BinaryType possibleFilenames) throws IOException, MojoFailureException { Boolean fileExtracted = false; ArrayList filenamesWeAreSearchingFor = possibleFilenames.getBinaryFilenames(); ArchiveInputStream fileInArchive; diff --git a/src/main/java/com/lazerycode/selenium/hash/HashTypeAdaptor.java b/src/main/java/com/lazerycode/selenium/hash/HashTypeAdaptor.java new file mode 100644 index 0000000..d26da92 --- /dev/null +++ b/src/main/java/com/lazerycode/selenium/hash/HashTypeAdaptor.java @@ -0,0 +1,16 @@ +package com.lazerycode.selenium.hash; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +public class HashTypeAdaptor extends XmlAdapter { + + @Override + public HashType unmarshal(String str) throws Exception { + return HashType.valueOf(str.toUpperCase()); + } + + @Override + public String marshal(HashType hashType) throws Exception { + return hashType.toString().toLowerCase(); + } +} diff --git a/src/main/java/com/lazerycode/selenium/extract/BinaryFileNames.java b/src/main/java/com/lazerycode/selenium/repository/BinaryType.java similarity index 83% rename from src/main/java/com/lazerycode/selenium/extract/BinaryFileNames.java rename to src/main/java/com/lazerycode/selenium/repository/BinaryType.java index 0001645..0b7f6c4 100644 --- a/src/main/java/com/lazerycode/selenium/extract/BinaryFileNames.java +++ b/src/main/java/com/lazerycode/selenium/repository/BinaryType.java @@ -1,8 +1,8 @@ -package com.lazerycode.selenium.extract; +package com.lazerycode.selenium.repository; import java.util.ArrayList; -public enum BinaryFileNames { +public enum BinaryType { INTERNETEXPLORER(new ArrayList() {{ add("IEDriverServer.exe"); }}), @@ -21,7 +21,7 @@ public enum BinaryFileNames { private final ArrayList binaryFilenames; - BinaryFileNames(ArrayList binaryFilenames) { + BinaryType(ArrayList binaryFilenames) { this.binaryFilenames = binaryFilenames; } diff --git a/src/main/java/com/lazerycode/selenium/repository/DriverContext.java b/src/main/java/com/lazerycode/selenium/repository/DriverContext.java new file mode 100644 index 0000000..d154781 --- /dev/null +++ b/src/main/java/com/lazerycode/selenium/repository/DriverContext.java @@ -0,0 +1,54 @@ +package com.lazerycode.selenium.repository; + +public class DriverContext { + private final BinaryType driverType; + private final SystemArchitecture systemArchitecture; + private final OperatingSystem operatingSystem; + + private DriverContext(BinaryType driverType, OperatingSystem operatingSystem, SystemArchitecture systemArchitecture) { + this.operatingSystem = operatingSystem; + this.driverType = driverType; + this.systemArchitecture = systemArchitecture; + } + + private DriverContext(String driverType, String operatingSystem, SystemArchitecture systemArchitecture) { + this.operatingSystem = OperatingSystem.valueOf(operatingSystem.toUpperCase()); + this.driverType = BinaryType.valueOf(driverType.toUpperCase()); + this.systemArchitecture = systemArchitecture; + } + + public static DriverContext binaryDataFor(BinaryType browserType, OperatingSystem osName, SystemArchitecture architecture) { + return new DriverContext(browserType, osName, architecture); + } + + public static DriverContext binaryDataFor(String browserType, String osName, SystemArchitecture architecture) { + return new DriverContext(browserType, osName, architecture); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + DriverContext that = (DriverContext) o; + + if (driverType != that.driverType) return false; + if (operatingSystem != that.operatingSystem) return false; + if (systemArchitecture != that.systemArchitecture) return false; + + return true; + } + + @Override + public int hashCode() { + int result = driverType.hashCode(); + result = 31 * result + systemArchitecture.hashCode(); + result = 31 * result + operatingSystem.hashCode(); + return result; + } + + @Override + public String toString() { + return this.operatingSystem.getOperatingSystemType() + " - " + this.driverType.toString().toLowerCase() + " - " + this.systemArchitecture.getSystemArchitectureType(); + } +} diff --git a/src/main/java/com/lazerycode/selenium/repository/DriverDetails.java b/src/main/java/com/lazerycode/selenium/repository/DriverDetails.java new file mode 100644 index 0000000..70daae8 --- /dev/null +++ b/src/main/java/com/lazerycode/selenium/repository/DriverDetails.java @@ -0,0 +1,34 @@ +package com.lazerycode.selenium.repository; + +import com.lazerycode.selenium.hash.HashType; +import com.lazerycode.selenium.hash.HashTypeAdaptor; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import java.net.URL; + +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class DriverDetails { + + @XmlElement(name = "filelocation") + URL fileLocation; + + @XmlElement() + String hash; + + @XmlElement(name = "hashtype") + @XmlJavaTypeAdapter(HashTypeAdaptor.class) + HashType hashType; + + @Override + public int hashCode() { + int result = fileLocation != null ? fileLocation.hashCode() : 0; + result = 31 * result + (hash != null ? hash.hashCode() : 0); + result = 31 * result + (hashType != null ? hashType.hashCode() : 0); + return result; + } +} \ No newline at end of file diff --git a/src/main/java/com/lazerycode/selenium/repository/DriverMap.java b/src/main/java/com/lazerycode/selenium/repository/DriverMap.java new file mode 100644 index 0000000..31482a0 --- /dev/null +++ b/src/main/java/com/lazerycode/selenium/repository/DriverMap.java @@ -0,0 +1,43 @@ +package com.lazerycode.selenium.repository; + +import java.util.HashMap; +import java.util.NoSuchElementException; +import java.util.TreeMap; + +public class DriverMap { + + protected HashMap> repository = new HashMap>(); + + public TreeMap getMapForDriverContext(DriverContext driverContext) { + if (!repository.containsKey(driverContext)) { + repository.put(driverContext, new TreeMap()); + } + + return repository.get(driverContext); + } + + public DriverDetails getDetailsForVersionOfDriverContext(DriverContext driverContext, String version) throws IllegalArgumentException { + if (!repository.containsKey(driverContext)) { + throw new IllegalArgumentException("Driver context not found in driver repository"); + } + + TreeMap driverVersions = repository.get(driverContext); + DriverDetails detailsToReturn = driverVersions.get(driverVersions.lastKey()); + if (detailsToReturn.hashCode() == 0) { + throw new NoSuchElementException("No driver version " + version + " exists for the context " + driverContext.toString()); + } + + return detailsToReturn; + } + + public DriverDetails getDetailsForLatestVersionOfDriverContext(DriverContext driverContext) { + if (!repository.containsKey(driverContext)) { + throw new IllegalArgumentException("Driver context not found in driver repository"); + } + + TreeMap driverVersions = repository.get(driverContext); + + return driverVersions.get(driverVersions.lastKey()); + } + +} \ No newline at end of file diff --git a/src/main/java/com/lazerycode/selenium/repository/FileDetails.java b/src/main/java/com/lazerycode/selenium/repository/FileDetails.java index 5923ef6..345b894 100644 --- a/src/main/java/com/lazerycode/selenium/repository/FileDetails.java +++ b/src/main/java/com/lazerycode/selenium/repository/FileDetails.java @@ -5,6 +5,7 @@ import java.net.MalformedURLException; import java.net.URL; +@Deprecated public class FileDetails { private URL fileLocation; diff --git a/src/main/java/com/lazerycode/selenium/repository/FileRepository.java b/src/main/java/com/lazerycode/selenium/repository/FileRepository.java new file mode 100644 index 0000000..9ef0067 --- /dev/null +++ b/src/main/java/com/lazerycode/selenium/repository/FileRepository.java @@ -0,0 +1,51 @@ +package com.lazerycode.selenium.repository; + +import org.apache.maven.plugin.MojoFailureException; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.xpath.XPathExpressionException; +import java.net.MalformedURLException; + +import static com.lazerycode.selenium.repository.SystemArchitecture.ARCHITECTURE_32_BIT; +import static com.lazerycode.selenium.repository.SystemArchitecture.ARCHITECTURE_64_BIT; + +public class FileRepository { + + public static DriverMap buildDownloadableFileRepository(NodeList nodesFound) throws MalformedURLException, MojoFailureException, JAXBException, XPathExpressionException { + DriverMap driverMap = new DriverMap(); + Unmarshaller unmarshaller = JAXBContext.newInstance(DriverDetails.class).createUnmarshaller(); + unmarshaller.setEventHandler(new unmarshallingEventHandler()); + for (int nodeNumber = 0; nodeNumber < nodesFound.getLength(); nodeNumber++) { + Node node = nodesFound.item(nodeNumber); + String operatingSystem = node.getParentNode().getParentNode().getParentNode().getNodeName(); + String driver = node.getParentNode().getParentNode().getAttributes().getNamedItem("id").getNodeValue(); + String version = node.getParentNode().getAttributes().getNamedItem("id").getNodeValue(); + boolean thisIs64Bit = false; + boolean thisIs32Bit = false; + if (node.getAttributes().getNamedItem("thirtytwobit") != null) { + if (Boolean.valueOf(node.getAttributes().getNamedItem("thirtytwobit").getNodeValue())) { + thisIs32Bit = true; + } + } + if (node.getAttributes().getNamedItem("sixtyfourbit") != null) { + if (Boolean.valueOf(node.getAttributes().getNamedItem("sixtyfourbit").getNodeValue())) { + thisIs64Bit = true; + } + } + + DriverDetails driverDetails = unmarshaller.unmarshal(node, DriverDetails.class).getValue(); + if (thisIs32Bit) { + driverMap.getMapForDriverContext(DriverContext.binaryDataFor(driver, operatingSystem, ARCHITECTURE_32_BIT)).put(version, driverDetails); + } + if (thisIs64Bit) { + driverMap.getMapForDriverContext(DriverContext.binaryDataFor(driver, operatingSystem, ARCHITECTURE_64_BIT)).put(version, driverDetails); + } + } + + return driverMap; + } +} diff --git a/src/main/java/com/lazerycode/selenium/repository/OperatingSystem.java b/src/main/java/com/lazerycode/selenium/repository/OperatingSystem.java new file mode 100644 index 0000000..f02c0a6 --- /dev/null +++ b/src/main/java/com/lazerycode/selenium/repository/OperatingSystem.java @@ -0,0 +1,31 @@ +package com.lazerycode.selenium.repository; + +public enum OperatingSystem { + + WINDOWS("windows"), + OSX("mac"), + LINUX("linux"); + + private String operatingSystemName; + + OperatingSystem(String operatingSystemName) { + this.operatingSystemName = operatingSystemName; + } + + String getOperatingSystemType() { + return operatingSystemName; + } + + static OperatingSystem getOperatingSystem() { + + String name = System.getProperties().getProperty("os.name"); + + for (OperatingSystem operatingSystemName : values()) { + if (name.toLowerCase().contains(operatingSystemName.getOperatingSystemType())) { + return operatingSystemName; + } + } + + throw new IllegalArgumentException("Unrecognised operating system name '" + name + "'"); + } +} diff --git a/src/main/java/com/lazerycode/selenium/repository/RepositoryParser.java b/src/main/java/com/lazerycode/selenium/repository/RepositoryParser.java index b5ac2a3..d73111f 100644 --- a/src/main/java/com/lazerycode/selenium/repository/RepositoryParser.java +++ b/src/main/java/com/lazerycode/selenium/repository/RepositoryParser.java @@ -13,6 +13,7 @@ import java.util.HashMap; import java.util.Map; +@Deprecated public class RepositoryParser { private static final Logger LOG = Logger.getLogger(RepositoryParser.class); diff --git a/src/main/java/com/lazerycode/selenium/repository/SystemArchitecture.java b/src/main/java/com/lazerycode/selenium/repository/SystemArchitecture.java new file mode 100644 index 0000000..b5d8a0d --- /dev/null +++ b/src/main/java/com/lazerycode/selenium/repository/SystemArchitecture.java @@ -0,0 +1,36 @@ +package com.lazerycode.selenium.repository; + +import java.util.Arrays; +import java.util.List; + +public enum SystemArchitecture { + + ARCHITECTURE_64_BIT("64 bit"), + ARCHITECTURE_32_BIT("32 bit"); + + private String systemArchitectureName; + + SystemArchitecture(String systemArchitectureName) { + this.systemArchitectureName = systemArchitectureName; + } + + String getSystemArchitectureType() { + return systemArchitectureName; + } + + public static final SystemArchitecture defaultSystemArchitecture = ARCHITECTURE_32_BIT; + private static List architecture64bitNames = Arrays.asList("amd64", "x86_64"); + + static SystemArchitecture getSystemArchitecture() { + + final String currentArchitecture = System.getProperties().getProperty("os.arch"); + + SystemArchitecture result = defaultSystemArchitecture; + + if (architecture64bitNames.contains(currentArchitecture)) { + result = ARCHITECTURE_64_BIT; + } + + return result; + } +} diff --git a/src/main/java/com/lazerycode/selenium/repository/XMLParser.java b/src/main/java/com/lazerycode/selenium/repository/XMLParser.java new file mode 100644 index 0000000..3010a5f --- /dev/null +++ b/src/main/java/com/lazerycode/selenium/repository/XMLParser.java @@ -0,0 +1,90 @@ +package com.lazerycode.selenium.repository; + +import com.lazerycode.selenium.OSType; +import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +import javax.xml.xpath.*; +import java.io.InputStream; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class XMLParser { + + private static XPathFactory factory = XPathFactory.newInstance(); + private static XPath xpath = factory.newXPath(); + final InputSource repositoryMap; + final Set operatingSystems; + final Map driverVersions; + final boolean thirtyTwoBit; + final boolean sixtyFourBit; + + + public XMLParser(InputStream repositoryMap, Set operatingSystems, Map driverVersions, boolean thirtyTwoBit, boolean sixtyFourBit) { + this.repositoryMap = new InputSource(repositoryMap); + this.operatingSystems = operatingSystems; + this.driverVersions = driverVersions; + this.thirtyTwoBit = thirtyTwoBit; + this.sixtyFourBit = sixtyFourBit; + } + + protected String operatingSystemSelector() { + if (operatingSystems.size() == 0) { + return ""; + } + StringBuilder operatingSystemsSelector = new StringBuilder(); + operatingSystemsSelector.append("["); + for (Iterator iterator = operatingSystems.iterator(); iterator.hasNext(); ) { + String operatingSystem = iterator.next().toString().toLowerCase(); + operatingSystemsSelector.append("parent::").append(operatingSystem); + if (iterator.hasNext()) { + operatingSystemsSelector.append("|"); + } + } + operatingSystemsSelector.append("]"); + + return operatingSystemsSelector.toString(); + } + + protected String driverVersionSelector() { + if (driverVersions.size() == 0) { + return ""; + } + StringBuilder versionSelector = new StringBuilder(); + versionSelector.append("["); + Iterator it = driverVersions.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry specificDriver = (Map.Entry) it.next(); + versionSelector.append("(parent::*[@id='"); + versionSelector.append(specificDriver.getKey()); + versionSelector.append("'] and @id='"); + versionSelector.append(specificDriver.getValue()); + versionSelector.append("')"); + if (it.hasNext()) { + versionSelector.append(" or "); + } + } + versionSelector.append("]"); + + return versionSelector.toString(); + } + + protected String calculateBitrate() { + if (thirtyTwoBit && sixtyFourBit) { + return "[@thirtytwobit='true' or @sixtyfourbit='true']"; + } else if (thirtyTwoBit) { + return "[@thirtytwobit='true']"; + } else if (sixtyFourBit) { + return "[@sixtyfourbit='true']"; + } + + return ""; + } + + public NodeList getAllNodesInScope() throws XPathExpressionException { + String nodeLocator = "//driver" + operatingSystemSelector() + "/version" + driverVersionSelector() + "/bitrate" + calculateBitrate(); + XPathExpression expression = xpath.compile(nodeLocator); + return (NodeList) expression.evaluate(repositoryMap, XPathConstants.NODESET); + } +} diff --git a/src/main/java/com/lazerycode/selenium/repository/unmarshallingEventHandler.java b/src/main/java/com/lazerycode/selenium/repository/unmarshallingEventHandler.java new file mode 100644 index 0000000..a486ee3 --- /dev/null +++ b/src/main/java/com/lazerycode/selenium/repository/unmarshallingEventHandler.java @@ -0,0 +1,11 @@ +package com.lazerycode.selenium.repository; + +import javax.xml.bind.ValidationEvent; +import javax.xml.bind.ValidationEventHandler; + +public class unmarshallingEventHandler implements ValidationEventHandler { + + public boolean handleEvent(ValidationEvent event) { + return false; + } +} diff --git a/src/test/java/com/lazerycode/selenium/extract/ExtractFilesFromArchiveTest.java b/src/test/java/com/lazerycode/selenium/extract/ExtractFilesFromArchiveTest.java index e0c1867..b5614ea 100644 --- a/src/test/java/com/lazerycode/selenium/extract/ExtractFilesFromArchiveTest.java +++ b/src/test/java/com/lazerycode/selenium/extract/ExtractFilesFromArchiveTest.java @@ -1,5 +1,6 @@ package com.lazerycode.selenium.extract; +import com.lazerycode.selenium.repository.BinaryType; import org.apache.commons.codec.digest.DigestUtils; import org.apache.maven.plugin.MojoFailureException; import org.junit.After; @@ -22,7 +23,7 @@ public class ExtractFilesFromArchiveTest { private final URL testZipFile = this.getClass().getResource("/jetty/files/download.zip"); private final URL testTarGZFile = this.getClass().getResource("/jetty/files/download.tar.gz"); private final URL testTarBZ2File = this.getClass().getResource("/jetty/files/download.tar.bz2"); - private final String tempDir = System.getProperty("java.io.tmpdir"); + private final String tempDir = System.getProperty("java.io.tmpdir") + File.separator + java.util.UUID.randomUUID(); private final boolean overwriteExistingFiles = true; private static File phantomJSTestFile; @@ -40,7 +41,7 @@ public void cleanUp() { @Test public void successfullyExtractFileFromZipArchive() throws Exception { - ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); @@ -50,27 +51,27 @@ public void successfullyExtractFileFromZipArchive() throws Exception { @Test public void overwriteExistingFileWhenExtractingFromZip() throws Exception { - ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); long lastModified = phantomJSTestFile.lastModified(); Thread.sleep(1000); //Wait 1 second so that the file isn't copied and then overwritten in the same second - ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); assertThat(phantomJSTestFile.lastModified(), is(not(equalTo(lastModified)))); } @Test public void doNotOverwriteExistingFileWhenExtractingFromZip() throws Exception { - ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); long lastModified = phantomJSTestFile.lastModified(); Thread.sleep(1000); //Wait 1 second so that the file isn't copied and then overwritten in the same second - ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, false, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.unzipFile(new File(testZipFile.getFile()), tempDir, false, BinaryType.PHANTOMJS); assertThat(phantomJSTestFile.lastModified(), is(equalTo(lastModified))); } @Test public void successfullyExtractFileFromTarGZipArchive() throws Exception { - ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); @@ -80,27 +81,27 @@ public void successfullyExtractFileFromTarGZipArchive() throws Exception { @Test public void overwriteExistingFileWhenExtractingFromTar() throws Exception { - ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); long lastModified = phantomJSTestFile.lastModified(); Thread.sleep(1000); //Wait 1 second so that the file isn't copied and then overwritten in the same second - ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); assertThat(phantomJSTestFile.lastModified(), is(not(equalTo(lastModified)))); } @Test public void doNotOverwriteExistingFileWhenExtractingFromTar() throws Exception { - ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); long lastModified = phantomJSTestFile.lastModified(); Thread.sleep(1000); //Wait 1 second so that the file isn't copied and then overwritten in the same second - ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, false, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.untarFile(new File(testTarGZFile.getFile()), tempDir, false, BinaryType.PHANTOMJS); assertThat(phantomJSTestFile.lastModified(), is(equalTo(lastModified))); } @Test public void successfullyExtractFileFromTarBZip2Archive() throws Exception { - ExtractFilesFromArchive.untarFile(new File(testTarBZ2File.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.untarFile(new File(testTarBZ2File.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); @@ -110,12 +111,12 @@ public void successfullyExtractFileFromTarBZip2Archive() throws Exception { @Test(expected = MojoFailureException.class) public void tryToUntarAnArchiveThatIsNotATarFile() throws Exception { - ExtractFilesFromArchive.untarFile(new File(test7ZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.untarFile(new File(test7ZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); } @Test public void successfullyWorkOutArchiveTypeAndExtractFileFromZipArchive() throws Exception { - ExtractFilesFromArchive.extractFileFromArchive(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.extractFileFromArchive(new File(testZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); @@ -125,7 +126,7 @@ public void successfullyWorkOutArchiveTypeAndExtractFileFromZipArchive() throws @Test public void successfullyWorkOutArchiveTypeAndExtractFileFromTarGZipArchive() throws Exception { - ExtractFilesFromArchive.extractFileFromArchive(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.extractFileFromArchive(new File(testTarGZFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); @@ -135,7 +136,7 @@ public void successfullyWorkOutArchiveTypeAndExtractFileFromTarGZipArchive() thr @Test public void successfullyWorkOutArchiveTypeAndExtractFileFromTarBZip2Archive() throws Exception { - ExtractFilesFromArchive.extractFileFromArchive(new File(testTarBZ2File.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.extractFileFromArchive(new File(testTarBZ2File.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); FileInputStream fileToCheck = new FileInputStream(phantomJSTestFile); String downloadedFileHash = DigestUtils.md5Hex(fileToCheck); fileToCheck.close(); @@ -145,6 +146,6 @@ public void successfullyWorkOutArchiveTypeAndExtractFileFromTarBZip2Archive() th @Test(expected = IllegalArgumentException.class) public void tryAndExtractFromAnUnsupportedArchive() throws Exception { - ExtractFilesFromArchive.extractFileFromArchive(new File(test7ZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryFileNames.PHANTOMJS); + ExtractFilesFromArchive.extractFileFromArchive(new File(test7ZipFile.getFile()), tempDir, overwriteExistingFiles, BinaryType.PHANTOMJS); } } diff --git a/src/test/java/com/lazerycode/selenium/hash/HashTypeAdaptorTest.java b/src/test/java/com/lazerycode/selenium/hash/HashTypeAdaptorTest.java new file mode 100644 index 0000000..2ffeca9 --- /dev/null +++ b/src/test/java/com/lazerycode/selenium/hash/HashTypeAdaptorTest.java @@ -0,0 +1,40 @@ +package com.lazerycode.selenium.hash; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class HashTypeAdaptorTest { + + @Test + public void returnsAValidHashTypeWithLowerCaseText() throws Exception { + HashTypeAdaptor hashTypeAdaptor = new HashTypeAdaptor(); + + assertThat(hashTypeAdaptor.unmarshal("sha1"), + is(equalTo(HashType.SHA1))); + } + + @Test + public void returnsAValidHashTypeWithUpperCaseText() throws Exception { + HashTypeAdaptor hashTypeAdaptor = new HashTypeAdaptor(); + + assertThat(hashTypeAdaptor.unmarshal("SHA1"), + is(equalTo(HashType.SHA1))); + } + + @Test(expected = IllegalArgumentException.class) + public void throwsAnIllegalArgumentExceptionIfHashTypeIsInvalid() throws Exception { + HashTypeAdaptor hashTypeAdaptor = new HashTypeAdaptor(); + hashTypeAdaptor.unmarshal("FOO"); + } + + @Test + public void returnsALowerCaseStringWhenMarshalling() throws Exception { + HashTypeAdaptor hashTypeAdaptor = new HashTypeAdaptor(); + + assertThat(hashTypeAdaptor.marshal(HashType.MD5), + is(equalTo("md5"))); + } +} diff --git a/src/test/java/com/lazerycode/selenium/repository/DriverContextTest.java b/src/test/java/com/lazerycode/selenium/repository/DriverContextTest.java new file mode 100644 index 0000000..90d3363 --- /dev/null +++ b/src/test/java/com/lazerycode/selenium/repository/DriverContextTest.java @@ -0,0 +1,19 @@ +package com.lazerycode.selenium.repository; + +import org.junit.Test; + +import static com.lazerycode.selenium.repository.BinaryType.GOOGLECHROME; +import static com.lazerycode.selenium.repository.OperatingSystem.OSX; +import static com.lazerycode.selenium.repository.SystemArchitecture.ARCHITECTURE_64_BIT; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class DriverContextTest { + + @Test + public void driverContextWithStringsMapsTpDriverContextWithEnums() { + assertThat(DriverContext.binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT), + is(equalTo(DriverContext.binaryDataFor("googlechrome", "osx", ARCHITECTURE_64_BIT)))); + } +} diff --git a/src/test/java/com/lazerycode/selenium/repository/DriverMapTest.java b/src/test/java/com/lazerycode/selenium/repository/DriverMapTest.java new file mode 100644 index 0000000..bd93a33 --- /dev/null +++ b/src/test/java/com/lazerycode/selenium/repository/DriverMapTest.java @@ -0,0 +1,101 @@ +package com.lazerycode.selenium.repository; + +import com.lazerycode.selenium.hash.HashType; +import org.junit.Test; + +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.TreeMap; + +import static com.lazerycode.selenium.repository.BinaryType.GOOGLECHROME; +import static com.lazerycode.selenium.repository.DriverContext.binaryDataFor; +import static com.lazerycode.selenium.repository.OperatingSystem.OSX; +import static com.lazerycode.selenium.repository.SystemArchitecture.ARCHITECTURE_64_BIT; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsInstanceOf.instanceOf; + +public class DriverMapTest { + + @Test + public void willReturnEmptyMap() { + DriverMap driverMap = new DriverMap(); + Map versionMap = driverMap.getMapForDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT)); + + assertThat(versionMap, + is(instanceOf(TreeMap.class))); + assertThat(versionMap.size(), + is(equalTo(0))); + } + + @Test + public void willReturnExistingMap() { + DriverMap driverMap = new DriverMap(); + Map originalVersionMap = driverMap.getMapForDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT)); + originalVersionMap.put("2.13", new DriverDetails()); + + Map versionMap = driverMap.getMapForDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT)); + + assertThat(versionMap, + is(instanceOf(TreeMap.class))); + assertThat(versionMap.size(), + is(equalTo(1))); + } + + @Test(expected = IllegalArgumentException.class) + public void willThrowAnExceptionWhenTryingToGetSpecificVersionMapForInvalidContext() { + DriverMap driverMap = new DriverMap(); + driverMap.getDetailsForVersionOfDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT), "4"); + } + + @Test(expected = NoSuchElementException.class) + public void willThrowAnExceptionWhenTryingToGetAnInvalidVersion() { + DriverMap driverMap = new DriverMap(); + Map originalVersionMap = driverMap.getMapForDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT)); + originalVersionMap.put("2.13", new DriverDetails()); + + driverMap.getDetailsForVersionOfDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT), "4"); + } + + @Test(expected = IllegalArgumentException.class) + public void willThrowAnExceptionWhenTryingToGetLatestVersionMapForInvalidContext() { + DriverMap driverMap = new DriverMap(); + driverMap.getDetailsForLatestVersionOfDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT)); + } + + @Test(expected = NoSuchElementException.class) + public void willThrowAnExceptionWhenTryingToGetAnInvalidLatestVersion() { + DriverMap driverMap = new DriverMap(); + driverMap.getMapForDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT)); + + driverMap.getDetailsForLatestVersionOfDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT)); + } + + @Test + public void correctlyReturnsLatestVersion() throws MalformedURLException { + DriverMap driverMap = new DriverMap(); + DriverDetails oldestVersion = new DriverDetails(); + oldestVersion.hash = "oldest"; + oldestVersion.hashType = HashType.SHA1; + oldestVersion.fileLocation = new URL("http://www.example.com/foo/bar"); + DriverDetails latestVersion = new DriverDetails(); + latestVersion.hash = "latest"; + latestVersion.hashType = HashType.MD5; + latestVersion.fileLocation = new URL("http://www.example.com/bar/foo"); + Map originalVersionMap = driverMap.getMapForDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT)); + originalVersionMap.put("2.13", oldestVersion); + originalVersionMap.put("4", latestVersion); + + DriverDetails returnedDetails = driverMap.getDetailsForLatestVersionOfDriverContext(binaryDataFor(GOOGLECHROME, OSX, ARCHITECTURE_64_BIT)); + + assertThat(returnedDetails.hash, + is(equalTo("latest"))); + assertThat(returnedDetails.hashType, + is(equalTo(HashType.MD5))); + assertThat(returnedDetails.fileLocation, + is(equalTo(new URL("http://www.example.com/bar/foo")))); + } +} diff --git a/src/test/java/com/lazerycode/selenium/repository/FileRepositoryTest.java b/src/test/java/com/lazerycode/selenium/repository/FileRepositoryTest.java new file mode 100644 index 0000000..5dff59a --- /dev/null +++ b/src/test/java/com/lazerycode/selenium/repository/FileRepositoryTest.java @@ -0,0 +1,30 @@ +package com.lazerycode.selenium.repository; + +import com.lazerycode.selenium.OSType; +import org.apache.maven.plugin.MojoFailureException; +import org.junit.Test; + +import javax.xml.bind.JAXBException; +import javax.xml.xpath.XPathExpressionException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.util.HashMap; +import java.util.HashSet; + +import static com.lazerycode.selenium.repository.FileRepository.buildDownloadableFileRepository; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +public class FileRepositoryTest { + + @Test + public void listOfStuffReturnedWhenPassingInARepositoryMap() throws XPathExpressionException, MojoFailureException, JAXBException, MalformedURLException { + InputStream xmlRepositoryMap = this.getClass().getResourceAsStream("/TestRepoMap.xml"); + XMLParser parser = new XMLParser(xmlRepositoryMap, new HashSet(), new HashMap(), false, false); + DriverMap driverMap = buildDownloadableFileRepository(parser.getAllNodesInScope()); + + assertThat(driverMap.repository.size(), + is(equalTo(8))); + } +} diff --git a/src/test/java/com/lazerycode/selenium/repository/XMLParserTest.java b/src/test/java/com/lazerycode/selenium/repository/XMLParserTest.java new file mode 100644 index 0000000..2b6cbb6 --- /dev/null +++ b/src/test/java/com/lazerycode/selenium/repository/XMLParserTest.java @@ -0,0 +1,141 @@ +package com.lazerycode.selenium.repository; + +import com.lazerycode.selenium.OSType; +import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList; +import org.junit.Test; +import org.w3c.dom.NodeList; + +import javax.xml.xpath.XPathExpressionException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.CombinableMatcher.either; +import static org.hamcrest.core.Is.is; + +public class XMLParserTest { + + @Test + public void returnsNoOperatingSystemsByDefault() { + Set none = new HashSet(); + XMLParser parser = new XMLParser(null, none, null, false, false); + + assertThat(parser.operatingSystemSelector(), + is(equalTo(""))); + } + + @Test + public void returnsASingleOperatingSystemWhenAListWithOnEntryIsSupplied() { + Set one = new HashSet(); + one.add(OSType.WINDOWS); + XMLParser parser = new XMLParser(null, one, null, false, false); + + assertThat(parser.operatingSystemSelector(), + is(equalTo("[parent::windows]"))); + } + + @Test + public void returnsACorrectlyFormattedStringWhenMultipleOperatingSystemsAreSupplied() { + Set two = new HashSet(); + two.add(OSType.WINDOWS); + two.add(OSType.LINUX); + XMLParser parser = new XMLParser(null, two, null, false, false); + + assertThat(parser.operatingSystemSelector(), + either(is("[parent::windows|parent::linux]")) + .or(is("[parent::linux|parent::windows]")) + ); + } + + @Test + public void returnsNothingIfNoSpecificVersionsSpecified() { + Map versionsToFind = new HashMap(); + XMLParser parser = new XMLParser(null, null, versionsToFind, false, false); + + assertThat(parser.driverVersionSelector(), + is(equalTo(""))); + } + + @Test + public void returnsASingleVersion() { + Map versionsToFind = new HashMap(); + versionsToFind.put("googlechrome", "2.14"); + XMLParser parser = new XMLParser(null, null, versionsToFind, false, false); + + assertThat(parser.driverVersionSelector(), + is(equalTo("[(parent::*[@id='googlechrome'] and @id='2.14')]"))); + } + + @Test + public void returnsMultipleVersions() { + Map versionsToFind = new HashMap(); + versionsToFind.put("googlechrome", "2.14"); + versionsToFind.put("internetexplorer", "2.45.0"); + XMLParser parser = new XMLParser(null, null, versionsToFind, false, false); + + assertThat(parser.driverVersionSelector(), + either(is("[(parent::*[@id='googlechrome'] and @id='2.14') or (parent::*[@id='internetexplorer'] and @id='2.45.0')]")) + .or(is("[(parent::*[@id='internetexplorer'] and @id='2.45.0') or (parent::*[@id='googlechrome'] and @id='2.14')]")) + ); + } + + @Test + public void returnsEmptyStringWhenNoBitratesSet() { + XMLParser parser = new XMLParser(null, null, null, false, false); + + assertThat(parser.calculateBitrate(), + is(equalTo(""))); + } + + @Test + public void returnsCorrectStringWhenThirtyTwoBitIsTrue() { + XMLParser parser = new XMLParser(null, null, null, true, false); + + assertThat(parser.calculateBitrate(), + is(equalTo("[@thirtytwobit='true']"))); + } + + @Test + public void returnsCorrectStringWhenSixtyFourBitIsTrue() { + XMLParser parser = new XMLParser(null, null, null, false, true); + + assertThat(parser.calculateBitrate(), + is(equalTo("[@sixtyfourbit='true']"))); + } + + @Test + public void returnsCorrectStringWhenBothThirtyTwoBitAndSixtyFourBitAreTrue() { + XMLParser parser = new XMLParser(null, null, null, true, true); + + assertThat(parser.calculateBitrate(), + is(equalTo("[@thirtytwobit='true' or @sixtyfourbit='true']"))); + } + + @Test + public void listWithAllNodesReturnedWhenPassingInARepositoryMap() throws XPathExpressionException { + InputStream xmlRepositoryMap = this.getClass().getResourceAsStream("/TestRepoMap.xml"); + XMLParser parser = new XMLParser(xmlRepositoryMap, new HashSet(), new HashMap(), false, false); + NodeList nodes = parser.getAllNodesInScope(); + + assertThat(nodes.getLength(), + is(equalTo(12))); + } + + @Test + public void returnsSpecificNodeLocatorWhenOptionsAreSpecified() throws XPathExpressionException { + Set one = new HashSet(); + one.add(OSType.WINDOWS); + Map versionsToFind = new HashMap(); + versionsToFind.put("googlechrome", "22"); + InputStream xmlRepositoryMap = this.getClass().getResourceAsStream("/TestRepoMap.xml"); + XMLParser parser = new XMLParser(xmlRepositoryMap, one, versionsToFind, true, true); + NodeList nodes = parser.getAllNodesInScope(); + + assertThat(nodes.getLength(), + is(equalTo(1))); + } +} \ No newline at end of file