Saturday, September 12, 2015

Test-Driven Java Development

Invoke TDD principles for end-to-end application development with Java

About This Book
  • Explore the most popular TDD tools and frameworks and become more proficient in building applications
  • Create applications with better code design, fewer bugs, and higher test coverage, enabling you to get them to market quickly
  • Implement test-driven programming methods into your development workflows
Who This Book Is For
If you're an experienced Java developer and want to implement more effective methods of programming systems and applications, then this book is for you.

What You Will Learn
  • Explore the tools and frameworks required for effective TDD development
  • Perform the Red-Green-Refactor process efficiently, the pillar around which all other TDD procedures are based
  • Master effective unit testing in isolation from the rest of your code
  • Design simple and easily maintainable codes by implementing different techniques
  • Use mocking frameworks and techniques to easily write and quickly execute tests
  • Develop an application to implement behaviour-driven development in conjunction with unit testing
  • Enable and disable features using Feature Toggles
In Detail
Test-driven development (TDD) is a development approach that relies on a test-first procedure that emphasises writing a test before writing the necessary code, and then refactoring the code to optimize it.

The value of performing TDD with Java, one of the most established programming languages, is to improve the productivity of programmers, the maintainability and performance of code, and develop a deeper understanding of the language and how to employ it effectively.

Starting with the basics of TDD and reasons why its adoption is beneficial, this book will take you from the first steps of TDD with Java until you are confident enough to embrace the practice in your day-to-day routine.

You'll be guided through setting up tools, frameworks, and the environment you need, and will dive right in to hands-on exercises with the goal of mastering one practice, tool, or framework at a time. You'll learn about the Red-Green-Refactor procedure, how to write unit tests, and how to use them as executable documentation.

With this book you'll also discover how to design simple and easily maintainable code, work with mocks, utilise behaviour-driven development, refactor old legacy code, and release a half-finished feature to production with feature toggles.

You will finish this book with a deep understanding of the test-driven development methodology and the confidence to apply it to application programming with Java.

Style and approach
An easy-to-follow, hands-on guide to building applications through effective coding practices. This book covers practical examples by introducing different problems, each one designed as a learning exercise to help you understand each aspect of TDD.

Thursday, September 10, 2015

Add data to JavaFX LineChart, and shift out the oldest data.


JavaFX example to update LineChart: user enter new data to the LineChart associated series, and remove the oldest item from the series. I tried three approachs to achieve the goal. The first one is obviously wrong. Check the code to know my implement.


