001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.List;
025:
026: @SuppressWarnings("unchecked")
027: public class SpinnerListModelTest extends BasicSwingTestCase {
028: private SpinnerListModel model;
029:
030: private ChangeController chl;
031:
032: @Override
033: public void setUp() {
034: model = new SpinnerListModel();
035: chl = new ChangeController();
036: model.addChangeListener(chl);
037: }
038:
039: @Override
040: public void tearDown() {
041: model = null;
042: chl = null;
043: }
044:
045: public void testSpinnerListModel() {
046: List list = model.getList();
047: assertNotNull(list);
048: assertEquals(1, list.size());
049: list = new ArrayList();
050: list.add("listline1");
051: list.add("listline2");
052: list.add("listline3");
053: model = new SpinnerListModel(list);
054: assertSame(list, model.getList());
055: testExceptionalCase(new IllegalArgumentCase() {
056: @Override
057: public void exceptionalAction() throws Exception {
058: model = new SpinnerListModel((Object[]) null);
059: }
060: });
061: testExceptionalCase(new IllegalArgumentCase() {
062: @Override
063: public void exceptionalAction() throws Exception {
064: model = new SpinnerListModel(new ArrayList());
065: }
066: });
067: list.clear();
068: list.add(null);
069: list.add(null);
070: list.add(null);
071: model = new SpinnerListModel(list);
072: assertNull(model.getList().get(2));
073: assertNull(model.getValue());
074: assertNull(model.getPreviousValue());
075: assertNull(model.getNextValue());
076: Object[] values = { "arrline1", "arrline2", new Integer(3) };
077: model = new SpinnerListModel(values);
078: assertEquals(model.getList(), Arrays.asList(values));
079: testExceptionalCase(new IllegalArgumentCase() {
080: @Override
081: public void exceptionalAction() throws Exception {
082: model = new SpinnerListModel(new Object[0]);
083: }
084: });
085: }
086:
087: public void testModelList() {
088: List list = model.getList();
089: assertNotNull(list);
090: assertEquals(1, list.size());
091: list = new ArrayList();
092: list.add(null);
093: list.add(null);
094: list.add(null);
095: model = new SpinnerListModel(list);
096: assertNull(model.getList().get(2));
097: assertNull(model.getValue());
098: assertNull(model.getPreviousValue());
099: assertNull(model.getNextValue());
100: }
101:
102: public void testSetGetList() {
103: List list = new ArrayList();
104: list.add("listline1");
105: list.add("listline2");
106: list.add("listline3");
107: model.setList(list);
108: assertSame(list, model.getList());
109: assertTrue(chl.isChanged());
110: chl.reset();
111: list.clear();
112: list.add(new Integer(1));
113: assertEquals(new Integer(1), model.getValue());
114: assertFalse(chl.isChanged());
115: chl.reset();
116: testExceptionalCase(new IllegalArgumentCase() {
117: @Override
118: public void exceptionalAction() throws Exception {
119: model.setList(null);
120: }
121: });
122: testExceptionalCase(new IllegalArgumentCase() {
123: @Override
124: public void exceptionalAction() throws Exception {
125: model.setList(new ArrayList());
126: }
127: });
128: assertSame(list, model.getList());
129: assertFalse(chl.isChanged());
130: }
131:
132: public void testSetGetValue() {
133: List list = new ArrayList();
134: list.add(new Integer(1));
135: list.add(new Integer(2));
136: list.add(new Integer(3));
137: model.setList(list);
138: assertEquals(new Integer(1), model.getValue());
139: chl.reset();
140: Object obj = new Integer(3);
141: model.setValue(obj);
142: assertEquals(obj, model.getValue());
143: assertTrue(chl.isChanged());
144: testExceptionalCase(new IllegalArgumentCase() {
145: @Override
146: public void exceptionalAction() throws Exception {
147: Object obj = new Integer(13);
148: model.setValue(obj);
149: }
150: });
151: }
152:
153: public void testNextPreviousValue() {
154: List list = new ArrayList();
155: list.add(new Integer(1));
156: list.add(new Integer(2));
157: list.add(new Integer(3));
158: model.setList(list);
159: assertEquals(new Integer(1), model.getValue());
160: assertNull(model.getPreviousValue());
161: assertEquals(new Integer(2), model.getNextValue());
162: assertEquals(new Integer(1), model.getValue());
163: model.setValue(model.getNextValue());
164: assertEquals(new Integer(3), model.getNextValue());
165: model.setValue(model.getNextValue());
166: assertEquals(new Integer(3), model.getValue());
167: assertNull(model.getNextValue());
168: }
169: }
|