Example #1
0
 /// <summary>
 /// Validates the constraint.
 /// </summary>
 private void Validate()
 {
     // If the constraint does not validate, throw an exception.
     if (!this.IsValid)
     {
         ConstraintViolationException exception =
             new ConstraintViolationException(this.ConstraintViolationMessage);
         _logger.ErrorException(this.ConstraintViolationMessage, exception);
         throw exception;
     }
 }
Example #2
0
 public BusinessRuleErrorFeedback(ConstraintViolationException businessRule, IDataImportJobSchemaService dataImportJobSchemaService)
 {
     this.businessRule = businessRule;
     this.dataImportJobSchemaService = dataImportJobSchemaService;
     if (businessRule.ConstraintReturns != null && businessRule.ConstraintReturns.Length > 0)
     {
         dataImportJobSchemaService.Test();
         Meal meal = new Meal()
         {
             Id = 1, ServerName = "Pepe"
         };
         dataImportJobSchemaService.Test2(meal);
         var test = dataImportJobSchemaService.AProperty;
     }
 }
Example #3
0
 public void GivenCompositeWithoutConstraintsWhenInstantiatedThenUseDeclarationOnConstraint()
 {
     this.PerformTestInAppDomain(() =>
     {
         var my   = this.StructureServices.NewCompositeBuilder <MyOne>(CompositeModelType.PLAIN).Instantiate();
         var list = new List <String>();
         list.Add(LIST_ITEM);
         my.DoSomething(STRING_PARAM, list);
         ConstraintViolationException exc = Assert.Throws <ConstraintViolationException>(new TestDelegate(() =>
         {
             my.DoSomething(VIOLATING_STRING_PARAM, new List <String>());
         }), "Should have thrown a ConstraintViolationException.");
         Assert.AreEqual(2, exc.Violations.Length);
     });
 }
Example #4
0
        public void CreatingMockObjectsWithAndWithoutArguments()
        {
            var constraintReturns = new ConstraintReturn[1];

            constraintReturns[0] = new ConstraintReturn();
            Meal[] meals = new Meal[1];
            var    meal  = MockRepository.GenerateMock <Meal>();
            var    test  = new ConstraintViolationException(constraintReturns);
            // mock exception without arguments
            var constraint1 = MockRepository.GenerateMock <ConstraintViolationException>();
            // mock exception with string arguments
            var constraint2 = MockRepository.GenerateMock <ConstraintViolationException>("with string arg");
            // mock exception with an object
            var constraint3 = MockRepository.GenerateMock <ConstraintViolationException>(meal);
            // mock exception with an object
            var constraint4 = MockRepository.GenerateMock <ConstraintViolationException>(new object[] { new Meal[0] });
            // mock service
            var dataImportJobSchemaService = MockRepository.GenerateMock <IDataImportJobSchemaService>();
            // run test
            var errorFeedback = new BusinessRuleErrorFeedback <Transaction>(constraint4, dataImportJobSchemaService);

            Assert.IsNotNull(errorFeedback);
        }
            public Exception Convert(AdoExceptionContextInfo contextInfo)
            {
                Exception result = null;
                var       sqle   = ADOExceptionHelper.ExtractDbException(contextInfo.SqlException) as SqlException;

                if (sqle != null)
                {
                    switch (sqle.Number)
                    {
                    case 547:
                        result = new ConstraintViolationException(
                            sqle.Message,
                            sqle,
                            contextInfo.Sql,
                            null);
                        break;

                    case 208:
                        result = new SQLGrammarException(
                            contextInfo.Message,
                            sqle,
                            contextInfo.Sql);
                        break;

                    case 3960:
                        result = new StaleObjectStateException(
                            contextInfo.EntityName,
                            contextInfo.EntityId);
                        break;
                    }
                }

                return(result ?? SQLStateConverter.HandledNonSpecificException(
                           contextInfo.SqlException,
                           contextInfo.Message,
                           contextInfo.Sql));
            }
Example #6
0
        public void IntegrityViolation()
        {
            //ISQLExceptionConverter converter = Dialect.BuildSQLExceptionConverter();
            ISQLExceptionConverter converter = sessions.Settings.SqlExceptionConverter;

            ISession session = OpenSession();

            session.BeginTransaction();
            IDbConnection connection = session.Connection;

            // Attempt to insert some bad values into the T_MEMBERSHIP table that should
            // result in a constraint violation
            IDbCommand ps = null;

            try
            {
                ps             = connection.CreateCommand();
                ps.CommandType = CommandType.Text;
                ps.CommandText = "INSERT INTO T_MEMBERSHIP (user_id, group_id) VALUES (@p1, @p2)";
                IDbDataParameter pr = ps.CreateParameter();
                pr.ParameterName = "p1";
                pr.DbType        = DbType.Int64;
                pr.Value         = 52134241L;         // Non-existent user_id
                ps.Parameters.Add(pr);

                pr = ps.CreateParameter();
                pr.ParameterName = "p2";
                pr.DbType        = DbType.Int64;
                pr.Value         = 5342L;         // Non-existent group_id
                ps.Parameters.Add(pr);

                session.Transaction.Enlist(ps);
                ps.ExecuteNonQuery();

                Assert.Fail("INSERT should have failed");
            }
            catch (Exception sqle)
            {
                ADOExceptionReporter.LogExceptions(sqle, "Just output!!!!");
                Exception adoException = converter.Convert(new AdoExceptionContextInfo {
                    SqlException = sqle
                });
                Assert.AreEqual(typeof(ConstraintViolationException), adoException.GetType(),
                                "Bad conversion [" + sqle.Message + "]");
                ConstraintViolationException ex = (ConstraintViolationException)adoException;
                Console.WriteLine("Violated constraint name: " + ex.ConstraintName);
            }
            finally
            {
                if (ps != null)
                {
                    try
                    {
                        ps.Dispose();
                    }
                    catch (Exception)
                    {
                        // ignore...
                    }
                }
            }

            session.Transaction.Rollback();
            session.Close();
        }