1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.demo.uif.library.elements;
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.krad.demo.uif.library.LibraryBase;
36 import org.kuali.rice.testtools.selenium.WebDriverLegacyITBase;
37 import org.openqa.selenium.By;
38
39
40
41
42 public class LibraryElementMultiFileUploadAft extends LibraryBase {
43
44
45
46
47 public static final String BOOKMARK_URL = "/kr-krad/kradsampleapp?viewId=Demo-MultiFileUploadView";
48
49
50 protected List<File> fileUploadList;
51
52 @Override
53 protected String getBookmarkUrl() {
54 return BOOKMARK_URL;
55 }
56
57 @Override
58 protected void navigate() throws Exception {
59 waitAndClickLibraryLink();
60 waitAndClickByLinkText("Elements");
61 waitAndClickByLinkText("Multi-file Upload");
62 }
63
64 protected void testElementMultiFileUpload() throws Exception {
65 fileUploadSetUpforText();
66 fileIngester("files");
67 selectByName("exampleShown","Max Size Multi-file Upload");
68 fileIngester("files1");
69 selectByName("exampleShown","Extra Fields Multi-file Upload");
70 fileIngester("files2");
71 selectByName("exampleShown","File Types Multi-file Upload");
72 fileUploadSetUpforJpg();
73 fileIngester("files3");
74 }
75
76 private void fileUploadSetUpforText() throws Exception {
77 setUpResourceDir("general","txt");
78 }
79
80 private void fileUploadSetUpforJpg() throws Exception {
81 setUpResourceDir("general","jpg");
82 }
83
84 protected void setUpResourceDir(String resourceDir,String fileExtension) {
85 try {
86 setUpFiles("src/test/resources/" + resourceDir,fileExtension);
87 System.out.println("Try for setUpResourceDir");
88 } catch (Exception e) {
89 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.");
90
91 try {
92 setUpResourceFiles(resourceDir);
93 } catch (Exception e1) {
94 e1.printStackTrace();
95 jiraAwareFail("Problems loading files as resources " + e1.getMessage());
96 }
97
98 System.out.println("Catch for setUpResourceDir");
99 }
100 }
101
102 protected void setUpResourceFiles(String resourceDir) throws Exception {
103 System.out.println("In for setUpResourceFiles");
104 String[] resources = getResourceListing(getClass(), resourceDir);
105 fileUploadList = new ArrayList<File>();
106
107 for (String resource : resources) {
108 InputStream inputStream = getClass().getResourceAsStream(resource);
109 File file = new File(System.getProperty("java.io.tmpdir") + File.separator + resource);
110 OutputStream outputStream = new FileOutputStream(file);
111 IOUtils.copy(inputStream, outputStream);
112 outputStream.close();
113 fileUploadList.add(file);
114 System.out.println("For for setUpResourceFiles");
115 }
116
117 Collections.sort(fileUploadList);
118 }
119
120 protected String[] getResourceListing(Class clazz, String pathStartsWith) throws Exception {
121 System.out.println("In for getResourceListing");
122 String classPath = clazz.getName().replace(".", "/")+".class";
123 URL dirUrl = clazz.getClassLoader().getResource(classPath);
124
125 if (!"jar".equals(dirUrl.getProtocol())) {
126 throw new UnsupportedOperationException("Cannot list files for URL " + dirUrl);
127 }
128
129 String jarPath = dirUrl.getPath().substring(5, dirUrl.getPath().indexOf("!"));
130 JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
131 Enumeration<JarEntry> entries = jar.entries();
132 Set<String> result = new HashSet<String>();
133
134 while(entries.hasMoreElements()) {
135 String entry = entries.nextElement().getName();
136 if (entry.startsWith(pathStartsWith) && !entry.endsWith("/")) {
137 result.add(entry);
138 }
139 }
140
141 return result.toArray(new String[result.size()]);
142 }
143
144 protected void setUpFiles(String path,String fileExtension) throws Exception {
145 System.out.println("In for setUpFiles");
146 fileUploadList = new ArrayList<File>();
147
148 File dir = new File(path);
149
150 if (dir != null && dir.listFiles().length > 0) {
151 Integer i = 1;
152
153 for (File file : dir.listFiles()) {
154 if (file.getName().endsWith(fileExtension)) {
155 fileUploadList.add(file);
156 }
157
158 i++;
159 }
160
161 Collections.sort(fileUploadList);
162 } else {
163 throw new Exception("----Resources not found----");
164 }
165 }
166
167
168
169
170
171 private void fileIngester(String name) throws Exception {
172 System.out.println("In for fileIngester");
173
174 if(fileUploadList!=null && fileUploadList.size()>0)
175 {
176 for (File file : fileUploadList) {
177 String path = file.getAbsolutePath().toString();
178 driver.findElement(By.name(name)).sendKeys(path);
179 System.out.println("In for -------");
180 }
181 }
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201 @Test
202 public void testElementMultiFileUploadBookmark() throws Exception {
203 testElementMultiFileUpload();
204 passed();
205 }
206
207 @Test
208 public void testElementMultiFileUploadNav() throws Exception {
209 testElementMultiFileUpload();
210 passed();
211 }
212 }