1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.ldap.server.tools.schema;
18
19
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.FileWriter;
23 import java.io.InputStream;
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: 264732 $
38 */
39 public class AbstractTestCase extends TestCase
40 {
41 private OpenLdapSchemaParser parser;
42 private String basedir;
43
44 protected void setUp() throws Exception
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
55 {
56 super.tearDown();
57 parser = null;
58 }
59
60 protected ObjectClassLiteral[] getObjectClasses( String schemaFile )
61 throws Exception
62 {
63 InputStream 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 schemaFile )
73 throws Exception
74 {
75 InputStream 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
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( schema.getDependencies().length ) );
97 context.put( "schemaDeps", new String[] { "dep1", "dep2" } ) ;
98 context.put( "attrTypes", attributeTypes );
99
100 FileReader template = getResourceReader( "AttributeTypes.template" );
101 FileWriter 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
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( schema.getDependencies().length ) );
123 context.put( "schemaDeps", new String[] { "dep1", "dep2" } ) ;
124 context.put( "objectClasses", objectClasses );
125
126 FileReader template = getResourceReader( "ObjectClasses.template" );
127 FileWriter 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 getResourceReader( String res ) throws Exception
137 {
138 String path = getClass().getResource( res ).getFile() ;
139 return new FileReader( path );
140 }
141
142 protected boolean mkdirs( String base, String path )
143 {
144 String[] comps = path.split( "/" );
145 File file = new File( 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( file, comps[ii] );
155 if ( ! file.exists() )
156 {
157 file.mkdirs();
158 }
159 }
160
161 return file.exists();
162 }
163
164 protected FileWriter getResourceWriter( String srcBase, String pkg,
165 String classname ) throws Exception
166 {
167 mkdirs( srcBase, pkg.replace( '.', File.separatorChar ) );
168 File base = new File( srcBase );
169 String relativePath = pkg.replace( '.', File.separatorChar );
170 File dir = new File( base, relativePath );
171 return new FileWriter( new File( dir, classname + ".java" ) );
172 }
173 }