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.representer;
    18
    19  import junit.framework.TestCase;
    20
    21  import org.yaml.snakeyaml.Yaml;
    22
    23  public class RepresentFieldTest extends TestCase {
    24
    25      public void testRepresent1() {
    26          Yaml yaml = new Yaml();
    27          WrongJavaBean bean = new WrongJavaBean();
    28          bean.packageField = "Value";// the field is present
    29          bean.publicField = "Michael Jackson";
    30          WrongJavaBean.staticField = "Another value";
    31          String output = yaml.dump(bean);
    32          assertEquals(
    33                  "!!org.yaml.snakeyaml.representer.WrongJavaBean {publicField: Michael Jackson}\n",
    34                  output);
    35      }
    36
    37      public void testWrongNotPublicField() {
    38          Yaml yaml = new Yaml();
    39          WrongJavaBean bean = new WrongJavaBean();
    40          bean.packageField = "Value";// the field is present
    41          try {
    42              yaml.load("!!org.yaml.snakeyaml.representer.WrongJavaBean {packageField: Gnome}\n");
    43              fail("Only public fields can be used.");
    44          } catch (Exception e) {
    45              assertEquals(
    46                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.representer.WrongJavaBean; exception=Cannot create property=packageField for JavaBean=WrongJavaBean; Unable to find property 'packageField' on class: org.yaml.snakeyaml.representer.WrongJavaBean",
    47                      e.getMessage());
    48              assertEquals(
    49                      "Cannot create property=packageField for JavaBean=WrongJavaBean; Unable to find property 'packageField' on class: org.yaml.snakeyaml.representer.WrongJavaBean",
    50                      e.getCause().getMessage());
    51          }
    52      }
    53
    54      public void testStaticField() {
    55          Yaml yaml = new Yaml();
    56          WrongJavaBean.staticField = "Value";// the field is present
    57          try {
    58              yaml.load("!!org.yaml.snakeyaml.representer.WrongJavaBean {staticField: Gnome}\n");
    59              fail("Static fields cannot be used.");
    60          } catch (Exception e) {
    61              assertEquals(
    62                      "null; Can't construct a java object for tag:yaml.org,2002:org.yaml.snakeyaml.representer.WrongJavaBean; exception=Cannot create property=staticField for JavaBean=WrongJavaBean; Unable to find property 'staticField' on class: org.yaml.snakeyaml.representer.WrongJavaBean",
    63                      e.getMessage());
    64              assertEquals(
    65                      "Cannot create property=staticField for JavaBean=WrongJavaBean; Unable to find property 'staticField' on class: org.yaml.snakeyaml.representer.WrongJavaBean",
    66                      e.getCause().getMessage());
    67          }
    68      }
    69  }