The Holy Java

Building the right thing, building it right, fast

How to create and run Gatling 2.0 tests

Posted by Jakub Holý on April 28, 2014

Getting up and running with Gatling perf. tests as I would like so I record this for my future reference.

0. Create a project:

$ mvn archetype:generate \
   -DarchetypeCatalog=http://repository.excilys.com/content/groups/public/archetype-catalog.xml
   -Dfilter=io.gatling:

(The trailing “:” in the filter is important.)

1. Import to IntelliJ

In IntelliJ choose to import an object, instead of “from sources” select “from external model” and then Maven. You will also need to have the Scala plugin installed and, when imported, you will likely need to right-click on pom.xml and Maven – Reimport.

2. Record a simulation

  1. Run the src/test/scala/Recorder.scala (right-click – Run ‘Recorder’)
  2. Set the port it should listen on, f.ex. 8000 (maybe you also need to set port for HTTPS, f.ex. 8001), set the target app (perhaps localhost, <some port>, <some https/dummy port>)
  3. Optionally set the class name of the recorded simulation and the target package (the output goes to src/test/scala/<package>/<name>.scala)
  4. Click [Start !]
  5. Go to your browser and configure it to use the recorder as its HTTP[s] proxy
  6. Browse localhost:8000/your_app as you want for the test
  7. Click [Stop and save] in the Recorder UI

Read the rest of this entry »

Posted in Testing, Tools | Tagged: , | Comments Off

Kioo: How to Troubleshoot Template Processing

Posted by Jakub Holý on April 8, 2014

So you have created an Om/Reagent/React component using a Kioo template and the result is not as you expected, perhaps just an empty element? Let me share what I have discovered about troubleshooting this (though I am no expert). You will se how to invoke the underlying Clojure function component* manually, how to expand the deftemplate macro, how to call the resulting JavaScript function, and what the intermediate forms look like.

Read the rest of this entry »

Posted in Languages | Tagged: , , | 3 Comments »

Kioo: How To Replace The Whole Body

Posted by Jakub Holý on April 8, 2014


This whole post in unnecessary since it is simply possible to use a snippet directly without a template, as kindly explained by Creighton Kirkendall.

Kioo, the enlive-inspired templating library for React.js and derived libs such as Om, normally works by matching selectors against elements inside <body> and transforming the matched elements while also keeping all the other ones. But what if you want to keep just the single matched element and drop all the others? You need some tricks and I will demonstrate one possible way.

Dislaimer: This is a result of my experimentation, not deep knowledge if Kioo.

Read the rest of this entry »

Posted in Languages | Tagged: , | Comments Off

Most interesting links of March ’14

Posted by Jakub Holý on March 31, 2014

Recommended Readings

Clojure Corner

  • Timo Mihaljov’s Pimp My REPL (3/2014)- really great tips – user.clj, :dev profile, user-wide config in .lein/profiles.clj, tools.namespace, making funs available everywhere & more via Vinyasa, form println with Spyscope, debug-repl, difform, clj-ns-browser

Tools/Libs

  • clj-ds – Clojure immutable datastructures extracted from Clojure and made easier for use directly in Java

Favourite Quotes

Posted in General, Top links of month | Tagged: | Comments Off

Framework Joy: Load in Hibernate Updates Data

Posted by Jakub Holý on March 31, 2014

Would you ever guess that this line

// Load Buyer from DB by id using Spring's HibernateTemplate:
final Buyer traveller = (Buyer) hibernateTemplate.load(Buyer .class, new Long(id));

could lead to a constraint-validation exception during a batch update or delete and re-insert rows (loosing all columns Hibernate does not know about)? I was quite surprised.

In particular buyer’s bonusCards get deleted and re-inserted, because Hibernate believes that the collection of cards is dirty, even though it has just loaded it from the DB. I am not exactly sure why (preventing false positives in dirty checks requires some black magic), this is the configuration:

<class name="Buyer" ...>
...
    <set name="bonusCards " table="bonus_cards">
        <key column="buyer_id"/>
        <composite-element class="BonusCard">
            <property name="number" column="number" not-null="false"/>
            <property name="expiredate" column="expdate"/>
            ...
        </composite-element>
    </set>
</class>

Of course Hibernate certainly has good reasons to update and delete+re-insert data upon load and we could certainly get rid of (some of) these updates by configuring Hibernate better. But it still demonstrates nicely the hidden cost of using a complex framework – in this case, it behaves quite unexpectedly and requires extensive knowledge to set up properly and to troubleshoot.

Posted in j2ee | Tagged: | Comments Off

How To Generate A Valid Credit Card Number For A Bin (First 6 Digits)

Posted by Jakub Holý on March 25, 2014

There is plenty of generators that can produce numbers that are valid credit card numbers according to the Luhn check and specific rules of the individual issuer companies. However I have not found anything that would generate the missing digits given a bin, i.e. the first 6 digits of a credit card (the “bank identification number”). So I created one, reverse-engineering org.apache.commons.validator.routines.CreditCardValidator from common-validator 1.4:

