Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts

Saturday, August 30, 2014

jUnit: Rules

Rules add special handling around tests, test cases or test suites. They can do additional validations common for all tests in the class, concurrently run multiple test instances, set up resources before each test or test case and tear them down afterwards.

The rule gets complete control over what will done with the test method, test case or test suite it is applied to. Complete control means that the rule decides what to do before and after running it and how to deal with thrown exceptions.

Saturday, August 23, 2014

jUnit: Dynamic Tests Generation

Dynamic tests generation is useful when you need to run the same set of tests on many different input values or configurations. It can be achieved either using parametrized tests or using theories.

Theories are valuable when you have a bunch of data to be used as parameters and want to run tests on all their combinations. You get less control, but you do not have to write combining and iterating code by yourself. Basics about how theories work are explained on java code geeks (original at java advent calendar), so this post focus on parametrized tests.

Parametrized tests are better when you need to have good control over the input values, e.g. directory with files that are served as an input or a list of meaningful parameters combinations.

Wednesday, April 2, 2014

Placeholdered Maps

Placeholdered map is able to associate keys with values. User can add key value pairs into it and ask for data associated with keys. It works exactly as it would work in an ordinary map.

The additional feature is ability to use placeholders. Placeholder knows when it was created and is useful to cheat on order in which data were added into the map. If the user adds data into placeholder, the datastructure behaves as if the data were added when the placeholder was created.

We will create two such structures. First keeps key value pairs and can return last value associated with the key. Value added into placeholder is returned only if user did not added the same key after the placeholder was created. Second returns all values associated with the key in order they were put in. Placeholders can be used to add data in the middle of those lists.

Thursday, June 20, 2013

Building JavaScript Library with Grunt.js

New release of typical non trivial JavaScript project needs to run unit tests, concatenate all source files into one and minify the result. Some of them also use code generators, coding style validators or other build time tools.

Grunt.js is an open source tool able to perform all above steps. It is pluginable and was written in JavaScript, so anyone working on JavaScript library or project should be able to extend it as he needs.

Wednesday, May 1, 2013

Less CSS - Expressions and Traps

In an average case, less expressions are easy to use, very useful and behave the same way as expressions in any other programming language. Unfortunately, their interactions with the rest of css syntax can occasionally cause somewhat quirky behavior. Adding to that, next less language version will change in backward incompatible way and expressions are affected too.

This post shows when and why quirky behavior happens, which problems are going to be solved by planned changes and which traps will still remain there.

Sunday, March 31, 2013

Less CSS - Tips and Trics

Less is declarative language designed to behave and look as closely to css as possible. Less started as an attempt to make css style sheets easier to read and maintain and then added more features and abilities. It evolved so much that it became theoretically possible to do anything with it.

This post shows some less known but very useful less features. It also contains list of potentially useful workarounds and tricks you can use if the default less syntax is not sufficient for what you need. Finally, last chapter shows one practical application of those tricks. We will create mixin to auto generate vendor prefixes for css declarations.

Monday, December 17, 2012

ANTLR - Semantic Predicates

Parsing simple grammar with antlr is simple. All you have to do is to use regular expressions to describe your language and let antlr generate lexer and parser. Parsing big or complicated languages occasionally require more, because describing them in regular expressions only can be hard or even impossible.

Semantic predicates are java (or C++, or JavaScript, or ...) conditions written inside the grammar. Antlr uses them either to choose between multiple alternatives or as additional assertions to be checked. They can be placed inside both lexer and parser, but this post focus only on their usage within the parser. They add a lot of power to antlr.

Saturday, September 1, 2012

Tackling Comments in ANTLR Compiler

Most compilers treat comments as just another meaningless whitespaces. They identify them in the source code and then throw them away. On the other hand, things are a bit harder if you need to know comments content and their position.

Such requirement is not too common and official ANTLR documentation does not says much about how to do it. The compiler we have been working on needed to preserve comments, so we had to come up with our own solution. This post documents it.

First chapter introduces our compiler. It describes what it does, where it is located and how it is written. Second chapter explains our comment preserving requirements. The rest of the post describes datastructures and algorithms used in our solution.

Wednesday, August 1, 2012

Wro4j, Page Load Optimization and Less.js

Wro4j is primary a JavaScript and CSS merge and minimization library. Its original purpose was to speed up the page load. However, its final design made it easy to add integration with LESS, CoffeScript and few other technologies.

Less was introduced in previous article. In short, it is CSS with object oriented inheritance, variables and few other additional features. It is compiled into regular style sheets and served to the browser. Less was written in JavaScript and usually runs in the browser. If you want to run it on the server side, you have to use wro4j or some other integration library.

The post is mostly wro4j tutorial with focus on Less integration. It explains how wro4j works and how to configure it. There is very little about Less and almost everything is about wro4j.

Sunday, July 1, 2012

Inheritance and Variables in CSS with Less

I always missed object oriented inheritance and variables in CSS. Style sheets could be much more readable and maintainable, if they would have ability to say:
  • I want this selector color to be just like that one.
  • I want this class to be just like that one, with one simple change.

As multiple projects run into CSS explosion and maintenance problems problems, it was only question of time until someone comes with another solution. The original solution came from the Ruby world and is quite simple and elegant:
  • extend CSS with needed features,
  • compile extended CSS into regular CSS,
  • serve the result to the browser.

The browser does not have to deal with a new syntax. It was given standard CSS file and will process it as usually. On the other hand, the programmer had variables, inheritance and few other features available.

Sunday, April 1, 2012

