001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.testtools.selenium;
017
018import org.apache.commons.io.IOUtils;
019import org.openqa.selenium.By;
020
021import java.io.File;
022import java.io.FileOutputStream;
023import java.io.InputStream;
024import java.io.OutputStream;
025import java.net.URL;
026import java.net.URLDecoder;
027import java.util.ArrayList;
028import java.util.Collections;
029import java.util.Enumeration;
030import java.util.HashSet;
031import java.util.List;
032import java.util.Set;
033import java.util.jar.JarEntry;
034import java.util.jar.JarFile;
035
036/**
037 * Methods for using files and resources in Afts
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 */
041public abstract class WebDriverFileResourceAftBase extends WebDriverLegacyITBase {
042
043    // values set by default for repeatable testing; left as configurable for load tests
044    protected List<File> fileUploadList;
045
046    protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
047        String classPath = clazz.getName().replace(".", "/")+".class";
048        URL dirUrl = clazz.getClassLoader().getResource(classPath);
049
050        if (!"jar".equals(dirUrl.getProtocol())) {
051            throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
052        }
053
054        String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); //strip out only the JAR file
055        JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
056        Enumeration<JarEntry> entries = jar.entries();
057        Set<String> result = new HashSet<String>();
058
059        while(entries.hasMoreElements()) {
060            String entry = entries.nextElement().getName();
061            if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) { //filter according to the pathStartsWith skipping directories
062                result.add(entry);
063            }
064        }
065
066        return result.toArray(new String[result.size()]);
067    }
068
069    protected void setUpFiles(String path, String fileExtension) throws Exception {
070        fileUploadList = new ArrayList<File>();
071
072        File dir = new File(path);
073
074        if (dir != null && dir.listFiles().length > 0) {
075            Integer i = 1;
076
077            for (File file : dir.listFiles()) {
078                if (file.getName().endsWith(fileExtension)) {
079                    fileUploadList.add(file);
080                }
081
082                i++;
083            }
084
085            Collections.sort(fileUploadList);
086        } else {
087            throw new Exception("----Resources not found----");
088        }
089    }
090
091    protected void setUpResourceDir(String resourceDir) {
092        setUpResourceDir(resourceDir, "txt");
093    }
094
095    protected void setUpResourceDir(String resourceDir, String fileExtension) {
096        try {
097            setUpFiles("src/test/resources/" + resourceDir, fileExtension);
098        } catch (Exception e) {
099            System.out.println("Problem loading files from filesystem ( " + e.getMessage() +
100                    "). If running from Intellij make sure working directory is " +
101                    "rice-framework/krad-sampleapp/web attempt to load as resource.");
102
103            try {
104                setUpResourceFiles(resourceDir);
105            } catch (Exception e1) {
106                e1.printStackTrace();
107                jiraAwareFail("Problems loading files as resources " + e1.getMessage());
108            }
109        }
110    }
111
112    protected void setUpResourceFiles(String resourceDir) throws Exception {
113        String[] resources = getResourceListing(getClass(), resourceDir);
114        fileUploadList = new ArrayList<File>();
115
116        for (String resource : resources) {
117            InputStream inputStream = getClass().getResourceAsStream(resource);
118            File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource);
119            OutputStream outputStream = new FileOutputStream(file);
120            IOUtils.copy(inputStream, outputStream);
121            outputStream.close();
122            fileUploadList.add(file);
123        }
124
125        Collections.sort(fileUploadList);
126    }
127
128    protected void fileIngesterBy(By by) {
129        if(fileUploadList!=null && fileUploadList.size()>0) {
130            for (File file : fileUploadList) {
131                String path = file.getAbsolutePath().toString();
132                driver.findElement(by).sendKeys(path);
133            }
134        }
135    }
136
137    protected void fileIngesterByName(String name) {
138        fileIngesterBy(By.name(name));
139    }
140
141    protected void fileIngesterCollection() throws Exception {
142        fileIngesterBy(By.xpath("//div[@data-label='Attached File']/fieldset/div/div/input[@type='file']"));
143    }
144}