1 5 package org.exoplatform.services.portletcontainer.imp; 6 7 8 import javax.portlet.PreferencesValidator; 9 import javax.portlet.ReadOnlyException; 10 import javax.portlet.ValidatorException; 11 import org.exoplatform.container.PortalContainer; 12 import org.exoplatform.services.portletcontainer.helper.PortletWindowInternal; 13 import org.exoplatform.services.portletcontainer.impl.PortletApplicationProxy; 14 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.PortletPreferencesImp; 15 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.persistenceImp.DefaultPersistenceManager; 16 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.persistenceImp.PersistenceManager; 17 import org.exoplatform.services.portletcontainer.pci.ExoWindowID; 18 import org.exoplatform.services.portletcontainer.pci.Input; 19 import org.exoplatform.services.portletcontainer.pci.model.ExoPortletPreferences; 20 import org.exoplatform.services.portletcontainer.pci.model.Portlet; 21 import java.io.IOException ; 22 import java.util.Enumeration ; 23 import java.util.Map ; 24 31 public class TestPortletPreferences extends BaseTest { 32 33 javax.portlet.PortletPreferences pP; 34 35 PortletApplicationProxy proxy; 36 private ExoWindowID windowID; 37 38 39 public TestPortletPreferences(String s) { 40 super(s); 41 } 42 43 public void setUp() throws Exception { 44 super.setUp(); 45 proxy = (PortletApplicationProxy) PortalContainer.getInstance(). 46 getComponentInstance("hello"); 47 ExoPortletPreferences prefs = ((Portlet) portletApp_.getPortlet().get(0)).getPortletPreferences(); 48 PreferencesValidator validator = proxy.getValidator(prefs.getPreferencesValidator()); 49 50 windowID = new ExoWindowID("exotest:/hello/HelloWorld/banner"); 51 pP = new PortletPreferencesImp(validator, prefs, windowID, persister); 52 } 53 54 public void testMap() { 55 Map map = pP.getMap(); 56 assertTrue("Map non empty is false", !map.isEmpty()); 57 } 58 59 public void testNames() { 60 Enumeration e = pP.getNames(); 61 while (e.hasMoreElements()) { 62 e.nextElement(); 63 } 64 } 65 66 public void testGetValue() { 67 assertEquals("DEFAULT", pP.getValue("not_exist", "DEFAULT")); 68 assertEquals("http://timeserver.myco.com", pP.getValue("time-server", "ops")); 69 assertEquals("HH", pP.getValue("time-format", "ops")); 70 } 71 72 public void testGetValues() { 73 assertEquals("DEFAULT", pP.getValues("not_exist", new String []{"DEFAULT"})[0]); 74 String [] arr = pP.getValues("time-format", new String []{}); 75 assertTrue("HH".equals(arr[0])); 76 } 77 78 public void testSetValue() throws ReadOnlyException { 79 pP.setValue("time-server", "MyValue"); 81 assertEquals("MyValue", pP.getValue("time-server", "ops")); 82 83 pP.setValue("time-format", "MyFormat"); 85 assertTrue(pP.getValues("time-format", new String []{}).length == 1); 86 87 try { 89 pP.setValue("port", "NotPossible"); 90 } catch (ReadOnlyException e) { 91 assertEquals("the value port can not be changed", e.getMessage()); 92 } 93 94 pP.setValue("NewValue", "MyNewValue"); 96 assertEquals("MyNewValue", pP.getValue("NewValue", "ops")); 97 98 pP.setValue("NewValue", "MyNewValue2"); 100 assertEquals("MyNewValue2", pP.getValue("NewValue", "ops")); 101 } 102 103 public void testSetValues() throws ReadOnlyException { 104 pP.setValues("time-server", new String [] {"val1", "val2"}); 106 assertTrue(pP.getValues("time-server", new String []{}).length == 2); 107 108 pP.setValues("time-format", new String [] {"val1", "val2"}); 110 assertTrue(pP.getValues("time-format", new String []{}).length == 2); 111 112 try { 114 pP.setValues("port", new String []{"NotPossible"}); 115 } catch (ReadOnlyException e) { 116 assertEquals("the value port can not be changed", e.getMessage()); 117 } 118 119 pP.setValues("NewValue", new String []{"MyNewValue1","MyNewValue2"}); 121 assertEquals("MyNewValue1", pP.getValues("NewValue", new String []{"ops"})[0]); 122 123 pP.setValues("NewValue", new String []{"MyNewValue1bis","MyNewValue2bis"}); 125 assertEquals("MyNewValue2bis", pP.getValues("NewValue", new String []{"ops"})[1]); 126 } 127 128 public void testIsReadOnly(){ 129 assertTrue(!pP.isReadOnly("not_exist")); 130 assertTrue(pP.isReadOnly("port")); 131 assertTrue(!pP.isReadOnly("time-server")); 132 assertTrue(!pP.isReadOnly("time-format")); 133 } 134 135 public void testReset() throws ReadOnlyException { 136 pP.setValue("time-server", "MyValueToBeReseted"); 138 assertEquals("MyValueToBeReseted", pP.getValue("time-server", "ops")); 139 pP.reset("time-server"); 140 assertEquals("http://timeserver.myco.com", pP.getValue("time-server", "ops")); 141 142 pP.setValue("NewValue", "MyValueToBeDeleted"); 145 assertEquals("MyValueToBeDeleted", pP.getValue("NewValue", "ops")); 146 pP.reset("NewValue"); 147 assertEquals("ops", pP.getValue("NewValue", "ops")); 148 149 pP.setValue("NewValue", null); 151 assertNull(pP.getValue("NewValue", "ops")); 152 pP.reset("NewValue"); 153 assertEquals("ops", pP.getValue("NewValue", "ops")); 154 155 pP.setValue("time-server", null); 157 assertNull(pP.getValue("time-server", "ops")); 158 pP.reset("time-server"); 159 assertEquals("http://timeserver.myco.com", pP.getValue("time-server", "ops")); 160 } 161 162 public void testStore() throws ReadOnlyException, IOException , ValidatorException { 163 pP.setValue("param-1", "value-1") ; 164 ((PortletPreferencesImp)pP).setMethodCalledIsAction(true); 165 pP.store(); 166 167 PersistenceManager manager = 168 (PersistenceManager) PortalContainer.getInstance(). 169 getComponentInstance(PersistenceManager.class); 170 Input input = new Input(); 171 input.setWindowID(windowID); 172 PortletWindowInternal pwi = manager.getWindow(input, null); 173 assertEquals("value-1", pwi.getPreferences().getValue("param-1", "opts")); 174 } 175 176 public void testStateChangeFlag(){ 177 ((PortletPreferencesImp)pP).setMethodCalledIsAction(true); 178 ((PortletPreferencesImp)pP).setStateChangeAuthorized(false); 179 try { 180 pP.store(); 181 fail("Sate change should NOT be authorized"); 182 } catch (Throwable t) { 183 } 184 } 185 } | Popular Tags |