Commits

Joseph Walton committed 05820c6

JOHNSON-7: Throw an exception when getConfig is called before initialisation.

  • Participants
  • Parent commits b757da3

Comments (0)

Files changed (2)

File core/src/main/java/com/atlassian/johnson/Johnson.java

  * functionality is exposed statically. This class provides both the mechanisms for initialising and terminating
  * Johnson, as well as for retrieving its configuration and the container of events.
  * <p/>
- * Where possible to avoid synchronisation issues, it is preferrable to bind Johnson to the lifecycle for the servlet
+ * Where possible to avoid synchronisation issues, it is preferable to bind Johnson to the lifecycle for the servlet
  * container, using {@link com.atlassian.johnson.context.JohnsonContextListener JohnsonContextListener} to initialise
  * and terminate Johnson.
  *
      * under an attribute with this key.
      */
     public static final String EVENT_CONTAINER_ATTRIBUTE = Johnson.class.getName() + ":EventContainer";
-    
+
     private static final Logger LOG = LoggerFactory.getLogger(Johnson.class);
-    
+
     private static JohnsonConfig config;
     private static JohnsonEventContainer eventContainer;
 
      * initialised.
      *
      * @return the working configuration
+     * @throws IllegalStateException if initialize has not been called
      */
     public static JohnsonConfig getConfig()
     {
+        if (config == null)
+        {
+            throw new IllegalStateException("Johnson.getConfig() was called before initialisation");
+        }
         return config;
     }
 
      * Note: This method is <i>not synchronised</i> and <i>not thread-safe</i>. It is left to the <i>calling code</i>
      * to ensure proper synchronisation. The easiest way to do this is to initialise Johnson by adding the
      * {@link com.atlassian.johnson.context.JohnsonContextListener} to {@code web.xml}.
-     * 
+     *
      * @param context the servlet context
      */
     public static void initialize(ServletContext context)
     {
         String location = StringUtils.defaultIfEmpty(context.getInitParameter(CONFIG_LOCATION_PARAM),
                 XmlJohnsonConfig.DEFAULT_CONFIGURATION_FILE);
-        
+
         initialize(location);
 
         context.setAttribute(CONFIG_ATTRIBUTE, config);

File core/src/test/java/com/atlassian/johnson/JohnsonTest.java

+package com.atlassian.johnson;
+
+import org.junit.Test;
+
+public class JohnsonTest
+{
+    @Test(expected = IllegalStateException.class)
+    public void getConfigThrowsAnExceptionWhenUninitialised()
+    {
+        Johnson.terminate();
+        Johnson.getConfig();
+    }
+}