geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kl...@apache.org
Subject incubator-geode git commit: [GEODE-38] Add gfsh init script support for .gfsh2rc files.
Date Thu, 18 Jun 2015 19:36:20 GMT
Repository: incubator-geode
Updated Branches:
  refs/heads/develop 508af1f87 -> 08182b183


[GEODE-38] Add gfsh init script support for .gfsh2rc files.

Fix up log4j2 configuration property value for Windows. Use
log4j2XML.toURI().toString() to avoid StatusLogger error about
MalformedURLException: unknown protocol: c on Windows.


Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/08182b18
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/08182b18
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/08182b18

Branch: refs/heads/develop
Commit: 08182b1830091d7a0ccc6288eb49ba39a7a9b9a0
Parents: 508af1f
Author: Neil Stevenson <neil.stevenson@em-chips.co.uk>
Authored: Thu May 28 23:17:13 2015 +0100
Committer: Kirk Lund <klund@pivotal.io>
Committed: Thu Jun 18 12:33:20 2015 -0700

----------------------------------------------------------------------
 .../management/internal/cli/shell/Gfsh.java     |  21 +
 .../internal/cli/shell/GfshConfig.java          |  50 +-
 .../cli/shell/GfshConfigInitFileJUnitTest.java  | 170 +++++++
 .../cli/shell/GfshInitFileJUnitTest.java        | 460 +++++++++++++++++++
 4 files changed, 695 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/08182b18/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
index d9a396a..3165830 100755
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/Gfsh.java
@@ -305,6 +305,7 @@ public class Gfsh extends JLineShell {
       synchronized (INSTANCE_LOCK) {
         if (instance == null) {
           instance = new Gfsh(launchShell, args, gfshConfig);
+          instance.executeInitFileIfPresent();
         }
       }
     }
@@ -344,6 +345,26 @@ public class Gfsh extends JLineShell {
     runner.join();
   }
 
