kinyerakenneth / lastfm-java

Automatically exported from code.google.com/p/lastfm-java
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

Expose ResponseBuilder #29

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
ResponseBuilder is currently package private. That's a bit uncool because it'd 
be a really helpful class to build unit tests for lastfm-java.

Example:
My application processes Last.fm events. In order to verify that this is done 
properly I 
a) save a Last.fm XML in my project
b) create a lastfm-java Event instance
c) process this event (code under test) 
d) verify the results

Of course, there are a number of ways how to build an Event object from XML but 
ResponseBuilder does exactly that. Hence, I'd like to use it in my tests -> 
make it public.

Currently I've to use an ugly reflection work around:
    @Before
    @SuppressWarnings("unchecked")
    public void setUp() throws Exception {
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final DocumentBuilder db = dbf.newDocumentBuilder();
        final Document document = db.parse(new ClassPathResource("lastfm-korn.xml").getInputStream());;
        final Result result = new Result(document);
        // ResponseBuilder is package private -> use reflection to employ it...
        final Class<?> clazz = Class.forName("de.umass.lastfm.ResponseBuilder");
        final Method method = clazz.getDeclaredMethod("buildCollection", Result.class, Class.class);
        method.setAccessible(true);
        event = ((List<Event>) method.invoke(clazz, result, Event.class)).get(0);
    }

Original issue reported on code.google.com by marcel@frightanic.com on 5 Jul 2011 at 6:13

GoogleCodeExporter commented 9 years ago
Having this class public is possibly a good idea for a number of use cases. In 
the meantime you can use this (slightly nicer than reflection) workaround for 
unit testing:
    Cache.setHashCacheEntryNames(false);
    Caller.getInstance().setCache(new FileSystemCache(new File("directory/with/sample/xmls")));
then name your xml files like this:
geo.getevents.distanceMyDistancelocationMyLocationpage1.xml
(syntax is: method name (lower case) then a dot then every parameter and its 
value ordered alphabetically. Note that you may have to include parameters such 
as page or limit even if you do not pass them explicitly to the method).
When calling
Geo.getEvents("MyLocation", "MyDistance", k);
the results will be loaded from your local file.

Original comment by jannikov...@gmail.com on 5 Jul 2011 at 8:51

GoogleCodeExporter commented 9 years ago

Original comment by jannikov...@gmail.com on 21 Dec 2011 at 4:54