Search in sources :

Example 46 with IFileSystemOperations

use of com.axway.ats.core.filesystem.model.IFileSystemOperations in project ats-framework by Axway.

the class MobileDeviceUtils method listFiles.

/**
    *
    * @param path a path on the mobile device. It can be a directory or file (if you want to check
    * for specific file existence)
    * <br/>
    * <b>Note for iOS:</b> You have to specify a relative path to the application data folder<br/>
    * For example: <i>"Documents/MyAppFiles"</i><br/>
    * and we'll internally search for files in:
    *  <i>"/Users/&lt;username&gt;/Library/Developer/CoreSimulator/Devices/&lt;device_id&gt;/data/Containers/Data/Application/&lt;app_id&gt;/Documents/MyAppFiles/"</i><br/>
    * which is the iOS Simulator application data folder path
    *
    * @return {@link FileInfo} array with all the files and folders from the target path
    */
public FileInfo[] listFiles(String path) {
    List<FileInfo> files = new ArrayList<FileInfo>();
    if (this.mobileDriver.isAndroidAgent()) {
        String[] commandArguments = new String[] { "shell", "ls", "-lan", path };
        try {
            IProcessExecutor processExecutor = executeAdbCommand(commandArguments, true);
            String result = processExecutor.getStandardOutput();
            if (!StringUtils.isNullOrEmpty(result)) {
                String[] lines = result.split("[\r\n]+");
                for (String line : lines) {
                    Matcher m = LS_ENTRY_PATTERN.matcher(line);
                    if (m.matches()) {
                        /*
                             * group 1 -> file size
                             * group 2 -> modification date and time
                             * group 3 -> file name
                             */
                        String fileName = m.group(3);
                        String absolutePath = new String(IoUtils.normalizeUnixDir(path) + fileName).replace("//", "/");
                        if (line.startsWith("l") && line.contains("->")) {
                            // a link
                            // after '->'
                            absolutePath = fileName.substring(fileName.lastIndexOf("->") + 2).trim();
                            // before '->'
                            fileName = fileName.substring(0, fileName.indexOf("->")).trim();
                        }
                        String fileSize = m.group(1);
                        FileInfo file = new FileInfo(fileName, absolutePath, line.startsWith("d") || fileSize.isEmpty());
                        if (!fileSize.isEmpty()) {
                            file.setSize(Long.parseLong(fileSize));
                        }
                        file.setModificationDate(LS_DATE_FORMAT.parse(m.group(2)));
                        files.add(file);
                    }
                }
            }
        } catch (Exception e) {
            throw new MobileOperationException("Unable to list files for path '" + path + "'", e);
        }
    } else {
        // iOS case supposed
        IFileSystemOperations fileSystemOperations = getFileSystemOperatoinsImpl();
        String[] filePaths = fileSystemOperations.findFiles(getiOSApplicationDataPath() + path, ".*", true, true, false);
        for (String filePath : filePaths) {
            boolean isDirectory = filePath.endsWith("/");
            if (isDirectory) {
                filePath = filePath.substring(0, filePath.length() - 1);
            }
            String fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
            FileInfo file = new FileInfo(fileName, filePath, isDirectory);
            file.setSize(fileSystemOperations.getFileSize(filePath));
            files.add(file);
        }
    }
    return files.toArray(new FileInfo[files.size()]);
}
Also used : Matcher(java.util.regex.Matcher) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) ArrayList(java.util.ArrayList) IFileSystemOperations(com.axway.ats.core.filesystem.model.IFileSystemOperations) MobileOperationException(com.axway.ats.uiengine.exceptions.MobileOperationException) NotSupportedOperationException(com.axway.ats.uiengine.exceptions.NotSupportedOperationException) IProcessExecutor(com.axway.ats.core.process.model.IProcessExecutor)

Aggregations

IFileSystemOperations (com.axway.ats.core.filesystem.model.IFileSystemOperations)46 Validator (com.axway.ats.core.validation.Validator)45 PublicAtsApi (com.axway.ats.common.PublicAtsApi)44 LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)6 FileMatchInfo (com.axway.ats.common.filesystem.FileMatchInfo)1 IProcessExecutor (com.axway.ats.core.process.model.IProcessExecutor)1 MobileOperationException (com.axway.ats.uiengine.exceptions.MobileOperationException)1 NotSupportedOperationException (com.axway.ats.uiengine.exceptions.NotSupportedOperationException)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1