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  
27  /***
28   * A base test class that can be used for testing file-based configurations.
29   * This class tests reload events, too.
30   *
31   * @version $Id: AbstractTestFileConfigurationEvents.java 439648 2006-09-02 20:42:10Z oheger $
32   */
33  public abstract class AbstractTestFileConfigurationEvents extends
34          AbstractTestConfigurationEvents
35  {
36      /***
37       * Initializes the file configuration for the tests.
38       *
39       * @throws ConfigurationException if an error occurs
40       */
41      protected void setUpFileConfiguration() throws ConfigurationException,
42              IOException
43      {
44          FileConfiguration fc = (FileConfiguration) config;
45          fc.setReloadingStrategy(new AlwaysReloadingStrategy());
46          fc.setURL(getSourceURL());
47  
48          // deregister event listener before load because load will cause
49          // other events being generated
50          config.removeConfigurationListener(l);
51          fc.load();
52          config.addConfigurationListener(l);
53      }
54  
55      /***
56       * Returns the URL of the file to be loaded. Must be implemented in concrete
57       * test classes.
58       *
59       * @return the URL of the file-based configuration
60       * @throws IOException if an error occurs
61       */
62      protected abstract URL getSourceURL() throws IOException;
63  
64      /***
65       * Tests events generated by the reload() method.
66       */
67      public void testReloadEvent() throws ConfigurationException, IOException
68      {
69          setUpFileConfiguration();
70          config.isEmpty(); // This should cause a reload
71          l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
72                  getSourceURL(), true);
73          l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
74                  getSourceURL(), false);
75          l.done();
76      }
77  
78      /***
79       * Tests events generated by the reload() method when detail events are
80       * enabled.
81       */
82      public void testReloadEventWithDetails() throws ConfigurationException,
83              IOException
84      {
85          setUpFileConfiguration();
86          config.setDetailEvents(true);
87          config.isEmpty(); // This should cause a reload
88          l.checkEventCount(2);
89          l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
90                  getSourceURL(), true);
91          l.skipToLast(AbstractFileConfiguration.EVENT_RELOAD);
92          l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
93                  getSourceURL(), false);
94          l.done();
95      }
96  
97      /***
98       * Tests accessing a property during a reload event to ensure that no
99       * infinite loops are possible.
100      */
101     public void testAccessPropertiesOnReload() throws ConfigurationException,
102             IOException
103     {
104         setUpFileConfiguration();
105         config.addConfigurationListener(new ConfigurationListener()
106         {
107             public void configurationChanged(ConfigurationEvent event)
108             {
109                 config.getString("test");
110             }
111         });
112         config.isEmpty();
113         l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
114                 getSourceURL(), true);
115         l.checkEvent(AbstractFileConfiguration.EVENT_RELOAD, null,
116                 getSourceURL(), false);
117         l.done();
118     }
119 
120     /***
121      * A dummy implementation of the ReloadingStrategy interface. This
122      * implementation will always indicate that a reload should be performed. So
123      * it can be used for testing reloading events.
124      */
125     static class AlwaysReloadingStrategy implements ReloadingStrategy
126     {
127         public void setConfiguration(FileConfiguration configuration)
128         {
129         }
130 
131         public void init()
132         {
133         }
134 
135         public boolean reloadingRequired()
136         {
137             return true;
138         }
139 
140         public void reloadingPerformed()
141         {
142         }
143     }
144 }