View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.testtools.selenium;
17  
18  import org.apache.commons.io.IOUtils;
19  import org.openqa.selenium.By;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.net.URL;
26  import java.net.URLDecoder;
27  import java.util.ArrayList;
28  import java.util.Collections;
29  import java.util.Enumeration;
30  import java.util.HashSet;
31  import java.util.List;
32  import java.util.Set;
33  import java.util.jar.JarEntry;
34  import java.util.jar.JarFile;
35  
36  /**
37   * Methods for using files and resources in Afts
38   *
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public abstract class WebDriverFileResourceAftBase extends WebDriverLegacyITBase {
42  
43      // values set by default for repeatable testing; left as configurable for load tests
44      protected List<File> fileUploadList;
45  
46      protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
47          String classPath = clazz.getName().replace(".", "/")+".class";
48          URL dirUrl = clazz.getClassLoader().getResource(classPath);
49  
50          if (!"jar".equals(dirUrl.getProtocol())) {
51              throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
52          }
53  
54          String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); //strip out only the JAR file
55          JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
56          Enumeration<JarEntry> entries = jar.entries();
57          Set<String> result = new HashSet<String>();
58  
59          while(entries.hasMoreElements()) {
60              String entry = entries.nextElement().getName();
61              if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) { //filter according to the pathStartsWith skipping directories
62                  result.add(entry);
63              }
64          }
65  
66          return result.toArray(new String[result.size()]);
67      }
68  
69      protected void setUpFiles(String path, String fileExtension) throws Exception {
70          fileUploadList = new ArrayList<File>();
71  
72          File dir = new File(path);
73  
74          if (dir != null && dir.listFiles().length > 0) {
75              Integer i = 1;
76  
77              for (File file : dir.listFiles()) {
78                  if (file.getName().endsWith(fileExtension)) {
79                      fileUploadList.add(file);
80                  }
81  
82                  i++;
83              }
84  
85              Collections.sort(fileUploadList);
86          } else {
87              throw new Exception("----Resources not found----");
88          }
89      }
90  
91      protected void setUpResourceDir(String resourceDir) {
92          setUpResourceDir(resourceDir, "txt");
93      }
94  
95      protected void setUpResourceDir(String resourceDir, String fileExtension) {
96          try {
97              setUpFiles("src/test/resources/" + resourceDir, fileExtension);
98          } catch (Exception e) {
99              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 }