DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ContextBasedTest.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 using System;
16 
18 using Deveel.Data.Services;
20 
21 using NUnit.Framework;
22 
23 namespace Deveel.Data {
24  [TestFixture]
25  public abstract class ContextBasedTest {
26  protected const string AdminUserName = "SA";
27  protected const string AdminPassword = "1234567890";
28  protected const string DatabaseName = "testdb";
29 
30  protected virtual bool SingleContext {
31  get { return false; }
32  }
33 
34  protected IQuery Query { get; private set; }
35 
36  protected ISystem System { get; private set; }
37 
38  protected IDatabase Database { get; private set; }
39 
40  protected ISession Session { get; private set; }
41 
42  protected virtual void RegisterServices(ServiceContainer container) {
43  }
44 
45  protected virtual ISystem CreateSystem() {
46  var builder = new TestSystemBuilder(this);
47  return builder.BuildSystem();
48  }
49 
50  protected virtual IDatabase CreateDatabase(ISystem system, IConfiguration configuration) {
51  return system.CreateDatabase(configuration, AdminUserName, AdminPassword);
52  }
53 
54  protected virtual ISession CreateAdminSession(IDatabase database) {
55  var user = database.Authenticate(AdminUserName, AdminPassword);
56  var transaction = database.CreateTransaction(IsolationLevel.Serializable);
57  return new Session(transaction, user);
58  }
59 
60  protected virtual IQuery CreateQuery(ISession session) {
61  return session.CreateQuery();
62  }
63 
64  protected ISession CreateUserSession(string userName, string password) {
65  return Database.CreateUserSession(userName, password);
66  }
67 
68  protected virtual void OnSetUp(string testName) {
69 
70  }
71 
72  protected virtual void OnTearDown() {
73 
74  }
75 
76  [SetUp]
77  public void TestSetUp() {
78  if (!SingleContext)
79  CreateContext();
80 
81  var testName = TestContext.CurrentContext.Test.Name;
82  OnSetUp(testName);
83  }
84 
85  [TestFixtureSetUp]
86  public void TestFixtureSetUp() {
87  if (SingleContext)
88  CreateContext();
89  }
90 
91  private void CreateContext() {
92  System = CreateSystem();
93 
94  var dbConfig = new Configuration.Configuration();
95  dbConfig.SetValue("database.name", DatabaseName);
96 
97  Database = CreateDatabase(System, dbConfig);
98  Session = CreateAdminSession(Database);
99  Query = CreateQuery(Session);
100  }
101 
102  private void DisposeContext() {
103  if (Query != null)
104  Query.Dispose();
105 
106  if (Database != null)
107  Database.Dispose();
108 
109  if (System != null)
110  System.Dispose();
111 
112  Query = null;
113  Database = null;
114  Database = null;
115  System = null;
116  }
117 
118  [TearDown]
119  public void TestTearDown() {
120  OnTearDown();
121 
122  if (!SingleContext)
123  DisposeContext();
124  }
125 
126  [TestFixtureTearDown]
127  public void TestFixtureTearDown() {
128  if (SingleContext)
129  DisposeContext();
130  }
131 
134 
136  this.test = test;
137  }
138 
139  protected override void OnServiceRegistration(ServiceContainer container) {
140  test.RegisterServices(container);
141  }
142  }
143  }
144 }
virtual IQuery CreateQuery(ISession session)
virtual ISystem CreateSystem()
virtual ISession CreateAdminSession(IDatabase database)
The default implementation of a database in a system.
Definition: Database.cs:38
IDatabase CreateDatabase(IConfiguration configuration, string adminUser, string adminPassword)
override void OnServiceRegistration(ServiceContainer container)
The representation of a single database in the system.
Definition: IDatabase.cs:40
virtual IDatabase CreateDatabase(ISystem system, IConfiguration configuration)
This is a session that is constructed around a given user and a transaction, to the given database...
Definition: Session.cs:32
ISession CreateUserSession(string userName, string password)
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
void Dispose()
Definition: Query.cs:80
virtual void OnSetUp(string testName)
Defines the contract for the configuration node of a component within the system or of the system its...
virtual void RegisterServices(ServiceContainer container)
Configuration(bool isRoot)
Constructs the Configuration.