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

Saturday, August 24, 2013

As I Listened: Graduate Studies Discussion

Back in May 29 I was listening to the lecture of Mister Joseph Beck on Graduate Studies in Worcester polytechnic Institute(WPI)

The discussion of Joseph Beck highlighted the theme of do not be scared for the Graduate school opportunity. He cited positive and negative points of studying in a graduate school particularly WPI and those points seemed valid. The introduction is followed by the descriptions of students who would be suited in pursuing a graduate degree in WPI or in the United States in general. The descriptions include students who are scientific and students who want to test ideas to solve problems on their own. He then described the life of a graduate student in the US. He then mentioned the cost then discouraged paying the tuition.

Afterwards, the speaker focused on scenarios of being a graduate student in his institution. Moreover, also stated that a graduate degree can take up to 7 years on average where 2-3 years are spent on coursework with research as well as finding a dissertation problem. However, there is flexibility in schedules. Eventually, the discussion informed about life as a person working on a problem and a person being part of the community in their campus. Graduate students can work on laboratories to work for their departments and advisors. Furthermore, he highlighted the importance of the relationship of the student and the advisor. Advisors have variation of styles in directing research one can be hands-on while another can be distant. 

The speaker also discussed the funding from assistantships. He described the Teacher Assistantship and the Research assistantship. Both cover the tuition with a pay that is livable. The works done while living of those assistantships are different. One requires the scholar to help teach undergraduates while the other requires one to assist in research for a faculty’s research. It was encouraged to try applying for these assistantships. Obtaining the assistantship requires impressiveness in writing letters. There must be effort in writing the letter and it was recommended to not make the letter seem generic. The research assistantship will require a letter that also indicates interest in current research work. The teaching assistantship requires impressing faculty with achievements and credentials. One way to impress is to show initiatives even in letters to be sent.
The speaker then focused on post-graduation life where the graduates can be post doc or researchers. Another possible position involves faculty. He then iterated that it takes years to have tenure in the US even after having a PhD. Afterwards he then cited reasons to study in WPI and what the institution is looking for. He cited good faculty, students and good employment opportunities. He also explained reasons to study in the US. Reason included networking with others, diverse research perspectives. He then concluded with degree offers of his institution.


I noticed the discussion was focused on student life there and what happens. The works their seemed appealing since they arouse my curiosity. I was so curious about what I can do while I am there that I did not think about the place itself. I did have an idea on what I can do but I have a blurry idea on what the place is like. I was disappointed that I did not see more pictures of the campus or its landmarks. I also wanted to see more of the laboratories they have. Though I think stuff there changes, I wanted to have a clearer visual idea of what is inside.

The research work aiming to rank educational content on search seemed redundant to Google’s page rank but it has its own value since it tries to find the most effective pages for educating the users. It seems to be a good project for people who need to learn. I imagine myself using it to learn new programming languages or studying new algorithms or any other complicated concept. Then I got curious on what would the average rank of Wikipedia be based effective educational articles. The researched piqued me enough to consider spending years in that institution. I reflected that they work on serious problems as graduate students should. I also pondered on how those things are done and thought of whether others who attended find it interesting as well.

I also then wondered if it is possible to rank web pages based on user preferences. I got the idea since people vary in learning preferences. I only think that is possible if the users are tested or inquired to notify their preferences. Then the system can arrange the results based on a dataset that indicates an effectiveness value for a certain generic preference that matches the user’s preference the most. Afterwards, I realized how the topic aroused me which made me think that if I really find joy in research that contributes to the world.

The speaker was able to convince me that there are worthwhile graduate study opportunities in the US since there seems to be a lot of opportunities to learn and innovate there. I think of such since he was able to mention their government funds research across many research centers in the country which I hear in the news. The tuition is deal breaker but the assistantship offers seem adequate. I liked how English proficiency was emphasized as a criterion for assistantship since I thought it would be something like credentials or technical competency given focus in getting assistance. As for lifestyle and usual scenarios, I feel like I would not mind to live their given the description he gave as a beneficiary of the assistantship. I am willing to adjust on the demands as long as I find meaning in what I do and the work there seems to have value.

The post graduate life does not appeal to me much. I was hoping for other opportunities but I thought maybe that there are unmentioned opportunities with those degrees. I just need to research on them. I also may need to research on what people do after they get high degrees to imagine myself if I take that path. Moving on, the tenure mentioned takes a long time to earn but I actually think that it is true. It just thought me on how demanding tenure can be.

One important lesson is that I should not be afraid to try studying abroad. It is difficult but people will help and being dedicated will help get through the challenges somehow. As I reflect, the talk only made me consider studying abroad for graduate studies. I am even still conflicted whether to study further or not since part of me wants to work while another wants to learn more and produce solutions that impact people’s lives. I will decide sooner or later but the discussion was able to have clearer options on what happens if I choose to pursue a graduate degree particularly in WPI. I am actually thankful for the discussion especially the parts that mentions on who the graduate programs are for.