Service container (that is a IServiceProvider) subordinated to an optional base IServiceProvider that acts as a fallback if the service is not found at this level. Service creation may be deferred thanks to callback registration and an optional remove callback can be registered with each entry.
This container is registered as the service associated to IServiceProvider and ISimpleServiceContainer thanks to the overridable GetDirectService. This method may be overridden to return other built-in services: these services take precedence over the registered services.
Inheritance: ISimpleServiceContainer, IDisposable
Ejemplo n.º 1
0
        MiniContext( string name )
        {
            ServiceContainer = new SimpleServiceContainer();
            ServiceContainer.Add( RequirementLayerSerializer.Instance );
            ServiceContainer.Add( SimpleTypeFinder.Default );

            ConfigContainer = SharedDictionary.Create( ServiceContainer );
            ConfigManager = ConfigurationManager.Create( ConfigContainer ).ConfigManager;

        }
Ejemplo n.º 2
0
        static SharedDicTestContext()
        {
            _allPlugins = new List<INamedVersionedUniqueId>();
            _allPluginsEx = new ReadOnlyListOnIList<INamedVersionedUniqueId>( _allPlugins );
            EnsurePlugins( 10 );

            var c = new SimpleServiceContainer();
            c.Add<ISimpleTypeFinder>( SimpleTypeFinder.Default );
            _serviceProvider = c;
        }
Ejemplo n.º 3
0
 public ReaderImpl( XmlReader reader, IServiceProvider baseServiceProvider, Version storageVersion, bool autoCloseReader )
     : base(null)
 {
     Current = this;
     _xmlReader = reader;
     _serviceContainer = new SimpleServiceContainer( baseServiceProvider );
     _path = new List<string>();
     _pathEx = new ReadOnlyListOnIList<string>( _path );
     _storageVersion = storageVersion;
     _mustCloseReader = autoCloseReader;
 }
Ejemplo n.º 4
0
        MiniContext( string name )
        {
            ServiceContainer = new SimpleServiceContainer();

            ContextObject = new object();
            ConfigContainer = SharedDictionary.Create( ServiceContainer );
            ConfigManager = ConfigurationManager.Create( ConfigContainer ).ConfigManager;
            PluginRunner = new PluginRunner( ServiceContainer, ConfigManager );
            PluginRunner.Initialize( ContextObject );
            ServiceContainer.Add<IConfigContainer>( ConfigContainer );
        }
Ejemplo n.º 5
0
 public WriterImpl( XmlWriter writer, IServiceProvider baseServiceProvider, bool writeElementHeader, bool autoCloseWriter )
     : base(null)
 {
     _xmlWriter = writer;
     _serviceContainer = new SimpleServiceContainer( baseServiceProvider );
     if( writeElementHeader )
     {
         Xml.WriteStartElement( "CK-Structured" );
         Xml.WriteAttributeString( "version", "2.5.5" );
         _mustEndElement = true;
     }
     _mustCloseWriter = autoCloseWriter;
     Current = this;
 }
Ejemplo n.º 6
0
        public void WriteReadUserConfig()
        {
            string path = Path.Combine( TestFolder, "UserConfig.xml" );
            Guid id = new Guid("{6AFBAE01-5CD1-4EDE-BB56-4590C5A253DF}");
            
            // Write ----------------------------------------------------------
            {
                ISharedDictionary dic = SharedDictionary.Create( null );
                IConfigManagerExtended config = ConfigurationManager.Create( dic );
                Assert.That( config, Is.Not.Null );
                Assert.That( config.HostUserConfig, Is.Not.Null );
                Assert.That( config.ConfigManager.UserConfiguration, Is.Not.Null );

                config.HostUserConfig["key1"] = "value1";
                config.HostUserConfig["key2"] = "value2";
                config.HostUserConfig["key3"] = "value3";
                config.ConfigManager.UserConfiguration.PluginsStatus.SetStatus( id, ConfigPluginStatus.AutomaticStart );

                Assert.That( config.IsUserConfigDirty );
                Assert.That( config.IsSystemConfigDirty, Is.False );

                using( Stream wrt = new FileStream( path, FileMode.Create ) )
                using( IStructuredWriter sw = SimpleStructuredWriter.CreateWriter( wrt, null ) )
                {
                    config.SaveUserConfig( sw );
                }
            }

            // Read ------------------------------------------------------------
            {
                ISimpleServiceContainer container = new SimpleServiceContainer();
                ISharedDictionary dic = SharedDictionary.Create( container );
                IConfigManagerExtended config = ConfigurationManager.Create( dic );

                using( Stream str = new FileStream( path, FileMode.Open ) )
                using( IStructuredReader sr = SimpleStructuredReader.CreateReader( str, container ) )
                {
                    config.LoadUserConfig( sr );
                }
                Assert.That( config.HostUserConfig["key1"], Is.EqualTo( "value1" ) );
                Assert.That( config.HostUserConfig["key2"], Is.EqualTo( "value2" ) );
                Assert.That( config.HostUserConfig["key3"], Is.EqualTo( "value3" ) );
                Assert.That( config.ConfigManager.UserConfiguration.PluginsStatus.GetStatus( id, ConfigPluginStatus.Disabled ) == ConfigPluginStatus.AutomaticStart );
            }
        }
