1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2010 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.response;
22  
23  import static org.junit.Assert.assertEquals;
24  
25  import org.junit.Test;
26  
27  /**
28   * Tests for {@link UploadResponse}.
29   * 
30   * @version: $Id: UploadResponseTest.java 4785 2009-12-21 20:10:28Z mosipov $
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  }