1   package net.fckeditor.response;
2   
3   import static org.junit.Assert.assertEquals;
4   
5   import org.junit.Test;
6   
7   /**
8    * Tests for {@link UploadResponse}.
9    * 
10   * @version: $Id: UploadResponseTest.java 2167 2008-07-03 21:33:15Z mosipov $
11   */
12  public class UploadResponseTest {
13  
14  	@Test(expected = IllegalArgumentException.class)
15  	public void noArguments() throws Exception {
16  		new UploadResponse();
17  	}
18  
19  	@Test(expected = IllegalArgumentException.class)
20  	public void tooManyArguments() throws Exception {
21  		new UploadResponse(101, "/some/url/file.txt", "file.txt",
22  				"something's wrong", "arg no. 5");
23  	}
24  
25  	@Test(expected = IllegalArgumentException.class)
26  	public void notANumber() throws Exception {
27  		new UploadResponse("1");
28  	}
29  
30  	@Test
31  	public void onlyErrorNumber() throws Exception {
32  		UploadResponse actual = new UploadResponse(
33  				UploadResponse.SC_INVALID_EXTENSION);
34  		String expected = new String("<script type=\"text/javascript\">\n"
35  				+ "(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"
36  				+ "window.parent.OnUploadCompleted(202);\n</script>");
37  		assertEquals(expected, actual.toString());
38  	}
39  	
40  	@Test
41  	public void fourArguments() throws Exception {
42  		UploadResponse actual = new UploadResponse(UploadResponse.SC_OK,"/fckeditor-java/userfiles/image/fredck.jpg");
43  		String expected = new String("<script type=\"text/javascript\">\n"
44  				+ "(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"
45  				+ "window.parent.OnUploadCompleted(0,'/fckeditor-java/userfiles/image/fredck.jpg');\n</script>");
46  		assertEquals(expected, actual.toString());
47  	}
48  	
49  	@Test
50  	public void renamedFile() throws Exception {
51  		UploadResponse actual = new UploadResponse(UploadResponse.SC_RENAMED,"/fckeditor-java/userfiles/image/hacked_php.txt","hacked_php.txt");
52  		String expected = new String("<script type=\"text/javascript\">\n"
53  				+ "(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"
54  				+ "window.parent.OnUploadCompleted(201,'/fckeditor-java/userfiles/image/hacked_php.txt','hacked_php.txt');\n</script>");
55  		assertEquals(expected, actual.toString());
56  	}
57  
58  	@Test
59  	public void customMessage() throws Exception {
60  		UploadResponse actual = new UploadResponse(UploadResponse.SC_ERROR);
61  		actual.setCustomMessage("some error");
62  		String expected = new String("<script type=\"text/javascript\">\n"
63  				+ "(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"
64  				+ "window.parent.OnUploadCompleted(1,'','','some error');\n</script>");
65  		assertEquals(expected, actual.toString());
66  	}
67  
68  
69  	@Test
70  	public void nullArguments() throws Exception {
71  		UploadResponse actual = new UploadResponse(UploadResponse.SC_ERROR,null,null,null);
72  		String expected = new String("<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(1,'','','');\n</script>");
75  		assertEquals(expected, actual.toString());
76  	}
77  
78  }