// Groovy:
/** Map RegExp from C.C.Validator to the total length of the CC# */
binReToLen = [
        (~/^(3[47]\d{0,13})$/) : 13+2, // amex
        (~/^30[0-5]\d{0,11}$/) : 11+3, // diners 1
        (~/^(3095\d{0,10})$/) : 10+4, // diners 2
        (~/^(36\d{0,12})$/) : 12+2,     // diners 3
        (~/^|3[8-9]\d{0,12}$/) : 12+2,  // diners 4
        (~/^(5[1-5]\d{0,14})$/) : 14+2, // master
        (~/^(4)(\d{0,12}|\d{15})$/) : 12+1 // visa
        // Discover cards omitted
]

/** Bin is e.g. 123456 */
def completeCCn(String bin) {
    def ccnFill = "1" * 19
    int ccnLen = lenForBin(bin)

    def ccnWithoutCheck = bin + ccnFill[0..<(ccnLen - 6 - 1)] // - bin, - check digit

    def check = computeLuhncheckDigit(ccnWithoutCheck)

    return "$ccnWithoutCheck$check"
}

def lenForBin(String bin) {
    def match = binReToLen.find { it.key.matcher(bin).matches() }
    if (match == null) {
        throw new RuntimeException("Bin $bin does not match any known CC issuer")
    }
    match.value
}

def computeLuhncheckDigit(def ccnWithoutCheck) {
    org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit.LUHN_CHECK_DIGIT.calculate(ccnWithoutCheck)
}

completeCCn('465944') // => 4659441111118

Testing FTW!

Posted in General, Languages, Testing | Tagged: , | Comments Off

HttpServletRequest: requestURI/requestURL/contextPath/servletPath/pathInfo/queryString

Posted by Jakub Holý on March 24, 2014

I never remember what some of these HttpServletRequest methods return so here is an example:

URL METHODS BREAKDOWN (JavaEE 6):
- requestURL  = http://localhost:8080/myapp/users/profile.xhtml
- requestURI  = /myapp/users/profile.xhtml = contextPath + servletPath + pathInfo
- contextPath = /myapp = the first segment after hostname (unless the app runs and the root app with context /)
- servletPath = /users/profile.xhtml (part after contextPath to the servlet that handled the request)
- pathInfo    = null (what remains after servletPath up to the queryString; would return '/dummy' if the url was '.../profile.xhtml/dummy?id=007')
- queryString = id=007

Posted in Languages | Tagged: , , | Comments Off

Ansible: Best practices for deriving host-level var from a group var

Posted by Jakub Holý on March 19, 2014

I have a cluster and a group variable (in test/staging/prod) holding the hostname of the cluster master. For each host, I want to derive a variable which is set to either “slave” or “master” and use it in templates.

This can be done with set_fact:

---
# group_vars/staging:
jboss_master_host: barad-dur.example.com

 

---
# roles/xxx/tasks/main.yml:
- name: Set default jboss_host_type
  set_fact: jboss_host_type=slave
- name: Set jboss_host_type to master if master
  set_fact: jboss_host_type=master
  when: jboss_master_host == inventory_hostname

We could simplify that by using more advanced Jinja:

---
# roles/xxx/tasks/main.yml:
- name: Set jboss_host_type var
  set_fact: jboss_host_type={{ 'master' if jboss_master_host == inventory_hostname else 'slave' }}

but it is preferred not to use logic coded in Jinja2.

// Ansible 1.5.3

Posted in Tools | Tagged: | Comments Off

Frustration-Driven Development – Towards DevOps, Lean, Clojure

Posted by Jakub Holý on March 17, 2014

A post about development practices, speed, and frustration.

My wife has mentioned that she likes my passion for doing things right in software development. That made me thinking, why do I actually care so much and do not just enjoy the coding itself? It boils down to that I am not happy until my code is in production. Seeking the satisfaction of having my code used by and helping people while trying to eliminate all unnecessary mental drain is behind all the practices that I embrace and evangelize. It’s a drug I like to take often, in small doses.

practices = f(max(delivered value), min(mental energy))

So how does this relate to DevOps, Continuous Delivery, testing, single-piece-flow, Lean Startup, Clojure? It is simple.

Read the rest of this entry »

Posted in SW development | Tagged: | 1 Comment »

Recursive Copy In Ansible 1.5 And –diff

Posted by Jakub Holý on March 5, 2014

Ansible 1.5 has partial support for recursive copy of files:

  • the synchronize module, using rsync
    • cons: does not support group=, owner=
    • -C and –diff – it does not print diff of the files changed; when running ansible with -v, it will print output of rsync’s –itemize-changes, i.e., for each changed file/dir, something like “<f.st…… conf/httpd.conf\n” (< = file uploaded, s = size changed, t = timestamp changed, . = this attribute has not been changed)
  • the copy module
    • -C –diff – it only reports “changed” without naming the changed files or showing diffs (unless there is only one changed file)
  • the local_action module, used to run rsync manually (essentially the same as synchronize but more control)
  • So the only way to do a kind of recursive copy with working diff is to use copy with with_fileglob for each directory and subdirectory :-(

To check differences (without diff) manually:

rsync -e ssh -vrnc --itemize-changes source/dir myuser@myserver:/opt/dest/

where v= verbose, r = recursive, n = dry-run, c = check based on checksum, not timestamp+size; a typical output for a changed file is <fcsT...... httpd.conf (< = to be uploaded, f = it is a file, c = checksum differ, s = size differ, T = timestamp would be updated).

Posted in Tools | Tagged: , | Comments Off