View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, 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   */
    16
    17  package org.yaml.snakeyaml.immutable;
    18
    19  import java.awt.Color;
    20  import java.awt.Insets;
    21  import java.awt.Rectangle;
    22
    23  import javax.swing.BorderFactory;
    24  import javax.swing.border.MatteBorder;
    25
    26  import junit.framework.TestCase;
    27
    28  import org.yaml.snakeyaml.DumperOptions;
    29  import org.yaml.snakeyaml.Yaml;
    30
    31  public class MoreImmutablesTest extends TestCase {
    32
    33      public void testInsets() {
    34          Yaml yaml = new Yaml(new ImmutablesRepresenter());
    35          Insets insets = new Insets(10, 20, 30, 40);
    36          String dump = yaml.dump(insets);
    37          assertEquals("!!java.awt.Insets [10, 20, 30, 40]\n", dump);
    38          Object loaded = yaml.load(dump);
    39          assertEquals(insets, loaded);
    40      }
    41
    42      public void testAwtColor() {
    43          Yaml yaml = new Yaml(new ImmutablesRepresenter());
    44          Color color = new Color(10, 20, 30, 40);
    45          String dump = yaml.dump(color);
    46          assertEquals("!!java.awt.Color [10, 20, 30, 40]\n", dump);
    47          Object loaded = yaml.load(dump);
    48          assertEquals(color, loaded);
    49      }
    50
    51      public void testRectangle() {
    52          Yaml yaml = new Yaml(new ImmutablesRepresenter());
    53          Rectangle rect = new Rectangle(10, 20, 30, 40);
    54          String dump = yaml.dump(rect);
    55          assertEquals("!!java.awt.Rectangle [10, 20, 30, 40]\n", dump);
    56          Object loaded = yaml.load(dump);
    57          assertEquals(rect, loaded);
    58      }
    59
    60      // matteborder - only with color - no icon
    61      public void testMatteBorder() {
    62          DumperOptions options = new DumperOptions();
    63          options.setWidth(400);
    64          Yaml yaml = new Yaml(new ImmutablesRepresenter(), options);
    65          Insets insets = new Insets(10, 20, 30, 40);
    66          Color color = new Color(100, 150, 200);
    67          MatteBorder border = BorderFactory.createMatteBorder(insets.top, insets.left,
    68                  insets.bottom, insets.right, color);
    69          String dump = yaml.dump(border);
    70          assertEquals(
    71                  "!!javax.swing.border.MatteBorder [!!java.awt.Insets [10, 20, 30, 40], !!java.awt.Color [100, 150, 200, 255]]\n",
    72                  dump);
    73          Object loaded = yaml.load(dump);
    74          assertTrue(loaded instanceof MatteBorder);
    75          MatteBorder loadedBorder = (MatteBorder) loaded;
    76          assertEquals(insets, loadedBorder.getBorderInsets());
    77          assertEquals(color, loadedBorder.getMatteColor());
    78      }
    79  }