FsUtils.java revision 6bf18bae60ae1ff0bf2407e8db115cbbab6f1b84
1/*
2 * Copyright (C) 2009 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 com.android.dumprendertree;
18
19import com.android.dumprendertree.forwarder.ForwardService;
20
21import android.util.Log;
22
23import java.io.BufferedOutputStream;
24import java.io.BufferedReader;
25import java.io.File;
26import java.io.FileOutputStream;
27import java.io.FileReader;
28import java.io.IOException;
29
30public class FsUtils {
31
32    private static final String LOGTAG = "FsUtils";
33    static final String HTTP_TESTS_PREFIX = "/sdcard/android/layout_tests/http/tests/";
34    static final String HTTPS_TESTS_PREFIX = "/sdcard/android/layout_tests/http/tests/ssl/";
35    static final String HTTP_LOCAL_TESTS_PREFIX = "/sdcard/android/layout_tests/http/tests/local/";
36    static final String HTTP_MEDIA_TESTS_PREFIX = "/sdcard/android/layout_tests/http/tests/media/";
37    static final String HTTP_WML_TESTS_PREFIX = "/sdcard/android/layout_tests/http/tests/wml/";
38
39    private FsUtils() {
40        //no creation of instances
41    }
42
43    public static void findLayoutTestsRecursively(BufferedOutputStream bos,
44            String dir) throws IOException {
45        Log.v(LOGTAG, "Searching tests under " + dir);
46
47        File d = new File(dir);
48        if (!d.isDirectory()) {
49            throw new AssertionError("A directory expected, but got " + dir);
50        }
51
52        String[] files = d.list();
53        for (int i = 0; i < files.length; i++) {
54            String s = dir + "/" + files[i];
55            if (FileFilter.ignoreTest(s)) {
56                Log.v(LOGTAG, "  Ignoring: " + s);
57                continue;
58            }
59            if (s.toLowerCase().endsWith(".html")
60                    || s.toLowerCase().endsWith(".xml")) {
61                bos.write(s.getBytes());
62                bos.write('\n');
63                continue;
64            }
65
66            File f = new File(s);
67            if (f.isDirectory()) {
68                findLayoutTestsRecursively(bos, s);
69                continue;
70            }
71
72            Log.v(LOGTAG, "Skipping " + s);
73        }
74    }
75
76    public static void updateTestStatus(String statusFile, String s) {
77        try {
78            BufferedOutputStream bos = new BufferedOutputStream(
79                    new FileOutputStream(statusFile));
80            bos.write(s.getBytes());
81            bos.close();
82        } catch (Exception e) {
83            Log.e(LOGTAG, "Cannot update file " + statusFile);
84        }
85    }
86
87    public static String readTestStatus(String statusFile) {
88        // read out the test name it stopped last time.
89        String status = null;
90        File testStatusFile = new File(statusFile);
91        if(testStatusFile.exists()) {
92            try {
93                BufferedReader inReader = new BufferedReader(
94                        new FileReader(testStatusFile));
95                status = inReader.readLine();
96                inReader.close();
97            } catch (IOException e) {
98                Log.e(LOGTAG, "Error reading test status.", e);
99            }
100        }
101        return status;
102    }
103
104    public static String getTestUrl(String path) {
105        String url = null;
106        if (!path.startsWith(HTTP_TESTS_PREFIX)) {
107            url = "file://" + path;
108        } else {
109            ForwardService.getForwardService().startForwardService();
110            if (path.startsWith(HTTPS_TESTS_PREFIX)) {
111                // still cut the URL after "http/tests/"
112                url = "https://127.0.0.1:8443/" + path.substring(HTTP_TESTS_PREFIX.length());
113            } else if (!path.startsWith(HTTP_LOCAL_TESTS_PREFIX)
114                    && !path.startsWith(HTTP_MEDIA_TESTS_PREFIX)
115                    && !path.startsWith(HTTP_WML_TESTS_PREFIX)) {
116                url = "http://127.0.0.1:8000/" + path.substring(HTTP_TESTS_PREFIX.length());
117            } else {
118                url = "file://" + path;
119            }
120        }
121        return url;
122    }
123
124}
125