Me Love You Runtime Code Monkey!

Resetting Oracle SYSTEM user password

I recently got locked out of my Oracle DB (before you think I am an Oracle fan, I am not, but hopefully this will help others who have had the struggle). Someone had changed the SYSTEM user password and not noted it down. I managed to work out a way of changing this. You do need access to the DB box of course. Then you need to go into SQL Plus, Oracle’s Command Line tool.

To open this time though, type:

sqlplus "/ as sysdba"

You should be logged into Oracle now. Have a look at whether you are now a SYS user by trying:

SQL> show user

It should respond as:

USER is "SYS"

If all is well by this point, you can then change the SYSTEM password.

SQL> passw system

Good luck to you.

Unit Testing Data Annotations

For home projects done in asp.net I tend to use the DataAnnotations on my models for validation. Of course anyone would want to write a unit test to make sure this is all in order. Let’s take some very simple test, I want to check my model is not validating when given nothing:

var controller = new HomeController(guestRepository.Object);
controller.Index(viewModel);
var isValid = controller.ViewData.ModelState.IsValid;

Assert.Equal(isValid, false);

I’ve put mocked up my repository and then call the method I want to test, in this case Index. I check the ModelState, but it says everything is fine, all valid and this fails. Damn it!

It’s all about context, as a unit test runs in a different context (not the web context), the model binder is never invoked. So it thinks the model is fine and valid.

I have a nice little helper for this which basically calls the model binder so we can test this:

public static void ValidateHelper(Controller controller, T model)
        {
            var validationContext = new ValidationContext(model, null, null);
            var validationResults = new List();
            Validator.TryValidateObject(model, validationContext, validationResults, true);
            foreach (var validationResult in validationResults)
                controller.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
        }

(make sure you are using using System.ComponentModel.DataAnnotations and System.Web.Mvc).

I made this generic so I can use different model types. You then just call this method before invoking the Index method, and you should be good.

NVelocity & calling functions

The other day a dev was using NVelocity for templating emails. He realised that When sending an email, you would need to html encode the data to stop Cross Site Scripting (XSS). Not so much javascript, but being able to embed an image or link which could be malicious. He discovered it is possible to call functions within NVelocity like:

Your Utility:

public class HtmlHelper
{
	public static string HtmlEncode(string value)
	{
		return HttpUtility.HtmlEncode(value);
	}
}

Pass to NVelocity:

var context = new VelocityContext(parameters);
context.Put("helper", new HtmlHelper());

And in your template you can then use your function:

$helper.HtmlEncode($user.FirstName)

Of course, nowadays, we’d tend to use Razor for templating. But this could be useful for your legacy projects or if you still have a soft spot for NVelocity.

Don't forget your (XPATH) descendants

Recently at work, we’ve been getting into Selenium tests for regression testing. With this comes a lot of XPath. I love a bit of XPath, and along with Regex and XSLT as long as you use the KISS principle, everything will be ok.

A developer was working on getting each link in a page within a div to be checked. The issue was that these could be on different levels (it was a Sitemap). e.g. one link may be in a list item, the next may be embedded within another sub-list item.

We had real issues in getting all the links in the right way. We tried:

//div[@id='mod-site-map']//a

(The a’s would be iterated through) with varying degrees of success.

I’d forgotten descendants completely. In a head slapping moment, we tried:

//div[@id='mod-site-map']/descendant::a

Done! One of my favourite XPath reference sites is still: http://zvon.org/xxl/XPathTutorial/General/examples.html. It’s been there since about 1997, but still helps out.

MS package and deploy

I’ve been looking into better ways of deploying recently. Microsoft have released their deploy tool which seems very capable, but it is quite tricky (mainly as it is a product which needs to cater for all types of environment setup within .net). It’s very simple to set up within Visual Studio 2010 once you have set up IIS.

All I need to do is run a package and push it to my server using MS Deploy. Once I have the commands I can easily add to my nant build script and then run a package and deploy from my build server (Bamboo) or, even better, use the Jira deploy integration with Bamboo and deploy from there. Well, let’s not go too crazy with that, that’s a whole other post right there. Let’s jump into some commands.

If I change directory (cd) to the site I want to deploy, go to the site project (I usually call this [projectname].Site mimicking my work’s standard). Once there, I just build my site project with msbuild: [path to msbuild, usually C:WindowsMicrosoft.NETFramework64v4.0.30319]msbuild.exe “MyProject.Site.csproj” /T:Package /P:Configuration=Release /P:PackageLocation=”D:ProjectsDeploymentMyProject.zip”

Let’s run through this. I execute msbuild pointing to my csproj (or vbproj if you are that way incline) file for the site I want to deploy. The /T:Package makes sure I am packaging that project, I set the /P:Configuration to release so it will take my relevant web.config transform and build in the correct way. PackageLocation says where to put my package as a zip.

So that’s the package done. I hope you’re with me so far. Next is the push to server. If you have read Scott Gu’s post linked to above, it shows you how to set up IIS to accept this type of deploy. Then you just need to change directory (cd) to where your package is built, it will have also added a few extra files, the one we want is MyProject.deploy.cmd. It’s just a command line template to run for this project. Run this:

MyProject.deploy.cmd /y /M:https://WebDeployUrl:8172/MsDeploy.axd /u:username /p:password –allowUntrusted /A:basic

fill in the server URL and username and password depending on your setup, and BAM! Project deployed. This can also be set up for an SSL connection. This is a simple view of how to do this, and it can be customised well so you can add to build servers, version etc.