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