Writing Eclipse Plugins Tutorial - Part 1

Eclipse is one of three most popular java development IDEs. One reason for its success is its extensibility. Writing eclipse plugins can be very fast and easy for anyone who knows what to do and already done it.

Unfortunately, doing something in Eclipse for the first time can be very time consuming and frustrating. Eclipse framework is huge, powerful and sometimes complicated. It may be difficult to figure out which features are available and how to use them.

This tutorial explains basics of all eclipse features needed to automate a simple java refactoring task. It shows how to add new items into the menu and how to analyze, modify and format java source code. It also shows how to use dialogs to communicate with users.

Thursday, March 1, 2012

JPA Tutorial

Java persistent API is a specification and set of interfaces that helps with persistence of java objects into the database. It has been created after success of object relational mapping frameworks - the most popular is Hibernate.

JPA defines standard basic set of features that every object relational mapping framework should have. Almost all current O/R frameworks implement JPA specification. Of course, most of them have additional features to set them apart.

Saving/loading data with JPA is easier, faster and less error prone than writing SQL and mapping query results to objects. However, JPA does not free you from relational database understanding. And while you do not have to write SQL anymore, you have to learn JPQL which is very similar.

Wednesday, February 1, 2012

Running JNDI and JPA Without J2EE Container

We wanted to test some JPA code with as simple setup as possible. The plan was to use only Java and Maven, without an application server or other J2EE container.

Our JPA configuration needs two things to run successfully:
  • database to store data,
  • JNDI to access the database.

This post has two parts. First part shows how to use standalone JNDI and an embedded in-memory database in test. Remaining chapters explain how the solution works.

Friday, January 6, 2012

JavaScript Testing with JSTestDriver

Js-test-driver is an open source JavaScript unit tests runner written in Java. The project was started at Google and is under active development. It is available under Apache License 2.0 license.

Js-test-driver is able to run from command line and reports results to the standard output. As a result, it is possible to fully automate JavaScript tests and run them on a continuous integration server.

Additionally, js-test-driver comes with IntelliJ IDEA plugin and Eclipse plugin. Of course, Eclipse plugin is compatible with Aptana Studio. If you use one of these IDEs, you can run tests directly from IDE and see progress and results in a view. It is very similar to java jUnit plugin.

Sunday, December 4, 2011

Apache Shiro Part 3 - Cryptography

Besides securing web pages and managing access rights Apache Shiro does also basic cryptography tasks. The framework is able to:
  • encrypt and decrypt data,
  • hash data,
  • generate random numbers.

Shiro does not implement any cryptography algorithms. All calculations are delegated to Java Cryptography Extension (JCE) API. The main benefit of using Shiro instead of what is already present in Java is ease of use and secure defaults. Shiro crypto module is written in higher abstraction level and by default implements all known best practices.

Thursday, September 29, 2011

ANTLR Tutorial - Expression Language

ANTLR tool is useful any time you need to create compiler, interpreter or parser of your own language. It takes so called grammar file as an input and generates two classes: lexer and parser.

This post explains what is lexer, what is parser and how to write grammar files to generate them. In the end of the post, you will be able to create a compiler into abstract syntax tree for any simple programming language.

Our example project, a boolean expression language, is written in Java and available on Github. Besides that, everything explained in this post is language independent. Grammar files are the same in all languages.

Friday, August 26, 2011

ANTLR Tutorial - Hello Word

Antlr stands for ANother Tool for Language Recognition. The tool is able to generate compiler or interpreter for any computer language. Besides obvious use, e.g. need to parse a real 'big' programming language such as Java, PHP or SQL, it can help with smaller, more common tasks.

It is useful any time you need to evaluate expressions unknown at compile-time or to parse non-trivial user input or files in a weird format. Of course, it is possible to create custom hand made parser for any of these tasks. However, it usually takes much more time and effort. A little knowledge of a good parser generator may turn these time-consuming tasks into easy and fast exercises.

Monday, May 30, 2011

AppSensor - Intrusion Detection

Imagine that you have created a nice web application and secured it to your best. Users came, used it and everything was OK until someone stumbled upon vulnerability in your application and used it. Of course, you analyzed logs and found that the bad guy was looking for the vulnerability for weeks until he found one.

Creators of AppSensor intrusion detection framework believe that the above situation should not happen. The application should not just lie there and let itself beat with SQL injections, XSS attacks and whatever else. It should take active measures to protect itself. As the average attacker has to make several attempts to find the vulnerability in the application, it should by possible to detect hacking attempts.

AppSensor - Integration with Shiro

AppSensor is intrusion detection framework described in an another post. Out of the box version assumes that underlying application supports ESAPI interfaces. In this post, we will take an application secured by Shiro framework which does not support ESAPI and integrate it with AppSensor.

This post is only about integration. It does not show how to add AppSensor to the application, nor what it is, nor how to use it. All that can be found in AppSensor - Intrusion Detection post.

Monday, April 18, 2011

Apache Shiro Part 2 - Realms, Database and PGP Certificates

This is second part of series dedicated to Apache Shiro. We started previous part with simple unsecured web application. When we finished, the application had basic authentication and authorization. Users could log in and log out. All web pages and buttons had access rights assigned and enforced. Both authorization and authentication data have been stored in static configuration file.

As we promised in the end of last part, we will move user account data to database. In addition, we will give users an option to authenticate themselves via PGP certificates. As a result, our application will have multiple alternative log in options: log in with user name/password and log in with certificate. We will finish by turning alternative log in options mandatory.