directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From plusplusjia...@apache.org
Subject directory-kerby git commit: Add JsonBackendBenchmark, refine some codes in backends and fix some tests. This based on Kai's work.
Date Thu, 06 Aug 2015 02:09:22 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/master 87af55d70 -> 216ee66e6


Add JsonBackendBenchmark,refine some codes in backends and fix some tests. This based on Kai's
work.


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/216ee66e
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/216ee66e
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/216ee66e

Branch: refs/heads/master
Commit: 216ee66e64616e01730bcf27dee116500aecb2b1
Parents: 87af55d
Author: plusplusjiajia <jiajia.li@intel.com>
Authored: Thu Aug 6 10:15:01 2015 +0800
Committer: plusplusjiajia <jiajia.li@intel.com>
Committed: Thu Aug 6 10:15:01 2015 +0800

----------------------------------------------------------------------
 benchmark/pom.xml                               | 166 +++++++++++--------
 .../kerby/benchmark/JsonBackendBenchmark.java   |  90 ++++++++++
 .../kerby/benchmark/KrbCodecBenchmark.java      |   9 +-
 .../identitybackend/JsonIdentityBackend.java    |  75 ++++-----
 .../kerb/identity/backend/JsonBackendTest.java  |   3 +-
 kerby-backend/pom.xml                           |   1 -
 .../kerberos/kdc/WithTokenKdcTestBase.java      |  13 --
 .../kerb/identity/backend/BackendTestUtil.java  |  23 +++
 .../backend/AbstractIdentityBackend.java        |  18 +-
 .../kerb/server/MultiRequestsKdcTest.java       |  14 +-
 10 files changed, 265 insertions(+), 147 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/benchmark/pom.xml
----------------------------------------------------------------------
diff --git a/benchmark/pom.xml b/benchmark/pom.xml
index 8b2fa00..4585f2d 100644
--- a/benchmark/pom.xml
+++ b/benchmark/pom.xml
@@ -17,7 +17,7 @@
   <parent>
     <artifactId>kerby-all</artifactId>
     <groupId>org.apache.kerby</groupId>
-    <version>${project.version}</version>
+    <version>1.0-SNAPSHOT</version>
   </parent>
 
   <groupId>org.apache.kerby</groupId>
@@ -68,6 +68,21 @@
       <version>${project.version}</version>
     </dependency>
     <dependency>
+      <groupId>org.apache.kerby</groupId>
+      <artifactId>kerb-identity-test</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.kerby</groupId>
+      <artifactId>json-backend</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.kerby</groupId>
+      <artifactId>zookeeper-backend</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
+    <dependency>
       <groupId>org.apache.directory.server</groupId>
       <artifactId>apacheds-core-api</artifactId>
       <version>${apache.ds.version}</version>
