MvelIntegrationTest.java :  » Development » iScreen » org » iscreen » mvel » Java Open Source

Java Open Source » Development » iScreen 
iScreen » org » iscreen » mvel » MvelIntegrationTest.java
/*
 * Copyright 2007 Dan Shellman
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.iscreen.mvel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import junit.framework.TestCase;

import org.iscreen.MockBean;
import org.iscreen.ValidationException;
import org.iscreen.ValidationFactory;
import org.iscreen.ValidationFactoryConfig;
import org.iscreen.ValidationFailure;
import org.iscreen.ValidationService;
import org.iscreen.ValidationServiceWrapper;
import org.iscreen.impl.DefaultValidationService;
import org.iscreen.MockValidator;
import org.iscreen.validators.NullValidator;

/**
 * This set of unit tests are designed to test integration of
 * the XML configuration together with the underlying object model.
 *
 * @author Shellman, Dan
 */
public class MvelIntegrationTest extends TestCase
{
  public MvelIntegrationTest( String name )
  {
    super( name );
  } //end MvelIntegrationTest()

  /**
   * Tests the use of a simple validation.
   */
  public void testSimpleStringValidation()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.simple_string" );
    assertNotNull( service );

    try
    {
      service.validate( "this is way too long" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
    }
  } //end testSimpleStringValidation()

  /**
   * Tests to verify that if there's no mapping, the default mapping is used.
   */
  public void testNoMapping()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.no_mapping" );
    assertNotNull( service );

