/// <summary>
        /// Initializes a new instance of the TemplatingContext class.
        /// </summary>
        /// <param name="xpe"> The XML persist engine in-effect. </param>
        /// <param name="tokenizer"> The tokenizer in-efect. </param>
        /// <param name="input"> The input mechanism in-effect. </param>
        /// <param name="output"> The output mechanism in-effect. </param>
        public TemplatingContext(IXmlPersistEngine xpe, Tokenizer tokenizer, IInputMechanism input, IOutputMechanism output)
        {
            if ((object)xpe == null)
                throw new ArgumentNullException("xpe");

            if ((object)tokenizer == null)
                throw new ArgumentNullException("tokenizer");

            if ((object)input == null)
                throw new ArgumentNullException("input");

            if ((object)output == null)
                throw new ArgumentNullException("output");

            this.xpe = xpe;
            this.tokenizer = tokenizer;
            this.input = input;
            this.output = output;
        }
Exemple #2
0
        public void ShouldCreateTest()
        {
            Tokenizer tokenizer;
            Mockery mockery;
            IDictionary<string, ITokenReplacementStrategy> mockTokenReplacementStrategies;

            mockery = new Mockery();
            mockTokenReplacementStrategies = mockery.NewMock<IDictionary<string, ITokenReplacementStrategy>>();

            tokenizer = new Tokenizer(true);

            Assert.IsNotNull(tokenizer);
            Assert.IsNotNull(tokenizer.TokenReplacementStrategies);
            Assert.IsTrue(tokenizer.StrictMatching);

            tokenizer = new Tokenizer(mockTokenReplacementStrategies, true);

            Assert.IsNotNull(tokenizer);
            Assert.IsNotNull(tokenizer.TokenReplacementStrategies);
            Assert.IsTrue(tokenizer.StrictMatching);
        }
Exemple #3
0
        public void ShouldExpandTokensLooseMatchingTest()
        {
            Tokenizer tokenizer;
            Mockery mockery;
            IDictionary<string, ITokenReplacementStrategy> mockTokenReplacementStrategies;
            ITokenReplacementStrategy mockTokenReplacementStrategy;
            string tokenizedValue;
            string expandedValue;
            string expectedValue;

            mockery = new Mockery();
            mockTokenReplacementStrategies = mockery.NewMock<IDictionary<string, ITokenReplacementStrategy>>();
            mockTokenReplacementStrategy = mockery.NewMock<ITokenReplacementStrategy>();

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myValueSemanticToken"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Return.Value("testValue"));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myFunctionSemanticToken0"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { new object[] { } }).Will(Return.Value("testValue"));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myFunctionSemanticToken1"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { new object[] { "a" } }).Will(Return.Value("testValue"));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myFunctionSemanticToken2"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { new object[] { "a", "b" } }).Will(Return.Value("testValue"));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myUnkSemanticToken"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", null), Return.Value(false));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myErrSemanticToken"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Throw.Exception(new Exception()));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("a"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Return.Value(""));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("b"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Return.Value(""));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("c"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Return.Value(""));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("d"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Throw.Exception(new Exception()));

            tokenizer = new Tokenizer(mockTokenReplacementStrategies, false);

            tokenizedValue = "";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...{myNoSemanticToken}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...{myNoSemanticToken}...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myValueSemanticToken}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...testValue...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myFunctionSemanticToken0()}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...testValue...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myFunctionSemanticToken1(`a`)}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...testValue...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myFunctionSemanticToken2(`a`,  `b`)}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...testValue...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myUnkSemanticToken}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...${myUnkSemanticToken}...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myErrSemanticToken}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...${myErrSemanticToken}...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${a}...${c}...${b}...${d}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "............${d}...";
            Assert.AreEqual(expectedValue, expandedValue);

            Assert.IsNotNull(tokenizer.OrderedPreviousExpansionTokens);
            Assert.AreEqual("a,b,c,d", string.Join(",", tokenizer.OrderedPreviousExpansionTokens));

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Exemple #4
0
        public void ShouldFailOnNullTokenReplStratsCreateTest()
        {
            Tokenizer tokenizer;
            Mockery mockery;
            IDictionary<string, ITokenReplacementStrategy> mockTokenReplacementStrategies;

            mockery = new Mockery();
            mockTokenReplacementStrategies = null;

            tokenizer = new Tokenizer(mockTokenReplacementStrategies, true);
        }
        private static void WriteSqlQuery(IEnumerable<SqlQuery> sqlQueries, IAssociativeXmlObject parentAssociativeXmlObject, Type connectionType, string connectionString, bool getSchemaOnly)
        {
            ArrayConstruct arrayConstruct;
            ObjectConstruct objectConstruct;
            PropertyConstruct propertyConstruct;
            Tokenizer tokenizer;

            IList<IDictionary<string, object>> objs;
            string commandText;
            int rcecordsAffected;

            if ((object)sqlQueries == null)
                throw new ArgumentNullException("sqlQueries");

            if ((object)parentAssociativeXmlObject == null)
                throw new ArgumentNullException("parentAssociativeXmlObject");

            if ((object)connectionType == null)
                throw new ArgumentNullException("connectionType");

            if ((object)connectionString == null)
                throw new ArgumentNullException("connectionString");

            if (DataType.IsWhiteSpace(connectionString))
                throw new ArgumentOutOfRangeException("connectionString");

            tokenizer = new Tokenizer(true);

            foreach (SqlQuery sqlQuery in sqlQueries.OrderBy(c => c.Key).ThenBy(c => c.Order))
            {
                arrayConstruct = new ArrayConstruct();
                arrayConstruct.Name = sqlQuery.Key;
                parentAssociativeXmlObject.Items.Add(arrayConstruct);

                commandText = tokenizer.ExpandTokens(sqlQuery.Text, new DynamicWildcardTokenReplacementStrategy(new object[] { parentAssociativeXmlObject }));

                using (IUnitOfWorkContext unitOfWorkContext = UnitOfWorkContext.Create(connectionType, connectionString, false))
                {
                    if (!getSchemaOnly)
                        objs = unitOfWorkContext.ExecuteDictionary(sqlQuery.Type, commandText, null, out rcecordsAffected);
                    else
                        objs = unitOfWorkContext.ExecuteSchema(sqlQuery.Type, commandText, null);
                }

                if ((object)objs != null)
                {
                    propertyConstruct = new PropertyConstruct();
                    propertyConstruct.Name = "RowCount";
                    propertyConstruct.RawValue = objs.Count;
                    arrayConstruct.Items.Add(propertyConstruct);

                    foreach (IDictionary<string, object> obj in objs)
                    {
                        objectConstruct = new ObjectConstruct();
                        arrayConstruct.Items.Add(objectConstruct);

                        if ((object)obj != null)
                        {
                            foreach (KeyValuePair<string, object> keyValuePair in obj)
                            {
                                propertyConstruct = new PropertyConstruct();
                                propertyConstruct.Name = keyValuePair.Key;
                                propertyConstruct.RawValue = keyValuePair.Value;

                                objectConstruct.Items.Add(propertyConstruct);
                            }
                        }

                        // correlated
                        WriteSqlQuery(sqlQuery.SubQueries, objectConstruct, connectionType, connectionString, getSchemaOnly);
                    }
                }
            }
        }