LDAP-240: Recursively try SUP attribute if no SYNTAX is found in AttributeDefinition.

Also re-enabled ignore SchemaToJava test case.
This commit is contained in:
Mattias Hellborg Arthursson
2013-08-15 15:06:37 +02:00
parent 2aa37a975a
commit 1f51dae6c9
3 changed files with 30 additions and 12 deletions

View File

@@ -18,5 +18,6 @@ dependencies {
testCompile project(":spring-ldap-test"),
"junit:junit:$junitVersion",
"jdepend:jdepend:2.9.1"
"jdepend:jdepend:2.9.1",
"commons-io:commons-io:2.4"
}

View File

@@ -1,15 +1,14 @@
package org.springframework.ldap.odm.tools;
import java.util.HashSet;
import java.util.Set;
import org.springframework.ldap.odm.tools.SyntaxToJavaClass.ClassInfo;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import org.springframework.ldap.odm.tools.SyntaxToJavaClass.ClassInfo;
import java.util.HashSet;
import java.util.Set;
// Processes LDAP Schema
/* package */ final class SchemaReader {
@@ -55,14 +54,28 @@ import org.springframework.ldap.odm.tools.SyntaxToJavaClass.ClassInfo;
return result;
}
private AttributeSchema createAttributeSchema(String name, DirContext schemaContext)
private AttributeSchema createAttributeSchema(String name, DirContext schemaContext)
throws NamingException, ClassNotFoundException {
// Get the schema definition
Attributes attributeSchema = schemaContext.getAttributes("AttributeDefinition/" + name);
// Get the syntax - ditching any trailing length value that { and whatever follows it
String syntax = ((String)attributeSchema.get("SYNTAX").get()).split("\\{")[0];
String syntax = null;
while(syntax == null) {
Attribute syntaxAttribute = attributeSchema.get("SYNTAX");
if(syntaxAttribute != null) {
syntax = ((String)syntaxAttribute.get()).split("\\{")[0];
} else {
// Try to recursively retrieve syntax for super definition.
Attribute supAttribute = attributeSchema.get("SUP");
if(supAttribute == null) {
// Well, at least we tried
throw new IllegalArgumentException("Unable to get syntax definition for attribute " + name);
} else {
attributeSchema = schemaContext.getAttributes("AttributeDefinition/" + supAttribute.get());
}
}
}
// Is it binary?
boolean isBinary=binarySet.contains(syntax);

View File

@@ -1,12 +1,12 @@
package org.springframework.ldap.odm.test;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ldap.core.DistinguishedName;
@@ -129,19 +129,21 @@ public final class TestSchemaToJava {
// 4) Use this OdmManager to read an entry from LDAP and check the results.
//
@Test
@Ignore
public void generate() throws Exception {
final String className="Person";
final String packageName="org.springframework.ldap.odm.testclasses";
File tempFile = File.createTempFile("test-odm-syntax-to-class-map", ".txt");
FileUtils.copyInputStreamToFile(new ClassPathResource("/syntax-to-class-map.txt").getInputStream(), tempFile);
// Add classes dir to class path - needed for compilation
System.setProperty("java.class.path",
System.getProperty("java.class.path")+File.pathSeparator+"target/classes");
String[] flags=new String[] {
"--url", "ldap://127.0.0.1:"+port,
"--objectclasses", "inetorgperson",
"--syntaxmap", "target/test-classes/syntax-to-class-map.txt",
"--objectclasses", "organizationalperson",
"--syntaxmap", tempFile.getAbsolutePath(),
"--class", className,
"--package", packageName,
"--outputdir", tempDir };
@@ -149,6 +151,8 @@ public final class TestSchemaToJava {
// Generate the code using SchemaToJava
SchemaToJava.main(flags);
tempFile.delete();
// Java 5 - we'll use the Java 6 Compiler API once we can drop support for Java 5.
String javaDir = calculateOutputDirectory(tempDir, packageName);