Saturday, August 23, 2014

Unit Testing C#

Unit test shown below are done with Microsoft unit test framework. Unit test is done to make sure properties, attributes and actions work as expected. A Professional IDE for developers needs to have unit test support since unit testing is a requirement in quality software development. Visual studio being a professional IDE, has capabilities for unit testing including the test explorer Microsoft cited to run unit tests efficiently. Moreover, Visual studio has capabilities in simplifying unit test creation. There are test cases and annotations already so Visual Studio is aware if a class is intended for unit testing or not. Microsoft recommended that test methods are made as soon as the method was created to define and check expectations.
First make sure there is/are class/classes to test on. Afterwards, add a test project in the solution. The test project is where test classes can be created and defined.


Next, make sure the test project has reference of the project that has the classes to be tested. This step ensures that the test classes (or the classes with test methods) can call the classes to be tested.


You can rename the auto generated test class or add your own Test class. This is also a good time to have the test explorer.


There is a class below that has mathematical functions. The other class is a test class that determines if the results from the mathematical functions are correct. Pardon the awful naming convention.
Mathematical function caller
public class MathOperator
    {
        public int Fibonacci(int number)
        {
            if (number <= 1)
                return number;
            return Fibonacci(number - 1) + Fibonacci(number - 2);
        }

        public int Summation(int maxNumber)
        {
            if (maxNumber <= 1)
                return maxNumber;
            return maxNumber + Summation(maxNumber - 1);
        }

    }

Test class
[TestClass]
    public class MathFuncTest
    {
        [TestMethod]
        public void TestFibonacci()
        {
            MathOperator toBeTested = new MathOperator();
            Assert.AreEqual(toBeTested.Fibonacci(2), 1);
        }

        [TestMethod]
        public void TestFibonacci_F3()
        {
            MathOperator toBeTested = new MathOperator();
            Assert.AreEqual(toBeTested.Fibonacci(3), 2);
        }

        [TestMethod]
        public void TestFibonacci_F8()
        {
            MathOperator toBeTested = new MathOperator();
            Assert.AreEqual(toBeTested.Fibonacci(8), 21);
        }

        [TestMethod]
        public void TestSummation_n100()
        {
            MathOperator toBeTested = new MathOperator();
            Assert.AreEqual(toBeTested.Summation(100), 5050);
        }
    }

As seen above, the test class contains an annotation [TestClass] indicating that the class is made for testing purposes and not to be part of the build. The [TestMethod] annotation indicates that the method is to be called when tests are commanded to be run. Running tests can be done from the test explorer where the user can click Run all. Tests can either pass or fail. The results will be visible on the Test explorer. Pass is given when the assertion has the expected result. A fail is given when he assertion did not have the expected result or when an unanticipated exception occurred. Furthermore, a right click on the test class name allows the user to run tests in the class by clicking the Run Tests option or using the keyboard shortcut specified.

All of these are done for the sake of having expectations for classes and their methods. Unit testing is a part of professional software development as evident by the fact that Visual Studio supports it.So be ready in making test classes especially on test driven software projects.

Sunday, July 6, 2014

Crashing Report: Visual Studio. Mysql Export

I experienced a fatal error when I tried to export data from a Mysql database in Visual Studio. The database is on an Azure VM.
The error occurred after I selected a database(censored database name on the combobox). At first, the combobox was empty. I selected A database then Visual Studio Crashed. I repeated the procedure and it all results into Visual Studio 2013 crashing.
I reopened Visual Studio and tried looking for any clues regarding the crash. I easily found a related error. 

The error appears after clicking the item highlighted above. Interacting with the prompt is harmless.
The prompt indicated that there is a problem in getting the information regarding the database. The Access issue might have also occurred in the fatal error. Nevertheless, Visual Studio should have not crashed due to a disconnection. There should have been a fail safe or an error handler for this kind of situations. An error prompt like the one that appeared on the non fatal issue would have been nice instead of crashing Visual Studio.

The experience highlights that connecting on an external Mysql database from an Azure VM can be troubling. I also experienced server disconnection as seen in server explorer. Question is how to make databases connections to Azure VMs be reliable

Sunday, June 29, 2014

Granting Mysql access from Azure Ubuntu server

It is assumed Mysql on the Ubuntu Vm has been installed
Set an endpoint for Mysql

