View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration.event;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  
22  import org.apache.commons.configuration.AbstractFileConfiguration;
23  import org.apache.commons.configuration.ConfigurationException;
24  import org.apache.commons.configuration.FileConfiguration;
25  import org.apache.commons.configuration.reloading.ReloadingStrategy;
26  import org.junit.Test;
27  
28  /**
29   * A base test class that can be used for testing file-based configurations.
30   * This class tests reload events, too.
31   *
32   * @version $Id: AbstractTestFileConfigurationEvents.java 1225648 2011-12-29 20:55:07Z oheger $
33   */
34  public abstract class AbstractTestFileConfigurationEvents extends
35          AbstractTestConfigurationEvents
36  {
37      /**
38       * Initializes the file configuration for the tests.
39       *
40       * @throws ConfigurationException if an error occurs
41       */
42      protected void setUpFileConfiguration() throws ConfigurationException,
43              IOException
44      {
45          FileConfiguration fc = (FileConfiguration) config;
46          fc.setReloadingStrategy(new AlwaysReloadingStrategy());
47          fc.setURL(getSourceURL());
48  
49          // deregister event listener before load because load will cause
50          // other events being generated
51          config.removeConfigurationListener(l);
52          fc.load();
53          config.addConfigurationListener(l);
54      }
55  
56      /**
57       * Returns the URL of the file to be loaded. Must be implemented in concrete
58       * test classes.
59       *
60       * @return the URL of the file-based configuration
61       * @throws IOException if an error occurs
62       */
63      protected abstract URL getSourceURL() throws IOException;
64  
65      /**
66       * Tests events generated by the reload() method.
67       */
68      @Test
69      public void testReloadEvent() throws ConfigurationException, IOException
70      {
71          setUpFileConfiguration();
72          config.isEmpty(); // This should cause a reload
73          l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
74                  getSourceURL(), true);
75          l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
76                  getSourceURL(), false);
77          l.done();
78      }
79  
80      /**
81       * Tests events generated by the reload() method when detail events are
82       * enabled.
83       */
84      @Test
85      public void testReloadEventWithDetails() throws ConfigurationException,
86              IOException
87      {
88          setUpFileConfiguration();
89          config.setDetailEvents(true);
90          config.isEmpty(); // This should cause a reload
91          l.checkEventCount(2);
92          l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
93                  getSourceURL(), true);
94          l.skipToLast(AbstractFileConfiguration.EVENT_RELOAD);
95          l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
96                  getSourceURL(), false);
97          l.done();
98      }
99  
100     /**
101      * Tests accessing a property during a reload event to ensure that no
102      * infinite loops are possible.
103      */
104     @Test
105     public void testAccessPropertiesOnReload() throws ConfigurationException,
106             IOException
107     {
108         setUpFileConfiguration();
109         config.addConfigurationListener(new ConfigurationListener()
110         {
111             public void configurationChanged(ConfigurationEvent event)
112             {
113                 config.getString("test");
114             }
115         });
116         config.isEmpty();
117         l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
118                 getSourceURL(), true);
119         l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
120                 getSourceURL(), false);
121         l.done();
122     }
123 
124     /**
125      * A dummy implementation of the ReloadingStrategy interface. This
126      * implementation will always indicate that a reload should be performed. So
127      * it can be used for testing reloading events.
128      */
129     static class AlwaysReloadingStrategy implements ReloadingStrategy
130     {
131         public void setConfiguration(FileConfiguration configuration)
132         {
133         }
134 
135         public void init()
136         {
137         }
138 
139         public boolean reloadingRequired()
140         {
141             return true;
142         }
143 
144         public void reloadingPerformed()
145         {
146         }
147     }
148 }