Ejemplo n.º 7
0
 public ReaderImpl( XmlReader reader, IServiceProvider baseServiceProvider, bool autoCloseReader )
     : base(null)
 {
     if( reader == null ) throw new ArgumentNullException( "reader" );
     Current = this;
     _xmlReader = reader;
     _serviceContainer = new SimpleServiceContainer( baseServiceProvider );
     _path = new List<string>();
     _pathEx = new ReadOnlyListOnIList<string>( _path );
     if( reader.IsStartElement( "CK-Structured" ) )
     {
         _storageVersion = new Version( reader.GetAttribute( "version" ) );
         _isEmpty = reader.IsEmptyElement;
         if( !_isEmpty ) _openingElementName = reader.Name;
         reader.Read();
     }
     _mustCloseReader = autoCloseReader;
 }
Ejemplo n.º 8
0
        public void EmptyFile()
        {
            string test = TestBase.GetTestFilePath( "Storage", "EmptyFile" );
            using( Stream wrt = new FileStream( test, FileMode.Create ) )
            {
                IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() );
                writer.Dispose();
            }

            Assert.That( File.Exists( test ) );

            using( Stream str = new FileStream( test, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    Assert.That( reader.StorageVersion, Is.GreaterThanOrEqualTo( new Version( 2, 5, 0 ) ) );
                }
            }
        }
Ejemplo n.º 9
0
 private void DoTestEnum( Action<XDocument> docModifier )
 {
     string test = TestBase.GetTestFilePath( "Storage", "TestEnum" );
     using( Stream wrt = new FileStream( test, FileMode.Create ) )
     {
         using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
         {
             writer.WriteObjectElement( "data", TestEnumValues.First );
             writer.WriteObjectElement( "data", TestEnumValues.Second );
             writer.WriteObjectElement( "After", 3712 * 2 );
         }
     }
     LoadAndModifyXml( test, docModifier );
     using( Stream str = new FileStream( test, FileMode.Open ) )
     {
         SimpleServiceContainer s = new SimpleServiceContainer();
         s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
         using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
         {
             TestEnumValues value1 = (TestEnumValues)reader.ReadObjectElement( "data" );
             TestEnumValues value2 = (TestEnumValues)reader.ReadObjectElement( "data" );
             Assert.That( value1, Is.EqualTo( TestEnumValues.First ) );
             Assert.That( value2, Is.EqualTo( TestEnumValues.Second ) );
             Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
         }
     }
 }
Ejemplo n.º 10
0
 public void BugUnexisting()
 {
     string xmlPath = TestBase.GetTestFilePath( "Storage", "BugUnexistingEnum" );
     using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
     {
         using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
         {
             writer.WriteObjectElement( "data", UnexistingTestEnumValues.First );
             writer.WriteObjectElement( "After", 3712 * 2 );
             writer.WriteObjectElement( "data", new MayBeUnexistingButValidXmlObject() );
             writer.WriteObjectElement( "After2", 3712 * 3 );
         }
     }
     TestBase.DumpFileToConsole( xmlPath );
     using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
     {
         SimpleServiceContainer s = new SimpleServiceContainer();
         s.Add<ISimpleTypeFinder>( new UnexistingTypeFinder() );
         using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
         {
             Assert.Throws<TypeLoadException>( () => reader.ReadObjectElement( "data" ) );
             // An exception does not break the reader.
             Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
             Assert.Throws<TypeLoadException>( () => reader.ReadObjectElement( "data" ) );
             // An exception does not break the reader.
             Assert.That( reader.ReadObjectElement( "After2" ), Is.EqualTo( 3712 * 3 ) );
         }
     }
 }
