main
 1using System.Collections.Generic;
 2using System.IO;
 3using Db4objects.Db4o;
 4using developwithpassion.bdd.contexts;
 5using Gorilla.Commons.Testing;
 6using gorilla.commons.utility;
 7
 8namespace momoney.database.db4o.Spiking
 9{
10    [Concern(typeof (Db4oFactory))]
11    public class when_opening_an_existing_database_ : concerns
12    {
13        before_each_observation be = () => {};
14
15        context c = () =>
16        {
17            original = new TestObject(88, "mo");
18            the_database_file = Path.GetTempFileName();
19            var configuration = Db4oFactory.NewConfiguration();
20            configuration.LockDatabaseFile(false);
21            database = Db4oFactory.OpenFile(configuration, the_database_file);
22        };
23
24        because b = () =>
25        {
26            database.Store(original);
27            database.Close();
28            database.Dispose();
29            database = Db4oFactory.OpenFile(the_database_file);
30            results = database.Query<ITestObject>().databind();
31        };
32
33        it should_be_able_to_load_the_original_contents = () => results.should_contain(original);
34
35        it they_should_be_equal = () => new TestObject(99, "gretzky").Equals(new TestObject(99, "gretzky"));
36
37        it should_only_contain_the_original_item = () => results.Count.should_be_equal_to(1);
38
39        after_each_observation ae = () =>
40        {
41            database.Close();
42            database.Dispose();
43        };
44
45        static ITestObject original;
46        static string the_database_file;
47        static IList<ITestObject> results;
48        static IObjectContainer database;
49    }
50
51    public interface ITestObject
52    {
53        int Id { get; }
54        string Name { get; }
55    }
56
57    public class TestObject : ITestObject
58    {
59        public TestObject(int id, string name)
60        {
61            Id = id;
62            Name = name;
63        }
64
65        public int Id { get; private set; }
66        public string Name { get; private set; }
67
68        public bool Equals(TestObject obj)
69        {
70            if (ReferenceEquals(null, obj)) return false;
71            if (ReferenceEquals(this, obj)) return true;
72            return obj.Id == Id && Equals(obj.Name, Name);
73        }
74
75        public override bool Equals(object obj)
76        {
77            if (ReferenceEquals(null, obj)) return false;
78            if (ReferenceEquals(this, obj)) return true;
79            if (obj.GetType() != typeof (TestObject)) return false;
80            return Equals((TestObject) obj);
81        }
82
83        public override int GetHashCode()
84        {
85            unchecked
86            {
87                return (Id*397) ^ (Name != null ? Name.GetHashCode() : 0);
88            }
89        }
90    }
91}