@@ -151,79 +166,84 @@
     </dependency>
   </dependencies>
 
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-compiler-plugin</artifactId>
-        <version>3.1</version>
-        <configuration>
-          <compilerVersion>${javac.target}</compilerVersion>
-          <source>${javac.target}</source>
-          <target>${javac.target}</target>
-        </configuration>
-      </plugin>
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-shade-plugin</artifactId>
-        <version>2.2</version>
-        <executions>
-          <execution>
-            <phase>package</phase>
-            <goals>
-              <goal>shade</goal>
-            </goals>
-            <configuration>
-              <finalName>${uberjar.name}</finalName>
-              <transformers>
-                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
-                  <mainClass>org.openjdk.jmh.Main</mainClass>
-                </transformer>
-              </transformers>
-              <filters>
-                <filter>
-                  <!--
-                      Shading signed JARs will fail without this.
-                      http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
-                  -->
-                  <artifact>*:*</artifact>
-                  <excludes>
-                    <exclude>META-INF/*.SF</exclude>
-                    <exclude>META-INF/*.DSA</exclude>
-                    <exclude>META-INF/*.RSA</exclude>
-                  </excludes>
-                </filter>
-              </filters>
-            </configuration>
-          </execution>
-        </executions>
-      </plugin>
-      <plugin>
-        <groupId>org.codehaus.mojo</groupId>
-        <artifactId>exec-maven-plugin</artifactId>
-        <executions>
-          <execution>
-            <id>run-benchmarks</id>
-            <phase>integration-test</phase>
-            <goals>
-              <goal>exec</goal>
-            </goals>
+ <profiles>
+    <profile>
+      <id>benchmark</id>
+      <activation>
+        <activeByDefault>true</activeByDefault>
+      </activation>
+      <build>
+        <plugins>
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-compiler-plugin</artifactId>
+            <version>3.1</version>
             <configuration>
-              <classpathScope>test</classpathScope>
-              <executable>java</executable>
-              <arguments>
-                <argument>-jar</argument>
-                <argument>target/benchmarks.jar</argument>
-                <argument>-wi</argument>
-                <argument>2</argument>
-                <argument>-i</argument>
-                <argument>5</argument>
-              </arguments>
+              <compilerVersion>${javac.target}</compilerVersion>
+              <source>${javac.target}</source>
+              <target>${javac.target}</target>
+              <encoding>UTF-8</encoding>
             </configuration>
-          </execution>
-        </executions>
-      </plugin>
-    </plugins>
-  </build>
+          </plugin>
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-shade-plugin</artifactId>
+            <version>2.2</version>
+            <executions>
+              <execution>
+                <phase>package</phase>
+                <goals>
+                  <goal>shade</goal>
+                </goals>
+                <configuration>
+                  <finalName>${uberjar.name}</finalName>
+                  <transformers>
+                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
+                      <mainClass>org.openjdk.jmh.Main</mainClass>
+                    </transformer>
+                  </transformers>
+                  <filters>
+                    <filter>
+                      <!--
+                          Shading signed JARs will fail without this.
+                          http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
+                      -->
+                      <artifact>*:*</artifact>
+                      <excludes>
+                        <exclude>META-INF/*.SF</exclude>
+                        <exclude>META-INF/*.DSA</exclude>
+                        <exclude>META-INF/*.RSA</exclude>
+                      </excludes>
+                    </filter>
+                  </filters>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+          <plugin>
+            <groupId>org.codehaus.mojo</groupId>
+            <artifactId>exec-maven-plugin</artifactId>
+            <executions>
+              <execution>
+                <id>run-benchmarks</id>
+                <phase>integration-test</phase>
+                <goals>
+                  <goal>exec</goal>
+                </goals>
+                <configuration>
+                  <classpathScope>test</classpathScope>
+                  <executable>java</executable>
+                  <arguments>
+                    <argument>-jar</argument>
+                    <argument>target/benchmarks.jar</argument>
+                  </arguments>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
+  </profiles>
 
 </project>

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/benchmark/src/main/java/org/apache/kerby/benchmark/JsonBackendBenchmark.java
----------------------------------------------------------------------
diff --git a/benchmark/src/main/java/org/apache/kerby/benchmark/JsonBackendBenchmark.java
b/benchmark/src/main/java/org/apache/kerby/benchmark/JsonBackendBenchmark.java
new file mode 100644
index 0000000..5b5f36f
--- /dev/null
+++ b/benchmark/src/main/java/org/apache/kerby/benchmark/JsonBackendBenchmark.java
@@ -0,0 +1,90 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.benchmark;
+
+import org.apache.kerby.config.Conf;
+import org.apache.kerby.config.Config;
+import org.apache.kerby.kerberos.kdc.identitybackend.JsonIdentityBackend;
+import org.apache.kerby.kerberos.kerb.KrbException;
+import org.apache.kerby.kerberos.kerb.identity.backend.BackendTestUtil;
+import org.apache.kerby.kerberos.kerb.identity.backend.IdentityBackend;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.TearDown;
+
+import java.io.File;
+import java.util.concurrent.TimeUnit;
+
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@State(Scope.Benchmark)
+public class JsonBackendBenchmark {
+
+    private IdentityBackend backend;
+
+    private File jsonBackendFile;
+
+    @Setup
+    public void setup() throws KrbException {
+        prepareBackend();
+        prepareIdentities();
+    }
+
+    private void prepareBackend() throws KrbException {
+        File testDir = new File(System.getProperty("test.dir", "target"));
+        jsonBackendFile = new File(testDir, "json-identity-backend-file");
+        String jsonBackendFileString = jsonBackendFile.getAbsolutePath();
+
+        Config backendConfig = new Conf();
+        backendConfig.setString(JsonIdentityBackend.JSON_IDENTITY_BACKEND_DIR,
+                jsonBackendFileString);
+        backend = new JsonIdentityBackend(backendConfig);
+        backend.initialize();
+    }
+
+    private void prepareIdentities() throws KrbException {
+        BackendTestUtil.createManyIdentities(backend, 100);
+        BackendTestUtil.createTheTestIdentity(backend);
+    }
+
+    @Benchmark
+    @Fork(1)
+    public void queryTest() throws Exception {
+        BackendTestUtil.getTheTestIdentity(backend);
+    }
+
+    @TearDown
+    public void cleanup() throws KrbException {
+        if (backend != null) {
+            backend.stop();
+            backend.release();
+        }
+
+        if (jsonBackendFile.exists()) {
+            jsonBackendFile.delete();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/benchmark/src/main/java/org/apache/kerby/benchmark/KrbCodecBenchmark.java
----------------------------------------------------------------------
diff --git a/benchmark/src/main/java/org/apache/kerby/benchmark/KrbCodecBenchmark.java b/benchmark/src/main/java/org/apache/kerby/benchmark/KrbCodecBenchmark.java
index fda9b0e..48c2703 100644
--- a/benchmark/src/main/java/org/apache/kerby/benchmark/KrbCodecBenchmark.java
+++ b/benchmark/src/main/java/org/apache/kerby/benchmark/KrbCodecBenchmark.java
@@ -27,6 +27,7 @@ import org.openjdk.jmh.annotations.BenchmarkMode;
 import org.openjdk.jmh.annotations.Fork;
 import org.openjdk.jmh.annotations.Mode;
 import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Warmup;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -34,7 +35,7 @@ import java.nio.ByteBuffer;
 import java.util.concurrent.TimeUnit;
 
 @BenchmarkMode(Mode.AverageTime)
-@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
 public class KrbCodecBenchmark {
 
     private static ByteBuffer apreqToken;
@@ -51,7 +52,8 @@ public class KrbCodecBenchmark {
     }
 
     @Benchmark
-    @Fork(2)
+    @Fork(1)
+    @Warmup(iterations = 5)
     public void decodeWithKerby() throws Exception {
         ApReq apReq = new ApReq();
         apReq.decode(apreqToken.duplicate());
@@ -62,7 +64,8 @@ public class KrbCodecBenchmark {
     }
 
     @Benchmark
-    @Fork(2)
+    @Fork(1)
+    @Warmup(iterations = 5)
     public void decodeWithApacheDS() throws Exception {
         ByteBuffer content = apreqToken.duplicate();
         Asn1Decoder krbDecoder = new Asn1Decoder();

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
----------------------------------------------------------------------
diff --git a/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
b/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
index 101c376..715a840 100644
--- a/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
+++ b/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
@@ -40,22 +40,25 @@ import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.LinkedHashMap;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.TreeMap;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * A Json file based backend implementation.
  *
  */
 public class JsonIdentityBackend extends AbstractIdentityBackend {
+    private static final Logger LOG = LoggerFactory.getLogger(JsonIdentityBackend.class);
     public static final String JSON_IDENTITY_BACKEND_DIR = "backend.json.dir";
     private File jsonKdbFile;
     private Gson gson;
-    private static final Logger LOG = LoggerFactory.getLogger(JsonIdentityBackend.class);
 
     // Identities loaded from file
-    private Map<String, KrbIdentity> ids;
+    private final Map<String, KrbIdentity> identities =
+        new ConcurrentHashMap<>(new TreeMap<String, KrbIdentity>());
     private long kdbFileTimeStamp;
 
     public JsonIdentityBackend() {
@@ -97,7 +100,7 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
             }
         }
 
