click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bck...@apache.org
Subject svn commit: r956415 - in /click/trunk/click/framework: src/org/apache/click/util/SessionMap.java test/org/apache/click/pages/SessionMapPage.java test/org/apache/click/util/SessionMapTest.java test/web/session-map.htm
Date Sun, 20 Jun 2010 20:48:48 GMT
Author: bckfnn
Date: Sun Jun 20 20:48:48 2010
New Revision: 956415

URL: http://svn.apache.org/viewvc?rev=956415&view=rev
Log:
added SessionMap.values(), checkstyle fixes and tests.

Added:
    click/trunk/click/framework/test/org/apache/click/pages/SessionMapPage.java   (with props)
    click/trunk/click/framework/test/org/apache/click/util/SessionMapTest.java   (with props)
    click/trunk/click/framework/test/web/session-map.htm
Modified:
    click/trunk/click/framework/src/org/apache/click/util/SessionMap.java

Modified: click/trunk/click/framework/src/org/apache/click/util/SessionMap.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/util/SessionMap.java?rev=956415&r1=956414&r2=956415&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/util/SessionMap.java (original)
+++ click/trunk/click/framework/src/org/apache/click/util/SessionMap.java Sun Jun 20 20:48:48
2010
@@ -18,10 +18,12 @@
  */
 package org.apache.click.util;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -213,7 +215,21 @@ public class SessionMap implements Map<S
      * @see java.util.Map#values()
      */
     public Collection<Object> values() {
-        throw new UnsupportedOperationException();
+        if (session != null) {
+            List<Object> values = new ArrayList<Object>();
+
+            Enumeration<?> enumeration = session.getAttributeNames();
+            while (enumeration.hasMoreElements()) {
+                String name = enumeration.nextElement().toString();
+                Object value = session.getAttribute(name);
+                values.add(value);
+            }
+
+            return values;
+
+        } else {
+            return Collections.emptyList();
+        }
     }
 
     /**
@@ -265,18 +281,21 @@ public class SessionMap implements Map<S
 
         @Override
         public final boolean equals(Object o) {
-            if (!(o instanceof Entry))
+            if (!(o instanceof Entry)) {
                 return false;
-            Entry e = (Entry)o;
+            }
+            Entry e = (Entry) o;
             Object k1 = getKey();
             Object k2 = e.getKey();
             if (k1 == k2 || (k1 != null && k1.equals(k2))) {
                 Object v1 = getValue();
                 Object v2 = e.getValue();
-                if (v1 == v2 || (v1 != null && v1.equals(v2)))
+                if (v1 == v2 || (v1 != null && v1.equals(v2))) {
                     return true;
+                }
             }
             return false;
         }
     }
 }
+

Added: click/trunk/click/framework/test/org/apache/click/pages/SessionMapPage.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/pages/SessionMapPage.java?rev=956415&view=auto
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/pages/SessionMapPage.java (added)
+++ click/trunk/click/framework/test/org/apache/click/pages/SessionMapPage.java Sun Jun 20
20:48:48 2010
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.pages;
+
+import org.apache.click.Page;
+
+/**
+ * Page with a template that renders $session.entrySet()
+ */
+public class SessionMapPage extends Page {
+    private static final long serialVersionUID = 1L;
+
+}

