1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41 public class LabsMultiFileUploadBasic1Aft extends WebDriverLegacyITBase {
42
43
44
45
46 public static final String BOOKMARK_URL = "/kr-krad/labs?viewId=Lab-MultiFileUpload1";
47
48
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 - Basic");
59 }
60
61 protected void testMultiFileUploadBasic1() 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("!"));
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("/")) {
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
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
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 }