1 package net.javacoding.jspider.functional.specific.general; 2 3 import junit.framework.TestCase; 4 import net.javacoding.jspider.JSpider; 5 import net.javacoding.jspider.Constants; 6 import net.javacoding.jspider.functional.TestingConstants; 7 import net.javacoding.jspider.api.event.EventSink; 8 import net.javacoding.jspider.api.event.JSpiderEvent; 9 import net.javacoding.jspider.api.event.resource.ResourceFetchedEvent; 10 import net.javacoding.jspider.core.util.config.*; 11 import net.javacoding.jspider.mockobjects.OverridingJSpiderConfiguration; 12 import net.javacoding.jspider.mockobjects.OverridingPropertySet; 13 import net.javacoding.jspider.mockobjects.plugin.JUnitEventSink; 14 15 import java.io.*; 16 import java.net.URL ; 17 18 21 public class UserAgentTest extends TestCase implements EventSink { 22 23 public final String TEST_URL="http://" + TestingConstants.HOST + "/testcases/specific/general/testUserAgent.php"; 24 25 protected JUnitEventSink sink; 26 protected JSpiderConfiguration config; 27 protected JSpiderConfiguration config2; 28 protected JSpiderConfiguration config3; 29 30 protected String userAgent; 31 32 35 public UserAgentTest ( ) { 36 super ( "UserAgentTest "); 37 } 38 39 43 protected void setUp() throws Exception { 44 System.err.println("setUp"); 45 userAgent = null; 46 config = ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST); 47 config2 = new OverridingJSpiderConfiguration ( config ); 48 ((OverridingPropertySet)config2.getJSpiderConfiguration()).setValue("jspider.userAgent", "JSpiderUnitTestGlobalLevel"); 49 config3 = new OverridingJSpiderConfiguration ( config ); 50 ((OverridingPropertySet)config3.getBaseSiteConfiguration()).setValue("site.userAgent", "JSpiderUnitTestSiteLevel"); 51 ConfigurationFactory.setConfiguration(config); 52 sink = JUnitEventSink.getInstance(); 53 sink.setOtherSink(this); 54 } 55 56 60 protected void tearDown() throws Exception { 61 System.err.println("tearDown"); 62 ConfigurationFactory.cleanConfiguration(); 63 sink.reset(); 64 } 65 66 public void testUserAgentNormalAgent ( ) throws Exception { 67 URL url = new URL ( TEST_URL ); 68 ConfigurationFactory.setConfiguration(config); 69 70 JSpider jspider = new JSpider ( url ); 71 jspider.start ( ); 72 String actual = userAgent; 73 String expected= ConfigurationFactory.getConfiguration().getJSpiderConfiguration().getString(ConfigConstants.CONFIG_USERAGENT, Constants.USERAGENT ); 74 System.err.println("actual: " + actual ); 75 System.err.println("expected: " + expected); 76 assertEquals(expected, actual); 77 } 78 79 public void testUserAgentGlobalLevel ( ) throws Exception { 80 URL url = new URL ( TEST_URL ); 81 ConfigurationFactory.setConfiguration(config2); 82 83 JSpider jspider = new JSpider ( url ); 84 jspider.start ( ); 85 86 String actual = userAgent; 87 String expected= ConfigurationFactory.getConfiguration().getJSpiderConfiguration().getString(ConfigConstants.CONFIG_USERAGENT, Constants.USERAGENT ); 88 System.err.println("actual: " + actual ); 89 System.err.println("expected: " + expected); 90 assertEquals(expected, actual); 91 } 92 93 public void testUserAgentSiteLevel ( ) throws Exception { 94 URL url = new URL ( TEST_URL ); 95 ConfigurationFactory.setConfiguration(config3); 96 97 JSpider jspider = new JSpider ( url ); 98 jspider.start ( ); 99 100 String actual = userAgent; 101 String expected= ConfigurationFactory.getConfiguration().getBaseSiteConfiguration().getString(ConfigConstants.SITE_USERAGENT, Constants.USERAGENT ); 102 System.err.println("actual: " + actual ); 103 System.err.println("expected: " + expected); 104 assertEquals(expected, actual); 105 } 106 107 public void notify(JSpiderEvent event) { 108 if ( event instanceof ResourceFetchedEvent ) { 109 ResourceFetchedEvent e = (ResourceFetchedEvent) event; 110 System.out.println(">>>> RESOURCE FETCHED : " + e.getResource().getURL() ); 111 if ( e.getResource().getURL().toString().equalsIgnoreCase(TEST_URL) ) { 112 InputStream is = e.getResource().getInputStream(); 113 BufferedReader br = new BufferedReader ( new InputStreamReader ( is ) ); 114 System.out.println("FILE:"); 115 try { 116 String line = br.readLine(); 117 while ( line != null ) { 118 System.out.println("LINE:" + line); 119 userAgent = line; 120 line = br.readLine(); 121 } 122 } catch (IOException e1) { 123 e1.printStackTrace(); 124 } 125 } 126 } 127 128 } 129 130 public void initialize() { 131 } 132 133 public void shutdown() { 134 } 135 } 136 | Popular Tags |