Propchange: click/trunk/click/framework/test/org/apache/click/pages/SessionMapPage.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: click/trunk/click/framework/test/org/apache/click/util/SessionMapTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/util/SessionMapTest.java?rev=956415&view=auto
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/util/SessionMapTest.java (added)
+++ click/trunk/click/framework/test/org/apache/click/util/SessionMapTest.java Sun Jun 20
20:48:48 2010
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.click.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpSession;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.click.MockContainer;
+import org.apache.click.pages.SessionMapPage;
+
+/**
+ * Tests for SessionMap.
+ */
+public class SessionMapTest extends TestCase  {
+    /**
+     * Test SessionMap with a null session.
+     */
+    public void testNoSession() {
+        SessionMap sm = new SessionMap(null);
+       
+        Assert.assertEquals(0, sm.size());
+        Assert.assertEquals(0, sm.keySet().size());
+        Assert.assertEquals(0, sm.entrySet().size());
+        Assert.assertEquals(0, sm.values().size());
+        
+        Assert.assertNull(sm.get("attrib1"));
+        Assert.assertNull(sm.get(null));
+        Assert.assertTrue(sm.isEmpty());
+        
+        Assert.assertFalse(sm.containsKey("attrib1"));
+        Assert.assertFalse(sm.containsKey(null));
+        
+        Assert.assertNull(sm.put("attrib1", "value1"));
+        Assert.assertNull(sm.remove("attrib1"));
+    }
+    
+    /**
+     * Test with a session.
+     */
+    public void testSession() {
+        MockContainer container = new MockContainer("web");
+        container.start();
+        HttpSession session = container.getRequest().getSession();
+        SessionMap sm = new SessionMap(session);
+       
+        Assert.assertEquals(0, sm.size());
+        Assert.assertEquals(0, sm.keySet().size());
+        Assert.assertEquals(0, sm.entrySet().size());
+        Assert.assertEquals(0, sm.values().size());
+        
+        Assert.assertNull(sm.get("attrib1"));
+        Assert.assertNull(sm.get(null));
+        Assert.assertTrue(sm.isEmpty());
+        
+        session.setAttribute("attrib1", "value1");
+        
+        Assert.assertEquals("value1", sm.get("attrib1"));
+        
+        Assert.assertEquals(1, sm.size());
+        Assert.assertEquals(1, sm.keySet().size());
+        Assert.assertTrue(sm.keySet().contains("attrib1"));
+        
+        Assert.assertEquals(1, sm.entrySet().size());
+        Map.Entry<String, Object> entry = sm.entrySet().iterator().next();
+        Assert.assertEquals("attrib1", entry.getKey());
+        Assert.assertEquals("value1", entry.getValue());
+
+        Assert.assertEquals(1, sm.values().size());
+        Assert.assertTrue(sm.values().contains("value1"));
+
+        container.stop();
+    }
+
+    /**
+     * Test changes to the SessionMap.
+     */
+    public void testPuts() {
+        MockContainer container = new MockContainer("web");
+        container.start();
+        HttpSession session = container.getRequest().getSession();
+        SessionMap sm = new SessionMap(session);
+       
+        Assert.assertEquals(0, sm.size());
+        
+        sm.put("attrib1", "value1");
+        
+        Assert.assertEquals("value1", session.getAttribute("attrib1"));
+        
+        sm.put("attrib1", "value2");
+
+        Assert.assertEquals("value2", session.getAttribute("attrib1"));
+
+        Assert.assertEquals("value2", sm.remove("attrib1"));
+        
+        Assert.assertEquals(0, sm.size());
+        
+        sm.putAll(null);
+
+        Assert.assertEquals(0, sm.size());
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("attrib2", "value2");
+        
+        sm.putAll(map);
+        Assert.assertEquals(1, sm.size());
+
+        sm.clear();
+        
+        Assert.assertEquals(0, sm.size());
+        
+        container.stop();
+    }
+    
+    /**
+     * Test iteration over entrySet from velocity.
+     */
+    public void testPage() {
+        MockContainer container = new MockContainer("web");
+        container.start();
+        HttpSession session = container.getRequest().getSession(true);
+        
+        session.setAttribute("attrib1", "value1");
+        
+        container.getRequest().setMethod("GET");
+
+        SessionMapPage page = (SessionMapPage) container.testPage(SessionMapPage.class);
+        Assert.assertTrue(container.getHtml().contains("attrib1=value1"));
+        
+        container.stop();
+    }
+}

Propchange: click/trunk/click/framework/test/org/apache/click/util/SessionMapTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: click/trunk/click/framework/test/web/session-map.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/web/session-map.htm?rev=956415&view=auto
==============================================================================
--- click/trunk/click/framework/test/web/session-map.htm (added)
+++ click/trunk/click/framework/test/web/session-map.htm Sun Jun 20 20:48:48 2010
@@ -0,0 +1,21 @@
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one
+   or more contributor license agreements.  See the NOTICE file
+   distributed with this work for additional information
+   regarding copyright ownership.  The ASF licenses this file
+   to you under the Apache License, Version 2.0 (the
+   "License"); you may not use this file except in compliance
+   with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing,
+   software distributed under the License is distributed on an
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+   KIND, either express or implied.  See the License for the
+   specific language governing permissions and limitations
+   under the License.
+-->
+#foreach( $e in $session.entrySet() )
+$e.key=$e.value
+#end



Mime
View raw message