Store() 공개 메소드

Store the contents of this collection of properties to the stream in the format used for Java ".properties" files using an instance of JavaPropertyWriter. The keys and values will be minimally escaped to ensure special characters are read back in properly. Keys are not sorted. The file will begin with a comment identifying the date - and an additional comment may be included.
public Store ( Stream streamOut, string comments ) : void
streamOut Stream An output stream to write the properties to.
comments string Optional additional comment to include at the head of the output.
리턴 void
예제 #1
0
        public void JavaPropertyWriter_shouldNotWriteTimestampIfFalse()
        {
            JavaProperties properties = new JavaProperties();

            using (var stream = new MemoryStream())
            {
                properties.Store(stream, false);

                string actual = Encoding.GetEncoding("iso-8859-1").GetString(stream.ToArray());
                Assert.IsEmpty(actual);
            }
        }
예제 #2
0
        public void JavaPropertyWriter_shouldWriteTimestampIfTrue()
        {
            JavaProperties properties = new JavaProperties();

            using (var stream = new MemoryStream())
            {
                properties.Store(stream, true);

                string actual = Encoding.GetEncoding("iso-8859-1").GetString(stream.ToArray());
                Assert.That(actual, Does.StartWith("#"));
            }
        }
예제 #3
0
        public void JavaPropertyWriter_shouldWriteComment()
        {
            String         comment    = "a comment";
            JavaProperties properties = new JavaProperties();

            using (var stream = new MemoryStream())
            {
                properties.Store(stream, comment);

                string actual = Encoding.GetEncoding("iso-8859-1").GetString(stream.ToArray());
                Assert.That(actual, Does.Contain(comment));
            }
        }
예제 #4
0
        public void JavaPropertyWriter_shouldEscapeExtendedCharsInValue()
        {
            JavaProperties properties = new JavaProperties();

            properties.Add("Greeting", "Привет");

            using (var stream = new MemoryStream())
            {
                properties.Store(stream, null);

                string actual = Encoding.GetEncoding("iso-8859-1").GetString(stream.ToArray());
                Assert.That(actual, Does.Contain("Greeting=\\u041F\\u0440\\u0438\\u0432\\u0435\\u0442"));
            }
        }
예제 #5
0
        public void JavaPropertyWriter_shouldEscapeColonInValue()
        {
            JavaProperties properties = new JavaProperties();

            properties.Add("first", ":anything");
            properties.Add("second", "noth:ing");
            properties.Add("third", "something:");

            using (var stream = new MemoryStream())
            {
                properties.Store(stream, null);

                string actual = Encoding.GetEncoding("iso-8859-1").GetString(stream.ToArray());
                Assert.That(actual, Does.Contain("first=\\:anything"));
                Assert.That(actual, Does.Contain("second=noth\\:ing"));
                Assert.That(actual, Does.Contain("third=something\\:"));
            }
        }
예제 #6
0
        public void JavaPropertyWriter_shouldEscapeEqualsInKey()
        {
            JavaProperties properties = new JavaProperties();

            properties.Add("=first", "anything");
            properties.Add("sec=ond", "nothing");
            properties.Add("third=", "something");

            using (var stream = new MemoryStream())
            {
                properties.Store(stream, null);

                string actual = Encoding.GetEncoding("iso-8859-1").GetString(stream.ToArray());
                Assert.That(actual, Does.Contain("\\=first=anything"));
                Assert.That(actual, Does.Contain("sec\\=ond=nothing"));
                Assert.That(actual, Does.Contain("third\\==something"));
            }
        }
        public void TestJavaPropertyWriter()
        {
            const string filename = JavaTestDataDirectory + "mixed.properties";
            const string outName = JavaOutputDirectory + "test-writer.properties";

            FileStream inStream = null;
            FileStream outStream = null;
            try
            {
                //  Read the mixed properties file.
                Console.WriteLine("Loading " + filename);
                inStream = File.OpenRead(filename);

                JavaProperties properties1 = new JavaProperties();
                properties1.Load( inStream );
                inStream.Close();
                inStream = null;

                // Write it to a new file - using the JavaPropertiesWriter we are testing.
                outStream = File.OpenWrite(outName);
                properties1.Store(outStream, "Testing JavaPropertyWriter");
                outStream.Flush();
                outStream.Close();
                outStream = null;

                //  Now re-read the output file into a second set of properties
                inStream = File.OpenRead(outName);

                JavaProperties properties2 = new JavaProperties();
                properties2.Load(inStream);

                // Now compare the values - find, compare and remove each value in turn.
                foreach (String name in properties2.Keys)
                {
                    String value2 = properties2.GetProperty(name);

                    if (properties1.ContainsKey(name))
                    {
                        String value1 = properties1.GetProperty(name);

                        if (value1 == null)
                        {
                            if (value2 != null)
                            {
                                Assert.Fail("Original value is null, new value is : \"" + value2 + "\"");
                            }
                        }
                        else if (!value1.Equals(value2))
                        {
                            Assert.Fail("Original value is \"" + value1 + "\", new value is : \"" + value2 + "\"");
                        }

                        properties1.Remove( name );
                    }
                    else
                    {
                        Assert.Fail("Missing property: \"" + name + "\" (with value \"" + value2 + "\")");
                    }
                }

                if( properties1.Count > 0 )
                {
                    Assert.Fail( properties1.Count + " values missing from output file.");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                if (inStream != null)
                {
                    inStream.Close();
                }

                if (outStream != null)
                {
                    outStream.Close();
                }
            }
        }
        public void TestJavaPropertyWriter()
        {
            string filename = JavaTestDataDirectory + "/mixed.properties";
            string outName  = JavaOutputDirectory + "/test-writer.properties";

            FileStream inStream  = null;
            FileStream outStream = null;

            try
            {
                //  Read the mixed properties file.
                Console.WriteLine("Loading " + filename);
                inStream = File.OpenRead(filename);

                JavaProperties properties1 = new JavaProperties();
                properties1.Load(inStream);
                inStream.Close();
                inStream = null;

                // Write it to a new file - using the JavaPropertiesWriter we are testing.
                outStream = File.OpenWrite(outName);
                properties1.Store(outStream, "Testing JavaPropertyWriter");
                outStream.Flush();
                outStream.Close();
                outStream = null;

                //  Now re-read the output file into a second set of properties
                inStream = File.OpenRead(outName);

                JavaProperties properties2 = new JavaProperties();
                properties2.Load(inStream);

                // Now compare the values - find, compare and remove each value in turn.
                foreach (String name in properties2.Keys)
                {
                    String value2 = properties2.GetProperty(name);

                    if (properties1.ContainsKey(name))
                    {
                        String value1 = properties1.GetProperty(name);

                        if (value1 == null)
                        {
                            if (value2 != null)
                            {
                                Assert.Fail("Original value is null, new value is : \"" + value2 + "\"");
                            }
                        }
                        else if (!value1.Equals(value2))
                        {
                            Assert.Fail("Original value is \"" + value1 + "\", new value is : \"" + value2 + "\"");
                        }

                        properties1.Remove(name);
                    }
                    else
                    {
                        Assert.Fail("Missing property: \"" + name + "\" (with value \"" + value2 + "\")");
                    }
                }

                if (properties1.Count > 0)
                {
                    Assert.Fail(properties1.Count + " values missing from output file.");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                if (inStream != null)
                {
                    inStream.Close();
                }

                if (outStream != null)
                {
                    outStream.Close();
                }
            }
        }