public void SerializeDeserializeWithDutExternalParameter()
        {
            var plan = new TestPlan();
            var step = new TestPlanTest.DutStep();
            var dut1 = new DummyDut {
                Name = "DUT1"
            };
            var dut2 = new DummyDut {
                Name = "DUT2"
            };

            DutSettings.Current.AddRange(new [] { dut1, dut2 });
            try
            {
                step.Dut = dut1;
                plan.ChildTestSteps.Add(step);
                plan.ExternalParameters.Add(step,
                                            TypeData.GetTypeData(step).GetMember(nameof(TestPlanTest.DutStep.Dut)), "dut");

                using (var memstr = new MemoryStream())
                {
                    plan.Save(memstr);

                    var serializer = new TapSerializer();
                    var ext        = serializer.GetSerializer <ExternalParameterSerializer>();
                    ext.PreloadedValues["dut"] = "DUT2";

                    memstr.Seek(0, SeekOrigin.Begin);
                    plan = (TestPlan)serializer.Deserialize(memstr);
                }

                step = (TestPlanTest.DutStep)plan.ChildTestSteps[0];
                Assert.AreEqual(step.Dut, dut2);
            }
            finally
            {
                DutSettings.Current.Remove(dut1);
                DutSettings.Current.Remove(dut2);
            }
        }
Exemple #2
0
        private void HandleExternalParametersAndLoadPlan(string planToLoad)
        {
            List <string> values     = new List <string>();
            var           serializer = new TapSerializer();
            var           extparams  = serializer.GetSerializer <Plugins.ExternalParameterSerializer>();

            if (External.Length > 0)
            {
                values.AddRange(External);
            }
            if (TryExternal.Length > 0)
            {
                values.AddRange(TryExternal);
            }
            Plan = new TestPlan();
            List <string> externalParameterFiles = new List <string>();

            foreach (var externalParam in values)
            {
                int equalIdx = externalParam.IndexOf('=');
                if (equalIdx == -1)
                {
                    externalParameterFiles.Add(externalParam);
                    continue;
                }
                var name  = externalParam.Substring(0, equalIdx);
                var value = externalParam.Substring(equalIdx + 1);
                extparams.PreloadedValues[name] = value;
            }
            var log = Log.CreateSource("CLI");

            var timer = Stopwatch.StartNew();

            using (var fs = new FileStream(planToLoad, FileMode.Open, FileAccess.Read))
            {
                // only cache the XML if there are no external parameters.
                bool cacheXml = values.Any() == false && externalParameterFiles.Any() == false;

                Plan = TestPlan.Load(fs, planToLoad, cacheXml, serializer, IgnoreLoadErrors);
                log.Info(timer, "Loaded test plan from {0}", planToLoad);
            }

            if (externalParameterFiles.Count > 0)
            {
                var importers = CreateInstances <IExternalTestPlanParameterImport>();
                foreach (var file in externalParameterFiles)
                {
                    var ext = Path.GetExtension(file);
                    log.Info($"Loading external parameters from '{file}'.");
                    var importer = importers.FirstOrDefault(i => i.Extension == ext);
                    if (importer != null)
                    {
                        importer.ImportExternalParameters(Plan, file);
                    }
                    else
                    {
                        log.Error($"No installed plugins provide loading of external parameters from '{ext}' files. No external parameters loaded from '{file}'.");
                    }
                }
            }

            if (External.Length > 0)
            {   // Print warnings if an --external parameter was not in the test plan.
                foreach (var externalParam in External)
                {
                    var equalIdx = externalParam.IndexOf('=');
                    if (equalIdx == -1)
                    {
                        continue;
                    }

                    var name = externalParam.Substring(0, equalIdx);
                    if (Plan.ExternalParameters.Get(name) != null)
                    {
                        continue;
                    }

                    log.Warning("External parameter '{0}' does not exist in the test plan.", name);
                    log.Warning("Statement '{0}' has no effect.", externalParam);
                    throw new ArgumentException("");
                }
            }
        }