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