コード例 #1
0
        //Overrides
        public override NodeValue CreateValue()
        {
            // TODO: consider adding some random version of this; determinism is now required because
            // this is used by check-in tests.
            TypeData typeData = TypeData.FindForType(this.ClrType);

            return(new NodeValue(typeData.NonNullValue, this));
        }
コード例 #2
0
        //Overrides
        public virtual NodeValue CreateRandomValue()
        {
            // TODO: consider adding some random version of this; determinism is now required because
            // this is used by check-in tests.
            TypeData typeData = TypeData.FindForType(this.ClrType);
            int      index    = AstoriaTestProperties.Random.Next(typeData.SampleValues.Length);

            return(new NodeValue(typeData.SampleValues[index], this));
        }
コード例 #3
0
ファイル: UriQueryBuilder.cs プロジェクト: zhonli/odata.net
        protected override String Visit(ExpNode caller, ExpNode node)
        {
            if (node is ProjectExpression)
            {
                ProjectExpression e = (ProjectExpression)node;
                if (e.Projections.Count == 0)
                {
                    return(String.Format("{0}",
                                         this.Visit(e, e.Input)
                                         ));
                }
                else if (e.Projections.Count == 1)
                {
                    if (e.Projections[0] is PropertyExpression)
                    {
                        return(String.Format("{0}/{1}",
                                             this.Visit(e, e.Input),
                                             this.Visit(e, e.Projections[0])
                                             ));
                    }
                }
                else
                {
                    throw new Exception("More than 1 projection, invalid");
                }
            }
            else if (node is ServiceOperationExpression)
            {
                AstoriaTestLog.WriteLine("Calling Service Operation");
                ServiceOperationExpression e = (ServiceOperationExpression)node;

                string serviceOp = this.Visit(e, e.Input);
                AstoriaTestLog.WriteLine(serviceOp);
                StringBuilder sbParametersString = new StringBuilder();
                if (!serviceOp.Contains("?"))
                {
                    sbParametersString.Append("?");
                }
                for (int index = 0; index < e.Arguments.Length; index++)
                {
                    ServiceOperationParameterExpression parameter = e.Arguments[index];
                    if (index > 0)
                    {
                        sbParametersString.Append("&");
                    }
                    string strLiteralString = CreateKeyStringValue(parameter.ParameterValue);
                    sbParametersString.AppendFormat("{0}={1}", parameter.ParameterName, strLiteralString);
                }
                return(String.Format("{0}{1}", serviceOp, sbParametersString.ToString()));
            }
            else if (node is ScanExpression)
            {
                ScanExpression e = (ScanExpression)node;

                if (e.Input is VariableExpression && ((VariableExpression)e.Input).Variable is ResourceContainer)
                {
                    return(String.Format("{0}",
                                         this.Visit(e, e.Input)
                                         ));
                }
                throw new Exception("Unsupported on in scan expression");
            }
            else if (node is PredicateExpression)
            {
                PredicateExpression e   = (PredicateExpression)node;
                KeyExpression       key = e.Predicate as KeyExpression;
                if (key != null)
                {
                    return(String.Format("{0}({1})",
                                         this.Visit(e, e.Input),
                                         this.Visit(e, e.Predicate)
                                         ));
                }
                else
                {
                    return(String.Format("{0}?$filter={1}",
                                         this.Visit(e, e.Input),
                                         this.Visit(e, e.Predicate)
                                         ));
                }
            }
            else if (node is CountExpression)
            {
                CountExpression e      = (CountExpression)node;
                string          sCount = "";
                string          visit  = "";

                if (!e.IsInline)
                {
                    visit  = this.Visit(e, e.Input);
                    sCount = string.Format("{0}/$count", visit);
                }
                else
                {
                    visit = this.Visit(e, e.Input);

                    if (visit.IndexOf("?$") == -1)
                    {
                        sCount = string.Format("{0}?$inlinecount={1}", visit, e.CountKind);
                    }
                    else
                    {
                        sCount = string.Format("{0}&$inlinecount={1}", visit, e.CountKind);
                    }
                }
                return(sCount);
            }
            else if (node is NewExpression)
            {
                NewExpression e   = (NewExpression)node;
                string        uri = this.Visit(e, e.Input);
                ExpNode[]     pe  = new ExpNode[] { };

                List <ExpNode> nodes = new List <ExpNode>();
                foreach (ExpNode parameter in e.Arguments)
                {
                    nodes.Add(parameter as ExpNode);
                }
                pe = nodes.ToArray();
                string _select = String.Format("{0}", this.Visit(e, pe[0]));

                if (nodes.Count > 1)
                {
                    for (int j = 1; j < nodes.Count; j++)
                    {
                        _select = String.Format("{0},{1}", _select, this.Visit(e, pe[j]));
                    }
                }

                if (uri.IndexOf("?$") == -1)
                {
                    return(string.Format("{0}?$select={1}", uri, _select));
                }
                else
                {
                    return(string.Format("{0}&$select={1}", uri, _select));
                }
            }
            else if (node is MemberBindExpression)
            {
                return(this.Visit(node, ((MemberBindExpression)node).SourceProperty));
            }
            else if (node is TopExpression)
            {
                TopExpression e     = (TopExpression)node;
                string        visit = this.Visit(e, e.Input);

                if (visit.IndexOf("?$") == -1)
                {
                    return(string.Format("{0}?$top={1}", visit, e.Predicate));
                }
                else
                {
                    return(string.Format("{0}&$top={1}", visit, e.Predicate));
                }
            }
            else if (node is SkipExpression)
            {
                SkipExpression e     = (SkipExpression)node;
                string         visit = this.Visit(e, e.Input);

                if (visit.IndexOf("?$") == -1)
                {
                    return(string.Format("{0}?$skip={1}", visit, e.Predicate));
                }
                else
                {
                    return(string.Format("{0}&$skip={1}", visit, e.Predicate));
                }
            }
            else if (node is OrderByExpression)
            {
                OrderByExpression e          = (OrderByExpression)node;
                string            ordervalue = String.Empty;
                int i = 0;

                string visit = this.Visit(e, e.Input);
                if (e.ExcludeFromUri)
                {
                    return(visit);
                }

                string propVisit = this.Visit(e, e.PropertiesExp[0]);

                switch (e.AscDesc)
                {
                case true:
                    if (visit.IndexOf("?$") == -1)
                    {
                        ordervalue = String.Format("{0}?$orderby={1}", visit, propVisit);
                    }
                    else
                    {
                        ordervalue = String.Format("{0}&$orderby={1}", visit, propVisit);
                    }
                    break;

                case false:
                    if (visit.IndexOf("?$") == -1)
                    {
                        ordervalue = String.Format("{0}?$orderby={1} desc", visit, propVisit);
                    }
                    else
                    {
                        ordervalue = String.Format("{0}&$orderby={1} desc", visit, propVisit);
                    }
                    break;
                }
                ;

                if (e.PropertiesExp.Length > 0)
                {
                    for (i++; i < e.PropertiesExp.Length; i++)
                    {
                        String nextValue = this.Visit(e, e.PropertiesExp[i]);
                        nextValue = String.Format("{0}", nextValue);

                        switch (e.AscDesc)
                        {
                        case true:
                            ordervalue = String.Format("{0},{1}", ordervalue, nextValue);
                            break;

                        case false:
                            ordervalue = String.Format("{0},{1} desc", ordervalue, nextValue);
                            break;
                        }
                    }
                }
                return(ordervalue);
            }
            else if (node is ThenByExpression)
            {
                ThenByExpression e          = (ThenByExpression)node;
                string           ordervalue = String.Empty;
                int i = 0;

                string visit = this.Visit(e, e.Input);
                if (e.ExcludeFromUri)
                {
                    return(visit);
                }

                switch (e.AscDesc)
                {
                case true:
                    ordervalue = String.Format("{0},{1}", visit, e.Properties[0].Name);
                    break;

                case false:
                    ordervalue = String.Format("{0},{1} desc", visit, e.Properties[0].Name);
                    break;
                }

                if (e.Properties.Length > 0)
                {
                    for (i++; i < e.Properties.Length; i++)
                    {
                        String nextValue = e.Properties[i].Name;
                        nextValue = String.Format("{0}", nextValue);

                        switch (e.AscDesc)
                        {
                        case true:
                            ordervalue = String.Format("{0},{1}", ordervalue, nextValue);
                            break;

                        case false:
                            ordervalue = String.Format("{0},{1} desc", ordervalue, nextValue);
                            break;
                        }
                    }
                }
                return(ordervalue);
            }
            else if (node is ExpandExpression)
            {
                ExpandExpression e   = (ExpandExpression)node;
                string           uri = this.Visit(e, e.Input);

                string expand = String.Format("{0}", this.Visit(e, e.PropertiesExp[0]));

                if (e.Properties.Length > 1)
                {
                    for (int i = 1; i < e.Properties.Length; i++)
                    {
                        expand = String.Format("{0},{1}", expand, this.Visit(e, e.PropertiesExp[i]));
                    }
                }

                if (uri.IndexOf("?$") == -1)
                {
                    return(string.Format("{0}?$expand={1}", uri, expand));
                }
                else
                {
                    return(string.Format("{0}&$expand={1}", uri, expand));
                }
            }
            else if (node is NavigationExpression)
            {
                NavigationExpression e = (NavigationExpression)node;
                if (!e.IsLink)
                {
                    return(String.Format("{0}/{1}", this.Visit(e, e.Input), this.Visit(e, e.PropertyExp)));
                }
                else
                {
                    return(String.Format("{0}/{1}/$ref", this.Visit(e, e.Input), this.Visit(e, e.PropertyExp)));
                }
            }
            else if (node is KeyExpression)
            {
                KeyExpression key = node as KeyExpression;
                return(CreateKeyString(key));
            }
            else if (node is NestedPropertyExpression)
            {
                NestedPropertyExpression e = (NestedPropertyExpression)node;
                string enitySetname        = e.Name;
                string nestedProperty      = "";

                foreach (PropertyExpression p in e.PropertyExpressions)
                {
                    string interim = this.Visit(e, p);
                    //AstoriaTestLog.WriteLine(interim);
                    if (p.ValueOnly)
                    {
                        nestedProperty = String.Format("{0}/{1}/{2},", nestedProperty, enitySetname, interim + "/$value").TrimStart('/');
                    }
                    else
                    {
                        nestedProperty = String.Format("{0}/{1}/{2},", nestedProperty, enitySetname, interim).TrimStart('/');
                    }
                }

                return(nestedProperty.TrimEnd(',').Replace(",/", ","));
            }
            else if (node is PropertyExpression)
            {
                PropertyExpression e = (PropertyExpression)node;
                if (e.ValueOnly)
                {
                    return(e.Property.Name + "/$value");
                }
                else
                {
                    return(e.Property.Name);
                }
            }
            else if (node is VariableExpression)
            {
                VariableExpression e = (VariableExpression)node;
                return(e.Variable.Name);
            }
            if (node is LogicalExpression)
            {
                LogicalExpression e     = (LogicalExpression)node;
                string            left  = this.Visit(e, e.Left);
                string            right = null;

                if (e.Operator != LogicalOperator.Not)
                {
                    right = this.Visit(e, e.Right);
                }

                string logical;
                switch (e.Operator)
                {
                case LogicalOperator.And:
                    logical = string.Format("{0} and {1}", left, right);
                    break;

                case LogicalOperator.Or:
                    logical = string.Format("{0} or {1}", left, right);
                    break;

                case LogicalOperator.Not:
                    logical = string.Format("not {0}", left);
                    break;

                default:
                    throw new Exception("Unhandled Comparison Type: " + e.Operator);
                }
                return(logical);
            }
            else if (node is ComparisonExpression)
            {
                ComparisonExpression e = (ComparisonExpression)node;
                String left            = this.Visit(e, e.Left);
                String right           = this.Visit(e, e.Right);

                switch (e.Operator)
                {
                case ComparisonOperator.Equal:
                    return(String.Format("{0} eq {1}", left, right));

                case ComparisonOperator.NotEqual:
                    return(String.Format("{0} ne {1}", left, right));

                case ComparisonOperator.GreaterThan:
                    return(String.Format("{0} gt {1}", left, right));

                case ComparisonOperator.GreaterThanOrEqual:
                    return(String.Format("{0} ge {1}", left, right));

                case ComparisonOperator.LessThan:
                    return(String.Format("{0} lt {1}", left, right));

                case ComparisonOperator.LessThanOrEqual:
                    return(String.Format("{0} le {1}", left, right));

                default:
                    throw new Exception("Unhandled Comparison Type: " + e.Operator);
                }
                ;
            }
            else if (node is IsOfExpression || node is CastExpression)
            {
                ExpNode  target;
                NodeType targetType;
                string   operation;
                if (node is IsOfExpression)
                {
                    IsOfExpression e = (IsOfExpression)node;
                    operation  = "isof";
                    target     = e.Target;
                    targetType = e.TargetType;
                }
                else
                {
                    CastExpression e = (CastExpression)node;
                    operation  = "cast";
                    target     = e.Target;
                    targetType = e.TargetType;
                }

                string targetTypeName = targetType.FullName;
                if (targetType is PrimitiveType)
                {
                    targetTypeName = TypeData.FindForType(targetType.ClrType).GetEdmTypeName();
                }

                if (target == null)
                {
                    return(String.Format("{0}('{1}')", operation, targetTypeName));
                }
                else
                {
                    return(String.Format("{0}({1}, '{2}')", operation, this.Visit(node, target), targetTypeName));
                }
            }
            else if (node is ArithmeticExpression)
            {
                ArithmeticExpression e = (ArithmeticExpression)node;
                String left            = this.Visit(e, e.Left);
                String right           = this.Visit(e, e.Right);

                switch (e.Operator)
                {
                case ArithmeticOperator.Add:
                    return(String.Format("{0} add {1}", left, right));

                case ArithmeticOperator.Div:
                    return(String.Format("{0} div {1}", left, right));

                case ArithmeticOperator.Mod:
                    return(String.Format("{0} mod {1}", left, right));

                case ArithmeticOperator.Mult:
                    return(String.Format("{0} mul {1}", left, right));

                case ArithmeticOperator.Sub:
                    return(String.Format("{0} sub {1}", left, right));

                default:
                    throw new Exception("Unhandled Arithmetic Type: " + e.Operator);
                }
                ;
            }
            else if (node is MethodExpression)
            {
                MethodExpression e = (MethodExpression)node;
                return(BuildMemberOrMethodExpression(node, e.Caller, e.Arguments, e.Name));
            }
            else if (node is MemberExpression)
            {
                MemberExpression e = (MemberExpression)node;
                return(BuildMemberOrMethodExpression(node, e.Caller, e.Arguments, e.Name));
            }
            else if (node is ConstantExpression)
            {
                ConstantExpression e     = (ConstantExpression)node;
                object             value = e.Value.ClrValue;

                string val = TypeData.FormatForKey(value, this.UseSmallCasing, false);
                if (this.EscapeUriValues)
                {
                    // FormatForKey already does this for doubles that don't have the D
                    if (!(value is double) || Versioning.Server.SupportsV2Features)
                    {
                        val = Uri.EscapeDataString(val);
                    }
                }

                if (value == null)
                {
                    val = "null";
                }
                else if (!(value is String))
                {
                    val = val.Replace("+", "").Replace("%2B", "");
                }

                return(val);
            }
            else if (node is NegateExpression)
            {
                NegateExpression e = (NegateExpression)node;
                return("-(" + this.Visit(e, e.Argument) + ")");
            }
            else if (node is FirstExpression)
            {
                FirstExpression first = node as FirstExpression;
                return(this.Visit(first, first.Input));
            }
            else if (node is FirstOrDefaultExpression)
            {
                FirstOrDefaultExpression first = node as FirstOrDefaultExpression;
                return(this.Visit(first, first.Input));
            }
            else if (node is SingleExpression)
            {
                SingleExpression first = node as SingleExpression;
                return(this.Visit(first, first.Input));
            }
            else if (node is SingleOrDefaultExpression)
            {
                SingleOrDefaultExpression first = node as SingleOrDefaultExpression;
                return(this.Visit(first, first.Input));
            }
            else
            {
                throw new Exception(this.GetType().Name + " Unhandled Node: " + node.GetType().Name);
            }
        }
