01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.test;
06:
07: import com.meterware.httpunit.HeadMethodWebRequest;
08: import com.meterware.httpunit.WebConversation;
09: import com.meterware.httpunit.WebRequest;
10: import com.meterware.httpunit.WebResponse;
11:
12: import java.net.URL;
13: import java.text.SimpleDateFormat;
14: import java.util.HashMap;
15: import java.util.Iterator;
16: import java.util.Map;
17: import java.util.Properties;
18:
19: import junit.framework.TestCase;
20:
21: /*
22: * Test reflector links INT-404
23: */
24: public class ReflectorTest extends TestCase {
25: private static final String reflectorUrl = "http://svn.terracotta.org/svn/tc/reflector/reflector.properties";
26: private Properties reflector = new Properties();
27: private Map brokenLinks = new HashMap();
28:
29: protected void setUp() throws Exception {
30: URL url = new URL(reflectorUrl);
31:
32: String expiredDate = "2007-12-15";
33: if (System.currentTimeMillis() > new SimpleDateFormat(
34: "yyyy-MM-dd").parse(expiredDate).getTime()) {
35: reflector.load(url.openStream());
36: } else {
37: System.out.println("Test disabled till: " + expiredDate);
38: }
39:
40: }
41:
42: public void testBrokenLinks() throws Exception {
43: for (Iterator it = reflector.entrySet().iterator(); it
44: .hasNext();) {
45: Map.Entry e = (Map.Entry) it.next();
46: if (e.getValue().toString().indexOf("svn.terracotta.org") > 0)
47: continue;
48: assertNotBroken((String) e.getKey(), (String) e.getValue());
49: }
50:
51: if (brokenLinks.size() > 0) {
52: StringBuffer keys = new StringBuffer();
53: System.err.println("Broken links:");
54: for (Iterator it = brokenLinks.entrySet().iterator(); it
55: .hasNext();) {
56: Map.Entry e = (Map.Entry) it.next();
57: System.err.println(e.getKey() + " = " + e.getValue());
58: keys.append((String) e.getKey()).append("\n");
59: }
60: fail("Broken links found on reflector. Please let Orion or Fiona know.\n"
61: + keys.toString());
62: }
63: }
64:
65: private void assertNotBroken(String desc, String link)
66: throws Exception {
67: WebRequest request = new HeadMethodWebRequest(link);
68: WebConversation wc = new WebConversation();
69: WebResponse response = wc.getResource(request);
70: if (response.getResponseCode() != 200) {
71: brokenLinks.put(desc, link);
72: }
73: }
74: }
|