001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.form;
016    
017    import org.apache.tapestry.junit.TapestryTestCase;
018    import org.apache.tapestry.spec.BeanLifecycle;
019    import org.testng.annotations.Test;
020    
021    /**
022     * Suite of tests for {@link org.apache.tapestry.form.ListEditMap}.
023     * 
024     * @author Howard Lewis Ship
025     * @since 3.0
026     */
027    @Test
028    public class TestListEditMap extends TapestryTestCase
029    {
030    
031        private ListEditMap create()
032        {
033            ListEditMap m = new ListEditMap();
034    
035            m.add("request", BeanLifecycle.REQUEST);
036            m.add("page", BeanLifecycle.PAGE);
037            m.add("render", BeanLifecycle.RENDER);
038    
039            return m;
040        }
041    
042        public void testAdd()
043        {
044            ListEditMap m = create();
045    
046            assertEquals("render", m.getKey());
047    
048            checkList("keys", new Object[]
049            { "request", "page", "render" }, m.getKeys());
050            checkList("all values", new Object[]
051            { BeanLifecycle.REQUEST, BeanLifecycle.PAGE, BeanLifecycle.RENDER }, m.getAllValues());
052            checkList("all values", new Object[]
053            { BeanLifecycle.REQUEST, BeanLifecycle.PAGE, BeanLifecycle.RENDER }, m.getValues());
054    
055            assertTrue(m.getDeletedKeys().isEmpty());
056        }
057    
058        public void testGet()
059        {
060            ListEditMap m = create();
061    
062            m.setKey("page");
063    
064            assertEquals("page", m.getKey());
065            assertSame(BeanLifecycle.PAGE, m.getValue());
066        }
067    
068        public void testGetUnknown()
069        {
070            ListEditMap m = create();
071    
072            m.setKey("unknown");
073    
074            assertNull(m.getValue());
075        }
076    
077        public void testMarkDeleted()
078        {
079            ListEditMap m = create();
080    
081            m.setKey("page");
082    
083            assertEquals(false, m.isDeleted());
084    
085            m.setDeleted(true);
086    
087            assertEquals(true, m.isDeleted());
088    
089            checkList("all values", new Object[]
090            { BeanLifecycle.REQUEST, BeanLifecycle.PAGE, BeanLifecycle.RENDER }, m.getAllValues());
091    
092            checkList("undeleted values", new Object[]
093            { BeanLifecycle.REQUEST, BeanLifecycle.RENDER }, m.getValues());
094    
095            checkList("deleted keys", new Object[]
096            { "page" }, m.getDeletedKeys());
097        }
098    
099        public void testMarkAlreadyDeleted()
100        {
101            ListEditMap m = create();
102    
103            m.setKey("page");
104    
105            assertEquals(false, m.isDeleted());
106    
107            m.setDeleted(false);
108    
109            assertEquals(false, m.isDeleted());
110        }
111    
112        public void testMarkMultipleDeleted()
113        {
114            ListEditMap m = create();
115    
116            m.setKey("page");
117            m.setDeleted(true);
118    
119            m.setKey("render");
120            assertEquals(false, m.isDeleted());
121            m.setDeleted(true);
122    
123            assertEquals(true, m.isDeleted());
124    
125            checkList("undeleted values", new Object[]
126            { BeanLifecycle.REQUEST }, m.getValues());
127        }
128    
129        public void testDeleteUndelete()
130        {
131            ListEditMap m = create();
132    
133            m.setKey("page");
134            m.setDeleted(true);
135            m.setDeleted(false);
136    
137            m.setKey("render");
138            m.setDeleted(true);
139    
140            checkList("undeleted values", new Object[]
141            { BeanLifecycle.REQUEST, BeanLifecycle.PAGE }, m.getValues());
142        }
143    
144        /** @since 4.0 */
145    
146        public void testPurgeDeletedKeys()
147        {
148            ListEditMap m = create();
149    
150            m.setKey("render");
151            m.setDeleted(true);
152    
153            checkList("deleted keys before purge", new Object[]
154            { "render" }, m.getDeletedKeys());
155    
156            m.purgeDeletedKeys();
157    
158            checkList("all values after purge", new Object[]
159            { BeanLifecycle.REQUEST, BeanLifecycle.PAGE }, m.getAllValues());
160            checkList("keys after purge", new Object[]
161            { "request", "page" }, m.getKeys());
162    
163            assertTrue(m.getDeletedKeys().isEmpty());
164    
165            m.purgeDeletedKeys();
166    
167            checkList("all values after second purge", new Object[]
168            { BeanLifecycle.REQUEST, BeanLifecycle.PAGE }, m.getAllValues());
169            checkList("keys after second purge", new Object[]
170            { "request", "page" }, m.getKeys());
171        }
172    
173    }