• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/db-4.8.30/test/scr037/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2009 Oracle.  All rights reserved.
5 *
6 */
7using System;
8using System.Collections;
9using System.Collections.Generic;
10using System.IO;
11using System.Text;
12using System.Threading;
13using NUnit.Framework;
14using BerkeleyDB;
15
16namespace CsharpAPITest
17{
18	[TestFixture]
19	public class RecnoCursorTest
20	{
21		private string testFixtureName;
22		private string testFixtureHome;
23		private string testName;
24		private string testHome;
25
26		[TestFixtureSetUp]
27		public void RunBeforeTests()
28		{
29			testFixtureName = "RecnoCursorTest";
30			testFixtureHome = "./TestOut/" + testFixtureName;
31		}
32
33		[Test]
34		public void TestCursor()
35		{
36			testName = "TestCursor";
37			testHome = testFixtureHome + "/" + testName;
38
39			Configuration.ClearDir(testHome);
40
41			GetCursorWithImplicitTxn(testHome,
42			    testName + ".db", false);
43		}
44
45		[Test]
46		public void TestCursorWithConfig()
47		{
48			testName = "TestCursorWithConfig";
49			testHome = testFixtureHome + "/" + testName;
50
51			Configuration.ClearDir(testHome);
52
53			GetCursorWithImplicitTxn(testHome,
54			    testName + ".db", true);
55		}
56
57		public void GetCursorWithImplicitTxn(string home,
58		    string dbFile, bool ifConfig)
59		{
60			DatabaseEnvironmentConfig envConfig =
61			    new DatabaseEnvironmentConfig();
62			envConfig.Create = true;
63			envConfig.UseCDB = true;
64			envConfig.UseMPool = true;
65			DatabaseEnvironment env = DatabaseEnvironment.Open(
66			    home, envConfig);
67
68			RecnoDatabaseConfig dbConfig =
69			    new RecnoDatabaseConfig();
70			dbConfig.Creation = CreatePolicy.IF_NEEDED;
71			dbConfig.Env = env;
72			RecnoDatabase db = RecnoDatabase.Open(dbFile,
73			    dbConfig);
74
75			RecnoCursor cursor;
76			if (ifConfig == false)
77				cursor = db.Cursor();
78			else
79				cursor = db.Cursor(new CursorConfig());
80
81			cursor.Close();
82			db.Close();
83			env.Close();
84		}
85
86		[Test]
87		public void TestCursorInTxn()
88		{
89			testName = "TestCursorInTxn";
90			testHome = testFixtureHome + "/" + testName;
91
92			Configuration.ClearDir(testHome);
93
94			GetCursorWithExplicitTxn(testHome,
95			    testName + ".db", false);
96		}
97
98		[Test]
99		public void TestConfigedCursorInTxn()
100		{
101			testName = "TestConfigedCursorInTxn";
102			testHome = testFixtureHome + "/" + testName;
103
104			Configuration.ClearDir(testHome);
105
106			GetCursorWithExplicitTxn(testHome,
107			    testName + ".db", true);
108		}
109
110		public void GetCursorWithExplicitTxn(string home,
111		    string dbFile, bool ifConfig)
112		{
113			DatabaseEnvironmentConfig envConfig =
114			    new DatabaseEnvironmentConfig();
115			envConfig.Create = true;
116			envConfig.UseTxns = true;
117			envConfig.UseMPool = true;
118			DatabaseEnvironment env = DatabaseEnvironment.Open(
119			    home, envConfig);
120
121			Transaction openTxn = env.BeginTransaction();
122			RecnoDatabaseConfig dbConfig =
123			    new RecnoDatabaseConfig();
124			dbConfig.Creation = CreatePolicy.IF_NEEDED;
125			dbConfig.Env = env;
126			RecnoDatabase db = RecnoDatabase.Open(dbFile,
127			    dbConfig, openTxn);
128			openTxn.Commit();
129
130			Transaction cursorTxn = env.BeginTransaction();
131			RecnoCursor cursor;
132			if (ifConfig == false)
133				cursor = db.Cursor(cursorTxn);
134			else
135				cursor = db.Cursor(new CursorConfig(), cursorTxn);
136			cursor.Close();
137			cursorTxn.Commit();
138			db.Close();
139			env.Close();
140		}
141
142		[Test]
143		public void TestDuplicate()
144		{
145			KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
146			RecnoDatabase db;
147			RecnoDatabaseConfig dbConfig;
148			RecnoCursor cursor, dupCursor;
149			string dbFileName;
150
151			testName = "TestDuplicate";
152			testHome = testFixtureHome + "/" + testName;
153			dbFileName = testHome + "/" + testName + ".db";
154
155			Configuration.ClearDir(testHome);
156
157			dbConfig = new RecnoDatabaseConfig();
158			dbConfig.Creation = CreatePolicy.IF_NEEDED;
159			db = RecnoDatabase.Open(dbFileName, dbConfig);
160			cursor = db.Cursor();
161
162			/*
163			 * Add a record(1, 1) by cursor and move
164			 * the cursor to the current record.
165			 */
166			AddOneByCursor(cursor);
167			cursor.Refresh();
168
169			//Duplicate a new cursor to the same position.
170			dupCursor = cursor.Duplicate(true);
171
172			// Overwrite the record.
173			dupCursor.Overwrite(new DatabaseEntry(
174			    ASCIIEncoding.ASCII.GetBytes("newdata")));
175
176			// Confirm that the original data doesn't exist.
177			pair = new KeyValuePair<DatabaseEntry, DatabaseEntry>(
178			    new DatabaseEntry(
179			    BitConverter.GetBytes((int)1)),
180			    new DatabaseEntry(
181			    BitConverter.GetBytes((int)1)));
182			Assert.IsFalse(dupCursor.Move(pair, true));
183
184			dupCursor.Close();
185			cursor.Close();
186			db.Close();
187		}
188
189		[Test]
190		public void TestInsertToLoc()
191		{
192			RecnoDatabase db;
193			RecnoDatabaseConfig dbConfig;
194			RecnoCursor cursor;
195			DatabaseEntry data;
196			KeyValuePair<DatabaseEntry, DatabaseEntry> pair;
197			string dbFileName;
198
199			testName = "TestInsertToLoc";
200			testHome = testFixtureHome + "/" + testName;
201			dbFileName = testHome + "/" + testName + ".db";
202
203			Configuration.ClearDir(testHome);
204
205			// Open database and cursor.
206			dbConfig = new RecnoDatabaseConfig();
207			dbConfig.Creation = CreatePolicy.IF_NEEDED;
208			dbConfig.Renumber = true;
209			db = RecnoDatabase.Open(dbFileName, dbConfig);
210			cursor = db.Cursor();
211
212			// Add record(1,1) into database.
213			/*
214			 * Add a record(1, 1) by cursor and move
215			 * the cursor to the current record.
216			 */
217			AddOneByCursor(cursor);
218			cursor.Refresh();
219
220			/*
221			 * Insert the new record(1,10) after the
222			 * record(1,1).
223			 */
224			data = new DatabaseEntry(
225				BitConverter.GetBytes((int)10));
226			cursor.Insert(data, Cursor.InsertLocation.AFTER);
227
228			/*
229			 * Move the cursor to the record(1,1) and
230			 * confirm that the next record is the one just inserted.
231			 */
232			pair = new KeyValuePair<DatabaseEntry, DatabaseEntry>(
233			    new DatabaseEntry(
234			    BitConverter.GetBytes((int)1)),
235			    new DatabaseEntry(
236			    BitConverter.GetBytes((int)1)));
237			Assert.IsTrue(cursor.Move(pair, true));
238			Assert.IsTrue(cursor.MoveNext());
239			Assert.AreEqual(BitConverter.GetBytes((int)10),
240			    cursor.Current.Value.Data);
241
242			cursor.Close();
243			db.Close();
244		}
245
246		public void AddOneByCursor(RecnoCursor cursor)
247		{
248			KeyValuePair<DatabaseEntry, DatabaseEntry> pair =
249			    new KeyValuePair<DatabaseEntry, DatabaseEntry>(
250			    new DatabaseEntry(
251			    BitConverter.GetBytes((int)1)),
252			    new DatabaseEntry(
253			      BitConverter.GetBytes((int)1)));
254			cursor.Add(pair);
255		}
256	}
257}
258