1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 package org.openoffice.test.common;
22 
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.net.HttpURLConnection;
29 import java.net.URL;
30 import java.util.logging.Level;
31 
32 import org.junit.runner.Description;
33 import org.junit.runner.Result;
34 import org.junit.runner.notification.RunListener;
35 
36 public class ReportUploader extends RunListener {
37 
38 	private Logger log = Logger.getLogger(this);
39 
40 	private File outputDir = Testspace.getFile("output");
41 
42 	private String suiteName = null;
43 
44 	private File reportZip = null;
45 
46 	private String url = null;
47 
ReportUploader()48 	public ReportUploader() {
49 		url = System.getProperty("report.to");
50 		System.setProperty("report.to", ""); //
51 	}
52 
53 	@Override
testRunStarted(Description description)54 	public void testRunStarted(Description description) throws Exception {
55 		suiteName = description.getDisplayName();
56 	}
57 
58 	@Override
testRunFinished(Result result)59 	public void testRunFinished(Result result) throws Exception {
60 		if (!FileUtil.isUrl(url))
61 			return;
62 
63 		// upload
64 		File output = new File(outputDir.getAbsolutePath() + "." + suiteName);
65 		if (!output.exists())
66 			return;
67 		try {
68 			reportZip = File.createTempFile("aoo_test_report", ".zip");
69 			FileUtil.zip(output, reportZip);
70 			upload();
71 		} finally {
72 			if (reportZip != null)
73 				reportZip.delete();
74 		}
75 	}
76 
77 
78 
79 	private static final String boundary = "-----AOOAOOAOO1d226f500d0";
80 	private static final String prefix = "--";
81 	private static final String newLine = "\r\n";
82 
upload()83 	private void upload() {
84 		InputStream fis = null;
85 		OutputStream out = null;
86 		try {
87 			HttpURLConnection connection = (HttpURLConnection)  new URL(url).openConnection();
88 			connection.setDoInput(true);
89 			connection.setDoOutput(true);
90 			connection.setConnectTimeout(20 * 1000);
91 			connection.setRequestMethod("POST");
92 			connection.setRequestProperty("Content-type", "multipart/form-data;boundary=" + boundary);
93 
94 			out = connection.getOutputStream();
95 			fis = new FileInputStream(reportZip);
96 
97 			StringBuffer params = new StringBuffer();
98 			params.append(prefix + boundary + newLine);
99 			params.append("Content-Disposition: form-data; name=\"name\"");
100 			params.append(newLine + newLine);
101 			params.append("aoo");
102 			params.append(newLine);
103 
104 			params.append(prefix + boundary + newLine);
105 			params.append("Content-Disposition: form-data; name=\"file\"; filename=\"report.zip\"");
106 			params.append(newLine);
107 			params.append("Content-Type: application/zip");
108 			params.append(newLine + newLine);
109 
110 			out.write(params.toString().getBytes());
111 
112 			byte[] buffer = new byte[1024];
113 			int len = 0;
114 			while ((len = fis.read(buffer)) != -1) {
115 				out.write(buffer, 0, len);
116 			}
117 
118 			params.setLength(0);
119 			params.append(newLine);
120 			params.append(prefix + boundary + prefix + newLine);
121 
122 			out.write(params.toString().getBytes());
123 			out.flush();
124 			out.close();
125 
126 			String response = FileUtil.readStreamAsString(connection.getInputStream(), "UTF-8");
127 			log.log(Level.INFO, "Upload report: \n" + response);
128 		} catch (Exception e) {
129 			log.log(Level.SEVERE, "Fail to upload report!", e);
130 		} finally {
131 			if (fis != null)
132 				try {
133 					fis.close();
134 				} catch (IOException e) {
135 				}
136 			if (out != null)
137 				try {
138 					out.close();
139 				} catch (IOException e) {
140 				}
141 		}
142 	}
143 
144 
145 
146 }
147