001/** 002 * Copyright 2005-2014 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.krad.labs; 017 018import java.io.File; 019import java.io.FileOutputStream; 020import java.io.InputStream; 021import java.io.OutputStream; 022import java.net.URL; 023import java.net.URLDecoder; 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.Enumeration; 027import java.util.HashSet; 028import java.util.List; 029import java.util.Set; 030import java.util.jar.JarEntry; 031import java.util.jar.JarFile; 032 033import org.apache.commons.io.IOUtils; 034import org.junit.Test; 035import org.kuali.rice.testtools.selenium.WebDriverLegacyITBase; 036import org.openqa.selenium.By; 037 038/** 039 * @author Kuali Rice Team (rice.collab@kuali.org) 040 */ 041public class LabsMultiFileUploadBasic1Aft extends WebDriverLegacyITBase { 042 043 /** 044 * /kr-krad/labs?viewId=Lab-MultiFileUpload1 045 */ 046 public static final String BOOKMARK_URL = "/kr-krad/labs?viewId=Lab-MultiFileUpload1"; 047 048 // values set by default for repeatable testing; left as configurable for load tests 049 protected List<File> fileUploadList; 050 051 @Override 052 protected String getBookmarkUrl() { 053 return BOOKMARK_URL; 054 } 055 056 @Override 057 protected void navigate() throws Exception { 058 waitAndClickByLinkText("MultiFile Upload - Basic"); 059 } 060 061 protected void testMultiFileUploadBasic1() throws Exception { 062 fileUploadSetUp(); 063 fileIngester(); 064 } 065 066 private void fileUploadSetUp() throws Exception { 067 setUpResourceDir("general"); 068 } 069 070 protected void setUpResourceDir(String resourceDir) { 071 try { 072 setUpFiles("src/test/resources/" + resourceDir); 073 System.out.println("Try for setUpResourceDir"); 074 } catch (Exception e) { 075 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."); 076 077 try { 078 setUpResourceFiles(resourceDir); 079 } catch (Exception e1) { 080 e1.printStackTrace(); 081 jiraAwareFail("Problems loading files as resources " + e1.getMessage()); 082 } 083 084 System.out.println("Catch for setUpResourceDir"); 085 } 086 } 087 088 protected void setUpResourceFiles(String resourceDir) throws Exception { 089 System.out.println("In for setUpResourceFiles"); 090 String[] resources = getResourceListing(getClass(), resourceDir); 091 fileUploadList = new ArrayList<File>(); 092 093 for (String resource : resources) { 094 InputStream inputStream = getClass().getResourceAsStream(resource); 095 File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource); 096 OutputStream outputStream = new FileOutputStream(file); 097 IOUtils.copy(inputStream, outputStream); 098 outputStream.close(); 099 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 if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) { //filter according to the pathStartsWith skipping directories 123 result.add(entry); 124 } 125 } 126 127 return result.toArray(new String[result.size()]); 128 } 129 130 protected void setUpFiles(String path) throws Exception { 131 System.out.println("In for setUpFiles"); 132 fileUploadList = new ArrayList<File>(); 133 134 File dir = new File(path); 135 136 if (dir != null && dir.listFiles().length > 0) { 137 Integer i = 1; 138 139 for (File file : dir.listFiles()) { 140 if (file.getName().endsWith(".txt")) { 141 fileUploadList.add(file); 142 } 143 144 i++; 145 } 146 147 Collections.sort(fileUploadList); 148 } else { 149 throw new Exception("----Resources not found----"); 150 } 151 } 152 153 /** 154 * Performs Ingesting files to fileupload component and asserts succesful ingestion. 155 * 156 */ 157 private void fileIngester() throws Exception { 158 System.out.println("In for fileIngester"); 159 160 if(fileUploadList!=null && fileUploadList.size()>0) 161 { 162 for (File file : fileUploadList) { 163 String path = file.getAbsolutePath().toString(); 164 driver.findElement(By.name("files")).sendKeys(path); 165 System.out.println("In for -------"); 166 } 167 } 168 } 169 170 /** 171 * Performs Ingesting files to fileupload component and asserts succesful ingestion. 172 * 173 */ 174 private void fileIngesterCollection() throws Exception { 175 System.out.println("In for fileIngester"); 176 177 if(fileUploadList!=null && fileUploadList.size()>0) 178 { 179 for (File file : fileUploadList) { 180 String path = file.getAbsolutePath().toString(); 181 driver.findElement(By.xpath("//div[@data-label='Attached File']/fieldset/div/div/input[@type='file']")).sendKeys(path); 182 System.out.println("In for -------"); 183 } 184 } 185 } 186 187 @Test 188 public void testMultiFileUploadBasic1Bookmark() throws Exception { 189 testMultiFileUploadBasic1(); 190 passed(); 191 } 192 193 @Test 194 public void testMultiFileUploadBasic1Nav() throws Exception { 195 testMultiFileUploadBasic1(); 196 passed(); 197 } 198}