use of org.apache.directory.api.ldap.model.schema.SyntaxChecker in project directory-ldap-api by apache.
the class SchemaManagerAddTest method testAddSyntaxCheckerWithWrongFQCN.
/**
* Test that we can't add two SyntaxCheckers with the same class code.
*/
@Test
public void testAddSyntaxCheckerWithWrongFQCN() throws Exception {
SchemaManager schemaManager = loadSystem();
int nrSize = schemaManager.getSyntaxCheckerRegistry().size();
int goidSize = schemaManager.getGlobalOidRegistry().size();
String oid = "0.0.0";
SyntaxChecker syntaxChecker = RegexSyntaxChecker.builder().setOid(oid).build();
// using java.sql.ResultSet cause it is very unlikely to get loaded
// in ADS, as the FQCN is not the one expected
syntaxChecker.setFqcn("java.sql.ResultSet");
assertFalse(schemaManager.add(syntaxChecker));
List<Throwable> errors = schemaManager.getErrors();
errors = schemaManager.getErrors();
assertEquals(1, errors.size());
assertEquals(nrSize, schemaManager.getSyntaxCheckerRegistry().size());
assertEquals(goidSize, schemaManager.getGlobalOidRegistry().size());
try {
schemaManager.lookupSyntaxCheckerRegistry(oid);
fail();
} catch (Exception e) {
// Expected
assertTrue(true);
}
}
use of org.apache.directory.api.ldap.model.schema.SyntaxChecker in project directory-ldap-api by apache.
the class SchemaManagerAddTest method testAddNewSyntaxChecker.
// =========================================================================
// SyntaxChecker addition tests
// -------------------------------------------------------------------------
@Test
public void testAddNewSyntaxChecker() throws Exception {
SchemaManager schemaManager = loadSystem();
int nrSize = schemaManager.getSyntaxCheckerRegistry().size();
int goidSize = schemaManager.getGlobalOidRegistry().size();
String oid = "0.0.0";
SyntaxChecker syntaxChecker = RegexSyntaxChecker.builder().setOid(oid).build();
assertTrue(schemaManager.add(syntaxChecker));
List<Throwable> errors = schemaManager.getErrors();
assertEquals(0, errors.size());
assertEquals(nrSize + 1, schemaManager.getSyntaxCheckerRegistry().size());
assertEquals(goidSize, schemaManager.getGlobalOidRegistry().size());
SyntaxChecker added = schemaManager.lookupSyntaxCheckerRegistry(oid);
assertNotNull(added);
assertEquals(syntaxChecker.getClass().getName(), added.getFqcn());
}
use of org.apache.directory.api.ldap.model.schema.SyntaxChecker in project directory-ldap-api by apache.
the class SchemaManagerDelTest method testDeleteExistingSyntaxCheckerUsedByRemovedSyntax.
/**
* Check that a SyntaxChecker which has been used by a deleted Syntax
* can be removed
*/
@Test
public void testDeleteExistingSyntaxCheckerUsedByRemovedSyntax() throws Exception {
SchemaManager schemaManager = loadSchema("system");
int scrSize = schemaManager.getSyntaxCheckerRegistry().size();
int srSize = schemaManager.getLdapSyntaxRegistry().size();
int goidSize = schemaManager.getGlobalOidRegistry().size();
String OID = "1.3.6.1.4.1.1466.115.121.1.33";
// Check that the S and SC are present
assertTrue(isSyntaxCheckerPresent(schemaManager, OID));
assertTrue(isSyntaxPresent(schemaManager, OID));
// Now try to remove the SC
SyntaxChecker sc = schemaManager.lookupSyntaxCheckerRegistry(OID);
// shouldn't be deleted cause there is a S associated with it
assertFalse(schemaManager.delete(sc));
List<Throwable> errors = schemaManager.getErrors();
assertFalse(errors.isEmpty());
assertTrue(errors.get(0) instanceof LdapProtocolErrorException);
// Now delete the using S : it should be OK
LdapSyntax syntax = new LdapSyntax(OID);
assertTrue(schemaManager.delete(syntax));
assertEquals(srSize - 1, schemaManager.getLdapSyntaxRegistry().size());
assertEquals(goidSize - 1, schemaManager.getGlobalOidRegistry().size());
assertFalse(isSyntaxPresent(schemaManager, OID));
// and try to delete the SC again
assertTrue(schemaManager.delete(sc));
assertFalse(isSyntaxCheckerPresent(schemaManager, OID));
assertEquals(scrSize - 1, schemaManager.getSyntaxCheckerRegistry().size());
assertEquals(goidSize - 1, schemaManager.getGlobalOidRegistry().size());
}
use of org.apache.directory.api.ldap.model.schema.SyntaxChecker in project directory-ldap-api by apache.
the class SchemaManagerDelTest method testDeleteExistingSyntaxCheckerUsedBySyntax.
@Test
public void testDeleteExistingSyntaxCheckerUsedBySyntax() throws Exception {
SchemaManager schemaManager = loadSchema("system");
int scrSize = schemaManager.getSyntaxCheckerRegistry().size();
int goidSize = schemaManager.getGlobalOidRegistry().size();
SyntaxChecker sc = schemaManager.lookupSyntaxCheckerRegistry("1.3.6.1.4.1.1466.115.121.1.1");
// FIXME should return false but is returning true
assertFalse(schemaManager.delete(sc));
List<Throwable> errors = schemaManager.getErrors();
assertFalse(errors.isEmpty());
assertTrue(errors.get(0) instanceof LdapProtocolErrorException);
assertEquals(scrSize, schemaManager.getSyntaxCheckerRegistry().size());
assertEquals(goidSize, schemaManager.getGlobalOidRegistry().size());
}
use of org.apache.directory.api.ldap.model.schema.SyntaxChecker in project directory-ldap-api by apache.
the class SchemaEntityFactory method classLoadSyntaxChecker.
/**
* Class load a syntaxChecker instance
*/
private SyntaxChecker classLoadSyntaxChecker(SchemaManager schemaManager, String oid, String className, Attribute byteCode) throws LdapException {
// Try to class load the syntaxChecker
Class<?> clazz;
SyntaxChecker syntaxChecker;
String byteCodeStr = StringConstants.EMPTY;
if (byteCode == null) {
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException cnfe) {
LOG.error(I18n.err(I18n.ERR_16048_CANNOT_FIND_SC_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16049_CANNOT_FIND_SC_CLASS, cnfe.getMessage()));
}
} else {
classLoader.setAttribute(byteCode);
try {
clazz = classLoader.loadClass(className);
} catch (ClassNotFoundException cnfe) {
LOG.error(I18n.err(I18n.ERR_16050_CANNOT_LOAD_SC_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16051_CANNOT_LOAD_SC_CLASS, cnfe.getMessage()));
}
byteCodeStr = new String(Base64.encode(byteCode.getBytes()));
}
// Create the syntaxChecker instance
try {
Method builder = clazz.getMethod("builder", null);
syntaxChecker = (SyntaxChecker) ((SCBuilder) builder.invoke(null, null)).setOid(oid).build();
} catch (NoSuchMethodException nsme) {
LOG.error(I18n.err(I18n.ERR_16052_CANNOT_INST_SC_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16053_CANNOT_INST_SC_CLASS, nsme.getMessage()));
} catch (InvocationTargetException | IllegalAccessException e) {
LOG.error(I18n.err(I18n.ERR_16054_CANNOT_ACCESS_SC_CTOR, className));
throw new LdapSchemaException(I18n.err(I18n.ERR_16055_CANNOT_ACCESS_SC_CLASS, e.getMessage()));
}
// Update the common fields
syntaxChecker.setBytecode(byteCodeStr);
syntaxChecker.setFqcn(className);
// Inject the SchemaManager for the comparator who needs it
syntaxChecker.setSchemaManager(schemaManager);
return syntaxChecker;
}
Aggregations