Prash's Blog

Unable to bind parameter id that is oracle guid type raw using ODP.Net September 28, 2012

Filed under: Uncategorized — prazjain @ 7:10 pm
Tags: , , , ,

I have had this problem of trying to save a GUID in oracle as type RAW.

When using oracle version 11.2.0.3, there was no problem saving it the way it was.

Working version for 11.2.0.3 :

cmd.Parameters.Add(":id", OracleDbType.Raw, ourCombinedList.OrderBy(x => x.ID).Select(e => e.ID).ToArray(), ParameterDirection.Input);

But when we downgraded to oracle version 11.2.0.2, this threw an error, saying unable to bind parameter id (which was a type GUID) to oracle raw types.

Workaround version for 11.2.0.2 :

cmd.Parameters.Add(":id", OracleDbType.Raw, ourCombinedList.OrderBy(x => x.ID).Select(e => e.ID.ToByteArray()).ToArray(), ParameterDirection.Input);
Advertisements
 

Querying XML with XPath in C#

Filed under: Uncategorized — prazjain @ 6:49 pm
Tags: ,

There have been so many times I had to resort to string comparision on XMLs, but this a handy article to using XPath for querying and even comparing values in XML through C#.

http://support.microsoft.com/kb/308333

http://www.codeproject.com/Articles/52079/Using-XPathNavigator-in-C

 

BadImageFormatException : An attempt was made to load a program with an incorrect format.

Filed under: Uncategorized — prazjain @ 9:00 am
Tags:

System.InvalidOperationException : Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.

System.BadImageFormatException : An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

This is the error I got when running nunit tests in my 64 bit Windows 7 machine.
I had 64 bit VS 2010, 64 bit Oracle installed.

I had nunit plugin for Visual studio, and I could right click on a test project and select “Test with NUnit”, but this was always loading up nunit-86 which is 32 bit version of nunit.
This meant it was loading up 32 Oracle client lib.

So I added nunit/bin dir to my path and loading nunit.exe from bin/debug of the test project dir and ran the dll in 64 bit nunit exe and lo and behold my tests pass!

 

Mocking internal methods with Moq in C# September 25, 2012

Filed under: Uncategorized — prazjain @ 12:20 pm
Tags: , ,

I have been using Moq for some time now and love it, but had one frustrating nag, that I could not moq internal methods for some reason.
But after several frustrating hours of figuring out how to moq internal methods in moq. This is how I did it.

In the assemblyinfo.cs file for the project (that has the source you want to test).
Add a line as below :

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

Interesting thing to note here is that to mock internal method it must be virtual. eg:

internal MI5Employee Search(string name)
{
    // other methods like this one will call the virtual method, so the virtual method can be mocked when this method is being tested.
    int id = GetEmployeeId(name);
    // next id is used below to do some other work.

   return myEmpObj;
}
internal virtual int GetEmployeeId(string name) { .. }

So for a method like this in Moq, you can mock it like this :

var mi5EmployeeMock = new Mock<MI5Employee>();
mi5EmployeeMock.SetUp(mi5EmployeeMock.GetEmployeeId("prash")).Returns(007);
// now the method is setup and any method you are testing that makes this method call internally then it will return 7. 
MI5Employee emp = mi5EmployeeMock.Object.Search("prash");
// this test will pass
Assert.IsTrue(emp.Id== 7); 

Now the tricky part is, if your class does not have an interface and you have to test the same method.

var mi5EmployeeMock = new Mock<MI5Employee>();
// lets assume all its dependencies are set up here
int res = mi5EmployeeMock.Object.GetEmployeeId("prash");
// this test will fail
Assert.IsTrue(res == 7, "This test fails to find James Bond!");

Here the test will fail, as res will have value 0, that is because the method GetEmployeeId is set as virtual and Moq cannot test virtual methods, it just will not call its actual implementation.
So though we are able to unit test Seach method we are not able to unit test GetEmployeeId method using Moq.
The way I got around it is by having another implementation that is non virtual and calling that from the virtual method. eg.

internal MI5Employee Search(string name)
{
    // other methods like this one will call the virtual method, so the virtual method can be mocked when this method is being tested.
    int id = GetEmployeeId(name);
    // next id is used below to do some other work.

   return myEmpObj;
}
// this method is used for mocking and called directly by other methods.
internal virtual int GetEmployeeId(string name) 
{
   return GetEmployeeId_NonVirtual(name);
}
// this method exists solely for testing GetEmployeeId logic using Moq.
internal int GetEmployeeId_NonVirtual(string name) { .. }

So our test case will look a bit like this :

var mi5EmployeeMock = new Mock<MI5Employee>();
// lets assume all its dependencies are set up here
int res = mi5EmployeeMock.Object.GetEmployeeId_NonVirtual("prash");
// this test will pass
Assert.IsTrue(res == 7);

This may not be elegant but it works and I get test all my code. Green is Good.

 

Handy Commands to troubleshoot linux box September 9, 2012

Filed under: Linux — prazjain @ 12:32 am

Some handy links for troubleshooting linux machine.

http://www.cyberciti.biz/faq/show-all-running-processes-in-linux/.

Another link contains a video on top command (helpful) : http://www.linuxnix.com/2011/08/linux-top-command-explained-2.html

I am trying to solve a problem on my linux box, the error is very similar to this : http://serverfault.com/questions/301531/java-eats-100-cpu-up, but it keeps me baffled.

 

Get Java Timezone list September 8, 2012

Filed under: Java — prazjain @ 7:48 pm
Tags: ,

If you have had same problem as me and you needed to know the java timezone id / name for a particular city / country and could not figure that out.
Here is a code snippet that can help you do that.

import java.util.*;

public class TestZones
{
	public static void main(String[] args)
	{
		for (String string : TimeZone.getAvailableIDs(TimeZone.getTimeZone("GMT+08:00").getRawOffset())) 
		{
    			System.out.println(string);
		}
	}
}
 

Android browser home page hijacked by searchmobileonline.com

Filed under: Android — prazjain @ 10:30 am
Tags: , , ,

I got my Android browser home page hijacked by searchmobileonline.com. I traced it to installing a random app “Remote Gallery 3d” I found on a blog. This app also created a shortcut “Search” in my android shortcut panel.
This is how I got rid of it, hope it will help you too.
1) Remove “Search” from the shortcut panel in android.
2) Go to Settings -> Applications -> All -> Internet.
3) Clear cache data and clear data.
4) Restart Android browser to check.