001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.swing;
038:
039: import junit.framework.TestCase;
040:
041: import java.util.Collection;
042: import java.util.Arrays;
043:
044: import javax.swing.JOptionPane;
045:
046: /**
047: * A JUnit test case class that tests {@link ScrollableListSelectionDialog}.
048: *
049: * @author Chris Warrington
050: * @version $Id$
051: * @since 2007-03-06
052: */
053: public class ScrollableListSelectionDialogTest extends TestCase {
054: private static final String TITLE = "DIALOG TITLE";
055: private static final String LEADER = "DIALOG LEADER";
056: private static final java.util.List<String> DATA = Arrays.asList(
057: "hello", "there");
058: private static final String DESC = "DIALOG DESCRIPTION";
059:
060: /**
061: * Tests that all the valid message types are accepted and that they
062: * are the only message types accepted.
063: */
064: public void testValidMessageTypes() {
065: try {
066: new ScrollableListSelectionDialog(
067: null,
068: TITLE,
069: LEADER,
070: DATA,
071: DESC,
072: ScrollableListSelectionDialog.SelectionState.SELECTED,
073: JOptionPane.ERROR_MESSAGE);
074: } catch (IllegalArgumentException e) {
075: fail("JOptionPane.ERROR_MESSAGE is a valid message type");
076: }
077:
078: try {
079: new ScrollableListSelectionDialog(
080: null,
081: TITLE,
082: LEADER,
083: DATA,
084: DESC,
085: ScrollableListSelectionDialog.SelectionState.SELECTED,
086: JOptionPane.INFORMATION_MESSAGE);
087: } catch (IllegalArgumentException e) {
088: fail("JOptionPane.INFORMATION_MESSAGE is a valid message type");
089: }
090:
091: try {
092: new ScrollableListSelectionDialog(
093: null,
094: TITLE,
095: LEADER,
096: DATA,
097: DESC,
098: ScrollableListSelectionDialog.SelectionState.SELECTED,
099: JOptionPane.WARNING_MESSAGE);
100: } catch (IllegalArgumentException e) {
101: fail("JOptionPane.WARNING_MESSAGE is a valid message type");
102: }
103:
104: try {
105: new ScrollableListSelectionDialog(
106: null,
107: TITLE,
108: LEADER,
109: DATA,
110: DESC,
111: ScrollableListSelectionDialog.SelectionState.SELECTED,
112: JOptionPane.QUESTION_MESSAGE);
113: } catch (IllegalArgumentException e) {
114: fail("JOptionPane.QUESTION_MESSAGE is a valid message type");
115: }
116:
117: try {
118: new ScrollableListSelectionDialog(
119: null,
120: TITLE,
121: LEADER,
122: DATA,
123: DESC,
124: ScrollableListSelectionDialog.SelectionState.SELECTED,
125: JOptionPane.PLAIN_MESSAGE);
126: } catch (IllegalArgumentException e) {
127: fail("JOptionPane.PLAIN_MESSAGE is a valid message type");
128: }
129:
130: try {
131: new ScrollableListSelectionDialog(
132: null,
133: TITLE,
134: LEADER,
135: DATA,
136: DESC,
137: ScrollableListSelectionDialog.SelectionState.SELECTED,
138: -128);
139: fail("-128 is not a valid message type");
140: } catch (IllegalArgumentException e) {
141: //we're good
142: }
143:
144: try {
145: new ScrollableListSelectionDialog(
146: null,
147: TITLE,
148: LEADER,
149: DATA,
150: DESC,
151: ScrollableListSelectionDialog.SelectionState.SELECTED,
152: 42);
153: fail("42 is not a valid message type");
154: } catch (IllegalArgumentException e) {
155: //we're good
156: }
157: }
158:
159: public void testNullData() {
160: try {
161: new ScrollableListSelectionDialog(null, TITLE, LEADER,
162: DATA, DESC);
163: } catch (IllegalArgumentException e) {
164: fail("Data was non-null, so it should have been accepted.");
165: }
166:
167: try {
168: new ScrollableListSelectionDialog(null, TITLE, LEADER,
169: null, DESC);
170: fail("Data was null, so it shouldn't have been accepted.");
171: } catch (IllegalArgumentException e) {
172: //we're good
173: }
174: }
175:
176: public void testDefaultSelection() {
177: //test default default selection
178: ScrollableListSelectionDialog dialog = new ScrollableListSelectionDialog(
179: null, TITLE, LEADER, DATA, DESC);
180: java.util.List<String> selectedItems = dialog.selectedItems();
181:
182: assertEquals(DATA.size(), selectedItems.size());
183: for (int i = 0; i < DATA.size(); ++i) {
184: assertEquals(DATA.get(i), selectedItems.get(i));
185: }
186:
187: //test default SELECTED
188: dialog = new ScrollableListSelectionDialog(null, TITLE, LEADER,
189: DATA, DESC,
190: ScrollableListSelectionDialog.SelectionState.SELECTED,
191: JOptionPane.PLAIN_MESSAGE);
192: selectedItems = dialog.selectedItems();
193:
194: assertEquals(DATA.size(), selectedItems.size());
195: for (int i = 0; i < DATA.size(); ++i) {
196: assertEquals(DATA.get(i), selectedItems.get(i));
197: }
198:
199: //test default UNSELECTED
200: dialog = new ScrollableListSelectionDialog(
201: null,
202: TITLE,
203: LEADER,
204: DATA,
205: DESC,
206: ScrollableListSelectionDialog.SelectionState.UNSELECTED,
207: JOptionPane.PLAIN_MESSAGE);
208: selectedItems = dialog.selectedItems();
209: assertEquals(0, selectedItems.size());
210: }
211: }
|