From bfe2373a3d91c2957b82e30a30dc288903d58eee Mon Sep 17 00:00:00 2001
From: Jean-Noel Rouvignac <jean-noel.rouvignac@forgerock.com>
Date: Fri, 08 Mar 2013 08:20:00 +0000
Subject: [PATCH] OPENDJ-803 (CR-1380) ldapsearch exits with wrong return code when server is down 

---
 opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchTestCase.java         |  174 +++---------------------------------------
 opends/src/server/org/opends/server/tools/LDAPConnection.java                                     |    3 
 opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchNoServerTestCase.java |   63 +++++++++++++++
 3 files changed, 81 insertions(+), 159 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/LDAPConnection.java b/opends/src/server/org/opends/server/tools/LDAPConnection.java
index c1d5688..08b4045 100644
--- a/opends/src/server/org/opends/server/tools/LDAPConnection.java
+++ b/opends/src/server/org/opends/server/tools/LDAPConnection.java
@@ -271,6 +271,9 @@
       Message msg = INFO_RESULT_CLIENT_SIDE_CONNECT_ERROR.get();
       throw new LDAPConnectionException(msg, CLIENT_SIDE_CONNECT_ERROR, null,
                                         ce);
+    } catch (LDAPConnectionException e)
+    {
+      throw e;
     } catch(Exception ex2)
     {
       if (debugEnabled())
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchNoServerTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchNoServerTestCase.java
new file mode 100644
index 0000000..15481cc
--- /dev/null
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchNoServerTestCase.java
@@ -0,0 +1,63 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Copyright 2013 ForgeRock AS
+ */
+package org.opends.server.tools;
+
+import static org.testng.Assert.*;
+
+import org.opends.server.protocols.ldap.LDAPResultCode;
+import org.testng.annotations.Test;
+
+/**
+ * A set of test cases for the LDAPSearch tool.
+ */
+public class LDAPSearchNoServerTestCase
+       extends ToolsTestCase
+{
+
+  /**
+   * Tests a simple search with the server down, we should get error code 91.
+   */
+  @Test
+  public void testSearchWithServerDown()
+  {
+    String[] args =
+    {
+      "-h", "127.0.0.1",
+      "-p", "1389",
+      "-D", "cn=Directory Manager",
+      "-w", "password",
+      "-b", "",
+      "-s", "base",
+      "--noPropertiesFile",
+      "(objectClass=*)"
+    };
+
+    assertEquals(LDAPSearch.mainSearch(args, false, null, System.err),
+        LDAPResultCode.CLIENT_SIDE_CONNECT_ERROR);
+  }
+
+}
diff --git a/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchTestCase.java b/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchTestCase.java
index b8d5646..e74f2fc 100644
--- a/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchTestCase.java
+++ b/opends/tests/unit-tests-testng/src/server/org/opends/server/tools/LDAPSearchTestCase.java
@@ -23,15 +23,15 @@
  *
  *
  *      Copyright 2006-2009 Sun Microsystems, Inc.
- *      Portions Copyright 2011 ForgeRock AS
+ *      Portions Copyright 2011-2013 ForgeRock AS
  */
 package org.opends.server.tools;
 
-
-
 import java.io.File;
 import java.io.FileWriter;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 
 import org.testng.annotations.AfterClass;
 import org.testng.annotations.BeforeClass;
@@ -46,7 +46,6 @@
 import org.opends.server.types.ResultCode;
 
 import static org.testng.Assert.*;
-
 import static org.opends.server.util.ServerConstants.*;
 
 
@@ -124,159 +123,16 @@
     argLists.add(args);
     reasonList.add("No arguments");
 
-    args = new String[]
+    List<String> options =
+        Arrays.asList("-b", "-D", "-w", "-j", "-Y", "-i", "-K", "-P", "-W",
+            "-h", "-p", "-V", "-f", "-J", "-z", "-l", "-s", "-a", "-o", "-c",
+            "--assertionFilter", "--matchedValuesFilter");
+    for (String option : options)
     {
-      "-b"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-b' argument");
-
-    args = new String[]
-    {
-      "-D"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-D' argument");
-
-    args = new String[]
-    {
-      "-w"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-w' argument");
-
-    args = new String[]
-    {
-      "-j"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-j' argument");
-
-    args = new String[]
-    {
-      "-Y"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-Y' argument");
-
-    args = new String[]
-    {
-      "-i"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-i' argument");
-
-    args = new String[]
-    {
-      "-K"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-K' argument");
-
-    args = new String[]
-    {
-      "-P"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-P' argument");
-
-    args = new String[]
-    {
-      "-W"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-W' argument");
-
-    args = new String[]
-    {
-      "-h"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-h' argument");
-
-    args = new String[]
-    {
-      "-p"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-p' argument");
-
-    args = new String[]
-    {
-      "-V"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-V' argument");
-
-    args = new String[]
-    {
-      "-f"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-f' argument");
-
-    args = new String[]
-    {
-      "-J"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-J' argument");
-
-    args = new String[]
-    {
-      "-z"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-z' argument");
-
-    args = new String[]
-    {
-      "-l"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-l' argument");
-
-    args = new String[]
-    {
-      "-s"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-s' argument");
-
-    args = new String[]
-    {
-      "-a"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-a' argument");
-
-    args = new String[]
-    {
-      "-o"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-o' argument");
-
-    args = new String[]
-    {
-      "-c"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '-c' argument");
-
-    args = new String[]
-    {
-      "--assertionFilter"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '--assertionFilter' argument");
-
-    args = new String[]
-    {
-      "--matchedValuesFilter"
-    };
-    argLists.add(args);
-    reasonList.add("No value for '--matchedValuesFilter' argument");
+      args = new String[] { option };
+      argLists.add(args);
+      reasonList.add("No value for '" + option + "' argument");
+    }
 
     args = new String[]
     {
@@ -2311,7 +2167,7 @@
          throws Exception
   {
     TestCaseUtils.clearJEBackend(true, "userRoot", "dc=example,dc=com");
-   
+
     String[] args =
     {
       "-h", "127.0.0.1",
@@ -2325,7 +2181,7 @@
       "(objectClass=*)"
     };
 
-    assertTrue(LDAPSearch.mainSearch(args, false, null, null) == 0);
+    assertEquals(LDAPSearch.mainSearch(args, false, null, null), 0);
   }
 
 
@@ -2384,7 +2240,7 @@
       "dn"
     };
 
-    assertTrue(LDAPSearch.mainSearch(args, false, null, null) == 0);
+    assertEquals(LDAPSearch.mainSearch(args, false, null, null), 0);
   }
 
 

--
Gitblit v1.10.0