View Javadoc

1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.service.impl;
24  
25  import java.util.Map;
26  import java.util.MissingResourceException;
27  import java.util.ResourceBundle;
28  
29  import org.apache.commons.beanutils.BeanUtils;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.jmock.MockObjectTestCase;
33  
34  import com.gisgraphy.util.ConvertUtil;
35  
36  public abstract class BaseManagerMockTestCase extends MockObjectTestCase {
37      // ~ Static fields/initializers
38      // =============================================
39  
40      protected final Log log = LogFactory.getLog(getClass());
41  
42      protected ResourceBundle rb;
43  
44      // ~ Constructors
45      // ===========================================================
46  
47      public BaseManagerMockTestCase() {
48  	// Since a ResourceBundle is not required for each class, just
49  	// do a simple check to see if one exists
50  	String className = this.getClass().getName();
51  
52  	try {
53  	    rb = ResourceBundle.getBundle(className);
54  	} catch (MissingResourceException mre) {
55  	    // log.warn("No resource bundle found for: " + className);
56  	}
57      }
58  
59      // ~ Methods
60      // ================================================================
61  
62      /**
63       * Utility method to populate a javabean-style object with values from a
64       * Properties file
65       * 
66       * @param obj
67       *                the model object to populate
68       * @return Object populated object
69       * @throws Exception
70       *                 if BeanUtils fails to copy properly
71       */
72      protected Object populate(Object obj) throws Exception {
73  	// loop through all the beans methods and set its properties from
74  	// its .properties file
75  	Map<String, String> map = ConvertUtil.convertBundleToMap(rb);
76  
77  	BeanUtils.copyProperties(obj, map);
78  
79  	return obj;
80      }
81  }