Package com.pugh.sockso.web.action

Source Code of com.pugh.sockso.web.action.AmazonCoverSearchTest

package com.pugh.sockso.web.action;

import com.pugh.sockso.tests.SocksoTestCase;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;

import com.pugh.sockso.tests.MyHttpURLConnection;

public class AmazonCoverSearchTest extends SocksoTestCase {
   
    public void testGetCoverFromSearchResults() throws IOException {

        final String origCover = "http://ecx.images-amazon.com/images/I/41YZSP49YBL._SL160_AA115_.jpg";
        final String data = "da khsdk jahdjk hkjas dkj" +
                            "           junk <img src=\"" +origCover+ "\" more crap\n" +
                            " blah blah";
        final MyHttpURLConnection cnn = new MyHttpURLConnection( data );
        final AmazonCoverSearch s = new AmazonCoverSearch( null );
        final String fetchedCover = s.getCoverFromSearchResults( cnn );

        assertEquals( origCover, fetchedCover );

    }

    public void testGetCoverFrom2011SearchResults() throws IOException {
       
        String[] paths = {
            "http://ecx.images-amazon.com/images/I/51G8JLb-dkL._AA160_.jpg",
            "http://ecx.images-amazon.com/images/I/61M2Itz5zmL._AA115_.jpg",
            "http://ecx.images-amazon.com/images/I/51CSdWeDz4L._AA160_.jpg"
        };

        for ( String origCover : paths ) {
               
            final String data = "da khsdk jahdjk hkjas dkj" +
                                "           junk <img src=\"" +origCover+ "\" more crap\n" +
                                " blah blah";
            final MyHttpURLConnection cnn = new MyHttpURLConnection( data );
            final AmazonCoverSearch s = new AmazonCoverSearch( null );
            final String fetchedCover = s.getCoverFromSearchResults( cnn );

            assertEquals( origCover, fetchedCover );
       
        }

    }

    public void testGetCoverFromSearchResultsNoResults() throws IOException {

        final AmazonCoverSearch s = new AmazonCoverSearch( null );
        final String data = "";
        final MyHttpURLConnection cnn = new MyHttpURLConnection( data );
        final String cover = s.getCoverFromSearchResults( cnn );

        assertNull( cover );

    }

    public void testGettingCoverFromAmazonResults() throws IOException {

        final AmazonCoverSearch s = new AmazonCoverSearch( null );
        final BufferedReader in = new BufferedReader( new FileReader("test/data/amazon-result.html") );
        final String expected = "http://ecx.images-amazon.com/images/I/51whwodX57L._SL160_AA115_.jpg";

        String line = "";

        while ( (line = in.readLine()) != null ) {
            final String actual = s.getCoverFromData( line );
            if ( actual != null ) {
                assertEquals( expected, actual );
                return;
            }
        }

        fail( "No cover found" );

    }

}
TOP

Related Classes of com.pugh.sockso.web.action.AmazonCoverSearchTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.