1   package test.wiki;
2   
3   import java.io.FileOutputStream;
4   import java.io.IOException;
5   
6   import javax.servlet.http.HttpServletRequest;
7   
8   import junit.extensions.TestSetup;
9   import junit.framework.Test;
10  import junit.framework.TestCase;
11  import junit.framework.TestSuite;
12  
13  import org.seasar.framework.container.S2Container;
14  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
15  import org.seasar.tuigwaa.cms.core.CmsConstants;
16  import org.seasar.tuigwaa.cms.core.CmsContainer;
17  import org.seasar.tuigwaa.cms.core.CmsRequest;
18  import org.seasar.tuigwaa.cms.core.CmsResponse;
19  import org.seasar.tuigwaa.cms.core.Page;
20  import org.seasar.tuigwaa.system.TgwSystemService;
21  import org.seasar.tuigwaa.util.TgwContext;
22  
23  
24  /***
25   * @author someda
26   */
27  public class PDFWikiVisitorTest extends TestCase {
28  
29  	private static TestPageFactory factory_;	
30  	private static TgwSystemService systemService;
31  	private static HttpServletRequest httpRequest;
32  	
33  	private CmsRequest request_;
34  	private CmsResponse response_;
35  	boolean pdf_ = true;
36  	
37  	private String fileName;
38  	
39  	// one-time initializer
40  	public static Test suite() throws Exception{
41  		
42  		TestSuite suite = new TestSuite(PDFWikiVisitorTest.class);
43  		TestSetup setup = new TestSetup(suite){
44  						
45  			public void setUp() throws Exception{				
46  				SingletonS2ContainerFactory.init();		
47  				S2Container container = SingletonS2ContainerFactory.getContainer();
48  				
49  				TgwContext.setAdmin();
50  				systemService = (TgwSystemService) container.getComponent(TgwSystemService.class);				
51  				factory_ = TestPageFactory.getInstance();
52  				httpRequest = container.getRequest();				
53  			}
54  			
55  			public void tearDown() throws Exception{
56  				systemService.destroy();				
57  			}			
58  		};
59  		return setup;
60  	}
61  	
62  	protected void setUp() throws Exception {
63  		
64  		request_ = new CmsRequest(httpRequest);
65  		response_ = new CmsResponse();
66  		request_.setSiteName("test");
67  
68  		if(pdf_){
69  			request_.setType(CmsConstants.OUTPUTTYPE_PDF);
70  		}else{
71  			request_.setType(CmsConstants.OUTPUTTYPE_RTF);
72  		}
73  	}
74  	
75  	public void testInlineElement(){
76  		doTest("inline.txt");
77  	}
78  	
79  	public void testExcerpt(){
80  		doTest("excerpt.txt");		
81  	}
82  
83  	public void testList(){
84  		doTest("list.txt");
85  	}
86  	
87  	public void testDList(){
88  		doTest("dlist.txt");
89  	}
90  	
91  	public void testTable(){
92  		doTest("table.txt");
93  	}
94  	
95  	public void testHeading(){
96  		doTest("heading.txt");
97  	}
98  	
99  	public void testPlugin(){
100 		doTest("plugin.txt");
101 	}
102 	
103 	public void testSentence(){
104 		doTest("sentence.txt");
105 	}	
106 	
107 	public void testChapter(){
108 		doTest("chapter.txt");
109 	}
110 	
111 	
112 	private void doTest(String filename){
113 				
114 		this.fileName = removeExtension(filename);
115 		Page page = factory_.getPage(filename,CmsConstants.CONTENTTYPE_XWIKI);
116 		request_.setPage(page);
117 		CmsContainer.INSTANCE.service(request_,response_);
118 		assertEquals(response_.getTotalError(),0);		
119 	}
120 	
121 	protected void tearDown() throws Exception {
122 		
123 		String outputFile = (pdf_)? fileName + ".pdf" : fileName + ".rtf";		
124 
125 		System.out.println("==================== result ====================");
126 		System.out.println("Syntax Error : " + response_.getNParseError());
127 		System.out.println("Lexical Error : " + response_.getNLexicalError());
128 		System.out.println();
129 		
130 		System.out.println("===== Start creating PDF document.");		
131 		try{
132 			FileOutputStream fos = new FileOutputStream(outputFile);
133 			fos.write(response_.getBytes());
134 			fos.close();
135 		}catch(IOException ioe){
136 			ioe.printStackTrace();
137 		}
138 		System.out.println("===== Creating PDF document completed.");
139 	}		
140 	
141 	private static String removeExtension(String filename){
142 		String ret = filename;
143 		int idx = 0;
144 		if((idx = filename.lastIndexOf(".")) != -1){
145 			ret = filename.substring(0,idx);
146 		}
147 		return ret;		
148 	}
149 	
150 }