Quick Guide to JUnit multiple parameter tests with Parameterized annotation

February 15th, 2015

It is extremely easy to create JUnit test classes to run and test your application or Java code.
JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
All it needs is to include the JUnit library and use the @Test annotation for the method you need to run as test.
However, sometimes we need the convenience of the same test method to run with different parameters.
Following is an example how it can be done easily with some changes to a single test method by adding @RunWith(Parameterized.class) before the class name and then initialize a collection of parameters with @Parameters.
You can also read the parameters from a data provider once using a initializing method with @BeforeClass.

package net.marvinlee.test;
 
import java.util.ArrayList;
import java.util.Collection;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
 

@RunWith(Parameterized.class)
public class JunitTest
{

	private String code;
	private String description;
	

	public JunitTest(String code, String description)
	{
		super();
		this.code = code;
		this.description = description;
	}

	 @Parameters
	 public static Collection getParameters() 
	 { 
		 ArrayList<Object[]> paramList = new ArrayList<Object[]>();
		 addTestcases(paramList);
		 return paramList;
	 }

	protected static void addTestcases(ArrayList<Object[]> paramList)
	{
		for(int i=1;i<=10;i++)
		{
			paramList.add(new Object[]{"Code" + i, "Description " + i});
		}
	}

	@BeforeClass
	public static void init() 
	{ 
		System.out.println("Current dir:" +  JunitTest.class.getClassLoader().getResource("").getPath()); 
	}
	 

	@Test
	public void validateCases() 
	{
		boolean matched = ((code.equals("Code5") && description.equals("Description 5"))
			|| (code.equals("Code6") && description.equals("Description 6")));
		Assert.assertTrue("Failed test=" + code, matched);
		 
	}
	 
}

JUnit test classes can be run with Eclipse IDE and the above test class will show run results similar to the following screenshot.
Only the 4th and 5th testcases that match with the hardcoded values will pass while we get remaining 8 failures.
junit

Have a run with it!

Java, Software ,





Getting Started with OpenShift – easy reference by Steven Pousty & Katie J. Miller

January 11th, 2015

With the advancement of cloud technologies, the options of PaaS (Platform as a Service) for web applications are aplenty. It also helps that there are many available selections to support Java.
Although Red Hat was placed in Gartner ‘Visionaries’ Magic Quadrant, Red Hat’s OpenShift platform is my preferred choice of PaaS at the moment.

OpenShift is a cloud computing platform as a service product from Red Hat. A version for private cloud is named OpenShift Enterprise.

The speed of developing and getting up and running for a product is absolutely crucial for a quick go-to-market advantage, hence any PaaS with the easiest use will have an upper hand over the rest.

For a newer platform like OpenShift, having a book like ‘Getting Started with OpenShift’ is one of the best way to get impatient beginners for a quick run hands-on experience with it. There are many other ways offered such as following the Getting Started – online version or simply the forums, but those are beside the point of this book review.

About The Authors

Steve Poutsy is a Developer Advocate for OpenShift. He has spoken at over 50 conferences and done over 30 workshops including Monktoberfest, MongoNY, JavaOne, FOSS4G, CTIA, AjaxWorld, GeoWeb, Where2.0, and OSCON. Before OpenShift, Steve was a developer evangelist for LinkedIn, deCarta, and ESRI.

Katie Miller is also a OpenShift Developer Advocate. Katie is a polyglot programmer with a penchant for Haskell. The functional programming enthusiast co-founded the Lambda Ladies online community and co-organizes the Brisbane Functional Programming Group. She is passionate about coding, open source, software quality, languages of all kinds, and encouraging more girls and women to pursue careers in technology.

About The Book

The book is meant as ‘A Guide For Impatient Beginners’ but I am glad to say it is not as brief a ‘Getting Started’ guide as any average technical books.

For anyone who doesn’t need any introduction about PaaS concepts, they can dive right in from Chapter 3 as the first couple of  chapters provide conceptual details about PaaS and the basics about cloud computing.

There are 2 important terms in OpenShift, gears and cartridges.