Ejemplo n.º 11
0
        public void BugBinarySizeDiffer()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "BugBinarySizeDiffer" );
            SerializableObject original = new SerializableObject() { Name = "coucou", Power = 20 };
            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.WriteObjectElement( "data", original );
                }
            }
            LoadAndModifyXml( xmlPath, d => d.Root.Element( "data" ).Attribute( "size" ).SetValue( "1" ) );

            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    Assert.Throws<CKException>( () => reader.ReadObjectElement( "data" ) );
                }
            }
        }
Ejemplo n.º 12
0
        public void StructuredSerializedObjectTest()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "FakeStructuredSerializedObject" );
            StructuredSerializableObject original = new StructuredSerializableObject() { OneInteger = 43, OneString = "Let's go..." };
            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.ObjectWriteExData += ( s, e ) =>
                        {
                            if( e.Obj == original )
                            {
                                e.Writer.Xml.WriteStartElement( "ExtraData" );
                                e.Writer.Xml.WriteAttributeString( "OneAtrribute", "23" );
                                e.Writer.Xml.WriteElementString( "SubValue", "string in element..." );
                                e.Writer.Xml.WriteEndElement();
                            }
                        };
                    writer.WriteObjectElement( "data", original );
                    writer.WriteObjectElement( "After", 3712 * 2 );
                }
            }
            TestBase.DumpFileToConsole( xmlPath );
            // Reads without reading ExtraData element.
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add<ISimpleTypeFinder>( SimpleTypeFinder.WeakDefault );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    object read = reader.ReadObjectElement( "data" );
                    Assert.That( read, Is.TypeOf( typeof( StructuredSerializableObject ) ) );
                    StructuredSerializableObject newOne = read as StructuredSerializableObject;
                    Assert.That( newOne.OneString, Is.EqualTo( original.OneString ) );
                    Assert.That( newOne.OneInteger, Is.EqualTo( original.OneInteger ) );

                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
            // Reads ExtraData element.
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add<ISimpleTypeFinder>( SimpleTypeFinder.WeakDefault );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    reader.ObjectReadExData += ( source, e ) =>
                        {
                            Assert.That( e.Reader.Xml.IsStartElement( "ExtraData" ) );
                            Assert.That( e.Reader.Xml.GetAttributeInt( "OneAtrribute", -12 ), Is.EqualTo( 23 ) );
                            e.Reader.Xml.Read();
                            Assert.That( e.Reader.Xml.ReadElementContentAsString(), Is.EqualTo( "string in element..." ) );
                            // Forget to read the end element.
                            Assert.That( e.Reader.Xml.NodeType == XmlNodeType.EndElement );
                        };

                    object read = reader.ReadObjectElement( "data" );
                    Assert.That( read, Is.TypeOf( typeof( StructuredSerializableObject ) ) );
                    StructuredSerializableObject newOne = read as StructuredSerializableObject;
                    Assert.That( newOne.OneString, Is.EqualTo( original.OneString ) );
                    Assert.That( newOne.OneInteger, Is.EqualTo( original.OneInteger ) );

                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
        }
Ejemplo n.º 13
0
 public void BugBinaryTooBigContent()
 {
     string xmlPath = TestBase.GetTestFilePath( "Storage", "BugBinaryTooBigContent" );
     SerializableObject original = new SerializableObject() { Name = "coucou", Power = 20 };
     using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
     {
         using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
         {
             writer.WriteObjectElement( "data", original );
         }
     }
     LoadAndModifyXml( xmlPath, d =>
     {
         var e = d.Root.Element( "data" );
         e.SetValue( e.Value.Insert( e.Value.Length / 2, "00FF00FF" ) );
     } );
     using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
     {
         SimpleServiceContainer s = new SimpleServiceContainer();
         s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
         using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
         {
             object obj = reader.ReadObjectElement( "data" );
         }
     }
 }
