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.Xml; 13using NUnit.Framework; 14using BerkeleyDB; 15 16namespace CsharpAPITest 17{ 18 [TestFixture] 19 public class SequenceConfigTest 20 { 21 private string testFixtureHome; 22 private string testFixtureName; 23 private string testName; 24 private string testHome; 25 26 [TestFixtureSetUp] 27 public void RunBeforeTests() 28 { 29 testFixtureName = "SequenceConfigTest"; 30 testFixtureHome = "./TestOut/" + testFixtureName; 31 32 Configuration.ClearDir(testFixtureHome); 33 } 34 35 36 [Test] 37 public void TestConfig() 38 { 39 testName = "TestConfig"; 40 41 SequenceConfig seqConfig = new SequenceConfig(); 42 XmlElement xmlElem = Configuration.TestSetUp( 43 testFixtureName, testName); 44 Config(xmlElem, ref seqConfig, true); 45 Confirm(xmlElem, seqConfig, true); 46 } 47 48 [Test] 49 public void TestConfigObj() 50 { 51 testName = "TestConfigObj"; 52 testHome = testFixtureHome + "/" + testName; 53 string dbFileName = testHome + "/" + testName + ".db"; 54 55 Configuration.ClearDir(testHome); 56 57 // Open a database. 58 BTreeDatabaseConfig btreeDBConfig = 59 new BTreeDatabaseConfig(); 60 btreeDBConfig.Creation = CreatePolicy.IF_NEEDED; 61 BTreeDatabase btreeDB = BTreeDatabase.Open( 62 dbFileName, btreeDBConfig); 63 64 /* Configure and initialize sequence. */ 65 SequenceConfig seqConfig = new SequenceConfig(); 66 seqConfig.BackingDatabase = btreeDB; 67 seqConfig.Creation = CreatePolicy.IF_NEEDED; 68 seqConfig.key = new DatabaseEntry( 69 ASCIIEncoding.ASCII.GetBytes("key")); 70 seqConfig.SetRange(Int64.MinValue, Int64.MaxValue); 71 Sequence seq = new Sequence(seqConfig); 72 73 // Confirm the objects set in SequenceConfig. 74 Assert.AreEqual(dbFileName, 75 seq.BackingDatabase.FileName); 76 Assert.AreEqual(ASCIIEncoding.ASCII.GetBytes("key"), 77 seq.Key.Data); 78 Assert.AreEqual(Int64.MinValue, seq.Min); 79 Assert.AreEqual(Int64.MaxValue, seq.Max); 80 81 /* Close sequence, database and environment. */ 82 seq.Close(); 83 btreeDB.Close(); 84 } 85 86 public static void Confirm(XmlElement xmlElement, 87 SequenceConfig seqConfig, bool compulsory) 88 { 89 Configuration.ConfirmInt(xmlElement, "CacheSize", 90 seqConfig.CacheSize, compulsory); 91 Configuration.ConfirmCreatePolicy(xmlElement, "Creation", 92 seqConfig.Creation, compulsory); 93 Configuration.ConfirmBool(xmlElement, "Decrement", 94 seqConfig.Decrement, compulsory); 95 Configuration.ConfirmBool(xmlElement, "FreeThreaded", 96 seqConfig.FreeThreaded, compulsory); 97 Configuration.ConfirmBool(xmlElement, "Increment", 98 seqConfig.Increment, compulsory); 99 Configuration.ConfirmLong(xmlElement, "InitialValue", 100 seqConfig.InitialValue, compulsory); 101 Configuration.ConfirmBool(xmlElement, "Wrap", 102 seqConfig.Wrap, compulsory); 103 } 104 105 public static void Config(XmlElement xmlElement, 106 ref SequenceConfig seqConfig, bool compulsory) 107 { 108 int intValue = new int(); 109 bool boolValue = new bool(); 110 long longValue = new long(); 111 112 if (Configuration.ConfigInt(xmlElement, "CacheSize", 113 ref intValue, compulsory)) 114 seqConfig.CacheSize = intValue; 115 Configuration.ConfigCreatePolicy(xmlElement, "Creation", 116 ref seqConfig.Creation, compulsory); 117 if (Configuration.ConfigBool(xmlElement, "Decrement", 118 ref boolValue, compulsory)) 119 seqConfig.Decrement = boolValue; 120 Configuration.ConfigBool(xmlElement, "FreeThreaded", 121 ref seqConfig.FreeThreaded, compulsory); 122 if (Configuration.ConfigBool(xmlElement, "Increment", 123 ref boolValue, compulsory)) 124 seqConfig.Increment = boolValue; 125 if (Configuration.ConfigLong(xmlElement, "InitialValue", 126 ref longValue, compulsory)) 127 seqConfig.InitialValue = longValue; 128 Configuration.ConfigBool(xmlElement, "Wrap", 129 ref seqConfig.Wrap, compulsory); 130 } 131 } 132} 133