+  /* If an init file is provided, as a system property or in the default
+   * location, run it as a command script.
+   */
+  private void executeInitFileIfPresent() {
+
+    String initFileName = this.gfshConfig.getInitFileName();
+    if (initFileName != null) {
+      this.gfshFileLogger.info("Using " + initFileName);
+      try {
+        File gfshInitFile = new File(initFileName);
+        boolean continueOnError = false;
+        this.executeScript(gfshInitFile, isQuietMode(), continueOnError);
+      } catch (Exception exception) {
+        this.gfshFileLogger.severe(initFileName, exception);
+        setLastExecutionStatus(-1);
+      }
+    }
+
+  }
+
   //////////////////// JLineShell Class Methods Start //////////////////////////
   //////////////////////// Implemented Methods ////////////////////////////////
   /**

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/08182b18/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/GfshConfig.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/GfshConfig.java
b/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/GfshConfig.java
index d7dba5a..815e45e 100644
--- a/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/GfshConfig.java
+++ b/gemfire-core/src/main/java/com/gemstone/gemfire/management/internal/cli/shell/GfshConfig.java
@@ -22,25 +22,26 @@ import com.gemstone.gemfire.internal.util.IOUtils;
 // According to 8.0 discussions, gfsh should have as less confing as possible
 // hence Persisting GfshConfig is not done
 public class GfshConfig {
+  public  static final String INIT_FILE_PROPERTY            = "gfsh.init-file";
   private static final String LOG_DIR_PROPERTY              = "gfsh.log-dir";
   private static final String LOG_LEVEL_PROPERTY            = "gfsh.log-level";
   private static final String LOG_FILE_SIZE_LIMIT_PROPERTY  = "gfsh.log-file-size-limit";
   private static final String LOG_DISK_SPACE_LIMIT_PROPERTY = "gfsh.log-disk-space-limit";
 
-
   private static final File  HISTORY_FILE   = new File(getHomeGemFireDirectory(), ".gfsh.history");
 
   // History file size
   private static final int MAX_HISTORY_SIZE = 500;
 
+  public  static final String DEFAULT_INIT_FILE_NAME 	= ".gfsh2rc";	
   private static final Level DEFAULT_LOGLEVEL           = Level.OFF;
   private static final int   DEFAULT_LOGFILE_SIZE_LIMIT = 1024*1024*10;
   private static final int   DEFAULT_LOGFILE_DISK_USAGE = 1024*1024*10;
 
-
   private static final String DEFAULT_PROMPT = "{0}gfsh{1}>";
 
   private String historyFileName;
+  private String initFileName;
   private String defaultPrompt;
   private int    historySize;
   private String logDir;
@@ -49,16 +50,22 @@ public class GfshConfig {
   private int    logFileDiskLimit;
 
   public GfshConfig() {
-    this(HISTORY_FILE.getAbsolutePath(), DEFAULT_PROMPT, MAX_HISTORY_SIZE, null, null, null,
null);
+    this(HISTORY_FILE.getAbsolutePath(), DEFAULT_PROMPT, MAX_HISTORY_SIZE, null, null, null,
null, null);
   }
 
   public GfshConfig(String historyFileName, String defaultPrompt,
       int historySize, String logDir, Level logLevel, Integer logLimit,
-      Integer logCount) {
+      Integer logCount, String initFileName) {
     this.historyFileName = historyFileName;
     this.defaultPrompt   = defaultPrompt;
     this.historySize     = historySize;
 
+    if (initFileName == null) {
+      this.initFileName = this.searchForInitFileName();
+    } else {
+      this.initFileName = initFileName;
+    }
+
     // Logger properties
     if (logDir == null) {
       this.logDir = System.getProperty(LOG_DIR_PROPERTY, ".");
@@ -87,6 +94,10 @@ public class GfshConfig {
     return historyFileName;
   }
 
+  public String getInitFileName() {
+    return initFileName;
+  }
+
   public String getDefaultPrompt() {
     return defaultPrompt;
   }
@@ -129,6 +140,9 @@ public class GfshConfig {
 
   private String getLoggerConfig() {
     StringBuilder builder = new StringBuilder();
+    builder.append(
+        "init-file=" + (getInitFileName() == null ? "" : getInitFileName()))
+        .append(Gfsh.LINE_SEPARATOR);
     builder.append("log-file="+getLogFilePath()).append(Gfsh.LINE_SEPARATOR);
     builder.append("log-level="+getLogLevel().getName()).append(Gfsh.LINE_SEPARATOR);
     builder.append("log-file-size-limit="+getLogFileSizeLimit()).append(Gfsh.LINE_SEPARATOR);
@@ -192,7 +206,9 @@ public class GfshConfig {
   public String toString() {
     StringBuilder builder = new StringBuilder();
     builder.append(getClass().getSimpleName());
-    builder.append(" [historyFileName=");
+    builder.append(" [initFileName=");
+    builder.append(getInitFileName() == null ? "" : getInitFileName());
+    builder.append(", historyFileName=");
     builder.append(getHistoryFileName());
     builder.append(", historySize=");
     builder.append(getHistorySize());
@@ -203,5 +219,27 @@ public class GfshConfig {
     builder.append("]");
     return builder.toString();
   }
-}
 
+  /*
+   * Search for the init file using the system property, then the current
+   * directory, then the home directory. It need not exist at all.
+   */
+  private String searchForInitFileName() {
+    String homeDirectoryInitFileName = System.getProperty("user.home")
+        + File.separatorChar + DEFAULT_INIT_FILE_NAME;
+    String currentDirectoryInitFileName = System.getProperty("user.dir")
+        + File.separatorChar + DEFAULT_INIT_FILE_NAME;
+    String systemPropertyInitFileName = System.getProperty(INIT_FILE_PROPERTY);
+
+    String[] initFileNames = { systemPropertyInitFileName,
+        currentDirectoryInitFileName, homeDirectoryInitFileName };
+
+    for (String initFileName : initFileNames) {
+      if (IOUtils.isExistingPathname(initFileName)) {
+        return initFileName;
+      }
+    }
+
+    return null;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/08182b18/gemfire-core/src/test/java/com/gemstone/gemfire/management/internal/cli/shell/GfshConfigInitFileJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/management/internal/cli/shell/GfshConfigInitFileJUnitTest.java
b/gemfire-core/src/test/java/com/gemstone/gemfire/management/internal/cli/shell/GfshConfigInitFileJUnitTest.java
new file mode 100755
index 0000000..c956a0d
--- /dev/null
+++ b/gemfire-core/src/test/java/com/gemstone/gemfire/management/internal/cli/shell/GfshConfigInitFileJUnitTest.java
@@ -0,0 +1,170 @@
+package com.gemstone.gemfire.management.internal.cli.shell;
+
+import static org.junit.Assert.*;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TemporaryFolder;
+
+import com.gemstone.junit.UnitTest;
+
+@Category(UnitTest.class)
+public class GfshConfigInitFileJUnitTest {
+  private static final String INIT_FILE_NAME = GfshConfig.DEFAULT_INIT_FILE_NAME;
+  private static final String INIT_FILE_PROPERTY = GfshConfig.INIT_FILE_PROPERTY;
+
+  private static String saveUserDir;
+  private static String saveUserHome;
+
+  @Rule
+  public TemporaryFolder temporaryFolder_HomeDirectory = new TemporaryFolder();
+  @Rule
+  public TemporaryFolder temporaryFolder_CurrentDirectory = new TemporaryFolder();
+  @Rule
+  public TemporaryFolder temporaryFolder_AnotherDirectory = new TemporaryFolder();
+
+  @BeforeClass
+  public static void setUpBeforeClass() throws Exception {
+    saveUserDir = System.getProperty("user.dir");
+    saveUserHome = System.getProperty("user.home");
+  }
+
+  @AfterClass
+  public static void tearDownAfterClass() throws Exception {
+    if (saveUserDir == null) {
+      System.clearProperty("user.dir");
+    } else {
+      System.setProperty("user.dir", saveUserDir);
+    }
+    if (saveUserHome == null) {
+      System.clearProperty("user.home");
+    } else {
+      System.setProperty("user.home", saveUserHome);
+    }
+  }
+
+  @Before
+  public void setUp() throws Exception {
+    String userDir = temporaryFolder_CurrentDirectory.getRoot()
+        .getAbsolutePath();
+    String userHome = temporaryFolder_HomeDirectory.getRoot().getAbsolutePath();
+
+    System.setProperty("user.dir", userDir);
+    System.setProperty("user.home", userHome);
+
+    // Abort all tests if system properties cannot be overridden
+    assertEquals("user.dir", System.getProperty("user.dir"), userDir);
+    assertEquals("user.home", System.getProperty("user.home"), userHome);
+  }
+
+  // If a file is given to the constructor, all other options should be ignored
+  @Test
+  public void constructorArgumentUsed() throws Exception {
+    temporaryFolder_HomeDirectory.newFile(INIT_FILE_NAME);
+    temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
+    System.setProperty(INIT_FILE_PROPERTY, temporaryFolder_AnotherDirectory
+        .newFile(INIT_FILE_NAME).getAbsolutePath());
+
+    String argument = temporaryFolder_AnotherDirectory.newFile("junit")
+        .getAbsolutePath();
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig("historyFileName", "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, argument);
+
+    String result = gfshConfig.getInitFileName();
+
+    assertEquals(argument, result);
+  }
+
+  // System property should be chosen ahead of current and home directories
+  @Test
+  public void systemPropertySelectedFirst() throws Exception {
+    temporaryFolder_HomeDirectory.newFile(INIT_FILE_NAME);
+    temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
+    String fileName = temporaryFolder_AnotherDirectory.newFile(INIT_FILE_NAME)
+        .getAbsolutePath();
+    System.setProperty(INIT_FILE_PROPERTY, fileName);
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig("historyFileName", "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, null);
+
+    String result = gfshConfig.getInitFileName();
+
+    assertEquals(fileName, result);
+  }
+
+  // Current directory file selected ahead of home directory file
+  @Test
+  public void currentDirectorySelectedSecond() throws Exception {
+    temporaryFolder_HomeDirectory.newFile(INIT_FILE_NAME);
+    String fileName = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME)
+        .getAbsolutePath();
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig("historyFileName", "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, null);
+
+    String result = gfshConfig.getInitFileName();
+
+    assertEquals(fileName, result);
+  }
+
+  // Home directory file selected if only one present
+  @Test
+  public void homeDirectorySelectedThird() throws Exception {
+    String fileName = temporaryFolder_HomeDirectory.newFile(INIT_FILE_NAME)
+        .getAbsolutePath();
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig("historyFileName", "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, null);
+
+    String result = gfshConfig.getInitFileName();
+
+    assertEquals(fileName, result);
+  }
+
+  // No files need match
+  @Test
+  public void noMatches() throws Exception {
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig("historyFileName", "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, null);
+
+    String result = gfshConfig.getInitFileName();
+
+    assertNull(result);
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/08182b18/gemfire-core/src/test/java/com/gemstone/gemfire/management/internal/cli/shell/GfshInitFileJUnitTest.java
----------------------------------------------------------------------
diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/management/internal/cli/shell/GfshInitFileJUnitTest.java
b/gemfire-core/src/test/java/com/gemstone/gemfire/management/internal/cli/shell/GfshInitFileJUnitTest.java
new file mode 100755
index 0000000..9cbc9b9
--- /dev/null
+++ b/gemfire-core/src/test/java/com/gemstone/gemfire/management/internal/cli/shell/GfshInitFileJUnitTest.java
@@ -0,0 +1,460 @@
+package com.gemstone.gemfire.management.internal.cli.shell;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.PrintStream;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.Handler;
+import java.util.logging.LogRecord;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TemporaryFolder;
+
+import com.gemstone.gemfire.management.internal.cli.LogWrapper;
+import com.gemstone.junit.UnitTest;
+
+/**
+ * <P>
+ * Unit tests for supplying an init file to Gfsh.
+ * </P>
+ * <P>
+ * Makes use of reflection to reset private static variables on some classes to
+ * replace loggers that would otherwise clutter the console.
+ * </P>
+ */
+@Category(UnitTest.class)
+public class GfshInitFileJUnitTest {
+  private static final String INIT_FILE_NAME = GfshConfig.DEFAULT_INIT_FILE_NAME;
+  private static final boolean APPEND = true;
+  private static final int BANNER_LINES = 1;
+  private static final int INIT_FILE_CITATION_LINES = 1;
+
+  private static String saveLog4j2Config;
+  private static String saveUserDir;
+  private static String saveUserHome;
+  private static java.util.logging.Logger julLogger;
+  private static Handler[] saveHandlers;
+
+  @ClassRule
+  public static TemporaryFolder temporaryFolder_Config = new TemporaryFolder();
+  @Rule
+  public TemporaryFolder temporaryFolder_CurrentDirectory = new TemporaryFolder();
+
+  private ByteArrayOutputStream sysout = new ByteArrayOutputStream();
+  private String gfshHistoryFileName;
+  private LogWrapper gfshFileLogger;
+  private JUnitLoggerHandler junitLoggerHandler;
+
+  /*
+   * Turn off console logging from JUL and Log4j2 for the duration of this
+   * class's tests, to reduce noise level of output in automated build.
+   */
+  @BeforeClass
+  public static void setUpBeforeClass() throws Exception {
+    saveLog4j2Config = System
+        .getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+    saveUserDir = System.getProperty("user.dir");
+    saveUserHome = System.getProperty("user.home");
+
+    julLogger = java.util.logging.Logger.getLogger("");
+    saveHandlers = julLogger.getHandlers();
+    for (Handler handler : saveHandlers) {
+      julLogger.removeHandler(handler);
+    }
+
+    File log4j2XML = temporaryFolder_Config.newFile("log4j2.xml");
+    FileUtils.writeStringToFile(log4j2XML, "<Configuration/>", APPEND);
+    System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
+        log4j2XML.toURI().toString());
+  }
+
+  /*
+   * Restore logging to state prior to the execution of this class
+   */
+  @AfterClass
+  public static void tearDownAfterClass() throws Exception {
+    for (Handler handler : saveHandlers) {
+      julLogger.addHandler(handler);
+    }
+
+    if (saveLog4j2Config == null) {
+      System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+    } else {
+      System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
+          saveLog4j2Config);
+      ((LoggerContext) LogManager.getContext(false)).reconfigure();
+    }
+
+    if (saveUserDir == null) {
+      System.clearProperty("user.dir");
+    } else {
+      System.setProperty("user.dir", saveUserDir);
+    }
+    if (saveUserHome == null) {
+      System.clearProperty("user.home");
+    } else {
+      System.setProperty("user.home", saveUserHome);
+    }
+  }
+
+  /*
+   * Reset $HOME and current directory for tests, and confirm this has been
+   * accepted.
+   */
+  @Before
+  public void setUp() throws Exception {
+    // Fake running from home directory
+    String userDir = temporaryFolder_CurrentDirectory.getRoot()
+        .getAbsolutePath();
+    String userHome = userDir;
+
+    System.setProperty("user.dir", userDir);
+    System.setProperty("user.home", userHome);
+
+    // Abort all tests if system properties cannot be overridden
+    assertEquals("user.dir", System.getProperty("user.dir"), userDir);
+    assertEquals("user.home", System.getProperty("user.home"), userHome);
+  }
+
+  @Before
+  public void setUp2() throws Exception {
+
+    // Null out static instance so can reinitialise
+    Field gfsh_instance = Gfsh.class.getDeclaredField("instance");
+    gfsh_instance.setAccessible(true);
+    gfsh_instance.set(null, null);
+
+    this.junitLoggerHandler = new JUnitLoggerHandler();
+
+    this.gfshFileLogger = LogWrapper.getInstance();
+    Field logWrapper_INSTANCE = LogWrapper.class.getDeclaredField("INSTANCE");
+    logWrapper_INSTANCE.setAccessible(true);
+    logWrapper_INSTANCE.set(null, this.gfshFileLogger);
+
+    Field logWrapper_logger = LogWrapper.class.getDeclaredField("logger");
+    logWrapper_logger.setAccessible(true);
+    julLogger.addHandler(this.junitLoggerHandler);
+    logWrapper_logger.set(this.gfshFileLogger, julLogger);
+
+    Gfsh.gfshout = new PrintStream(sysout);
+    this.gfshHistoryFileName = temporaryFolder_CurrentDirectory.newFile(
+        "historyFile").getAbsolutePath();
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    julLogger.removeHandler(this.junitLoggerHandler);
+  }
+
+  @Test
+  public void testInitFile_NotProvided() throws Exception {
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, null);
+    assertNull(INIT_FILE_NAME, gfshConfig.getInitFileName());
+
+    /*
+     * boolean launchShell, String[] args, GfshConfig gfshConfig
+     */
+    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);
+
+    int actualStatus = gfsh.getLastExecutionStatus();
+    int expectedStatus = 0;
+    assertEquals("Status 0==success", expectedStatus, actualStatus);
+
+    int expectedLogCount = BANNER_LINES;
+    assertEquals("Log records written", expectedLogCount,
+        this.junitLoggerHandler.getLog().size());
+    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
+      assertNull("No exceptions in log", logRecord.getThrown());
+    }
+  }
+
+  @Test
+  public void testInitFile_NotFound() throws Exception {
+    // Construct the file name but not the file
+    String initFileName = temporaryFolder_CurrentDirectory.getRoot()
+        .getAbsolutePath() + File.separator + INIT_FILE_NAME;
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, initFileName);
+    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());
+
+    /*
+     * boolean launchShell, String[] args, GfshConfig gfshConfig
+     */
+    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);
+
+    int actualStatus = gfsh.getLastExecutionStatus();
+    int expectedStatus = 0;
+    assertNotEquals("Status <0==failure", expectedStatus, actualStatus);
+
+    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
+    assertEquals("Log records written", expectedLogCount,
+        this.junitLoggerHandler.getLog().size());
+    Throwable exception = null;
+    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
+      if (logRecord.getThrown() != null) {
+        exception = logRecord.getThrown();
+        break;
+      }
+    }
+    assertNotNull("Exceptions in log", exception);
+  }
+
+  @Test
+  public void testInitFile_Empty() throws Exception {
+    File initFile = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, initFile.getAbsolutePath());
+    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());
+
+    /*
+     * boolean launchShell, String[] args, GfshConfig gfshConfig
+     */
+    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);
+
+    int actualStatus = gfsh.getLastExecutionStatus();
+    int expectedStatus = 0;
+    assertEquals("Status 0==success", expectedStatus, actualStatus);
+
+    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
+    assertEquals("Log records written", expectedLogCount,
+        this.junitLoggerHandler.getLog().size());
+    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
+      assertNull("No exceptions in log", logRecord.getThrown());
+    }
+  }
+
+  @Test
+  public void testInitFile_OneGoodCommand() throws Exception {
+    File initFile = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
+    FileUtils.writeStringToFile(initFile, "echo --string=hello"
+        + Gfsh.LINE_SEPARATOR, APPEND);
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, initFile.getAbsolutePath());
+    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());
+
+    /*
+     * boolean launchShell, String[] args, GfshConfig gfshConfig
+     */
+    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);
+
+    int actualStatus = gfsh.getLastExecutionStatus();
+    int expectedStatus = 0;
+    assertEquals("Status 0==success", expectedStatus, actualStatus);
+
+    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
+    assertEquals("Log records written", expectedLogCount,
+        this.junitLoggerHandler.getLog().size());
+    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
+      assertNull("No exceptions in log", logRecord.getThrown());
+    }
+  }
+
+  @Test
+  public void testInitFile_TwoGoodCommands() throws Exception {
+    File initFile = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
+    FileUtils.writeStringToFile(initFile, "echo --string=hello"
+        + Gfsh.LINE_SEPARATOR, APPEND);
+    FileUtils.writeStringToFile(initFile, "echo --string=goodbye"
+        + Gfsh.LINE_SEPARATOR, APPEND);
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, initFile.getAbsolutePath());
+    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());
+
+    /*
+     * boolean launchShell, String[] args, GfshConfig gfshConfig
+     */
+    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);
+
+    int actualStatus = gfsh.getLastExecutionStatus();
+    int expectedStatus = 0;
+    assertEquals("Status 0==success", expectedStatus, actualStatus);
+
+    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
+    assertEquals("Log records written", expectedLogCount,
+        this.junitLoggerHandler.getLog().size());
+    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
+      assertNull("No exceptions in log", logRecord.getThrown());
+    }
+  }
+
+  @Test
+  public void testInitFile_OneBadCommand() throws Exception {
+    File initFile = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
+    FileUtils.writeStringToFile(initFile, "fail" + Gfsh.LINE_SEPARATOR, APPEND);
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, initFile.getAbsolutePath());
+    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());
+
+    /*
+     * boolean launchShell, String[] args, GfshConfig gfshConfig
+     */
+    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);
+
+    int actualStatus = gfsh.getLastExecutionStatus();
+    int expectedStatus = 0;
+    assertNotEquals("Status <0==failure", expectedStatus, actualStatus);
+
+    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
+    assertEquals("Log records written", expectedLogCount,
+        this.junitLoggerHandler.getLog().size());
+    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
+      assertNull("No exceptions in log", logRecord.getThrown());
+    }
+  }
+
+  @Test
+  public void testInitFile_TwoBadCommands() throws Exception {
+    File initFile = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
+    FileUtils.writeStringToFile(initFile, "fail" + Gfsh.LINE_SEPARATOR, APPEND);
+    FileUtils.writeStringToFile(initFile, "fail" + Gfsh.LINE_SEPARATOR, APPEND);
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, initFile.getAbsolutePath());
+    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());
+
+    /*
+     * boolean launchShell, String[] args, GfshConfig gfshConfig
+     */
+    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);
+
+    int actualStatus = gfsh.getLastExecutionStatus();
+    int expectedStatus = 0;
+    assertNotEquals("Status <0==failure", expectedStatus, actualStatus);
+
+    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
+    assertEquals("Log records written", expectedLogCount,
+        this.junitLoggerHandler.getLog().size());
+    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
+      assertNull("No exceptions in log", logRecord.getThrown());
+    }
+  }
+
+  @Test
+  public void testInitFile_BadAndGoodCommands() throws Exception {
+    File initFile = temporaryFolder_CurrentDirectory.newFile(INIT_FILE_NAME);
+    FileUtils.writeStringToFile(initFile, "fail" + Gfsh.LINE_SEPARATOR, APPEND);
+    FileUtils.writeStringToFile(initFile, "echo --string=goodbye"
+        + Gfsh.LINE_SEPARATOR, APPEND);
+
+    /*
+     * String historyFileName, String defaultPrompt, int historySize, String
+     * logDir, Level logLevel, Integer logLimit, Integer logCount, String
+     * initFileName
+     */
+    GfshConfig gfshConfig = new GfshConfig(this.gfshHistoryFileName, "", 0,
+        temporaryFolder_CurrentDirectory.getRoot().getAbsolutePath(), null,
+        null, null, initFile.getAbsolutePath());
+    assertNotNull(INIT_FILE_NAME, gfshConfig.getInitFileName());
+
+    /*
+     * boolean launchShell, String[] args, GfshConfig gfshConfig
+     */
+    Gfsh gfsh = Gfsh.getInstance(false, new String[] {}, gfshConfig);
+
+    int actualStatus = gfsh.getLastExecutionStatus();
+    int expectedStatus = 0;
+    assertNotEquals("Status <0==failure", expectedStatus, actualStatus);
+
+    int expectedLogCount = BANNER_LINES + INIT_FILE_CITATION_LINES + 1;
+    assertEquals("Log records written", expectedLogCount,
+        this.junitLoggerHandler.getLog().size());
+    for (LogRecord logRecord : this.junitLoggerHandler.getLog()) {
+      assertNull("No exceptions in log", logRecord.getThrown());
+    }
+  }
+
+  /**
+   * <P>
+   * Log handler for testing. Capture logged messages for later inspection.
+   * </P>
+   * 
+   * @see java.util.logging.Handler#publish(java.util.logging.LogRecord)
+   */
+  private class JUnitLoggerHandler extends Handler {
+    private List<LogRecord> log;
+
+    public JUnitLoggerHandler() {
+      log = new ArrayList<>();
+    }
+
+    public List<LogRecord> getLog() {
+      return log;
+    }
+
+    @Override
+    public void publish(LogRecord record) {
+      log.add(record);
+    }
+
+    @Override
+    public void flush() {
+    }
+
+    @Override
+    public void close() throws SecurityException {
+    }
+  }
+}


Mime
View raw message