Gears are secure containers for your code. Each gear is allocated CPU, memory, disk, and network bandwidth. A single gear can be used to create an entire web application complete with a private database instance. Multiple gears can be used to create multiple applications or configure your applications to automatically scale in response to web traffic. There are currently three gear types on OpenShift Online: small, medium, and large. Each size provides 1 GB of disk space by default. The large gear has 2 GB of RAM, the medium gear has 1 GB of RAM, and the small and small.highcpu gears have 512 MB of RAM.

Cartridges are plug-ins added to a gear. Cartridges are the plug-ins that house the framework or components that can be used to create and run an application. One or more cartridges run on each gear, and the same cartridge can run on many gears for clustering or scaling. There is quite a list of supported cartridges and OpenShift also supports D-I-Y cartridges, which means most of the popular technologies that you need are already supported by default. Example cartridges are the application servers (Jboss, Tomcat, PHP, Ruby, Node.js, Perl), databases (MongoDB, MySQL, PostgreSQL), and integration tools (Jenkins, cron).

Due to the nature of cloud computing, secure access is important for the application to be accessed or managed. A sub-chapter is dedicated for this and there are sufficient resource in the online guide to get the setup done.

Advanced concepts are also covered in the later chapters of the book, such as storage (it’s shared hosting), websockets, backup options and team collaboration.

 

 

Table of Contents

1. Introduction
    What Is the Difference Between IaaS, PaaS, and SaaS?
    The Three Versions of OpenShift
    Choosing the Right Solution for You
    Things to Understand
        Words You Need to Understand
        Technology You Need to Understand
2. Creating Applications
    Preliminary Steps
    Setting Up the Command-Line Tools
    Creating Your First Application
    Autoscaling and Why You Should Use It by Default
    Reasons to Move to the Paid Tier
3. Making Code Modifications
    Cloning Code to Your Local Machine
    Modifying Application Code
    Building and Deploying Your Code
    Action Hook Scripts
    Hot-Deploying Code
4. Adding Application Components
    Database-Related Cartridges
    Nondatabase Cartridges
        Cron
        Continuous Integration
        Metrics and Monitoring
    Finding Cartridges and QuickStarts
        Adding Third-Party Cartridges
5. Environment and Application Management
    SSH Access
        Using SSH to Interact with a Database
        Importing SQL in an SSH Session
    Environment Variables
        Preconfigured Environment Variables
        Custom Environment Variables
        Overriding Preconfigured Environment Variables
    Log Access
    Changing Application Server or Database Settings
        Application Server Configuration Changes
        Database Configuration Changes
    Using Marker Files
6. Library Dependencies
    Where to Declare Dependencies
    Incorporating Your Own Binary Dependencies
    Modifying Your Application to Use the Database
        Code to Connect to the Database
        Code to Close the Database Connection
        Code to Query the Terms for the Insult
        What We Have Gained by Adding a Database
7. Networking
    WebSockets
    SSH Port Forwarding
    Custom URLs
    SSL Certificates
    Talking to Other Services
    Addressable Ports
8. Disk Usage
    Where You Can Write “to Disk”
    Determining How Much Disk Space Is Used
    Copying Files to or from Your Local Machine
    Other Storage Options
9. Backup
    Managing Deployments and Rollbacks
        Manual Deployments
        Keeping and Utilizing Deployment History
    Application Snapshots with RHC
    Backing Up Your Database
        Writing a Cron Script
        Moving Data off the Gear
10. Team Collaboration
    Managing Multiple SSH Keys
    Domain Access for Teams
    Possible Workflows
11. Summary
    What We Covered
    Other Areas to Explore
    Final Words
Appendix A. Basic Linux for Non-Linux Users

Conclusion

I’m going to make known that I do prefer OpenShift over many PaaS providers.

My preference of OpenShift over other PaaS competitors:
– Low barrier of entry, free tier with no credit card required
– A free tier provides 3 gears – that is 3 web applications hosting for free!
– Minimal technical restrictions, non-proprietary platform API or database
– Easy upgrade from free tier to paid tier, affordability
– Backed by a reputable technology corporation