package javafx_linechart;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_LineChart extends Application {
    
    Label label1, label2, label3;
    XYChart.Series series1, series2, series3;
    final int MAX_NUM = 10;
    @Override
    public void start(Stage primaryStage) {

        TextField textField = new TextField();
        Button btn = new Button();
        btn.setText("Add item");
        btn.setOnAction((ActionEvent event) -> {
            try{
                int i = Integer.parseInt(textField.getText());
                System.out.println(String.valueOf(i));
                
                removeAndAdd(series1, i);
                reduceSeriesXValue(series2, i);
                shiftSeriesYValue(series3, i);
                
                printSeries(series1, label1);
                printSeries(series2, label2);
                printSeries(series3, label3);
                
            }catch (NumberFormatException ex){
                System.out.println(ex.toString());
            }
        });
        
        label1 = new Label();
        label2 = new Label();
        label3 = new Label();
        
        //prepare LineChart 1
        final NumberAxis xAxis1 = new NumberAxis();
        final NumberAxis yAxis1 = new NumberAxis();
        final LineChart<Number,Number> lineChart1 = 
                new LineChart<>(xAxis1,yAxis1);
        series1 = new XYChart.Series();
        series1.setName("series1");
        lineChart1.getData().add(series1);
        
        //prepare LineChart 2
        final NumberAxis xAxis2 = new NumberAxis();
        final NumberAxis yAxis2 = new NumberAxis();
        final LineChart<Number,Number> lineChart2 = 
                new LineChart<>(xAxis2,yAxis2);
        series2 = new XYChart.Series();
        series2.setName("series2");
        lineChart2.getData().add(series2);
        
        //prepare LineChart 3
        final NumberAxis xAxis3 = new NumberAxis();
        final NumberAxis yAxis3 = new NumberAxis();
        final LineChart<Number,Number> lineChart3 = 
                new LineChart<>(xAxis3,yAxis3);
        series3 = new XYChart.Series();
        series3.setName("series3");
        lineChart3.getData().add(series3);
        
        //init dummy data
        for(int i=0; i<MAX_NUM; i++){
            series1.getData().add(new XYChart.Data(i, i));
            series2.getData().add(new XYChart.Data(i, i));
            series3.getData().add(new XYChart.Data(i, i));
        }
        
        printSeries(series1, label1);
        printSeries(series2, label2);
        printSeries(series3, label3);
        
        VBox vBox1 = new VBox();
        vBox1.getChildren().addAll(lineChart1, label1);
        VBox vBox2 = new VBox();
        vBox2.getChildren().addAll(lineChart2, label2);
        VBox vBox3 = new VBox();
        vBox3.getChildren().addAll(lineChart3, label3);
        
        HBox chartBox = new HBox();
        chartBox.getChildren().addAll(vBox1, vBox2, vBox3);
        
        CheckBox cbAnimated = new CheckBox("animated");
        cbAnimated.setSelected(true);
        cbAnimated.selectedProperty().addListener(
                (ObservableValue<? extends Boolean> observable, 
                        Boolean oldValue, Boolean newValue) -> {
            lineChart1.setAnimated(newValue);
            lineChart2.setAnimated(newValue);
            lineChart3.setAnimated(newValue);
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(cbAnimated, textField, btn, chartBox);
        
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        
        Scene scene = new Scene(root, 1000, 650);
        
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    //Remove the first item, and add the new item - WRONG result!
    private void removeAndAdd(XYChart.Series series, int newValue){
        series.getData().remove(0);
        XYChart.Data newData = 
                new XYChart.Data(series.getData().size(), newValue);
        series.getData().add(newData);
    }
    
    //remove the first item
    //reduce XValue of all items by 1
    //add the new item
    public void reduceSeriesXValue(XYChart.Series series, int newValue){
        series.getData().remove(0);
        int numOfPoint = series.getData().size();
        for(int i=0; i<numOfPoint; i++){
            //reduce XValue
            XYChart.Data<Number, Number> data = 
                    (XYChart.Data<Number, Number>)series.getData().get(i);
            int x = (int)data.getXValue();
            data.setXValue(x-1);
        }
        
        series.getData().add(new XYChart.Data(numOfPoint, newValue));

    }
    
    //Shift all YValue
    //add the new item
    public void shiftSeriesYValue(XYChart.Series series, int newValue){
        int numOfPoint = series.getData().size();
        for(int i=0; i<numOfPoint-1; i++){
            XYChart.Data<Number, Number> ShiftDataUp = 
                    (XYChart.Data<Number, Number>)series.getData().get(i+1);
            Number shiftValue = ShiftDataUp.getYValue();
            XYChart.Data<Number, Number> ShiftDataDn = 
                    (XYChart.Data<Number, Number>)series.getData().get(i);
            ShiftDataDn.setYValue(shiftValue);
        }
        XYChart.Data<Number, Number> lastData = 
            (XYChart.Data<Number, Number>)series.getData().get(numOfPoint-1);
        lastData.setYValue(newValue);
    }
    
    //display the data of series
    private void printSeries(XYChart.Series series, Label label){
        String printOut = "";
        int numOfPoint = series.getData().size();
        for(int i=0; i<numOfPoint; i++){
            XYChart.Data<Number, Number> data = 
                    (XYChart.Data<Number, Number>)series.getData().get(i);
            printOut += i + " - " + data.getXValue() 
                    + " : " + data.getYValue() + "\n";
        }
        
        label.setText(printOut);
    }
    
    public static void main(String[] args) {
        launch(args);
    }
    
}

Sunday, September 6, 2015

Mastering NetBeans

Master building complex applications with NetBeans to become more proficient programmers


About This Book
  • Customize NetBeans to fit your unique needs
  • Excel in NetBeans IDE, learning the shortcuts and hidden features to become more productive
  • A comprehensive guide to become more productive at application development using NetBeans IDE
Who This Book Is For
If you are a competent developer who wants to fast-track your application development with NetBeans IDE, then this book is for you. Reasonable knowledge and an understanding of Java programming and NetBeans IDE is assumed.

What You Will Learn
  • Install NetBeans either from a distribution package or from source code
  • Test, debug, and run production code using the NetBeans IDE
  • Use external services such as PaaS environments and web services
  • Create desktop applications using Swing tools
  • Manage and configure relational databases
  • Build a Java business model and web tiers using Java EE and Spring technologies
  • Explore web services both with XML and RESTful approaches
  • Handle external services such as databases , Maven repositories, and cloud providers
  • Extend NetBeans for those situations where you require more from your IDE
In Detail
With the increasing complexity of software development and the abundance of tools available, learning your IDE in-depth will instantly increase your developer productivity. NetBeans is the only IDE that can be downloaded with Java itself and provides you with many cutting edge features not readily available with many IDEs. The IDE also provides a great set of tools for PHP and C/C++ developers. It is free and open source and has a large community of users and developers around the world.

This book will teach you to ace NetBeans IDE and make use of it in creating Java business and web services. It will help you to become a proficient developer and use NetBeans for software development. You will learn effective third-party interaction and enable yourself for productive database development.

Moving on, you will see how to create EJB projects and write effective and efficient web applications. Then you will learn how to use Swing and manage and configure a relational database. By the end of the book, you will be able to handle external services such as databases, Maven repositories, and cloud providers, and extend your NetBeans when you require more from your IDE.

Style and approach
An easy-to-follow yet comprehensive guide to help you master the exhaustive range of NetBeans features in order to become more efficient at Java programing. More advanced topics are covered in each chapter, with subjects grouped according to their complexity as well as their utility.