View Javadoc
1   /**
2    * Copyright 2005-2014 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.krad.labs;
17  
18  import java.io.File;
19  import java.io.FileOutputStream;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.URL;
23  import java.net.URLDecoder;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.Enumeration;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  import java.util.jar.JarEntry;
31  import java.util.jar.JarFile;
32  
33  import org.apache.commons.io.IOUtils;
34  import org.junit.Test;
35  import org.kuali.rice.testtools.selenium.WebDriverLegacyITBase;
36  import org.openqa.selenium.By;
37  
38  /**
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   */
41  public class LabsMultiFileUploadMultipleUsesOnOnePageAft extends WebDriverLegacyITBase {
42  
43      /**
44       * /kr-krad/labs?viewId=Lab-MultiFileUpload2
45       */
46      public static final String BOOKMARK_URL = "/kr-krad/labs?viewId=Lab-MultiFileUpload2";
47     
48      // values set by default for repeatable testing; left as configurable for load tests
49      protected List<File> fileUploadList;
50      
51      @Override
52      protected String getBookmarkUrl() {
53          return BOOKMARK_URL;
54      }
55  
56      @Override
57      protected void navigate() throws Exception {
58      	waitAndClickByLinkText("MultiFile Upload - Multiple Uses on One Page");
59      }
60  
61      protected void testMultiFileUploadMultipleUsesOnOnePage() throws Exception {
62      	fileUploadSetUp();
63      	fileIngester();
64      }
65      
66      private void fileUploadSetUp() throws Exception {
67      	setUpResourceDir("general");
68      }
69      
70      protected void setUpResourceDir(String resourceDir) {
71          try {
72              setUpFiles("src/test/resources/" + resourceDir);
73              System.out.println("Try for setUpResourceDir");
74          } catch (Exception e) {
75              System.out.println("Problem loading files from filesystem ( " + e.getMessage() + "). If running from Intellij make sure working directory is rice-framework/krad-sampleapp/web attempt to load as resource.");
76              
77              try {
78                  setUpResourceFiles(resourceDir);
79              } catch (Exception e1) {
80                  e1.printStackTrace();
81                  jiraAwareFail("Problems loading files as resources " + e1.getMessage());
82              }
83              
84              System.out.println("Catch for setUpResourceDir");
85          }
86      }
87      
88      protected void setUpResourceFiles(String resourceDir) throws Exception {
89          System.out.println("In for setUpResourceFiles");
90          String[] resources = getResourceListing(getClass(), resourceDir);
91          fileUploadList = new ArrayList<File>();
92  
93          for (String resource : resources) {
94              InputStream inputStream = getClass().getResourceAsStream(resource);
95              File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource);
96              OutputStream outputStream = new FileOutputStream(file);
97              IOUtils.copy(inputStream, outputStream);
98              outputStream.close();
99              fileUploadList.add(file);
100             System.out.println("For for setUpResourceFiles");
101         }
102         
103         Collections.sort(fileUploadList);
104     }
105 
106     protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
107     	System.out.println("In for getResourceListing");
108         String classPath = clazz.getName().replace(".", "/")+".class";
109         URL dirUrl = clazz.getClassLoader().getResource(classPath);
110 
111         if (!"jar".equals(dirUrl.getProtocol())) {
112             throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
113         }
114 
115         String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!")); //strip out only the JAR file
116         JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
117         Enumeration<JarEntry> entries = jar.entries();
118         Set<String> result = new HashSet<String>();
119 
120         while(entries.hasMoreElements()) {
121             String entry = entries.nextElement().getName();
122             
123             if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) { //filter according to the pathStartsWith skipping directories
124                 result.add(entry);
125             }
126         }
127 
128         return result.toArray(new String[result.size()]);
129     }
130 
131     protected void setUpFiles(String path) throws Exception {
132     	System.out.println("In for setUpFiles");
133         fileUploadList = new ArrayList<File>();
134 
135         File dir = new File(path);
136 
137         if (dir != null && dir.listFiles().length > 0) {
138             Integer i = 1;
139             
140             for (File file : dir.listFiles()) {
141                 if (file.getName().endsWith(".txt")) {
142                         fileUploadList.add(file);
143                 }
144                 
145                 i++;
146             }
147             
148             Collections.sort(fileUploadList);
149         } else {
150             throw new Exception("----Resources not found----");
151         }
152     }
153     
154     /**
155      * Performs Ingesting files to fileupload component and asserts succesful ingestion.
156      *
157      */
158     private void fileIngester() throws Exception {
159     	System.out.println("In for fileIngester");
160     	
161         if(fileUploadList!=null && fileUploadList.size()>0)
162         {
163 	        for (File file : fileUploadList) {
164 	            String path = file.getAbsolutePath().toString();
165 	            driver.findElement(By.name("files2")).sendKeys(path);
166 	            System.out.println("In for -------");
167 	        }
168         }
169     }
170     
171     /**
172      * Performs Ingesting files to fileupload component and asserts succesful ingestion.
173      *
174      */
175     private void fileIngesterCollection() throws Exception {
176     	System.out.println("In for fileIngester");
177     	
178         if(fileUploadList!=null && fileUploadList.size()>0)
179         {
180 	        for (File file : fileUploadList) {
181 	            String path = file.getAbsolutePath().toString();
182 	            driver.findElement(By.xpath("//div[@data-label='Attached File']/fieldset/div/div/input[@type='file']")).sendKeys(path);
183 	            System.out.println("In for -------");
184 	        }
185         }
186     }
187 
188     @Test
189     public void testMultiFileUploadMultipleUsesOnOnePageBookmark() throws Exception {
190     	testMultiFileUploadMultipleUsesOnOnePage();
191         passed();
192     }
193 
194     @Test
195     public void testMultiFileUploadMultipleUsesOnOnePageNav() throws Exception {
196     	testMultiFileUploadMultipleUsesOnOnePage();
197         passed();
198     }
199 }