Search in sources :

Example 16 with SyntaxChecker

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);
    }
}
Also used : OctetStringSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker) RegexSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.RegexSyntaxChecker) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) IOException(java.io.IOException) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test)

Example 17 with SyntaxChecker

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());
}
Also used : OctetStringSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker) RegexSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.RegexSyntaxChecker) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) Test(org.junit.Test)

Example 18 with SyntaxChecker

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());
}
Also used : BooleanSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.BooleanSyntaxChecker) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) LdapProtocolErrorException(org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException) LdapSyntax(org.apache.directory.api.ldap.model.schema.LdapSyntax) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) Test(org.junit.Test)

Example 19 with SyntaxChecker

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());
}
Also used : BooleanSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.BooleanSyntaxChecker) SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) LdapProtocolErrorException(org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) Test(org.junit.Test)

Example 20 with SyntaxChecker

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;
}
Also used : SyntaxChecker(org.apache.directory.api.ldap.model.schema.SyntaxChecker) LdapSchemaException(org.apache.directory.api.ldap.model.exception.LdapSchemaException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

SyntaxChecker (org.apache.directory.api.ldap.model.schema.SyntaxChecker)24 LdapSyntax (org.apache.directory.api.ldap.model.schema.LdapSyntax)9 Test (org.junit.Test)9 MutableAttributeType (org.apache.directory.api.ldap.model.schema.MutableAttributeType)7 MutableMatchingRule (org.apache.directory.api.ldap.model.schema.MutableMatchingRule)7 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)7 DefaultSchemaManager (org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)7 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)6 PrepareString (org.apache.directory.api.ldap.model.schema.PrepareString)5 DeepTrimToLowerNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.DeepTrimToLowerNormalizer)5 OctetStringSyntaxChecker (org.apache.directory.api.ldap.model.schema.syntaxCheckers.OctetStringSyntaxChecker)5 Normalizer (org.apache.directory.api.ldap.model.schema.Normalizer)4 BooleanSyntaxChecker (org.apache.directory.api.ldap.model.schema.syntaxCheckers.BooleanSyntaxChecker)4 LdapProtocolErrorException (org.apache.directory.api.ldap.model.exception.LdapProtocolErrorException)3 LdapSchemaException (org.apache.directory.api.ldap.model.exception.LdapSchemaException)3 LdapUnwillingToPerformException (org.apache.directory.api.ldap.model.exception.LdapUnwillingToPerformException)3 NoOpNormalizer (org.apache.directory.api.ldap.model.schema.normalizers.NoOpNormalizer)3 RegexSyntaxChecker (org.apache.directory.api.ldap.model.schema.syntaxCheckers.RegexSyntaxChecker)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2