1 5 6 package org.exoplatform.services.portletregistery.impl; 7 8 9 import java.util.List ; 10 import java.util.Collection ; 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import org.exoplatform.Constants; 14 import org.exoplatform.container.PortalContainer; 15 import org.exoplatform.services.database.HibernateService; 16 import org.exoplatform.services.portletcontainer.monitor.*; 17 import org.exoplatform.services.portletregistery.*; 18 import org.exoplatform.test.BasicTestCase; 19 20 25 public class TestPortletRegistery extends BasicTestCase { 26 static protected PortletRegisteryService service_ ; 27 28 public TestPortletRegistery(String name) { 29 super(name); 30 } 31 32 public void setUp() throws Exception { 33 if (service_ == null) { 34 PortalContainer manager = PortalContainer.getInstance(); 35 service_ = (PortletRegisteryService) manager. 36 getComponentInstanceOfType(PortletRegisteryService.class) ; 37 } 38 } 39 40 public void tearDown() throws Exception { 41 PortalContainer manager = PortalContainer.getInstance(); 42 HibernateService hservice = 43 (HibernateService) manager.getComponentInstanceOfType(HibernateService.class) ; 44 hservice.closeSession(); 45 } 46 47 public void testPortletService() throws Exception { 48 List portletCategoriesList = service_.getPortletCategories() ; 49 assertTrue(portletCategoriesList.isEmpty()); 50 PortletCategory cat = createPortletCategory("portletCategory", "this is a test") ; 51 assertEquals("check portletCategory name: ", "portletCategory", cat.getPortletCategoryName()) ; 52 assertEquals("check portletCategory desc: ", "this is a test", cat.getDescription()) ; 53 portletCategoriesList = service_.getPortletCategories() ; 54 assertEquals("check number of portlet categories: ", 1 , portletCategoriesList.size()) ; 55 56 Portlet portlet = createPortlet(cat, "portlet", "this is a test") ; 57 assertEquals("check portlet name: ", "portlet", portlet.getPortletName()) ; 58 assertEquals("check portlet desc: ", "this is a test", portlet.getDescription()) ; 59 60 PortletRole portletRole = createPortletRole(portlet, "portletRole", "this is a test") ; 61 assertEquals("check portletRole : ", "portletRole", portletRole.getPortletRoleName()) ; 62 63 portlet = service_.getPortlet(portlet.getId()) ; 64 portletRole = service_.getPortletRole(portletRole.getId()) ; 65 66 portletRole = service_.getPortletRole(portletRole.getId()) ; 67 portlet = service_.getPortlet(portlet.getId()) ; 68 69 try { 70 service_.removePortletRole(portletRole.getId()) ; 71 portletRole = service_.getPortletRole(portletRole.getId()) ; 72 } catch (PortletRegisteryException ex) { 73 assertEquals("check portletRole not found exception ", 74 PortletRegisteryException.PORTLET_ROLE_NOT_FOUND, ex.getErrorCode()) ; 75 } 76 77 try { 78 service_.removePortlet(portlet.getId()) ; 79 portlet = service_.getPortlet(portlet.getId()) ; 80 } catch (PortletRegisteryException ex) { 81 assertEquals("check portlet not found exception ", 82 PortletRegisteryException.PORTLET_NOT_FOUND, ex.getErrorCode()) ; 83 } 84 85 portlet = createPortlet(cat, "portlet", "this is a test") ; 86 String portletId = portlet.getId(); 87 String portletRoleId = ""; 88 for (int i = 0; i < 25; i++) { 89 portletRoleId = createPortletRole(portlet, "portletRole" + i, "this is a test").getId() ; 90 } 91 List portletRoleList = service_.getPortletRoles(portlet.getId()) ; 92 assertEquals("check number of portletRoles in portlet: ", 25 , portletRoleList.size()) ; 93 94 try { 95 service_.removePortletCategory(cat.getId()) ; 96 assertNull(service_.getPortletCategory(cat.getId())) ; 97 } catch (PortletRegisteryException ex) { 98 assertEquals("check portletCategory not found exception ", 99 PortletRegisteryException.PORTLET_CATEGORY_NOT_FOUND, ex.getErrorCode()) ; 100 } 101 102 try { 104 service_.getPortlet(portletId); 105 fail("exception should have been thrown"); 106 } catch (PortletRegisteryException e) { 107 } 108 try { 109 service_.getPortletRole(portletRoleId); 110 fail("exception should have been thrown"); 111 } catch (Exception e) { 112 } 113 } 114 115 public void testClearPortletRoles() throws Exception { 116 PortletCategory cat = createPortletCategory("portletCategory", "this is a test") ; 117 Portlet portlet = createPortlet(cat, "portlet", "this is a test") ; 118 PortletRole portletRole1 = createPortletRole(portlet, "portletRole", "this is a test") ; 119 PortletRole portletRole2 = createPortletRole(portlet, "portletRole2", "this is a test") ; 120 121 assertNotNull(service_.getPortletRole(portletRole1.getId())); 122 123 service_.clearPortletRoles(portlet.getId()); 124 125 try { 126 service_.getPortletRole(portletRole1.getId()); 127 fail("exception should have been thrown"); 128 } catch (Exception e) { 129 } 130 try { 131 service_.getPortletRole(portletRole2.getId()); 132 fail("exception should have been thrown"); 133 } catch (Exception e) { 134 } 135 } 136 137 public void testUpdateRoles() throws Exception { 138 PortletCategory cat = createPortletCategory("portletCategory", "this is a test") ; 139 Portlet portlet = createPortlet(cat, "portlet", "this is a test") ; 140 createPortletRole(portlet, "portletRole", "this is a test") ; 141 createPortletRole(portlet, "portletRole2", "this is a test") ; 142 143 Collection newRoles = new ArrayList (); 144 newRoles.add("newRole1"); 145 newRoles.add("newRole2"); 146 newRoles.add("newRole3"); 147 148 service_.updatePortletRoles(portlet.getId(), newRoles); 149 150 List roles = service_.getPortletRoles(portlet.getId()); 151 assertTrue(roles.size() == 3); 152 for (Iterator iterator = roles.iterator(); iterator.hasNext();) { 153 PortletRole portletRole = (PortletRole) iterator.next(); 154 assertTrue(portletRole.getPortletRoleName().startsWith("newRole")); 155 } 156 } 157 158 public void testImportPortlets() throws Exception { 159 Collection mocks = new ArrayList (); 160 MockPortletRuntimeData mock = new MockPortletRuntimeData("app1", "name1"); 161 mocks.add(mock); 162 mock = new MockPortletRuntimeData("app1", "name2"); 163 mocks.add(mock); 164 mock = new MockPortletRuntimeData("app2", "name21"); 165 mocks.add(mock); 166 167 service_.importPortlets(mocks); 168 169 assertNotNull(service_.findPortletCategoryByName("app1")); 170 171 PortletCategory portletCategory = service_.findPortletCategoryByName("app2"); 172 assertNotNull(portletCategory); 173 List portlets = service_.getPortlets(portletCategory.getId()); 174 for (Iterator iterator = portlets.iterator(); iterator.hasNext();) { 175 Portlet portlet = (Portlet) iterator.next(); 176 assertTrue(portlet.getDisplayName().startsWith("name2")); 177 List roles = service_.getPortletRoles(portlet.getId()); 178 for (Iterator iterator1 = roles.iterator(); iterator1.hasNext();) { 179 PortletRole portletRole = (PortletRole) iterator1.next(); 180 assertEquals(Constants.USER_ROLE, portletRole.getPortletRoleName()); 181 } 182 } 183 } 184 185 public void testClearRepository() throws Exception { 186 PortletCategory cat = createPortletCategory("portletCategory", "this is a test") ; 187 Portlet portlet = createPortlet(cat, "portlet", "this is a test") ; 188 PortletRole portletRole = createPortletRole(portlet, "portletRole", "this is a test") ; 189 190 191 service_.clearRepository(); 192 193 try { 194 service_.getPortletCategory(cat.getId()); 195 fail("exception should have been thrown"); 196 } catch (Exception e) { 197 } 198 try { 199 service_.getPortlet(portlet.getId()); 200 fail("exception should have been thrown"); 201 } catch (Exception e) { 202 } 203 try { 204 service_.getPortletRole(portletRole.getId()); 205 fail("exception should have been thrown"); 206 } catch (Exception e) { 207 } 208 } 209 210 private PortletCategory createPortletCategory(String name, String desc) throws Exception { 211 PortletCategory portletCategory = service_.createPortletCategoryInstance() ; 212 portletCategory.setPortletCategoryName(name) ; 213 portletCategory.setDescription(desc) ; 214 portletCategory = service_.addPortletCategory(portletCategory) ; 215 return portletCategory ; 216 } 217 218 private Portlet createPortlet(PortletCategory cat, String name, String desc) throws Exception { 219 Portlet portlet = service_.createPortletInstance() ; 220 portlet.setPortletName(name) ; 221 portlet.setDescription(desc) ; 222 portlet = service_.addPortlet(cat, portlet) ; 223 return portlet ; 224 } 225 226 private PortletRole createPortletRole(Portlet portlet, String name, String desc) throws Exception { 227 PortletRole portletRole = service_.createPortletRoleInstance() ; 228 portletRole.setPortletRoleName(name) ; 229 portletRole = service_.addPortletRole(portlet, portletRole) ; 230 return portletRole ; 231 } 232 233 protected String getDescription() { 234 return "Test Portlet Registery Service" ; 235 } 236 237 private class MockPortletRuntimeData implements PortletRuntimeData { 238 private String portletAppName; 239 private String portletName; 240 241 public MockPortletRuntimeData(String appName , String name) { 242 this.portletAppName = appName; 243 this.portletName = name; 244 } 245 246 public String getPortletAppName() { 247 return portletAppName; 248 } 249 250 public String getPortletName() { 251 return portletName; 252 } 253 254 public boolean isInitialized() { 255 return false; 256 } 257 258 public long getInitializationTime() { 259 return 0; 260 } 261 262 public long getLastAccessTime() { 263 return 0; 264 } 265 266 public long getLastFailureAccessTime() { 267 return 0; 268 } 269 270 public long getLastInitFailureAccessTime() { 271 return 0; 272 } 273 274 public void setLastInitFailureAccessTime(long lastInitFailureAccessTime) { 275 } 276 277 public long getUnavailabilityPeriod() { 278 return 0; 279 } 280 281 public boolean isDataCached(String key, boolean isCacheGlobal) { 282 return false; 283 } 284 285 public CachedData getCachedData(String key, boolean isCacheGlobal) { 286 return null; 287 } 288 289 public int getCacheExpirationPeriod() { 290 return 0; 291 } 292 293 public PortletRequestMonitorData[] getPortletRequestMonitorData() { 294 return new PortletRequestMonitorData[0]; 295 } 296 297 } 298 } | Popular Tags |