1/** 2 * Copyright (c) 2008, http://www.snakeyaml.org 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package org.yaml.snakeyaml.immutable; 17 18import java.awt.Color; 19import java.awt.Insets; 20import java.awt.Rectangle; 21 22import javax.swing.BorderFactory; 23import javax.swing.border.MatteBorder; 24 25import junit.framework.TestCase; 26 27import org.yaml.snakeyaml.DumperOptions; 28import org.yaml.snakeyaml.Yaml; 29 30public class MoreImmutablesTest extends TestCase { 31 32 public void testInsets() { 33 Yaml yaml = new Yaml(new ImmutablesRepresenter()); 34 Insets insets = new Insets(10, 20, 30, 40); 35 String dump = yaml.dump(insets); 36 assertEquals("!!java.awt.Insets [10, 20, 30, 40]\n", dump); 37 Object loaded = yaml.load(dump); 38 assertEquals(insets, loaded); 39 } 40 41 public void testAwtColor() { 42 Yaml yaml = new Yaml(new ImmutablesRepresenter()); 43 Color color = new Color(10, 20, 30, 40); 44 String dump = yaml.dump(color); 45 assertEquals("!!java.awt.Color [10, 20, 30, 40]\n", dump); 46 Object loaded = yaml.load(dump); 47 assertEquals(color, loaded); 48 } 49 50 public void testRectangle() { 51 Yaml yaml = new Yaml(new ImmutablesRepresenter()); 52 Rectangle rect = new Rectangle(10, 20, 30, 40); 53 String dump = yaml.dump(rect); 54 assertEquals("!!java.awt.Rectangle [10, 20, 30, 40]\n", dump); 55 Object loaded = yaml.load(dump); 56 assertEquals(rect, loaded); 57 } 58 59 // matteborder - only with color - no icon 60 public void testMatteBorder() { 61 DumperOptions options = new DumperOptions(); 62 options.setWidth(400); 63 Yaml yaml = new Yaml(new ImmutablesRepresenter(), options); 64 Insets insets = new Insets(10, 20, 30, 40); 65 Color color = new Color(100, 150, 200); 66 MatteBorder border = BorderFactory.createMatteBorder(insets.top, insets.left, 67 insets.bottom, insets.right, color); 68 String dump = yaml.dump(border); 69 assertEquals( 70 "!!javax.swing.border.MatteBorder [!!java.awt.Insets [10, 20, 30, 40], !!java.awt.Color [100, 150, 200, 255]]\n", 71 dump); 72 Object loaded = yaml.load(dump); 73 assertTrue(loaded instanceof MatteBorder); 74 MatteBorder loadedBorder = (MatteBorder) loaded; 75 assertEquals(insets, loadedBorder.getBorderInsets()); 76 assertEquals(color, loadedBorder.getMatteColor()); 77 } 78} 79