Look for my.cnf and change the bind address. They should be in one of the following locations.
·         /etc/my.cnf
·         /etc/mysql/my.cnf
·         $MYSQL_HOME/my.cnf
·         [datadir]/my.cnf
·         ~/.my.cnf

#Replace 127.0.0.1 with your IP Address
bind-address        = 0.0.0.0
Now use Mysql as root and execute the command below
CREATE USER 'trustedUser'@'%' IDENTIFIED BY 'SomePassword';
GRANT SELECT,INSERT,UPDATE,DELETE,LOCK TABLES ON *.* TO 'trustedUser'@'%';


Afterwards, try to connect to mysql database.
mysql -h hostname.cloudapp.net -u trustedUser -p


Saturday, June 7, 2014

Notes On JSP

Well I have been using JSP for years and I below was my notes when I was a novice. Oh how they seemed silly now though they are correct.
Some old code I used as a novice

<% 
if(userProfile==null)
out.println(request.getParameter("Firstname")+" "+request.getParameter("surname"));
else out.println(userProfile.getFirstName()+" "+userProfile.getLastName());
%>


Special JSP tags

A JSP page consists of three kinds of elements, each with its own special markup tag
that’s similar to HTML:
Scriptlets—Java code executed when the page is loaded. Each of these state-
ments is surrounded by <%and %>tags.
Expressions—Java expressions that are evaluated, producing output displayed on
the page. These are surrounded by <%= and %>tags.
Declarations—Statements to create instance variables and handle other setup tasks

required in the presentation of the page. These are surrounded by  <%! and %>tags.

Servlet objects used in Expressions

There are several servlet objects you can refer to in expressions and other elements of a
JSP page using the following variable names:
 out —The servlet output stream
 request—The HTTP servlet request
 response —The HTTP servlet responses
session—The current HTTP session
application —The servlet context used to communicate with the web server
config —The servlet configuration object used to see how the servlet was initialized

Wednesday, April 9, 2014

Map MySQL schema with Entity framework 6



Visual Studio does not support Mysql after installation by default. This default behavior prevents anyone from using a MySql schema to be mapped with the entity data Framework. Visual Studio will not even recognize Mysql Servers as a data source as seen in the picture below unless a configuration is done. First we need the MySql plugin for Visual studio and the .Net connector


Preparation

Here are links to the Mysql .net Connector and the Mysql Visual Studio Plug in. Both of these are needed to generate Entity framework with Visual Studio. Just download those files and intsall them

Installing the connector

Run the file to start the installation process and click Next.

Choose the typical and install


After the files are installed, click finish and move on to the plug in

The Plug In

Now for the plug-in the process is the same. Run the installation file downloaded earlier then click ‘Next’


Click typical and click Install when the button appears

The files will be installed and there the plugin and connector are now installed.


Configuration Steps

Add this under the provider element in App/Web.config then close the file.
invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />

Just Add the references as seen below and rebuild the solution.

Now try mapping from Mysql

Add a new item to the project

Now Select the Data category and choose the Entity Data Model. Afterwards name the data model before pressing ‘Add’

Choose the ‘Generate From Database’ option and click ‘Next’

Now Press the New Connection button if the Connection needed is still not made.

You should see the MySQL database option available. Select it and continue.

Now modify the Connection properties, make sure valid properties are entered. Make sure to test for a successful connection before pressing ‘OK’.

The connection string should be generated. Press ‘Next’

Wait for the data retrieval to complete. Select the items to be added to the solution and press ‘Finish’

Press ‘OK’ for the prompts that may appear.


There, a successful data mapping of a MySQL database in .Net using Visual Studio 2013.

Tuesday, March 11, 2014

Palindrome in C#

A Palindrome as define in the dictionary is "a word, phrase, or number that reads the same backward or forward".


This can be done in C#

The C# code
private bool isTextPalindrome(string Text)
        {
            int low = 0, high = Text.Length - 1;
            bool ans = true;
            while (ans && low <= high)
            {
                ans = Text[low] == Text[high];
                low++;
                high--;
            }

            return ans;
        }

Monday, September 9, 2013

What is a framework (Programming)

Framework is described as incomplete software containing reusable and common structures shared among applications. Frame works can have varied purposes.

Frameworks include
  • .Net
  • jUnit
  • Google Web toolkit
  • Struts
  • WebOnSwing