package org.buddycloud.channelserver;

import static org.junit.Assert.fail;

import java.io.FileInputStream;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class MainTest {

    private static final String CONFIGURATION_PROPERTIES = "src/test/resources/configuration.properties";

    Configuration configuration;

    @Before
    public void setUp() throws Exception {
        configuration = Configuration.getInstance();
        configuration.load(new FileInputStream(CONFIGURATION_PROPERTIES));
    }

    @After
    public void tearDown() throws Exception {}

    @Test
    public void throwsExceptionIfNoChannelComponentConfigurationSet() throws Exception {

      configuration.removeKey(Configuration.CONFIGURATION_SERVER_CHANNELS_DOMAIN);
      try {
          Main.main(null);
          fail();
      } catch (IllegalArgumentException e) {
          Assert.assertEquals(Main.MISSING_CHANNEL_COMPONENT_CONFIGURATION, e.getMessage());
      }
    }
    
    @Test
    public void attemptsToReconnectIfInitialConnectionFails() throws Exception {
      configuration.setProperty(Configuration.COMPONENT_STARTUP_DELAY, "1");
      XmppComponent xmpp = Mockito.mock(XmppComponent.class);
      Mockito.when(xmpp.run()).thenReturn(false);
      try {
        Main.setChannelComponent(xmpp);
          Main.main(null);
          fail();
      } catch (Exception e) {
          Assert.assertEquals(Main.UNABLE_TO_CONNECT_CHANNEL_COMPONENT, e.getMessage());
      }
    }
    
    @Test
    public void attemptsToReconnectIfTopicComponentInitialConnectionFails() throws Exception {
      configuration.setProperty(Configuration.COMPONENT_STARTUP_DELAY, "1");
      XmppComponent channelsComponent = Mockito.mock(XmppComponent.class);
      Mockito.when(channelsComponent.run()).thenReturn(true);
      TopicsComponent topicsComponent = Mockito.mock(TopicsComponent.class);
      Mockito.when(topicsComponent.run()).thenReturn(false);
      try {
        Main.setTopicComponent(topicsComponent);
        Main.setChannelComponent(channelsComponent);
        Main.main(null);
        fail();
    } catch (Exception e) {
        Assert.assertEquals(Main.UNABLE_TO_CONNECT_TOPIC_COMPONENT, e.getMessage());
    }
    }
}

@@ -1201,32 +1201,403 @@
 UP_DELAY, %221%22);%0A
+      try %7B%0A          Main.main(null);%0A          fail();%0A      %7D catch (Exception e) %7B%0A          Assert.assertEquals(Main.UNABLE_TO_CONNECT_CHANNEL_COMPONENT, e.getMessage());%0A      %7D%0A    %7D%0A    %0A    @Test%0A    public void attemptsToReconnectIfInitialConnectionFailsWithMock() throws Exception %7B%0A      configuration.setProperty(Configuration.COMPONENT_STARTUP_DELAY, %221%22);%0A
       XmppCompon
@@ -1634,32 +1634,32 @@
 mponent.class);%0A
-
       Mockito.wh
@@ -2242,24 +2242,591 @@
 turn(true);%0A
+      try %7B%0A        Main.setChannelComponent(channelsComponent);%0A        Main.main(null);%0A        fail();%0A      %7D catch (Exception e) %7B%0A        Assert.assertEquals(Main.UNABLE_TO_CONNECT_TOPIC_COMPONENT, e.getMessage());%0A      %7D%0A    %7D%0A    %0A    @Test%0A    public void attemptsToReconnectIfTopicComponentInitialConnectionFailsWithMock() throws Exception %7B%0A      configuration.setProperty(Configuration.COMPONENT_STARTUP_DELAY, %221%22);%0A      XmppComponent channelsComponent = Mockito.mock(XmppComponent.class);%0A      Mockito.when(channelsComponent.run()).thenReturn(true);%0A
       Topics
@@ -3102,24 +3102,26 @@
     fail();%0A
+  
     %7D catch 
@@ -3128,32 +3128,32 @@
 (Exception e) %7B%0A
-
         Assert.a
@@ -3217,24 +3217,26 @@
 Message());%0A
+  
     %7D%0A    %7D%0A

Test with/without mocks