1   /*
2    * Copyright 2009 minuteFForts
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package com.minutefforts.aspects.property.support;
17  
18  import static com.minutefforts.test.util.assertion.GoldenMasterFile.goldenMasterFile;
19  import static com.minutefforts.test.util.logging.UnitTestAppender.getDefaultTestAppender;
20  import static org.junit.Assert.assertEquals;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import com.minutefforts.test.util.logging.UnitTestAppender;
26  
27  /**
28   * Unit tests for the bound property support.
29   * 
30   * @author Hans-Joachim Belz
31   */
32  public class BoundPropertyBeanTest {
33  
34    private SampleBoundPropertyBean sampleBoundPropertyBean;
35  
36  
37    /**
38     * Unit Test: Set some bound properties.
39     */
40    @Test
41    public void boundProperties() {
42  
43      PropertyChangeListenerMock generalChangeListener = new PropertyChangeListenerMock();
44      ((BoundPropertyObservable) this.sampleBoundPropertyBean).addPropertyChangeListener(generalChangeListener);
45  
46      PropertyChangeListenerMock descriptionChangeListener = new PropertyChangeListenerMock();
47      ((BoundPropertyObservable) this.sampleBoundPropertyBean).addPropertyChangeListener("Description",
48          descriptionChangeListener);
49  
50      this.sampleBoundPropertyBean.setId(99);
51      this.sampleBoundPropertyBean.setId(100);
52  
53      this.sampleBoundPropertyBean.setAmount(10f);
54      assertEquals(this.sampleBoundPropertyBean.getAmount(), 10f);
55  
56      this.sampleBoundPropertyBean.setDescription("Some arbitrary text.");
57      this.sampleBoundPropertyBean.setDescription("And another text.");
58      
59      this.sampleBoundPropertyBean.setInternalValue("Setting this property must be ignored!");
60      
61      this.sampleBoundPropertyBean.setValid(true);
62      this.sampleBoundPropertyBean.setIsSomeFlag(true);
63  
64      // assert change log of mock listeners
65      assertEquals("Id: -1 => 99\n" //
66          + "Id: 99 => 100\n" //
67          + "Amount: null => 10.0\n" //
68          + "Description:  => Some arbitrary text.\n" //
69          + "Description: Some arbitrary text. => And another text.\n" //
70          + "Valid: false => true\n"
71          + "IsSomeFlag: false => true\n", //
72          generalChangeListener.resultLog);
73      assertEquals("Description:  => Some arbitrary text.\n" //
74          + "Description: Some arbitrary text. => And another text.\n", //
75          descriptionChangeListener.resultLog);
76  
77      // assert standard logging behaviour of the aspect
78      assertEquals( //
79          goldenMasterFile(this, "GoldenMaster-logBoundProperties.txt"), //
80          UnitTestAppender.getDefaultTestAppender().getLogAsString());
81  
82      // uncomment to have a peek at the logging behaviour
83      System.out.println(UnitTestAppender.getDefaultTestAppender().getLogAsString());
84    }
85  
86  
87    /**
88     * Set up for the unit tests.
89     */
90    @Before
91    public void setUp() {
92  
93      // clear all unit test log appenders before every test execution
94      getDefaultTestAppender().clear();
95  
96      this.sampleBoundPropertyBean = new SampleBoundPropertyBean();
97    }
98  }