View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program is distributed in the hope that it will be useful,
    13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server.startup.healthcheck;
    21
    22  import static org.hamcrest.Matchers.containsString;
    23  import static org.junit.Assert.assertFalse;
    24  import static org.junit.Assert.assertNotNull;
    25  import static org.junit.Assert.assertThat;
    26  import static org.junit.Assert.assertTrue;
    27
    28  import java.util.Properties;
    29
    30  import org.junit.Test;
    31  import org.neo4j.server.logging.InMemoryAppender;
    32
    33
    34  public class StartupHealthCheckTest {
    35
    36      @Test
    37      public void shouldPassWithNoRules() {
    38          StartupHealthCheck check = new StartupHealthCheck();
    39          assertTrue(check.run());
    40      }
    41
    42      @Test
    43      public void shouldRunAllHealthChecksToCompletionIfNonFail() {
    44          StartupHealthCheck check = new StartupHealthCheck(getPassingRules());
    45          assertTrue(check.run());
    46      }
    47
    48      @Test
    49      public void shouldFailIfOneOrMoreHealthChecksFail() {
    50          StartupHealthCheck check = new StartupHealthCheck(getWithOneFailingRule());
    51          assertFalse(check.run());
    52      }
    53
    54      @Test
    55      public void shouldLogFailedRule() {
    56          StartupHealthCheck check = new StartupHealthCheck(getWithOneFailingRule());
    57          InMemoryAppender appender = new InMemoryAppender(StartupHealthCheck.log);
    58          check.run();
    59
    60          assertThat(appender.toString(), containsString("ERROR - blah blah"));
    61      }
    62
    63      @Test
    64      public void shouldAdvertiseFailedRule() {
    65          StartupHealthCheck check = new StartupHealthCheck(getWithOneFailingRule());
    66          check.run();
    67          assertNotNull(check.failedRule());
    68      }
    69
    70      private StartupHealthCheckRule[] getWithOneFailingRule() {
    71          StartupHealthCheckRule[] rules = new StartupHealthCheckRule[5];
    72
    73          for(int i = 0; i < rules.length; i++) {
    74              rules[i] = new StartupHealthCheckRule() {
    75                  public boolean execute(Properties properties) {
    76                      return true;
    77                  }
    78
    79                  public String getFailureMessage() {
    80                      return "blah blah";
    81                  }};
    82          }
    83
    84          rules[rules.length / 2] = new StartupHealthCheckRule() {
    85              public boolean execute(Properties properties) {
    86                  return false;
    87              }
    88
    89              public String getFailureMessage() {
    90                  return "blah blah";
    91              }};
    92
    93          return rules;
    94      }
    95
    96      private StartupHealthCheckRule[] getPassingRules() {
    97          StartupHealthCheckRule[] rules = new StartupHealthCheckRule[5];
    98
    99          for(int i = 0; i < rules.length; i++) {
    100             rules[i] = new StartupHealthCheckRule() {
    101                 public boolean execute(Properties properties) {
    102                     return true;
    103                 }
    104
    105                 public String getFailureMessage() {
    106                     return "blah blah";
    107                 }};
    108         }
    109
    110         return rules;
    111     }
    112 }