コード例 #4
0
        public void PlainSerializersBasicTest()
        {
            CombinatorialEngine engine = CombinatorialEngine.FromDimensions(
                new Dimension("ValidMime", new object[] { true, false }),
                new Dimension("SpecificMime", new object[] { true, false }),
                new Dimension("TypeData", TypeData.Values));
            Hashtable table = new Hashtable();

            while (engine.Next(table))
            {
                bool     validMime    = (bool)table["ValidMime"];
                bool     specificMime = (bool)table["SpecificMime"];
                TypeData typeData     = (TypeData)table["TypeData"];

                // If ValidMime is false, whether it's specific or not doesn't matter.
                if (!validMime && specificMime)
                {
                    continue;
                }

                if (!typeData.IsTypeSupported)
                {
                    continue;
                }

                using (TestWebRequest request = TestWebRequest.CreateForLocation(WebServerLocation.InProcess))
                {
                    Type valueType  = typeData.ClrType;
                    Type entityType = typeof(TypedEntity <,>).MakeGenericType(typeof(int), valueType);
                    CustomDataContextSetup dataContextSetup = new CustomDataContextSetup(entityType);
                    dataContextSetup.Id          = 1;
                    dataContextSetup.MemberValue = typeData.NonNullValue;

                    Type serviceType = dataContextSetup.DataServiceType;
                    request.DataServiceType  = serviceType;
                    request.RequestUriString = "/Values(1)/Member/$value";
                    if (validMime)
                    {
                        request.Accept = TypeData.FindForType(valueType).DefaultContentType;
                    }
                    else
                    {
                        request.Accept = "application/unexpected";
                    }

                    try
                    {
                        request.SendRequest();
                        if (!validMime)
                        {
                            Assert.Fail("Request should have failed.");
                        }
                    }
                    catch (WebException)
                    {
                        if (!validMime)
                        {
                            continue;
                        }
                        throw;
                    }

                    string expectedType = request.Accept;
                    Assert.AreEqual(expectedType, TestUtil.GetMediaType(request.ResponseContentType));

                    Stream stream = request.GetResponseStream();
                    if (valueType == typeof(byte[]))
                    {
                        byte[] bytes = (byte[])dataContextSetup.MemberValue;
                        for (int i = 0; i < bytes.Length; i++)
                        {
                            Assert.AreEqual(bytes[i], (byte)stream.ReadByte());
                        }
                    }
                    else
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            typeData.VerifyAreEqual(dataContextSetup.MemberValue, typeData.ValueFromXmlText(reader.ReadToEnd(), request.Accept), request.Accept);
                        }
                    }
                }
            }
        }