View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  package org.yaml.snakeyaml.constructor;
17  
18  import java.util.Date;
19  
20  import junit.framework.TestCase;
21  
22  import org.yaml.snakeyaml.Yaml;
23  
24  public class MockDateBeanConstructorTest extends TestCase {
25  
26      public void testConstructor() {
27          String className = "!!org.yaml.snakeyaml.constructor.MockDateBeanConstructorTest$DateBean {number: 24, date: 2009-07-24}";
28          Yaml yaml = new Yaml();
29          try {
30              yaml.load(className);
31              fail("MockDate cannot be constructed.");
32          } catch (Exception e) {
33              assertEquals(
34                      "Cannot create property=date for JavaBean=<DateBean n=24>; Cannot construct: 'class org.yaml.snakeyaml.constructor.MockDateBeanConstructorTest$MockDate'",
35                      e.getCause().getMessage());
36          }
37      }
38  
39      public static class DateBean {
40          private int number;
41          private MockDate date;
42  
43          public int getNumber() {
44              return number;
45          }
46  
47          public void setNumber(int number) {
48              this.number = number;
49          }
50  
51          public MockDate getDate() {
52              return date;
53          }
54  
55          public void setDate(MockDate date) {
56              this.date = date;
57          }
58  
59          @Override
60          public String toString() {
61              return "<DateBean n=" + number + ">";
62          }
63      }
64  
65      public static class MockDate extends Date {
66          private static final long serialVersionUID = 621384692653658062L;
67  
68          public MockDate(long date) {
69              throw new RuntimeException("Test error.");
70          }
71      }
72  }