1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.fckeditor.response;
22
23 import static org.junit.Assert.assertEquals;
24
25 import org.junit.Test;
26
27
28
29
30
31
32 public class UploadResponseTest {
33
34 @Test(expected = IllegalArgumentException.class)
35 public void noArguments() throws Exception {
36 new UploadResponse();
37 }
38
39 @Test(expected = IllegalArgumentException.class)
40 public void tooManyArguments() throws Exception {
41 new UploadResponse(101, "/some/url/file.txt", "file.txt",
42 "something's wrong", "arg no. 5");
43 }
44
45 @Test(expected = IllegalArgumentException.class)
46 public void notANumber() throws Exception {
47 new UploadResponse("1");
48 }
49
50 @Test
51 public void onlyErrorNumber() throws Exception {
52 UploadResponse actual = new UploadResponse(
53 UploadResponse.EN_INVALID_FILE_TYPE_ERROR);
54 String expected = "<script type=\"text/javascript\">\n"
55 + "(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n"
56 + "window.parent.OnUploadCompleted(202);\n</script>";
57 assertEquals(expected, actual.toString());
58 }
59
60 @Test
61 public void fourArguments() throws Exception {
62 UploadResponse actual = new UploadResponse(UploadResponse.EN_OK,"/fckeditor-java/userfiles/image/fredck.jpg");
63 String expected = "<script type=\"text/javascript\">\n"
64 + "(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n"
65 + "window.parent.OnUploadCompleted(0,'/fckeditor-java/userfiles/image/fredck.jpg');\n</script>";
66 assertEquals(expected, actual.toString());
67 }
68
69 @Test
70 public void renamedFile() throws Exception {
71 UploadResponse actual = new UploadResponse(UploadResponse.EN_FILE_RENAMED_WARNING,"/fckeditor-java/userfiles/image/hacked_php.txt","hacked_php.txt");
72 String expected = "<script type=\"text/javascript\">\n"
73 + "(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n"
74 + "window.parent.OnUploadCompleted(201,'/fckeditor-java/userfiles/image/hacked_php.txt','hacked_php.txt');\n</script>";
75 assertEquals(expected, actual.toString());
76 }
77
78 @Test
79 public void customMessage() throws Exception {
80 UploadResponse actual = new UploadResponse(UploadResponse.EN_CUSTOM_ERROR);
81 actual.setCustomMessage("some error");
82 String expected = "<script type=\"text/javascript\">\n"
83 + "(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n"
84 + "window.parent.OnUploadCompleted(1,'','','some error');\n</script>";
85 assertEquals(expected, actual.toString());
86 }
87
88
89 @Test
90 public void nullArguments() throws Exception {
91 UploadResponse actual = new UploadResponse(UploadResponse.EN_CUSTOM_ERROR,null,null,null);
92 String expected = "<script type=\"text/javascript\">\n"
93 + "(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n"
94 + "window.parent.OnUploadCompleted(1,'','','');\n</script>";
95 assertEquals(expected, actual.toString());
96 }
97
98 }