Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../../img/srcFileCovDistChart2.png 45% of files have more coverage
33   115   9   8,25
6   64   0,27   4
4     2,25  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  FileRefererFilterDAO       Line # 22 33 0% 9 35 18,6% 0.18604651
 
  (16)
 
1    package net.sourceforge.pebble.dao.file;
2   
3    import net.sourceforge.pebble.dao.PersistenceException;
4    import net.sourceforge.pebble.dao.RefererFilterDAO;
5    import net.sourceforge.pebble.domain.Blog;
6    import net.sourceforge.pebble.domain.RefererFilter;
7    import net.sourceforge.pebble.domain.RefererFilterManager;
8    import org.apache.commons.logging.Log;
9    import org.apache.commons.logging.LogFactory;
10   
11    import java.io.*;
12    import java.util.ArrayList;
13    import java.util.Collection;
14    import java.util.Iterator;
15   
16    /**
17    * A mock implementation of the CategoryDAO interface that does nothing. This
18    * is used when performing unit tests.
19    *
20    * @author Simon Brown
21    */
 
22    public class FileRefererFilterDAO implements RefererFilterDAO {
23   
24    /** the name of the file containing the filter */
25    private static final String FILTERS_FILE = "refererFilters.txt";
26   
27    /** the log used by this class */
28    private static Log log = LogFactory.getLog(RefererFilterManager.class);
29   
30    /**
31    * Loads the referer filters.
32    *
33    * @param rootBlog the owning Blog instance
34    * @return a Collection of RefererFilter instances
35    * @throws PersistenceException if filters cannot be loaded
36    */
 
37  16 toggle public Collection getRefererFilters(Blog rootBlog) throws PersistenceException {
38  16 ArrayList filters = new ArrayList();
39  16 String root = rootBlog.getRoot();
40  16 try {
41   
42  16 File filtersFile = new File(root, FILTERS_FILE);
43  16 if (!filtersFile.exists()) {
44  16 return filters;
45    }
46   
47  0 BufferedReader reader = new BufferedReader(new FileReader(filtersFile));
48  0 String expression = reader.readLine();
49  0 while (expression != null) {
50  0 filters.add(new RefererFilter(expression));
51  0 expression = reader.readLine();
52    }
53   
54  0 reader.close();
55    } catch (IOException ioe) {
56  0 log.error("A " + FILTERS_FILE + " file at " + root + " cannot be loaded", ioe);
57    }
58   
59  0 return filters;
60    }
61   
62    /**
63    * Adds the specified referer filter.
64    *
65    * @param filter the RefererFilter instance to be added
66    * @param rootBlog the owning Blog instance
67    * @throws PersistenceException if something goes wrong storing the filters
68    */
 
69  0 toggle public void addRefererFilter(RefererFilter filter, Blog rootBlog) throws PersistenceException {
70  0 Collection filters = getRefererFilters(rootBlog);
71  0 filters.add(filter);
72  0 store(filters, rootBlog);
73    }
74   
75    /**
76    * Removes the specified referer filter.
77    *
78    * @param filter the RefererFilter instance to be removed
79    * @param rootBlog the owning Blog instance
80    * @throws PersistenceException if something goes wrong removing the filter
81    */
 
82  0 toggle public void deleteRefererFilter(RefererFilter filter, Blog rootBlog) throws PersistenceException {
83  0 Collection filters = getRefererFilters(rootBlog);
84  0 filters.remove(filter);
85  0 store(filters, rootBlog);
86    }
87   
88    /**
89    * Helper method to store all filters for a given blog.
90    *
91    * @param filters the Collection of RefererFilter instances to store
92    * @param rootBlog the blog to which the filters belong
93    */
 
94  0 toggle private void store(Collection filters, Blog rootBlog) throws PersistenceException {
95  0 try {
96  0 String root = rootBlog.getRoot();
97  0 BufferedWriter writer = new BufferedWriter(new FileWriter(new File(root, FILTERS_FILE)));
98   
99  0 Iterator it = filters.iterator();
100  0 RefererFilter filter;
101  0 while (it.hasNext()) {
102  0 filter = (RefererFilter)it.next();
103  0 writer.write(filter.getExpression());
104  0 writer.newLine();
105    }
106   
107  0 writer.flush();
108  0 writer.close();
109    } catch (IOException ioe) {
110  0 log.error(ioe);
111  0 throw new PersistenceException("Filters could not be saved : " + ioe.getMessage());
112    }
113    }
114   
115    }