1 2 package org.roller; 3 4 import java.sql.Timestamp ; 5 import java.util.ArrayList ; 6 import java.util.Calendar ; 7 import java.util.Date ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.LinkedList ; 11 import java.util.List ; 12 import java.util.Map ; 13 14 import junit.framework.TestCase; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.roller.business.BookmarkManagerTest; 19 import org.roller.model.Roller; 20 import org.roller.model.RollerFactory; 21 import org.roller.model.UserManager; 22 import org.roller.model.WeblogManager; 23 import org.roller.pojos.CommentData; 24 import org.roller.pojos.UserData; 25 import org.roller.pojos.WeblogCategoryData; 26 import org.roller.pojos.WeblogEntryData; 27 import org.roller.pojos.WebsiteData; 28 29 34 public abstract class RollerTestBase extends TestCase 35 { 36 public static Log mLogger = 37 LogFactory.getFactory().getInstance(BookmarkManagerTest.class); 38 39 private Roller mRoller = null; 40 41 42 protected WebsiteData mWebsite = null; 43 protected String testUsername = "testuser"; 44 45 46 protected List mWebsites = new LinkedList (); 47 48 49 protected int mBlogCount = 1; 50 51 52 protected int mCatCount = 2; 53 54 protected int mCatDepth = 2; 55 56 protected int mEntriesPerCatCount = 6; 58 protected int mCommentCount = 2; 59 60 protected int mExpectedEntryCount = mEntriesPerCatCount + 61 mEntriesPerCatCount*mCatCount + 62 mEntriesPerCatCount*(mCatCount*mCatDepth); 63 protected int mExpectedPublishedEntryCount = (int)(mEntriesPerCatCount*0.5) + 64 (int)(mEntriesPerCatCount*0.5)*mCatCount + 65 (int)(mEntriesPerCatCount*0.5)*(mCatCount*mCatDepth); 66 67 68 protected List mUsersCreated = new ArrayList (); 69 70 protected List mCategoriesCreated = new ArrayList (); 71 72 protected List mEntriesCreated = new ArrayList (); 73 74 protected List mCommentsCreated = new ArrayList (); 75 76 77 protected Calendar mCalendar = null; 78 79 public RollerTestBase() 81 { 82 super(); 83 } 84 85 public RollerTestBase(String name) 87 { 88 super(name); 89 } 90 91 95 protected void setUp() throws Exception 96 { 97 super.setUp(); 98 UserManager umgr = getRoller().getUserManager(); 99 100 getRoller().begin(UserData.SYSTEM_USER); 101 102 UserData user = createUser(umgr, 104 testUsername, 105 "password", 106 "TestUser", 107 "testuser@example.com"); 108 109 mWebsite = umgr.getWebsite(user.getUserName()); 111 getRoller().commit(); 112 } 113 114 protected UserData createUser( 116 UserManager umgr, 117 String username, 118 String password, 119 String fullName, 120 String email) throws RollerException 121 { 122 UserData ud = new UserData(null, 123 username, password, fullName, email, new java.util.Date () ); 129 Map pages = new HashMap (); 130 pages.put("Weblog","Weblog page content"); 131 pages.put("_day","Day page content"); 132 pages.put("css","CSS page content"); 133 umgr.addUser(ud, pages, "basic", "en_US_WIN", "America/Los_Angeles"); 134 return ud; 135 } 136 137 139 public void setUpTestWeblogs() throws Exception 140 { 141 UserManager umgr = getRoller().getUserManager(); 142 WeblogManager wmgr = getRoller().getWeblogManager(); 143 144 for (int i=0; i<mBlogCount; i++) 146 { 147 getRoller().begin(UserData.SYSTEM_USER); 148 149 UserData ud = createUser(umgr, 150 "testuser"+i, "password", "Test User #"+i, "test"+i+"@test.com" ); 155 WebsiteData website = umgr.getWebsite(ud.getUserName()); 156 mWebsites.add(website); 157 mUsersCreated.add(ud); 158 159 getRoller().commit(); 160 161 mLogger.debug("Created user "+ud.getUserName()); 162 163 mCalendar = Calendar.getInstance(); 165 mCalendar.setTime(new Date ()); 166 167 getRoller().begin(UserData.SYSTEM_USER); 169 website = umgr.retrieveWebsite(website.getId()); 170 WeblogCategoryData rootCat = wmgr.getRootWeblogCategory(website); 171 createCategoryPostsAndComments(0, wmgr, website, rootCat); 172 getRoller().commit(); 173 } 174 175 } 177 178 private void createCategoryPostsAndComments( 179 int depth, 180 WeblogManager wmgr, 181 WebsiteData website, 182 WeblogCategoryData rootCat) throws RollerException 183 { 184 Calendar commentCalendar = Calendar.getInstance(); 185 186 Timestamp day; 187 WeblogEntryData wd; 188 if (depth == 0) 189 { 190 mCalendar.roll(Calendar.DATE, true); 192 day = new Timestamp (mCalendar.getTime().getTime()); 193 day.setNanos(0); wd = new WeblogEntryData( 195 null, rootCat, website, "Future Blog", null, 200 "Blog to the Future", null, day, day, Boolean.TRUE ); wd.save(); 206 207 mCalendar.roll(Calendar.DATE, false); 209 } 210 211 for ( int k=0; k<mEntriesPerCatCount; k++ ) 213 { 214 day = new Timestamp (mCalendar.getTime().getTime()); 215 day.setNanos(0); 217 boolean published = k%2==0 ? true : false; 218 219 wd = new WeblogEntryData( 220 null, rootCat, website, rootCat.getName() + ":entry"+k, null, 225 rootCat.getName() + ":entry"+k, null, day, day, new Boolean (published) ); wd.save(); 231 232 mEntriesCreated.add(0, wd); 234 235 Timestamp now = wd.getPubTime(); 236 for ( int l=0; l<mCommentCount; l++ ) 237 { 238 commentCalendar.setTime(now); 241 commentCalendar.add(Calendar.HOUR, l); 242 now = new Timestamp (commentCalendar.getTime().getTime()); 243 CommentData comment = new CommentData(null, 244 wd, "name"+l, "test"+l+"@test.com", "url"+l, "This is my comment", now, Boolean.FALSE, Boolean.FALSE); comment.save(); 253 mCommentsCreated.add(comment); 254 mLogger.debug(" Created comment [" 255 +comment.getId()+"]"+ comment.getName()); 256 } 257 258 mCalendar.add(Calendar.DATE, -1); 259 } 260 261 for ( int j=0; j<mCatCount; j++ ) 263 { 264 WeblogCategoryData cat = wmgr.createWeblogCategory( 265 website, rootCat, rootCat.getName()+"-cat"+j, "desc", null ); cat.save(); 271 mCategoriesCreated.add(cat); 272 mLogger.debug(" Created cat ["+cat.getId()+"]"+cat.getName()); 273 274 if (depth < mCatDepth) 275 { 276 createCategoryPostsAndComments(depth+1, wmgr, website, cat); 277 } 278 } 279 } 280 281 283 public void tearDownTestWeblogs() throws Exception 284 { 285 getRoller().begin(UserData.SYSTEM_USER); 286 UserManager umgr = getRoller().getUserManager(); 287 for (Iterator iter = mUsersCreated.iterator(); iter.hasNext();) 288 { 289 UserData element = (UserData) iter.next(); 290 element = umgr.retrieveUser(element.getId()); 291 element.remove(); 292 } 293 getRoller().commit(); 294 } 295 296 298 305 public void tearDown() throws Exception 306 { 307 try 308 { 309 deleteWebsite(testUsername); 310 } 312 catch (RollerException e) 313 { 314 mLogger.error("Tearing down",e); 315 } 316 super.tearDown(); 317 } 318 319 324 private void deleteWebsite(String deleteMe) throws RollerException 325 { 326 mLogger.debug("try to delete " + deleteMe); 327 getRoller().begin(UserData.SYSTEM_USER); 328 mWebsite = getRoller().getUserManager().getWebsite(deleteMe); 329 if (mWebsite != null) 330 { 331 mWebsite.getUser().remove(); 332 } 333 getRoller().commit(); 334 } 335 336 340 public Roller getRoller() throws RollerException 341 { 342 if ( mRoller == null ) 343 { 344 mRoller = RollerFactory.getRoller(); 345 } 346 return mRoller; 347 } 348 } 349 | Popular Tags |