001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.directory.server.core.integ; 020 021 022import org.apache.directory.server.annotations.CreateLdapServer; 023import org.apache.directory.server.core.api.DirectoryService; 024import org.apache.directory.server.factory.ServerAnnotationProcessor; 025import org.apache.directory.server.ldap.LdapServer; 026import org.junit.rules.TestRule; 027import org.junit.runner.Description; 028import org.junit.runners.model.Statement; 029import org.slf4j.Logger; 030import org.slf4j.LoggerFactory; 031 032 033/** 034 * A {@link TestRule} for processing 035 * {@link CreateLdapServer @CreateLdapServer} annotations. 036 * 037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 038 */ 039public class CreateLdapServerRule extends CreateDsRule 040{ 041 private static final Logger LOG = LoggerFactory.getLogger( CreateLdapServerRule.class ); 042 043 private CreateLdapServerRule classCreateLdapServerRule; 044 private LdapServer ldapServer; 045 046 047 public CreateLdapServerRule() 048 { 049 this( null ); 050 } 051 052 053 public CreateLdapServerRule( CreateLdapServerRule classCreateLdapServerRule ) 054 { 055 super( classCreateLdapServerRule ); 056 this.classCreateLdapServerRule = classCreateLdapServerRule; 057 } 058 059 060 @Override 061 public Statement apply( final Statement base, final Description description ) 062 { 063 return super.apply( buildStatement( base, description ), description ); 064 } 065 066 067 private Statement buildStatement( final Statement base, final Description description ) 068 { 069 final CreateLdapServer createLdapServer = description.getAnnotation( CreateLdapServer.class ); 070 if ( createLdapServer == null ) 071 { 072 return new Statement() 073 { 074 @Override 075 public void evaluate() throws Throwable 076 { 077 LdapServer ldapServer = getLdapServer(); 078 DirectoryService directoryService = getDirectoryService(); 079 if ( ldapServer != null && directoryService != ldapServer.getDirectoryService() ) 080 { 081 LOG.trace( "Changing to new directory service" ); 082 DirectoryService oldDirectoryService = ldapServer.getDirectoryService(); 083 ldapServer.setDirectoryService( directoryService ); 084 085 try 086 { 087 base.evaluate(); 088 } 089 finally 090 { 091 LOG.trace( "Reverting to old directory service" ); 092 ldapServer.setDirectoryService( oldDirectoryService ); 093 } 094 095 } 096 else 097 { 098 LOG.trace( "no @CreateLdapServer on: {}", description ); 099 base.evaluate(); 100 } 101 } 102 }; 103 } 104 else 105 { 106 return new Statement() 107 { 108 @Override 109 public void evaluate() throws Throwable 110 { 111 LOG.trace( "Creating ldap server" ); 112 ldapServer = ServerAnnotationProcessor.createLdapServer( description, 113 getDirectoryService() ); 114 115 try 116 { 117 base.evaluate(); 118 } 119 finally 120 { 121 LOG.trace( "Stopping ldap server" ); 122 ldapServer.stop(); 123 } 124 } 125 }; 126 } 127 } 128 129 130 public LdapServer getLdapServer() 131 { 132 return ldapServer == null 133 ? ( classCreateLdapServerRule == null ? null : classCreateLdapServerRule.getLdapServer() ) 134 : ldapServer; 135 } 136}