My preferred key benefits from those listed on OpenShift website:
– Application portability
– Extensible cartridge system for adding services
– Automatic application stack provisioning and application scaling
– Choice of cloud infrastructure – preventing lock-in
– Minimized vendor lock-in – built on open source technologies

You can now download this book in ebook format for FREE or optionally you can purchase it from Amazon.com

Books, Java, Software, Technology , , , , ,



Learning Chef by Mischa Taylor and Seth Vargo

January 2nd, 2015

What is Chef?
Chef is a configuration management tool written in Ruby and Erlang. It uses a pure-Ruby, domain-specific language (DSL) for writing system configuration “recipes”. Chef is used to streamline the task of configuring and maintaining a company’s servers, and can integrate with cloud-based platforms such as Rackspace, Amazon EC2, Google Cloud Platform, OpenStack, SoftLayer and Microsoft Azure to automatically provision and configure new machines.
In short, it is the latest tool for system administration to perform configuration tasks on server as well as administrative tasks automation.

About The Authors
Mischa Taylor is a consultant at Chef, a fast-growing Seattle-based startup responsible for creating the Chef platform, which makes it easy to quickly automate development processes and move business processes into the cloud. He has spent his career focusing on building high quality products and increasing engineering productivity within organizations. Mischa is an author, speaker and mentor on software development topics and neuromorphic computing.

Seth Vargo is currently a software engineer and open source advocate at at HashiCorp. Previously, Seth worked at Chef (Opscode), CustomInk, and a few Pittsburgh-based startups. He is passionate about inequality in technology and organizational culture. When he is not writing software or working on open source, Seth enjoys speaking at local user groups and conferences.

So how does Chef works?
1. Chef relies on reusable definitions known as recipes to automate infrastructure tasks. Examples of recipes are instructions for configuring web servers, databases and load balancers.
2. The Chef server stores your recipes as well as other configuration data.
3. The Chef client is installed on each node in your network. A node can be a physical server, a virtual server or a container instance.
4. The Chef client periodically polls the Chef server for the latest recipes and checks to see if the node is in compliance with the policy defined by these recipes. If the node is out of date, the Chef client runs them on the node to bring it up to date.

Chapters
Chapter 1 Configuration Management and Chef
Chapter 2 Configure Your Chef Development Environment
Chapter 3 Ruby and Chef Syntax
Chapter 4 Write Your First Chef Recipe
Chapter 5 Manage Sandbox Environments with Test Kitchen
Chapter 6 Manage Nodes with Chef Client
Chapter 7 Cookbook Authoring and Use
Chapter 8 Attributes
Chapter 9 Manage Multiple Nodes at Once with Chef Server
Chapter 10 Community and the Chef-Client Cookbook
Chapter 11 Chef Zero
Chapter 12 Search
Chapter 13 Data Bags
Chapter 14 Roles
Chapter 15 Environments
Chapter 16 Testing
Chapter 17 Conclusion
Appendix Open Source Chef Server
Appendix Hosted Enterprise Chef

Conclusion
Learning Chef is a book to introduce system administrators and software developers to the automation of systems or infrastructure automation.
The introduction chapters guide the readers to setup the Chef Development Environment, installing necessary tools and the ‘Test Kitchen’ sandbox environment.
Chef itself is written in Ruby, so there is a chapter introducing Ruby for new users.
The book is quite hands-on for readers so you can follow the source code examples that came with the book organized in chapters.
Readers will get to learn how to write recipes, cookbooks, and managing the Chef Server or Nodes.
Although Chef is designed to be a tool for administrative or operational tasks, it has many features including searching for recipes based on IP addresses, shared information for nodes (data bags), roles grouping and environment categorization.
If you are a system administrator, this is definitely a book for you.

Other Resources
Chef Website – http://www.chef.io/
Learn Chef – http://learn.chef.io/
Chef Youtube Channel – http://www.youtube.com/user/getchef
Chef Wiki – http://wiki.opscode.com/display/chef/Home

You can now purchase this title from Amazon:

Books, Review, Technology , , ,