using Perforce.P4;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
namespace UnitTests
{
///
///This is a test class for P4UserTest and is intended
///to contain all P4UserTest Unit Tests
///
[TestClass()]
public class P4UserTest
{
String TestDir = "c:\\MyTestDir";
private TestContext testContextInstance;
///
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion
///
///A test for Fetch
///
[TestMethod()]
public void FetchTest()
{
bool unicode = false;
string server = "localhost:6666";
string user = "admin";
string pass = string.Empty;
string ws_client = "admin_space";
for (int i = 0; i < 2; i++) // run once for ascii, once for unicode
{
Process p4d = Utilities.DeployP4TestServer(TestDir, unicode);
try
{
using (P4Server target = new P4Server(server, user, pass, ws_client))
{
P4User actual = P4User.Fetch(target, "admin");
Assert.IsNotNull(actual);
Assert.AreEqual("admin",actual.User);
}
}
catch (Exception ex)
{
Assert.Fail("Exception Thrown: {0} : {1}", ex.ToString(), ex.Message);
}
finally
{
Utilities.RemoveTestServer(p4d, TestDir);
}
unicode = !unicode;
}
}
///
///A test for Delete
///
[TestMethod()]
public void DeleteTest()
{
bool unicode = false;
string server = "localhost:6666";
string user = "admin";
string pass = string.Empty;
string ws_client = "admin_space";
for (int i = 0; i < 2; i++) // run once for ascii, once for unicode
{
Process p4d = Utilities.DeployP4TestServer(TestDir, unicode);
try
{
using (P4Server target = new P4Server(server, user, pass, ws_client))
{
P4User actual = P4User.Fetch(target, "admin");
Assert.IsNotNull(actual);
actual.Delete(true);
}
}
catch (Exception ex)
{
Assert.Fail("Exception Thrown: {0} : {1}", ex.ToString(), ex.Message);
}
finally
{
Utilities.RemoveTestServer(p4d, TestDir);
}
unicode = !unicode;
}
}
///
///A test for Fetch
///
[TestMethod()]
public void FetchTest1()
{
bool unicode = false;
string server = "localhost:6666";
string pass = string.Empty;
string ws_client = "admin_space";
for (int i = 0; i < 2; i++) // run once for ascii, once for unicode
{
Process p4d = Utilities.DeployP4TestServer(TestDir, unicode);
try
{
String user = "Alex";
if (unicode)
user = "Алексей";
using (P4Server target = new P4Server(server, "admin", pass, ws_client))
{
P4User actual = new P4User(target, user);
actual.Fetch();
Assert.IsNotNull(actual);
Assert.AreEqual(actual.User, user);
}
}
catch (Exception ex)
{
Assert.Fail("Exception Thrown: {0} : {1}", ex.ToString(), ex.Message);
}
finally
{
Utilities.RemoveTestServer(p4d, TestDir);
}
unicode = !unicode;
}
}
///
///A test for FromTaggedOutput
///
///
// Tested by the Fetch tests
//[TestMethod()]
//public void FromTaggedOutputTest()
//{
//}
///
///A test for Parse
///
[TestMethod()]
public void ParseTest()
{
P4Server pserver = new P4Server(false);
P4User target = new P4User(pserver, "fred"); // TODO: Initialize to an appropriate value
target.Parse(spec);
Assert.AreEqual(target.User, "fred");
}
private String spec =
@"# A Perforce User Specification.
#
# User: The user's user name.
# Type: Either 'service' or 'standard'. Default is 'standard'.
# Email: The user's email address; for email review.
# Update: The date this specification was last modified.
# Access: The date this user was last active. Read only.
# FullName: The user's real name.
# JobView: Selects jobs for inclusion during changelist creation.
# Password: If set, user must have matching $P4PASSWD on client.
# PasswordChange:
# The date this password was last changed. Read only.
# Reviews: Listing of depot files to be reviewed by user.
User: fred
Email: fred@unittests.com
Update: 2010/07/30 16:17:35
Access: 2010/12/01 15:18:55
FullName: Fred Farnsworth
JobView: type=bug & ^status=closed
Reviews:
//depot/poetry\r\n
//depot/movies\r\n
Password: ******";
///
///A test for Save
///
[TestMethod()]
public void SaveTest()
{
bool unicode = false;
string server = "localhost:6666";
string user = "admin";
string pass = string.Empty;
string ws_client = "admin_space";
for (int i = 0; i < 2; i++) // run once for ascii, once for unicode
{
Process p4d = Utilities.DeployP4TestServer(TestDir, unicode);
try
{
using (P4Server target = new P4Server(server, user, pass, ws_client))
{
P4User actual = P4User.Fetch(target, "admin");
Assert.IsNotNull(actual);
actual.FullName = "Steady Freddy";
actual.Save( false );
actual = P4User.Fetch(target, "admin");
Assert.IsNotNull(actual);
Assert.AreEqual(actual.FullName, "Steady Freddy");
}
}
catch (Exception ex)
{
Assert.Fail("Exception Thrown: {0} : {1}", ex.ToString(), ex.Message);
}
finally
{
Utilities.RemoveTestServer(p4d, TestDir);
}
unicode = !unicode;
}
}
///
///A test for ToString
///
// ToString is invoke during the Save Test, so no separate test is needed.
//[TestMethod()]
//public void ToStringTest()
//{
//}
///
///A test for Access
///
//[TestMethod()]
//public void AccessTest()
//{
// P4Server pserver = null;
// P4User target = new P4User(pserver, "Fred");
// DateTime expected = DateTime.Now;
// DateTime actual;
// target.LastAccess = expected;
// actual = target.LastAccess;
// Assert.AreEqual(expected, actual);
//}
///
///A test for Email
///
[TestMethod()]
public void EmailTest()
{
P4Server pserver = null;
P4User target = new P4User(pserver, "Fred");
string expected = "HandyManny@sheetrockhills.dc";
string actual;
target.Email = expected;
actual = target.Email;
Assert.AreEqual(expected, actual);
}
///
///A test for FullName
///
[TestMethod()]
public void FullNameTest()
{
P4Server pserver = null;
P4User target = new P4User(pserver, "Fred");
string expected = "Handy Manny";
string actual;
target.FullName = expected;
actual = target.FullName;
Assert.AreEqual(expected, actual);
}
///
///A test for JobView
///
[TestMethod()]
public void JobViewTest()
{
P4Server pserver = null;
P4User target = new P4User(pserver, "Fred");
string expected = "type=bug & ^status=closed";
string actual;
target.JobView = expected;
actual = target.JobView;
Assert.AreEqual(expected, actual);
}
///
///A test for Password
///
[TestMethod()]
public void PasswordTest()
{
P4Server pserver = null;
P4User target = new P4User(pserver, "Fred");
string expected = "Password";
string actual;
target.Password = expected;
actual = target.Password;
Assert.AreEqual(expected, actual);
}
///
///A test for Reviews
///
[TestMethod()]
public void ReviewsTest()
{
P4Server pserver = null;
P4User target = new P4User(pserver, "Fred");
StringList expected = new StringList();
expected.Add("//depot/poetry");
expected.Add("//depot/movies");
StringList actual;
target.Reviews = expected;
actual = target.Reviews;
Assert.AreEqual(expected, actual);
}
///
///A test for Update
///
//[TestMethod()]
//public void UpdateTest()
//{
// P4Server pserver = null;
// P4Workspace target = new P4Workspace(pserver, "Fred");
// DateTime expected = DateTime.Now;
// DateTime actual;
// target.LastUpdate = expected;
// actual = target.LastUpdate;
// Assert.AreEqual(expected, actual);
//}
///
///A test for User
///
[TestMethod()]
public void UserTest()
{
P4Server pserver = null;
string expected = "Charlie";
P4User target = new P4User(pserver, expected);
string actual;
// target.User = expected; set in constructor
actual = target.User;
Assert.AreEqual(expected, actual);
}
}
}