KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ldap > server > tools > schema > AbstractTestCase


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package org.apache.ldap.server.tools.schema;
18
19
20 import java.io.File JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.apache.ldap.server.schema.bootstrap.BootstrapSchema;
28 import org.apache.ldap.server.schema.bootstrap.ProducerTypeEnum;
29 import org.apache.velocity.VelocityContext;
30 import org.apache.velocity.app.Velocity;
31
32
33 /**
34  * An abstract test case that incorporates both the parser and code generators.
35  *
36  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
37  * @version $Rev: 169198 $
38  */

39 public class AbstractTestCase extends TestCase
40 {
41     private OpenLdapSchemaParser parser;
42     private String JavaDoc basedir;
43
44     protected void setUp() throws Exception JavaDoc
45     {
46         super.setUp();
47
48         basedir = System.getProperty( "basedir", "." );
49
50         parser = new OpenLdapSchemaParser();
51         parser.setParserMonitor( new ConsoleParserMonitor() );
52     }
53
54     protected void tearDown() throws Exception JavaDoc
55     {
56         super.tearDown();
57         parser = null;
58     }
59
60     protected ObjectClassLiteral[] getObjectClasses( String JavaDoc schemaFile )
61         throws Exception JavaDoc
62     {
63         InputStream JavaDoc in = getClass().getResourceAsStream( schemaFile );
64         parser.parse( in );
65         int size = parser.getObjectClassTypes().size();
66         ObjectClassLiteral[] objectClasses = new ObjectClassLiteral[size];
67         objectClasses = ( ObjectClassLiteral[] )
68             parser.getObjectClassTypes().values().toArray( objectClasses );
69         return objectClasses;
70     }
71
72     protected AttributeTypeLiteral[] getSchemaAttributes( String JavaDoc schemaFile )
73         throws Exception JavaDoc
74     {
75         InputStream JavaDoc in = getClass().getResourceAsStream( schemaFile );
76         parser.parse( in );
77         int size = parser.getAttributeTypes().size();
78         AttributeTypeLiteral[] attributeTypes = new AttributeTypeLiteral[size];
79         attributeTypes = ( AttributeTypeLiteral[] )
80             parser.getAttributeTypes().values().toArray( attributeTypes );
81         return attributeTypes;
82     }
83
84     protected void generateAttributeTypeProducer( BootstrapSchema schema )
85         throws Exception JavaDoc
86     {
87         AttributeTypeLiteral[] attributeTypes =
88             getSchemaAttributes( schema.getSchemaName() + ".schema" );
89
90         VelocityContext context = new VelocityContext();
91         context.put( "package", schema.getPackageName() );
92         context.put( "classname",
93             schema.getUnqualifiedClassName( ProducerTypeEnum.ATTRIBUTE_TYPE_PRODUCER ) );
94         context.put( "schema", schema.getSchemaName() );
95         context.put( "owner", schema.getOwner() ) ;
96         context.put( "schemaDepCount", new Integer JavaDoc( schema.getDependencies().length ) );
97         context.put( "schemaDeps", new String JavaDoc[] { "dep1", "dep2" } ) ;
98         context.put( "attrTypes", attributeTypes );
99
100         FileReader JavaDoc template = getResourceReader( "AttributeTypes.template" );
101         FileWriter JavaDoc writer = getResourceWriter( basedir + "/target/schema",
102             schema.getPackageName(),
103             schema.getUnqualifiedClassName( ProducerTypeEnum.ATTRIBUTE_TYPE_PRODUCER ) );
104         Velocity.init();
105         Velocity.evaluate( context, writer, "LOG", template );
106         writer.flush();
107         writer.close();
108     }
109
110     protected void generateObjectClassProducer( BootstrapSchema schema )
111         throws Exception JavaDoc
112     {
113         ObjectClassLiteral[] objectClasses =
114             getObjectClasses( schema.getSchemaName() + ".schema" );
115
116         VelocityContext context = new VelocityContext();
117         context.put( "package", schema.getPackageName() );
118         context.put( "classname",
119             schema.getUnqualifiedClassName( ProducerTypeEnum.OBJECT_CLASS_PRODUCER ) );
120         context.put( "schema", schema.getSchemaName() );
121         context.put( "owner", schema.getOwner() ) ;
122         context.put( "schemaDepCount", new Integer JavaDoc( schema.getDependencies().length ) );
123         context.put( "schemaDeps", new String JavaDoc[] { "dep1", "dep2" } ) ;
124         context.put( "objectClasses", objectClasses );
125
126         FileReader JavaDoc template = getResourceReader( "ObjectClasses.template" );
127         FileWriter JavaDoc writer = getResourceWriter( basedir + "/target/schema",
128             schema.getPackageName(),
129             schema.getUnqualifiedClassName( ProducerTypeEnum.OBJECT_CLASS_PRODUCER ) );
130         Velocity.init();
131         Velocity.evaluate( context, writer, "LOG", template );
132         writer.flush();
133         writer.close();
134     }
135
136     protected FileReader JavaDoc getResourceReader( String JavaDoc res ) throws Exception JavaDoc
137     {
138         String JavaDoc path = getClass().getResource( res ).getFile() ;
139         return new FileReader JavaDoc( path );
140     }
141
142     protected boolean mkdirs( String JavaDoc base, String JavaDoc path )
143     {
144         String JavaDoc[] comps = path.split( "/" );
145         File JavaDoc file = new File JavaDoc( base );
146
147         if ( ! file.exists() )
148         {
149             file.mkdirs();
150         }
151
152         for ( int ii = 0; ii < comps.length; ii++ )
153         {
154             file = new File JavaDoc( file, comps[ii] );
155             if ( ! file.exists() )
156             {
157                 file.mkdirs();
158             }
159         }
160
161         return file.exists();
162     }
163
164     protected FileWriter JavaDoc getResourceWriter( String JavaDoc srcBase, String JavaDoc pkg,
165                                             String JavaDoc classname ) throws Exception JavaDoc
166     {
167         mkdirs( srcBase, pkg.replace( '.', File.separatorChar ) );
168         File JavaDoc base = new File JavaDoc( srcBase );
169         String JavaDoc relativePath = pkg.replace( '.', File.separatorChar );
170         File JavaDoc dir = new File JavaDoc( base, relativePath );
171         return new FileWriter JavaDoc( new File JavaDoc( dir, classname + ".java" ) );
172     }
173 }
174
Popular Tags