-        jsonKdbFile = new File(jsonFileDir, "json_identity_backend_file");
+        jsonKdbFile = new File(jsonFileDir, "json-backend.json");
 
         if (!jsonKdbFile.exists()) {
             try {
@@ -114,11 +117,11 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
      * Check kdb file timestamp to see if it's changed or not. If
      * necessary load the kdb again.
      */
-    private void checkAndLoad() {
+    private synchronized void checkAndLoad() {
         long nowTimeStamp = jsonKdbFile.lastModified();
 
         if (kdbFileTimeStamp == 0 || nowTimeStamp != kdbFileTimeStamp) {
-            //load ids
+            //load identities
             String existsFileJson = null;
             try {
                 existsFileJson = IOUtil.readFile(jsonKdbFile);
@@ -126,13 +129,13 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
                 throw new RuntimeException("Failed to read file", e);
             }
 
-            ids = gson.fromJson(existsFileJson,
-                new TypeToken<LinkedHashMap<String, KrbIdentity>>() {
+            Map<String, KrbIdentity> loaded = gson.fromJson(existsFileJson,
+                new TypeToken<HashMap<String, KrbIdentity>>() {
                 }.getType());
-        }
 
-        if (ids == null) {
-            ids = new LinkedHashMap<>();
+            if (loaded != null) {
+                identities.putAll(loaded);
+            }
         }
     }
 
@@ -142,24 +145,18 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
     @Override
     protected KrbIdentity doGetIdentity(String principalName) {
         checkAndLoad();
-        return ids.get(principalName);
+        return identities.get(principalName);
     }
 
     /**
      * {@inheritDoc}
      */
     @Override
-    protected KrbIdentity doAddIdentity(KrbIdentity identity) {
+    protected KrbIdentity doAddIdentity(KrbIdentity identity) throws KrbException {
         checkAndLoad();
 
-        String principalName = identity.getPrincipalName();
-        if (ids.containsKey(principalName)) {
-            LOG.error("Error occurred while adding identity, principal " + principalName
+ " already exists.");
-            throw new RuntimeException("Principal already exists.");
-        }
-
-        ids.put(identity.getPrincipalName(), identity);
-        idsToFile(ids);
+        identities.put(identity.getPrincipalName(), identity);
+        idsToFile(identities);
 
         return doGetIdentity(identity.getPrincipalName());
     }
@@ -168,16 +165,11 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
      * {@inheritDoc}
      */
     @Override
-    protected KrbIdentity doUpdateIdentity(KrbIdentity identity) {
+    protected KrbIdentity doUpdateIdentity(KrbIdentity identity) throws KrbException {
         checkAndLoad();
-        String principalName = identity.getPrincipalName();
-        if (ids.containsKey(principalName)) {
-            ids.put(principalName, identity);
-        } else {
-            LOG.error("Error occurred while updating identity, principal " + principalName
+ " does not exists.");
-            throw new RuntimeException("Principal does not exist.");
-        }
-        idsToFile(ids);
+        identities.put(identity.getPrincipalName(), identity);
+        idsToFile(identities);
+
         return doGetIdentity(identity.getPrincipalName());
     }
 
@@ -185,15 +177,12 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
      * {@inheritDoc}
      */
     @Override
-    protected void doDeleteIdentity(String principalName) {
+    protected void doDeleteIdentity(String principalName) throws KrbException {
         checkAndLoad();
-        if (ids.containsKey(principalName)) {
-            ids.remove(principalName);
-        } else {
-            LOG.error("Error occurred while deleting identity, principal " + principalName
+ " does not exists.");
-            throw new RuntimeException("Principal does not exist.");
+        if (identities.containsKey(principalName)) {
+            identities.remove(principalName);
         }
-        idsToFile(ids);
+        idsToFile(identities);
     }
 
     /**
@@ -201,7 +190,7 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
      */
     @Override
     protected Iterable<String> doGetIdentities() throws KrbException {
-        List<String> principals = new ArrayList<>(ids.keySet());
+        List<String> principals = new ArrayList<>(identities.keySet());
         Collections.sort(principals);
 
         return principals;
@@ -221,16 +210,16 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
     }
 
     /**
-     * Write ids into a file
-     * @param ids the ids to write into the json file
+     * Write identities into a file
+     * @param ids the identities to write into the json file
      */
-    private void idsToFile(Map<String, KrbIdentity> ids) {
+    private synchronized void idsToFile(Map<String, KrbIdentity> ids) throws KrbException
{
         String newFileJson = gson.toJson(ids);
         try {
             IOUtil.writeFile(newFileJson, jsonKdbFile);
         } catch (IOException e) {
-            LOG.error("Error occurred while writing ids to file: " + jsonKdbFile);
-            throw new RuntimeException("Failed to write file", e);
+            LOG.error("Error occurred while writing identities to file: " + jsonKdbFile);
+            throw new KrbException("Failed to write file", e);
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/kerby-backend/json-backend/src/test/java/org/apache/kerby/kerberos/kerb/identity/backend/JsonBackendTest.java
----------------------------------------------------------------------
diff --git a/kerby-backend/json-backend/src/test/java/org/apache/kerby/kerberos/kerb/identity/backend/JsonBackendTest.java
b/kerby-backend/json-backend/src/test/java/org/apache/kerby/kerberos/kerb/identity/backend/JsonBackendTest.java
index 2d4951e..1f579f4 100644
--- a/kerby-backend/json-backend/src/test/java/org/apache/kerby/kerberos/kerb/identity/backend/JsonBackendTest.java
+++ b/kerby-backend/json-backend/src/test/java/org/apache/kerby/kerberos/kerb/identity/backend/JsonBackendTest.java
@@ -41,7 +41,8 @@ public class JsonBackendTest extends BackendTestBase {
         String jsonBackendFileString = jsonBackendFile.getAbsolutePath();
 
         Config backendConfig = new Conf();
-        backendConfig.setString(JsonIdentityBackend.JSON_IDENTITY_BACKEND_DIR, jsonBackendFileString);
+        backendConfig.setString(JsonIdentityBackend.JSON_IDENTITY_BACKEND_DIR,
+                jsonBackendFileString);
         backend = new JsonIdentityBackend(backendConfig);
         backend.initialize();
     }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/kerby-backend/pom.xml
----------------------------------------------------------------------
diff --git a/kerby-backend/pom.xml b/kerby-backend/pom.xml
index 1c76fa3..2ad0838 100644
--- a/kerby-backend/pom.xml
+++ b/kerby-backend/pom.xml
@@ -28,7 +28,6 @@
 
   <modules>
     <module>ldap-backend</module>
-    <module>mavibot-backend</module>
     <module>json-backend</module>
     <module>zookeeper-backend</module>
   </modules>

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/WithTokenKdcTestBase.java
----------------------------------------------------------------------
diff --git a/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/WithTokenKdcTestBase.java
b/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/WithTokenKdcTestBase.java
index 3976bbf..d84d11b 100644
--- a/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/WithTokenKdcTestBase.java
+++ b/kerby-kdc-test/src/test/java/org/apache/kerby/kerberos/kdc/WithTokenKdcTestBase.java
@@ -19,7 +19,6 @@
  */
 package org.apache.kerby.kerberos.kdc;
 
-import org.apache.kerby.kerberos.kerb.KrbException;
 import org.apache.kerby.kerberos.kerb.KrbRuntime;
 import org.apache.kerby.kerberos.kerb.ccache.Credential;
 import org.apache.kerby.kerberos.kerb.ccache.CredentialCache;
@@ -55,18 +54,6 @@ public class WithTokenKdcTestBase extends KdcTestBase {
         super.setUp();
     }
 
-    @Override
-    protected void createPrincipals() throws KrbException {
-        super.createPrincipals();
-        getKdcServer().createPrincipal(getClientPrincipal(), getClientPassword());
-    }
-
-    @Override
-    protected void deletePrincipals() throws KrbException {
-        super.deletePrincipals();
-        getKdcServer().deletePrincipal(getClientPrincipal());
-    }
-
     protected AuthToken getKrbToken() {
         return krbToken;
     }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/kerby-kerb/kerb-identity-test/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/BackendTestUtil.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-identity-test/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/BackendTestUtil.java
b/kerby-kerb/kerb-identity-test/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/BackendTestUtil.java
index 843e3ee..d125cc5 100644
--- a/kerby-kerb/kerb-identity-test/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/BackendTestUtil.java
+++ b/kerby-kerb/kerb-identity-test/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/BackendTestUtil.java
@@ -45,6 +45,16 @@ public final class BackendTestUtil {
             EncryptionType.DES3_CBC_SHA1_KD
     };
 
+    public static void createManyIdentities(IdentityBackend backend,
+                                            int count) throws KrbException {
+        int howMany = count > 0 ? count : 20;
+        List<KrbIdentity> identities = createManyIdentities(howMany);
+
+        for (KrbIdentity identity : identities) {
+            backend.addIdentity(identity);
+        }
+    }
+
     public static KrbIdentity[] createManyIdentities() throws KrbException {
         List<KrbIdentity> results = createManyIdentities(20);
         return results.toArray(new KrbIdentity[results.size()]);
@@ -62,6 +72,19 @@ public final class BackendTestUtil {
         return results;
     }
 
+    public static void createTheTestIdentity(
+            IdentityBackend backend) throws KrbException {
+        backend.addIdentity(createOneIdentity(TEST_PRINCIPAL));
+    }
+
+    public static void getTheTestIdentity(
+            IdentityBackend backend) throws KrbException {
+        KrbIdentity identity = backend.getIdentity(TEST_PRINCIPAL);
+        if (identity == null) {
+            throw new KrbException("Failed to get the test principal");
+        }
+    }
+
     public static KrbIdentity createOneIdentity() throws KrbException {
         return createOneIdentity(TEST_PRINCIPAL);
     }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/kerby-kerb/kerb-identity/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/AbstractIdentityBackend.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-identity/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/AbstractIdentityBackend.java
b/kerby-kerb/kerb-identity/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/AbstractIdentityBackend.java
index a4f56c6..2ed1848 100644
--- a/kerby-kerb/kerb-identity/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/AbstractIdentityBackend.java
+++ b/kerby-kerb/kerb-identity/src/main/java/org/apache/kerby/kerberos/kerb/identity/backend/AbstractIdentityBackend.java
@@ -147,6 +147,10 @@ public abstract class AbstractIdentityBackend
         if (identity == null) {
             throw new IllegalArgumentException("null identity to add");
         }
+        if (doGetIdentity(identity.getPrincipalName()) != null) {
+            throw new KrbException("Principal already exists: "
+                    + identity.getPrincipalName());
+        }
 
         KrbIdentity added = doAddIdentity(identity);
         logger.debug("addIdentity {}, principalName = {}",
@@ -170,8 +174,14 @@ public abstract class AbstractIdentityBackend
             throw new IllegalArgumentException("null identity to update");
         }
 
+        if (doGetIdentity(identity.getPrincipalName()) == null) {
+            logger.error("Error occurred while updating identity, principal "
+                + identity.getPrincipalName() + " does not exists.");
+            throw new KrbException("Principal does not exist.");
+        }
+
         KrbIdentity updated = doUpdateIdentity(identity);
-        logger.debug("addIdentity {}, principalName = {}",
+        logger.debug("updateIdentity {}, principalName = {}",
                 (updated != null ? "successful" : "failed"), identity.getPrincipalName());
 
         return updated;
@@ -194,6 +204,12 @@ public abstract class AbstractIdentityBackend
             throw new IllegalArgumentException("null identity to remove");
         }
 
+        if (doGetIdentity(principalName) == null) {
+            logger.error("Error occurred while deleting identity, principal "
+                    + principalName + " does not exists.");
+            throw new KrbException("Principal does not exist.");
+        }
+
         doDeleteIdentity(principalName);
     }
 

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/216ee66e/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/MultiRequestsKdcTest.java
----------------------------------------------------------------------
diff --git a/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/MultiRequestsKdcTest.java
b/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/MultiRequestsKdcTest.java
index c13b972..61a560b 100644
--- a/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/MultiRequestsKdcTest.java
+++ b/kerby-kerb/kerb-kdc-test/src/test/java/org/apache/kerby/kerberos/kerb/server/MultiRequestsKdcTest.java
@@ -19,7 +19,6 @@
  */
 package org.apache.kerby.kerberos.kerb.server;
 
-import org.apache.kerby.kerberos.kerb.KrbException;
 import org.apache.kerby.kerberos.kerb.spec.ticket.ServiceTicket;
 import org.apache.kerby.kerberos.kerb.spec.ticket.TgtTicket;
 import org.junit.Assert;
@@ -28,16 +27,7 @@ import org.junit.Test;
 import static org.assertj.core.api.Assertions.assertThat;
 
 public class MultiRequestsKdcTest extends KdcTestBase {
-    private String clientPrincipal;
     private String serverPrincipal;
-    private String password = "123456";
-
-    @Override
-    protected void createPrincipals() throws KrbException {
-        super.createPrincipals();
-        clientPrincipal = getClientPrincipal();
-        getKdcServer().createPrincipal(clientPrincipal, password);
-    }
 
     @Test
     public void multiRequestsTest() throws Exception {
@@ -46,7 +36,7 @@ public class MultiRequestsKdcTest extends KdcTestBase {
 
         // With good password
         try {
-            tgt = getKrbClient().requestTgtWithPassword(clientPrincipal, password);
+            tgt = getKrbClient().requestTgtWithPassword(getClientPrincipal(), getClientPassword());
             assertThat(tgt).isNotNull();
 
             serverPrincipal = getServerPrincipal();
@@ -68,7 +58,7 @@ public class MultiRequestsKdcTest extends KdcTestBase {
 
         // With good password again
         try {
-            tgt = getKrbClient().requestTgtWithPassword(clientPrincipal, password);
+            tgt = getKrbClient().requestTgtWithPassword(getClientPrincipal(), getClientPassword());
             assertThat(tgt).isNotNull();
 
             tkt = getKrbClient().requestServiceTicketWithTgt(tgt, serverPrincipal);


Mime
View raw message