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   */
20  package org.apache.directory.server.core.entry;
21  
22  import java.util.Comparator;
23  
24  import javax.naming.NamingException;
25  import javax.naming.directory.InvalidAttributeValueException;
26  
27  import org.apache.directory.shared.ldap.schema.AbstractAttributeType;
28  import org.apache.directory.shared.ldap.schema.AbstractMatchingRule;
29  import org.apache.directory.shared.ldap.schema.AbstractSyntax;
30  import org.apache.directory.shared.ldap.schema.AttributeType;
31  import org.apache.directory.shared.ldap.schema.ByteArrayComparator;
32  import org.apache.directory.shared.ldap.schema.DeepTrimToLowerNormalizer;
33  import org.apache.directory.shared.ldap.schema.MatchingRule;
34  import org.apache.directory.shared.ldap.schema.Normalizer;
35  import org.apache.directory.shared.ldap.schema.Syntax;
36  import org.apache.directory.shared.ldap.schema.syntax.SyntaxChecker;
37  import org.apache.directory.shared.ldap.util.StringTools;
38  
39  /**
40   * Some common declaration used by the serverEntry tests.
41   *
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   * @version $Rev$, $Date$
44   */
45  public class TestServerEntryUtils
46  {
47      /**
48       * A local Syntax class for tests
49       */
50      static class AT extends AbstractAttributeType
51      {
52          private static final long serialVersionUID = 0L;
53          AttributeType superior;
54          Syntax syntax;
55          MatchingRule equality;
56          MatchingRule ordering;
57          MatchingRule substr;
58  
59          protected AT( String oid )
60          {
61              super( oid );
62          }
63  
64          public AttributeType getSuperior() throws NamingException
65          {
66              return superior;
67          }
68  
69  
70          public Syntax getSyntax() throws NamingException
71          {
72              return syntax;
73          }
74  
75  
76          public MatchingRule getEquality() throws NamingException
77          {
78              return equality;
79          }
80  
81  
82          public MatchingRule getOrdering() throws NamingException
83          {
84              return ordering;
85          }
86  
87  
88          public MatchingRule getSubstr() throws NamingException
89          {
90              return substr;
91          }
92  
93  
94          public void setSuperior( AttributeType superior )
95          {
96              this.superior = superior;
97          }
98  
99  
100         public void setSyntax( Syntax syntax )
101         {
102             this.syntax = syntax;
103         }
104 
105 
106         public void setEquality( MatchingRule equality )
107         {
108             this.equality = equality;
109         }
110 
111 
112         public void setOrdering( MatchingRule ordering )
113         {
114             this.ordering = ordering;
115         }
116 
117 
118         public void setSubstr( MatchingRule substr )
119         {
120             this.substr = substr;
121         }
122     }
123 
124     /**
125      * A local MatchingRule class for tests
126      */
127     static class MR extends AbstractMatchingRule
128     {
129         private static final long serialVersionUID = 0L;
130         Syntax syntax;
131         Comparator comparator;
132         Normalizer normalizer;
133 
134         protected MR( String oid )
135         {
136             super( oid );
137         }
138 
139         public Syntax getSyntax() throws NamingException
140         {
141             return syntax;
142         }
143 
144         public Comparator getComparator() throws NamingException
145         {
146             return comparator;
147         }
148 
149 
150         public Normalizer getNormalizer() throws NamingException
151         {
152             return normalizer;
153         }
154 
155 
156         public void setSyntax( Syntax syntax )
157         {
158             this.syntax = syntax;
159         }
160 
161 
162         public void setComparator( Comparator<?> comparator )
163         {
164             this.comparator = comparator;
165         }
166 
167 
168         public void setNormalizer( Normalizer normalizer )
169         {
170             this.normalizer = normalizer;
171         }
172     }
173 
174 
175     /**
176      * A local Syntax class used for the tests
177      */
178     static class S extends AbstractSyntax
179     {
180         private static final long serialVersionUID = 0L;
181         SyntaxChecker checker;
182 
183         public S( String oid, boolean humanReadible )
184         {
185             super( oid, humanReadible );
186         }
187 
188         public void setSyntaxChecker( SyntaxChecker checker )
189         {
190             this.checker = checker;
191         }
192 
193         public SyntaxChecker getSyntaxChecker() throws NamingException
194         {
195             return checker;
196         }
197     }
198 
199     /* no protection*/ static AttributeType getCaseIgnoringAttributeNoNumbersType()
200     {
201         S s = new S( "1.1.1.1", true );
202 
203         s.setSyntaxChecker( new SyntaxChecker()
204         {
205             public String getSyntaxOid()
206             {
207                 return "1.1.1.1";
208             }
209             public boolean isValidSyntax( Object value )
210             {
211                 if ( !( value instanceof String ) )
212                 {
213                     return false;
214                 }
215 
216                 String strval = ( String ) value;
217                 
218                 for ( char c:strval.toCharArray() )
219                 {
220                     if ( Character.isDigit( c ) )
221                     {
222                         return false;
223                     }
224                 }
225                 return true;
226             }
227 
228             public void assertSyntax( Object value ) throws NamingException
229             {
230                 if ( ! isValidSyntax( value ) )
231                 {
232                     throw new InvalidAttributeValueException();
233                 }
234             }
235         } );
236 
237         final MR mr = new MR( "1.1.2.1" );
238         mr.syntax = s;
239         mr.comparator = new Comparator<String>()
240         {
241             public int compare( String o1, String o2 )
242             {
243                 return ( o1 == null ? 
244                     ( o2 == null ? 0 : -1 ) :
245                     ( o2 == null ? 1 : o1.compareTo( o2 ) ) );
246             }
247 
248             int getValue( String val )
249             {
250                 if ( val.equals( "LOW" ) ) 
251                 {
252                     return 0;
253                 }
254                 else if ( val.equals( "MEDIUM" ) ) 
255                 {
256                     return 1;
257                 }
258                 else if ( val.equals( "HIGH" ) ) 
259                 {
260                     return 2;
261                 }
262                 
263                 throw new IllegalArgumentException( "Not a valid value" );
264             }
265         };
266         
267         mr.normalizer = new Normalizer()
268         {
269             private static final long serialVersionUID = 1L;
270 
271             public Object normalize( Object value ) throws NamingException
272             {
273                 if ( value instanceof String )
274                 {
275                     return ( ( String ) value ).toLowerCase();
276                 }
277 
278                 throw new IllegalStateException( "expected string to normalize" );
279             }
280         };
281         
282         AT at = new AT( "1.1.3.1" );
283         at.setEquality( mr );
284         at.setSyntax( s );
285         return at;
286     }
287 
288 
289     /* no protection*/ static AttributeType getIA5StringAttributeType()
290     {
291         AT at = new AT( "1.1" );
292 
293         S s = new S( "1.1.1", true );
294 
295         s.setSyntaxChecker( new SyntaxChecker()
296         {
297             public String getSyntaxOid()
298             {
299                 return "1.1.1";
300             }
301             public boolean isValidSyntax( Object value )
302             {
303                 return ((String)value == null) || (((String)value).length() < 7) ;
304             }
305 
306             public void assertSyntax( Object value ) throws NamingException
307             {
308                 if ( ! isValidSyntax( value ) )
309                 {
310                     throw new InvalidAttributeValueException();
311                 }
312             }
313         } );
314 
315         final MR mr = new MR( "1.1.2" );
316         mr.syntax = s;
317         mr.comparator = new Comparator<String>()
318         {
319             public int compare( String o1, String o2 )
320             {
321                 return ( ( o1 == null ) ? 
322                     ( o2 == null ? 0 : -1 ) :
323                     ( o2 == null ? 1 : o1.compareTo( o2 ) ) );
324             }
325         };
326         
327         mr.normalizer = new DeepTrimToLowerNormalizer();
328         
329         at.setEquality( mr );
330         at.setSyntax( s );
331         return at;
332     }
333 
334 
335     /* No protection */ static AttributeType getBytesAttributeType()
336     {
337         AT at = new AT( "1.2" );
338 
339         S s = new S( "1.2.1", true );
340 
341         s.setSyntaxChecker( new SyntaxChecker()
342         {
343             public String getSyntaxOid()
344             {
345                 return "1.2.1";
346             }
347             public boolean isValidSyntax( Object value )
348             {
349                 return ( value == null ) || ( ((byte[])value).length < 5 );
350             }
351 
352             public void assertSyntax( Object value ) throws NamingException
353             {
354                 if ( ! isValidSyntax( value ) )
355                 {
356                     throw new InvalidAttributeValueException();
357                 }
358             }
359         } );
360 
361         final MR mr = new MR( "1.2.2" );
362         mr.syntax = s;
363         mr.comparator = new Comparator<byte[]>()
364         {
365             public int compare( byte[] o1, byte[] o2 )
366             {
367                 return ( ( o1 == null ) ? 
368                     ( o2 == null ? 0 : -1 ) :
369                     ( o2 == null ? 1 : ByteArrayComparator.INSTANCE.compare( o1, o2 ) ) );
370             }
371         };
372         
373         mr.normalizer = new Normalizer()
374         {
375             private static final long serialVersionUID = 1L;
376             
377             public Object normalize( Object value ) throws NamingException
378             {
379                 if ( value instanceof byte[] )
380                 {
381                     byte[] val = (byte[])value;
382                     // each byte will be changed to be > 0, and spaces will be trimmed
383                     byte[] newVal = new byte[ val.length ];
384                     int i = 0;
385                     
386                     for ( byte b:val )
387                     {
388                         newVal[i++] = (byte)(b & 0x007F); 
389                     }
390                     
391                     return StringTools.trim( newVal );
392                 }
393 
394                 throw new IllegalStateException( "expected byte[] to normalize" );
395             }
396         };
397         
398         at.setEquality( mr );
399         at.setSyntax( s );
400         return at;
401     }
402 }