001 /* 002 * Copyright 2002-2004 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.settings.support; 017 018 import javax.swing.JLabel; 019 import javax.swing.JSplitPane; 020 021 import org.springframework.richclient.settings.TransientSettings; 022 023 import junit.framework.TestCase; 024 025 /** 026 * @author Peter De Bruycker 027 */ 028 public class SplitPaneMementoTests extends TestCase { 029 private JSplitPane splitPane; 030 031 private TransientSettings settings; 032 033 public void testConstructor() { 034 try { 035 new SplitPaneMemento(null); 036 fail("Should throw IllegalArgumentException"); 037 } catch (IllegalArgumentException e) { 038 // test passes 039 } 040 041 try { 042 splitPane.setName(null); 043 new SplitPaneMemento(splitPane, ""); 044 fail("Should throw IllegalArgumentException: splitPane has no name"); 045 } catch (Exception e) { 046 // test passes 047 } 048 049 splitPane.setName("splitPane0"); 050 051 SplitPaneMemento memento = new SplitPaneMemento(splitPane); 052 assertEquals(splitPane, memento.getSplitPane()); 053 assertEquals("splitPane0", memento.getKey()); 054 055 memento = new SplitPaneMemento(splitPane, "key"); 056 assertEquals(splitPane, memento.getSplitPane()); 057 assertEquals("key", memento.getKey()); 058 } 059 060 public void testSaveState() { 061 SplitPaneMemento memento = new SplitPaneMemento(splitPane, "split"); 062 063 splitPane.setDividerLocation(333); 064 065 memento.saveState(settings); 066 067 assertEquals(333, settings.getInt("split.dividerLocation")); 068 } 069 070 public void testRestoreState() { 071 SplitPaneMemento memento = new SplitPaneMemento(splitPane, "split"); 072 073 splitPane.setDividerLocation(333); 074 assertEquals(333, splitPane.getDividerLocation()); 075 076 settings.setInt("split.dividerLocation", 250); 077 078 memento.restoreState(settings); 079 080 assertEquals(250, splitPane.getDividerLocation()); 081 } 082 083 protected void setUp() throws Exception { 084 splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); 085 splitPane.setLeftComponent(new JLabel("Left")); 086 splitPane.setRightComponent(new JLabel("Right")); 087 splitPane.setSize(800, 600); 088 089 settings = new TransientSettings(); 090 } 091 }