001 /*
002 * Copyright 2002-2006 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.command;
017
018 import junit.framework.TestCase;
019
020 import java.beans.PropertyChangeEvent;
021 import java.beans.PropertyChangeListener;
022
023 /**
024 * @author Mathias Broekelmann
025 *
026 */
027 public abstract class AbstractCommandTests extends TestCase {
028
029 private TestListener testListener;
030
031 protected void setUp() throws Exception {
032 testListener = new TestListener();
033 }
034
035 public void testEnabledState() throws Exception {
036 TestAbstractCommand command = new TestAbstractCommand();
037 command.addEnabledListener(testListener);
038 // test initial value
039 command.secondaryEnabledState = false;
040 assertFalse(command.isEnabled());
041 command.setSecondaryEnabledState(false);
042 assertEquals(0, testListener.eventCount);
043 assertFalse(command.isEnabled());
044
045 // test state change
046 testListener.eventCount = 0;
047 command.setSecondaryEnabledState(true);
048 assertEquals(1, testListener.eventCount);
049 assertTrue(command.isEnabled());
050
051 // test initial value
052 command = new TestAbstractCommand();
053 command.addEnabledListener(testListener);
054 testListener.eventCount = 0;
055 command.secondaryEnabledState = true;
056 assertTrue(command.isEnabled());
057 command.setSecondaryEnabledState(false);
058 assertEquals(1, testListener.eventCount);
059 assertFalse(command.isEnabled());
060 }
061
062 public void testVisibleState() throws Exception {
063 TestAbstractCommand command = new TestAbstractCommand();
064 command.addPropertyChangeListener("visible", testListener);
065 // test initial value
066 command.secondaryVisibleState = false;
067 command.setSecondaryVisibleState(false);
068 assertEquals(0, testListener.eventCount);
069 assertFalse(command.isVisible());
070
071 // test state change
072 testListener.eventCount = 0;
073 command.setSecondaryVisibleState(true);
074 assertEquals(1, testListener.eventCount);
075 assertTrue(command.isVisible());
076
077 // test initial value
078 command = new TestAbstractCommand();
079 command.addPropertyChangeListener("visible", testListener);
080 testListener.eventCount = 0;
081 command.secondaryVisibleState = true;
082 assertTrue(command.isVisible());
083 command.setSecondaryVisibleState(false);
084 assertEquals(1, testListener.eventCount);
085 assertFalse(command.isVisible());
086 }
087
088 protected class TestListener implements PropertyChangeListener {
089
090 int eventCount = 0;
091
092 public void propertyChange(PropertyChangeEvent evt) {
093 eventCount++;
094 }
095
096 }
097
098 protected class TestAbstractCommand extends AbstractCommand {
099
100 boolean secondaryEnabledState;
101
102 boolean secondaryVisibleState;
103
104 public boolean isEnabled() {
105 return super.isEnabled() && secondaryEnabledState;
106 }
107
108 public boolean isVisible() {
109 return super.isVisible() && secondaryVisibleState;
110 }
111
112 public void setSecondaryEnabledState(boolean secondaryEnabledState) {
113 if (secondaryEnabledState != this.secondaryEnabledState) {
114 this.secondaryEnabledState = secondaryEnabledState;
115 updatedEnabledState();
116 }
117 }
118
119 public void setSecondaryVisibleState(boolean secondaryVisibleState) {
120 if (secondaryVisibleState != this.secondaryVisibleState) {
121 this.secondaryVisibleState = secondaryVisibleState;
122 updatedVisibleState();
123 }
124 }
125
126 public void execute() {
127 }
128 }
129 }