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 Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing;
021:
022: import javax.accessibility.AccessibleContext;
023: import javax.accessibility.AccessibleRole;
024: import javax.swing.text.BadLocationException;
025: import javax.swing.text.Document;
026: import javax.swing.text.PlainDocument;
027:
028: public class JPasswordFieldTest extends SwingTestCase {
029: JPasswordField pf;
030:
031: boolean bWasException;
032:
033: @Override
034: protected void setUp() throws Exception {
035: super .setUp();
036: pf = new JPasswordField();
037: bWasException = false;
038: }
039:
040: @Override
041: protected void tearDown() throws Exception {
042: super .tearDown();
043: }
044:
045: public void testCopy() {
046: // TODO: uncomment when System clipboard is properly supported
047: // pf.setText("abcdef");
048: // setContentInSystemClipboard("1234");
049: // pf.copy();
050: // assertEquals("1234", getSystemClipboardContent());
051: }
052:
053: public void testCut() {
054: // TODO: uncomment when System clipboard is properly supported
055: // pf.setText("abcdef");
056: // setContentInSystemClipboard("1234");
057: // pf.cut();
058: // assertEquals("1234", getSystemClipboardContent());
059: // assertEquals("abcdef", pf.getText());
060: }
061:
062: public void testJPasswordField() {
063: assertTrue(pf.getDocument() instanceof PlainDocument);
064: assertEquals(0, pf.getColumns());
065: assertEquals("", pf.getText());
066: }
067:
068: public void testJPasswordFieldString() {
069: pf = new JPasswordField(null);
070: assertTrue(pf.getDocument() instanceof PlainDocument);
071: assertEquals(0, pf.getColumns());
072: assertEquals("", pf.getText());
073: pf = new JPasswordField("abc");
074: assertTrue(pf.getDocument() instanceof PlainDocument);
075: assertEquals(0, pf.getColumns());
076: assertEquals("abc", pf.getText());
077: }
078:
079: public void testJPasswordFieldStringint() {
080: pf = new JPasswordField("abc", 5);
081: assertTrue(pf.getDocument() instanceof PlainDocument);
082: assertEquals(5, pf.getColumns());
083: assertEquals("abc", pf.getText());
084: }
085:
086: public void testJPasswordFieldint() {
087: pf = new JPasswordField(5);
088: assertTrue(pf.getDocument() instanceof PlainDocument);
089: assertEquals(5, pf.getColumns());
090: assertEquals("", pf.getText());
091: }
092:
093: public void testJPasswordFieldDocumentStringint() {
094: Document doc = new PlainDocument();
095: pf = new JPasswordField(doc, "abc", 5);
096: assertEquals(doc, pf.getDocument());
097: assertEquals(5, pf.getColumns());
098: assertEquals("abc", pf.getText());
099: }
100:
101: public void testEchoCharIsSet() {
102: assertTrue(pf.echoCharIsSet());
103: pf.setEchoChar('\0');
104: assertFalse(pf.echoCharIsSet());
105: }
106:
107: public void testGetAccessibleContext() {
108: AccessibleContext context = pf.getAccessibleContext();
109: assertTrue(context instanceof JPasswordField.AccessibleJPasswordField);
110: assertEquals(AccessibleRole.PASSWORD_TEXT, context
111: .getAccessibleRole());
112: }
113:
114: public void testSetGetEchoChar() {
115: char echoChar = '*';
116: assertEquals(echoChar, pf.getEchoChar());
117: echoChar = 'j';
118: pf.setEchoChar(echoChar);
119: assertEquals(echoChar, pf.getEchoChar());
120: echoChar = '\0';
121: pf.setEchoChar(echoChar);
122: assertEquals(echoChar, pf.getEchoChar());
123: }
124:
125: public void testGetPassword() {
126: String content = "abcd";
127: pf.setText("abcd");
128: char[] pwd = pf.getPassword();
129: for (int i = 0; i < pwd.length; i++) {
130: assertEquals(content.charAt(i), pwd[i]);
131: }
132: }
133:
134: public void testGetText() {
135: pf.setText("abcd");
136: assertEquals("abcd", pf.getText());
137: }
138:
139: private void checkException() {
140: assertTrue(bWasException);
141: bWasException = false;
142: }
143:
144: public void testGetTextintint() {
145: pf.setText("abcd");
146: try {
147: assertEquals("", pf.getText(1, 0));
148: assertEquals("c", pf.getText(2, 1));
149: } catch (BadLocationException e) {
150: assertTrue("Unexpected exception: ", false);
151: }
152: try {
153: pf.getText(100, 3);
154: } catch (BadLocationException e) {
155: bWasException = true;
156: }
157: checkException();
158: try {
159: pf.getText(1, -63);
160: } catch (BadLocationException e) {
161: bWasException = true;
162: }
163: checkException();
164: }
165:
166: public void testGetUIClassID() {
167: assertEquals("PasswordFieldUI", pf.getUIClassID());
168: }
169: }
|