1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package tests.support; 18 19import java.io.ByteArrayOutputStream; 20import java.io.File; 21import java.io.FileInputStream; 22import java.io.FileNotFoundException; 23import java.io.IOException; 24import java.io.UnsupportedEncodingException; 25import java.util.Date; 26 27/** 28 * Represents test data used by the Request API tests 29 */ 30public class Support_TestWebData { 31 public final static byte[] test1 = utfBytes(); 32 public final static byte[] test2 = newBinaryFile(8192); 33 34 private static byte[] utfBytes() { 35 try { 36 return "<html>\n<body>\n<h1>Hello World!</h1>\n</body>\n</html>\n".getBytes("UTF-8"); 37 } catch (UnsupportedEncodingException ex) { 38 throw new AssertionError(); 39 } 40 } 41 42 private static byte[] newBinaryFile(int byteCount) { 43 byte[] result = new byte[byteCount]; 44 for (int i = 0; i < result.length; ++i) { 45 result[i] = (byte) i; 46 } 47 return result; 48 } 49 50 // string for test request post body 51 public final static String postContent = "user=111"; 52 53 // Array of all test data 54 public final static byte[][] tests = { 55 test1, 56 test2 57 }; 58 59 /** 60 * List of static test cases for use with test server 61 */ 62 public static Support_TestWebData[] testParams = { 63 new Support_TestWebData(test1.length, 14000000, "test1", "text/html", false, 0), 64 new Support_TestWebData(test2.length, 14000002, "test2", "unknown/unknown", false, 65 new Date().getTime() + 100000) 66 }; 67 68 /** 69 * List of response strings for use by the test server 70 */ 71 public static String[] testServerResponse = { 72 "Redirecting 301", 73 "Redirecting 302", 74 "Redirecting 303", 75 "Redirecting 307" 76 }; 77 78 // Redirection indices into testServerResponse 79 public final static int REDIRECT_301 = 0; 80 public final static int REDIRECT_302 = 1; 81 public final static int REDIRECT_303 = 2; 82 public final static int REDIRECT_307 = 3; 83 84 /** 85 * Creates a data package with information used by the server when responding 86 * to requests 87 */ 88 Support_TestWebData(int length, int lastModified, String name, String type, boolean isDir, long expDate) { 89 testLength = length; 90 testLastModified = lastModified; 91 testName = name; 92 testType = type; 93 testDir = isDir; 94 testExp = expDate; 95 } 96 97 /** 98 * Creates a data package with information used by the server when responding 99 * to requests 100 */ 101 private Support_TestWebData(String path, String type) { 102 File file = new File(path); 103 testLength = file.length(); 104 testLastModified = file.lastModified(); 105 testName = file.getName(); 106 testType = type; 107 testDir = file.isDirectory(); 108 ByteArrayOutputStream out = new ByteArrayOutputStream(); 109 FileInputStream in = null; 110 try { 111 in = new FileInputStream(file); 112 while (in.available() > 0) { 113 out.write(in.read()); 114 } 115 in.close(); 116 out.flush(); 117 test0Data = out.toByteArray(); 118 out.close(); 119 test0DataAvailable = true; 120 return; 121 } catch (Exception e) { 122 // ignore 123 e.printStackTrace(); 124 } finally { 125 try { 126 if (in != null) { 127 in.close(); 128 } 129 if (out != null) { 130 out.close(); 131 } 132 } catch (IOException e) { 133 // ignore 134 } 135 } 136 } 137 138 public static void initDynamicTestWebData(String path, String type) { 139 test0Params = new Support_TestWebData(path, type); 140 } 141 142 // Length of test entity body 143 public long testLength; 144 145 // Last modified date value (milliseconds) 146 public long testLastModified; 147 148 // Test identification name 149 public String testName; 150 151 // The MIME type to assume for this test 152 public String testType; 153 154 // The expiration date 155 public long testExp; 156 157 // Indicates if this is a directory or not 158 public boolean testDir; 159 160 // Indicate if test0 data has bin initialized 161 public static boolean test0DataAvailable = false; 162 163 // test0 data 164 public static byte[] test0Data; 165 166 // test0 parameters 167 public static Support_TestWebData test0Params; 168} 169