View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.directory.server.core.integ;
20  
21  
22  import org.apache.directory.api.util.FileUtils;
23  import org.apache.directory.server.core.annotations.CreateDS;
24  import org.apache.directory.server.core.api.DirectoryService;
25  import org.apache.directory.server.core.api.changelog.Tag;
26  import org.apache.directory.server.core.factory.DSAnnotationProcessor;
27  import org.junit.rules.TestRule;
28  import org.junit.runner.Description;
29  import org.junit.runners.model.Statement;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * A {@link TestRule} for processing {@link CreateDS @CreateDS} annotations.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public class CreateDsRule implements TestRule
40  {
41      private static final Logger LOG = LoggerFactory.getLogger( CreateDsRule.class );
42  
43      private DirectoryService directoryService;
44      private CreateDsRule outerCreateDsRule;
45  
46  
47      public CreateDsRule()
48      {
49      }
50  
51  
52      public CreateDsRulef="../../../../../../org/apache/directory/server/core/integ/CreateDsRule.html#CreateDsRule">CreateDsRule( CreateDsRule outerCreateDsRule )
53      {
54          this.outerCreateDsRule = outerCreateDsRule;
55      }
56  
57  
58      public DirectoryService getDirectoryService()
59      {
60          return directoryService == null
61              ? ( outerCreateDsRule == null
62                  ? null
63                  : outerCreateDsRule.getDirectoryService() )
64              : directoryService;
65      }
66  
67  
68      @Override
69      public Statement apply( final Statement base, final Description description )
70      {
71          final CreateDS createDs = description.getAnnotation( CreateDS.class );
72          
73          if ( createDs == null )
74          {
75              final DirectoryService directoryService = getDirectoryService();
76              if ( directoryService != null && directoryService.getChangeLog().isEnabled() )
77              {
78                  return new Statement()
79                  {
80                      @Override
81                      public void evaluate() throws Throwable
82                      {
83                          Tag tag = directoryService.getChangeLog().tag();
84                          DSAnnotationProcessor.applyLdifs( description, directoryService );
85                          LOG.debug( "Tagged change log: {}", tag );
86                          try
87                          {
88                              base.evaluate();
89                          }
90                          finally
91                          {
92                              if ( directoryService.getChangeLog().getCurrentRevision() > tag.getRevision() )
93                              {
94                                  LOG.debug( "Reverting to tag: {}", tag );
95                                  directoryService.revert( tag.getRevision() );
96                              }
97                              else
98                              {
99                                  LOG.debug( "No changes made, nothing to revert" );
100                             }
101                         }
102                     }
103                 };
104             }
105             else
106             {
107                 LOG.trace( "no @CreateDS and no outer @CreateDS on: {}", description );
108                 return base;
109             }
110         }
111         else
112         {
113             return new Statement()
114             {
115                 @Override
116                 public void evaluate() throws Throwable
117                 {
118                     LOG.trace( "Creating directory service" );
119                     directoryService = DSAnnotationProcessor.getDirectoryService( description );
120                     DSAnnotationProcessor.applyLdifs( description, directoryService );
121 
122                     try
123                     {
124                         base.evaluate();
125                     }
126                     finally
127                     {
128                         LOG.trace( "Shutting down directory service" );
129                         directoryService.shutdown();
130                         FileUtils.deleteDirectory( directoryService.getInstanceLayout().getInstanceDirectory() );
131                     }
132                 }
133             };
134         }
135     }
136 }