예제 #1
0
        public void OleDbParameterCollection_MultipleScenarios_Success()
        {
            OleDbParameterCollection opc = command.Parameters;

            Assert.True(opc.Count == 0);
            Assert.False(opc.IsReadOnly);
            Assert.False(opc.IsFixedSize);
            Assert.False(opc.IsSynchronized);
            Assert.Throws <IndexOutOfRangeException>(() => opc[0].ParameterName);
            Assert.Throws <IndexOutOfRangeException>(() => opc["@p1"].ParameterName);
            Assert.Throws <ArgumentNullException>(() => opc.Add(null));

            opc.Add((object)new OleDbParameter());
            opc.Add((object)new OleDbParameter());
            Collections.IEnumerator enm = opc.GetEnumerator();
            Assert.True(enm.MoveNext());
            Assert.Equal("Parameter1", ((OleDbParameter)enm.Current).ParameterName);
            Assert.True(enm.MoveNext());
            Assert.Equal("Parameter2", ((OleDbParameter)enm.Current).ParameterName);

            opc.Add(new OleDbParameter(null, null));
            opc.Add(null, OleDbType.Integer, 0, null);
            Assert.Equal("Parameter4", opc["Parameter4"].ParameterName);

            opc.Add(new OleDbParameter("Parameter5", OleDbType.LongVarWChar, 20));
            opc.Add(new OleDbParameter(null, OleDbType.WChar, 20, "a"));

            opc.RemoveAt(opc[3].ParameterName);
            Assert.Equal(-1, opc.IndexOf(null));
            Assert.False(opc.Contains(null));
            Assert.Throws <IndexOutOfRangeException>(() => opc.RemoveAt(null));

            OleDbParameter p = opc[0];

            Assert.Throws <ArgumentException>(() => opc.Add((object)p));
            Assert.Throws <ArgumentException>(() => new OleDbCommand().Parameters.Add(p));
            Assert.Throws <ArgumentNullException>(() => opc.Remove(null));

            string pname = p.ParameterName;

            p.ParameterName = pname;
            p.ParameterName = pname.ToUpper();
            p.ParameterName = pname.ToLower();
            p.ParameterName = "@p1";
            p.ParameterName = pname;

            opc.Clear();
            opc.Add(p);

            opc.Clear();
            opc.AddWithValue("@p1", null);

            Assert.Equal(-1, opc.IndexOf(p.ParameterName));

            opc[0] = p;
            Assert.Equal(0, opc.IndexOf(p.ParameterName));

            Assert.True(opc.Contains(p.ParameterName));
            Assert.True(opc.Contains(opc[0]));

            opc[0] = p;
            opc[p.ParameterName] = new OleDbParameter(p.ParameterName, null);
            opc[p.ParameterName] = (OleDbParameter)OleDbFactory.Instance.CreateParameter();
            opc.RemoveAt(0);

            new OleDbCommand().Parameters.Clear();
            new OleDbCommand().Parameters.CopyTo(new object[0], 0);
            Assert.False(new OleDbCommand().Parameters.GetEnumerator().MoveNext());

            Assert.Throws <InvalidCastException>(() => new OleDbCommand().Parameters.Add(0));
            Assert.Throws <ArgumentNullException>(() => new OleDbCommand().Parameters.AddRange(null));
            Assert.Throws <InvalidCastException>(() => new OleDbCommand().Parameters.Insert(0, 0));
            Assert.Throws <InvalidCastException>(() => new OleDbCommand().Parameters.Remove(0));

            Assert.Throws <ArgumentException>(() => opc.Remove(new OleDbParameter()));
        }