%@ page session="false" %>
<%@ taglib uri='/WEB-INF/tld/portlet.tld' prefix='portlet'%>
<%@ page import="javax.portlet.*"%>
<%@ page import="java.util.*"%>
Testing PortletPreferences...
All preference keys known to this entity:
<%
PortletPreferences preferences = renderRequest.getPreferences();
Enumeration names = preferences.getNames();
out.print("");
while (names.hasMoreElements())
{
String name = (String)names.nextElement();
out.print("
");
%>");
String[] values = preferences.getValues(name, new String[]{"no Values"});
if (values == null)
{
out.print("
getValue(), setValue(), try to set value null:
<%
boolean allOk = true;
String attribute = preferences.getValue("TEST_KEY_1","noValue");
if (attribute=="noValue")
{ out.print("#1 passed..."); }
else { out.print("#1 failed..."); allOk = false; }
preferences.setValue("TEST_KEY_1", "testValue");
attribute = preferences.getValue("TEST_KEY_1","noValue");
if (attribute.equals("testValue"))
{ out.print("#2 passed..."); }
else { out.print("#2 failed..."); allOk = false; }
preferences.setValue("TEST_KEY_1", null);
attribute = preferences.getValue("TEST_KEY_1","noValue");
if (attribute==null)
{ out.print("#3 passed..."); }
else { out.print("#3 failed..."); allOk = false; }
if (allOk) { out.print("all passed."); }
else { out.print("failed."); }
%>
getValues(), getValue() returns the first value,
setValues(), try to set values with null:
<%
allOk = true;
String defValues[] = {"no weekday","no weekend"};
String attributes[] = preferences.getValues("TEST_KEY_2",defValues);
if (attributes.equals(defValues))
{ out.print("#1 passed..."); }
else { out.print("#1 failed..."); allOk = false; }
String days[] = {"monday","tuesday","wednesday","thursday","friday","saturday","sunday"};
preferences.setValues("TEST_KEY_2", days);
attribute = preferences.getValue("TEST_KEY_2","noValue");
if (attribute.equals(days[0]))
{ out.print("#2 passed..."); }
else { out.print("#2 failed..."); allOk = false; }
%>
<%
attributes = preferences.getValues("TEST_KEY_2",defValues);
boolean ok = true;
for (int i=0;i
Testing getMap():
<%
allOk = true;
Map map = preferences.getMap();
names = preferences.getNames();
while (names.hasMoreElements())
{
String name = (String)names.nextElement();
String[] values = preferences.getValues(name, new String[]{"no Values"});
if (!map.containsKey(name))
allOk = false;
else if (values == null)
{
if ((String[])map.get(name) != null)
allOk = false;
}
else
{
String[] mapValues = (String[])map.get(name);
if ( mapValues == null)
allOk = false;
else
{
for (int i=0; i
Testing reset():
<%
allOk = true;
preferences.setValue("TEST_KEY_3", "Hello");
if (!preferences.getValue("TEST_KEY_3", "Default").equals("Hello"))
allOk = false;
preferences.reset("TEST_KEY_3");
if (!preferences.getValue("TEST_KEY_3", "Default").equals("Default"))
allOk = false;
if (allOk) { out.print("passed."); }
else { out.print("failed."); }
%>
Testing store() (only allowed with Portlet Actions)...
<%
PortletURL url1 = renderResponse.createActionURL();
url1.setParameter("checkPreferences","storeAndReset");
url1.setParameter("jspNameTransfer","test6.jsp");
%>
<%
PortletURL url = renderResponse.createRenderURL();
url.setParameter("jspName","test7.jsp");
%>