    try
    {
      service.validate( null );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
    }
  } //end testNoMapping()

  /**
   * Tests the fail fast feature on a validation set.
   */
  public void testFailFastOnSet()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.fail_fast" );
    assertNotNull( service );

    try
    {
      service.validate( "too short and too long" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( "Expected only one validation failure.",
                    1,
                    e.getFailures().size() );
    }
  } //end testFailFastOnSet()

  /**
   * Tests the default values on the mapping element for a use-validator.
   */
  public void testDefaultMappingsOnSet()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.default_mapping" );
    assertNotNull( service );

    try
    {
      service.validate( "this is way too long" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
    }
  } //end testDefaultMappingsOnSet()

  /**
   * Tests the setting of a service on a validator.
   */
  public void testSettingService()
  {
    ValidationFactory             factory;
    ValidationService             service;
    HashMap                       serviceMap = new HashMap();
    List                          wrappers;
    MvelConfiguredValidator       validatorWrapper;
    MockValidator                 validator;

    serviceMap.put( "mockService", "the service" );
    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              serviceMap );
    service = factory.getValidationService( "org.iscreen.test.service_test" );
    assertNotNull( service );

    wrappers = ( ( DefaultValidationService ) service ).getAllWrappers();
    validatorWrapper = ( MvelConfiguredValidator ) wrappers.get( 0 );
    validator = ( MockValidator ) validatorWrapper.getConfiguredValidator();

    assertEquals( "The service was not set properly.",
                  "the service",
                  validator.getTestService() );
  } //end testSettingService()

  /**
   * Tests the setting of failure objects and their proper usage.
   */
  public void testSettingFailure()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.failure_test" );
    assertNotNull( service );

    try
    {
      service.validate( "this is way too long" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      String                      failure;

      failure = ( ( ValidationFailure ) e.getFailures().get( 0 ) ).getMessage();
      assertEquals( "Failure message is invalid.",
                    "Failure with label My Test and max characters of 10.",
                    failure );
    }
  } //end testSettingFailure()

  /**
   * Tests the usage of an embedded validation set within a set.  This
   * test will not use any 'if', 'iterate', or 'mapping' settings.
   */
  public void testIncludeValidationSet()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.include_set_test" );
    assertNotNull( service );

    try
    {
      service.validate( "this is way too long" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
    }
  } //end testIncludeValidationSet()

  /**
   * Tests the usage of an embedded validation set within a set.  This
   * test will use the 'iterate' setting.
   */
  public void testIterateValidationSet()
  {
    ValidationFactory             factory;
    ValidationService             service;
    List                          strings = new ArrayList();

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.set_iterate_test" );
    assertNotNull( service );

    strings.add( "this is too long 1" );
    strings.add( "this is too long 2" );
    strings.add( "this is too long 3" );
    strings.add( "this is too long 4" );

    try
    {
      service.validate( strings );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( 4, e.getFailureMessages().size() );
    }
  } //end testIterateValidationSet()

  /**
   * Tests the usage of an embedded validation set within a set.  This
   * test will use the 'if' setting.
   */
  public void testIfValidationSet()
  {
    ValidationFactory             factory;
    ValidationService             service;
    MockBean                      bean = new MockBean();

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.set_if_test" );
    assertNotNull( service );

    bean.setSomeFlag( false );
    bean.setSomeString( "this is way too long" );

    try
    {
      service.validate( bean );
    }
    catch ( ValidationException e )
    {
      fail( "Expected no validation failures!" );
    }
  } //end testIfValidationSet()

  /**
   * Tests the usage of an embedded validation set within a set.  This
   * test will use the 'map' setting.
   */
  public void testMapValidationSet()
  {
    ValidationFactory             factory;
    ValidationService             service;
    MockBean                      bean = new MockBean();

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.set_map_test" );
    assertNotNull( service );

    bean.setSomeFlag( true );
    bean.setSomeString( "this is way too long" );

    try
    {
      service.validate( bean );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
    }
  } //end testMapValidationSet()

  /**
   * Tests the usage of an embedded validation set within a set.  This
   * test will use the 'if', 'iterate', and 'map' settings.
   */
  public void testIfIterateMapValidationSet()
  {
    ValidationFactory             factory;
    ValidationService             service;
    List                          strings = new ArrayList();
    MockBean                      bean = new MockBean();

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.set_if_it_map_test" );
    assertNotNull( service );

    strings.add( "this is way too long 1" );
    strings.add( "short" );
    strings.add( "very, very, very, very long" );

    bean.setSomeFlag( true );
    bean.setSomeString( "this is way too long" );
    bean.setSomeObject( strings );

    try
    {
      service.validate( bean );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( 2, e.getFailureMessages().size() );
    }
  } //end testIfIterateMapValidationSet()

  /**
   * Tests the usage of an embedded validation set within a set.  This
   * test will use the 'fail-fast' setting.
   */
  public void testFailFastOnValidationSet()
  {
    ValidationFactory             factory;
    ValidationService             service;
    List                          strings = new ArrayList();

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.set_failfast_test" );
    assertNotNull( service );

    strings.add( "this is too long 1" );
    strings.add( "this is too long 2" );
    strings.add( "this is too long 3" );
    strings.add( "this is too long 4" );

    try
    {
      service.validate( strings );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( 4, e.getFailureMessages().size() );
    }
  } //end testFailFastOnValidationSet()

  /**
   * Tests the usage of an embedded validation set within a set.  This
   * test will use the 'fail-fast' setting within a validator that's within
   * an embedded set.
   */
  public void testFailFastOnEmbeddedValidator()
  {
    ValidationFactory             factory;
    ValidationService             service;
    MockBean                      bean = new MockBean();

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.set_embedded_failfast_test" );
    assertNotNull( service );

    bean.setSomeString( "this is way too long" );

    try
    {
      service.validate( bean );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( 2, e.getFailureMessages().size() );
    }
  } //end testFailFastOnEmbeddedValidator()

  /**
   * Tests to ensure that the failure message is reported correctly (that is,
   * that the text of the message is correct).
   */
  public void testFailureMessage()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.simple_string" );
    assertNotNull( service );

    try
    {
      service.validate( "this is way too long" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( "My Test must not be greater than 10 characters long.",
                    e.getFailureMessages().get( 0 ) );
    }
  } //end testFailureMessage()

  /**
   * Tests to ensure that the failure message has the appropriate name.
   */
  public void testFailureMessageName()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.simple_string" );
    assertNotNull( service );

    try
    {
      service.validate( "this is way too long" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( "testField",
                    ( ( ValidationFailure ) e.getFailures().get( 0 ) ).getName() );
    }
  } //end testFailureMessageName()

  /**
   * Tests to ensure that the failure message has the appropriate name.
   */
  public void testFailureMessageNameFromSet()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.set_call" );
    assertNotNull( service );

    try
    {
      service.validate( "this is way too long" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( "validationField",
                    ( ( ValidationFailure ) e.getFailures().get( 0 ) ).getName() );
    }
  } //end testFailureMessageNameFromSet()

  /**
   * Tests to ensure that the failure message has the appropriate name.
   */
  public void testFailureMessageNameFromSetNoName()
  {
    ValidationFactory             factory;
    ValidationService             service;

    factory = ValidationFactory.buildFactory( ValidationFactory.FACTORY_MVEL_XML,
                                              "org/iscreen/mvel/integration_test.xml",
                                              new HashMap() );
    service = factory.getValidationService( "org.iscreen.test.set_call_no_name" );
    assertNotNull( service );

    try
    {
      service.validate( "this is way too long" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( "testField",
                    ( ( ValidationFailure ) e.getFailures().get( 0 ) ).getName() );
    }
  } //end testFailureMessageNameFromSetNoName()

  /**
   * Tests the email address validator to ensure that it catches and reports
   * failures properly.
   */
  public void testEmailAddressValidatorFailure()
  {
    ValidationFactoryConfig       factory;
    ValidationService             service;

    factory = new ValidationFactoryConfig( "org/iscreen/mvel/integration_test.xml" );
    service = new ValidationServiceWrapper( factory, "org.iscreen.test.email_validation" );

    try
    {
      service.validate( "this.is.an_invalid@email.address@com" );
      fail( "Expected validation failure!" );
    }
    catch ( ValidationException e )
    {
      assertEquals( "The address this.is.an_invalid@email.address@com given for the label is not in a valid email address format.",
                    e.getFailureMessages().get( 0 ) );
    }
  } //end testEmailAddressValidatorFailure()

  /**
   * Tests the email address validator to ensure that a valid email address
   * is accepted and not reported.
   */
  public void testEmailAddressValidatorSuccess()
  {
    ValidationFactoryConfig       factory;
    ValidationService             service;

    factory = new ValidationFactoryConfig( "org/iscreen/mvel/integration_test.xml" );
    service = new ValidationServiceWrapper( factory, "org.iscreen.test.email_validation" );

    try
    {
      service.validate( "this.is.an_invalid@email.address.com" );
    }
    catch ( ValidationException e )
    {
      fail( "No failure should have been reported: " + e.getFailureMessages().get( 0 ) );
    }
  } //end testEmailAddressValidatorSuccess()

  /**
   * Tests to ensure that a mapping is inherited from a configured Validator
   * to a validator reference within a validation set.
   */
  public void testMappingInheritance()
  {
    ValidationFactoryConfig       factory;
    ValidationService             service;
    MockBean                      bean = new MockBean();

    factory = new ValidationFactoryConfig( "org/iscreen/mvel/integration_test.xml" );
    service = new ValidationServiceWrapper( factory, "org.iscreen.test.inherited_mapping" );

    bean.setSomeString( "this is valid" );

    try
    {
      service.validate( bean );
    }
    catch ( ValidationException e )
    {
      fail( "No failure should have been reported: " + e.getFailureMessages().get( 0 ) );
    }
  } //end testMappingInheritance()

  /**
   * Tests the use of internationalization in failure message and the locale
   * provided during call to the validate() method.
   */
  public void testLocalizedFailureMessage()
  {
    ValidationFactoryConfig       factory;
    ValidationService             service;

    factory = new ValidationFactoryConfig( "org/iscreen/mvel/integration_test.xml" );
    service = new ValidationServiceWrapper( factory, "org.iscreen.test.international_test" );

    try
    {
      service.validate( null, Locale.FRENCH );
      fail( "Failure should have been reported." );
    }
    catch ( ValidationException e )
    {
      assertEquals( "French Test Value 2",
                    e.getFailureMessages().get( 0 ) );
    }
  } //end testLocalizedFailureMessage()

  /**
   * Tests the use of internationalization in failure message and the locale
   * provided during call to the validate() method.
   */
  public void testLocalizedFailureMessageWithFactory()
  {
    ValidationFactoryConfig       factory;
    ValidationService             service;

    factory = new ValidationFactoryConfig( "org/iscreen/mvel/integration_test.xml" );
    factory.setDefaultLocale( Locale.FRENCH );
    service = new ValidationServiceWrapper( factory, "org.iscreen.test.international_test" );

    try
    {
      service.validate( null );
      fail( "Failure should have been reported." );
    }
    catch ( ValidationException e )
    {
      assertEquals( "French Test Value 2",
                    e.getFailureMessages().get( 0 ) );
    }
  } //end testLocalizedFailureMessageWithFactory()
} //end MvelIntegrationTest
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.