1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.settings.support;
17  
18  import javax.swing.JLabel;
19  import javax.swing.JSplitPane;
20  
21  import org.springframework.richclient.settings.TransientSettings;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * @author Peter De Bruycker
27   */
28  public class SplitPaneMementoTests extends TestCase {
29  	private JSplitPane splitPane;
30  
31  	private TransientSettings settings;
32  
33  	public void testConstructor() {
34  		try {
35  			new SplitPaneMemento(null);
36  			fail("Should throw IllegalArgumentException");
37  		} catch (IllegalArgumentException e) {
38  			// test passes
39  		}
40  
41  		try {
42  			splitPane.setName(null);
43  			new SplitPaneMemento(splitPane, "");
44  			fail("Should throw IllegalArgumentException: splitPane has no name");
45  		} catch (Exception e) {
46  			// test passes
47  		}
48  
49  		splitPane.setName("splitPane0");
50  
51  		SplitPaneMemento memento = new SplitPaneMemento(splitPane);
52  		assertEquals(splitPane, memento.getSplitPane());
53  		assertEquals("splitPane0", memento.getKey());
54  
55  		memento = new SplitPaneMemento(splitPane, "key");
56  		assertEquals(splitPane, memento.getSplitPane());
57  		assertEquals("key", memento.getKey());
58  	}
59  
60  	public void testSaveState() {
61  		SplitPaneMemento memento = new SplitPaneMemento(splitPane, "split");
62  
63  		splitPane.setDividerLocation(333);
64  
65  		memento.saveState(settings);
66  
67  		assertEquals(333, settings.getInt("split.dividerLocation"));
68  	}
69  
70  	public void testRestoreState() {
71  		SplitPaneMemento memento = new SplitPaneMemento(splitPane, "split");
72  
73  		splitPane.setDividerLocation(333);
74  		assertEquals(333, splitPane.getDividerLocation());
75  
76  		settings.setInt("split.dividerLocation", 250);
77  
78  		memento.restoreState(settings);
79  
80  		assertEquals(250, splitPane.getDividerLocation());
81  	}
82  
83  	protected void setUp() throws Exception {
84  		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
85  		splitPane.setLeftComponent(new JLabel("Left"));
86  		splitPane.setRightComponent(new JLabel("Right"));
87  		splitPane.setSize(800, 600);
88  
89  		settings = new TransientSettings();
90  	}
91  }