Ejemplo n.º 14
0
        public void BinarySerializableObject()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "TestBinarySerializableObject" );
            SerializableObject o = new SerializableObject();
            o.Name = "TestName";
            o.Power = 20;

            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.WriteObjectElement( "Before", 3712 );
                    writer.WriteObjectElement( "data", o );
                    writer.WriteObjectElement( "After", 3712 * 2 );
                }
            }
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    Assert.That( reader.ReadObjectElement( "Before" ), Is.EqualTo( 3712 ) );
                    
                    SerializableObject o2 = (SerializableObject)reader.ReadObjectElement( "data" );
                    Assert.AreEqual( o.Name, o2.Name );
                    Assert.AreEqual( o.Power, o2.Power );

                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
        }
Ejemplo n.º 15
0
        public void ArrayListWithSerializableObjects()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "TestGenericListOfString" );
            ArrayList list = new ArrayList();
            SerializableObject firstObject = new SerializableObject() { Name = "Albert", Power = 34 };
            list.Add( firstObject );
            list.Add( new DateTime( 2009, 01, 11 ) );
            list.Add( "Franchement, les mecs, vous trouvez que c'est normal que ce soit Spi qui se cogne tous les tests unitaires ?" );

            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.WriteObjectElement( "data", list );
                    writer.WriteObjectElement( "After", 3712 * 2 );
                }
            }
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    ArrayList list2 = (ArrayList)reader.ReadObjectElement( "data" );
                    Assert.AreEqual( ((SerializableObject)list2[0]).Name, ((SerializableObject)list[0]).Name );
                    Assert.AreEqual( ((SerializableObject)list2[0]).Power, ((SerializableObject)list[0]).Power );
                    CheckExactTypeAndValue( typeof( DateTime ), list[1], list2[1] );
                    CheckExactTypeAndValue( typeof( string ), list[2], list2[2] );

                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
        }
Ejemplo n.º 16
0
 public void ColorStruct()
 {
     string xmlPath = TestBase.GetTestFilePath( "Storage", "TestColor" );
     using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
     {
         using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
         {
             writer.WriteObjectElement( "data", Color.Red );
             writer.WriteObjectElement( "data", Color.Blue );
             writer.WriteObjectElement( "After", 3712 * 2 );
         }
     }
     using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
     {
         SimpleServiceContainer s = new SimpleServiceContainer();
         //s.Add( typeof( ISimpleTypeFinder ), SimpleTypeFinder.WeakDefault, null );
         using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
         {
             CheckExactTypeAndValue( typeof( Color ), Color.Red, reader.ReadObjectElement( "data" ) );
             CheckExactTypeAndValue( typeof( Color ), Color.Blue, reader.ReadObjectElement( "data" ) );
             Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
         }
     }
 }
Ejemplo n.º 17
0
        public void GenericListOfString()
        {
            string xmlPath = TestBase.GetTestFilePath( "Storage", "TestGenericListOfString" );
            List<string> list = new List<string>();
            list.Add( "content1" );
            list.Add( "content2" );
            list.Add( "content3" );

            using( Stream wrt = new FileStream( xmlPath, FileMode.Create ) )
            {
                using( IStructuredWriter writer = SimpleStructuredWriter.CreateWriter( wrt, new SimpleServiceContainer() ) )
                {
                    writer.WriteObjectElement( "Before", 3712 );
                    writer.WriteObjectElement( "data", list );
                    writer.WriteObjectElement( "After", 3712 * 2 );
                }
            }
            using( Stream str = new FileStream( xmlPath, FileMode.Open ) )
            {
                SimpleServiceContainer s = new SimpleServiceContainer();
                s.Add<ISimpleTypeFinder>( SimpleTypeFinder.WeakDefault );
                using( IStructuredReader reader = SimpleStructuredReader.CreateReader( str, s ) )
                {
                    Assert.That( reader.ReadObjectElement( "Before" ), Is.EqualTo( 3712 ) );
                    CheckExactTypeAndValue( typeof( List<string> ), list, reader.ReadObjectElement( "data" ) );
                    Assert.That( reader.ReadObjectElement( "After" ), Is.EqualTo( 3712 * 2 ) );
                }
            }
        }