Wednesday, 30 July 2014

Moving your Git working directory from Linux to Windows? There are two issues that you're probably going to get

The simpler solution would be to just check out the repository, but in
some cases you probably have some branches and stashes in your local
repository that you don't want to lose. So if you want to keep your
current local repo, you'll definitely have to deal with these two
issues. Here's how to solve them.

A. The line endings issue. You want to make sure that you're not
inadvertently checking in CRLF line endings.

Make sure you set this:

git config --global core.autocrlf true

This tells Git to use the system's default line ending (CRLF) when
checking out and LF when checking in.

B. File permissions being changed by Cygwin

The executable bit on Git repository files will get changed, and you
will have permissions "changes" in your git respository immediately
after checkout. Fix this by turning git's core.filemode config to
false. Refer to this post:
https://groups.google.com/a/chromium.org/forum/?fromgroups=#!topic/chromium-dev/0EdNev3NNsw

1. Turn it for globally: git config --global core.filemode false
2. Turn it off for each repo: git config core.filemode false
3. Turn it off for all the submodules of each repo: git submodule
foreach --recursive git config core.filemode false

If you don't change do (B), then when you do 'git status' you'll find it listing a lot of files that have been changed. Then when you do a 'git diff' on one of them, you'll see that the only change was in the executable bit in the permissions, ie

$ git diff scripts/e2e-test.sh
diff --git a/scripts/e2e-test.sh b/scripts/e2e-test.sh
old mode 100755
new mode 100644


Tuesday, 1 April 2014

Mockito - don't use @InjectMocks if your constructor will be calling methods on your injected mocks

I had this:

@Mock
MessageSource mockMessageSource;

@InjectMocks
PhoneNumberUtil phoneNumberUtil;


@Before
public void setup() {

    MockitoAnnotations.initMocks(this);


    when(mockMessageSource.getMessage(eq("first.key"), (Object[]) eq(null), anyString(),eq(Locale.getDefault()))).thenReturn("This is a default message");
    when(mockMessageSource.getMessage(eq("second.key"), (Object[]) eq(null), anyString(),eq(Locale.getDefault()))).thenReturn("second Value");


}


Where


public PhoneNumberUtil(MessageSource messageSource) {
    this.firstValue = messageSource.getMessage("first.key"), null, FIRST_DEFAULT_VALUE, Locale.getDefault());
    this.secondValue = messageSource.getMessage("second.key"), null, SECOND_DEFAULT_VALUE, Locale.getDefault());
}



I was running my unit test but wondering why the fields "firstValue" and "secondValue" were still coming out as null.

I already mocked the messageSource.getMessage() calls. Why weren't my mocks getting called??

Then I realised: the when().thenReturn() calls weren't getting called until AFTER PhoneNumberUtil was already initialized!


I was using @InjectMocks and once the line

MockitoAnnotations.initMocks(this);

was called, phoneNumberUtil would already get initialised with mockMessageSource. But at that point, mockMessageSource isn't setup to do anything yet.

So the fix was to do this in my setup()



@Mock
MessageSource mockMessageSource;

PhoneNumberUtil phoneNumberUtil;

@Before
public void setup() {

    MockitoAnnotations.initMocks(this);


    when(mockMessageSource.getMessage(eq("first.key"), (Object[]) eq(null), anyString(),eq(Locale.getDefault()))).thenReturn("This is a default message");
    when(mockMessageSource.getMessage(eq("second.key"), (Object[]) eq(null), anyString(),eq(Locale.getDefault()))).thenReturn("second Value");

    phoneNumberUtil = new PhoneNumberUtil(mockMessageSource);

}


If you need to make a call on a mocked dependency in the constructor of the class you're testing, then you can't use the @InjectMocks pattern.

You need to create the class you are testing using code, not annotations, using constructor injection to pass in the mocked dependencies, and you need to create it after you've mocked the methods being called in the constructor.


Friday, 14 March 2014

Prevent Googlebot from crawling some links, but still have them useable by the user

WHAT:

Prevent Googlebot from crawling some links, so that other more
important links on our page are given priority and indexed faster


HOW:

1. remove the link, replace with "#" so it's still a valid HREF
2. Put the value as data attribute, ie, data-goto
3. Reverse the value because if we had data-goto="/productlist.html",
Googlebot would still crawl it.
Instead we have data-goto="lmth.tsiltcudorp/"
4. On loading the page, run a javascript to go through all the links
that have this attribute, reverse the string and place it in the
"href" attribute. Googlebot can't run javascript so it never sees the
HREF values in there.

RETURNED BY SERVER

<a class="unseen-link" href="#" data-goto="lmth.tsiltcudorp/">Product List</a>


SEEN BY USER

<a class="unseen-link" href="/productlist.html"
data-goto="lmth.tsiltcudorp/">Product List</a>



function prepareMissingLinks() {
var stringReverse = function(string) {
return string.split('').reverse().join('');
};
var $hiddenLink = $('.unseen-link');

$hiddenLink.each(function() {
$(this).attr("href", stringReverse($(this).data('goto')));
});
}



I had an earlier implementation of this using the hover event but that
doesn't work for touchscreen devices. This works on desktop and
mobile, and is also simpler.

Wednesday, 26 February 2014

How to change extension of multiple files in Linux?

When files are in same folder:

rename 's/.abc$/.edefg/' *.abc


Recursively rename files:

# Bash
# Also requires GNU or BSD find(1)
# Recursively change all *.foo files to *.bar

find . -type f -name '*.foo' -print0 | while IFS= read -r -d '' f; do
mv -- "$f" "${f%.foo}.bar"
done



From:
http://askubuntu.com/questions/35922/how-to-change-extension-of-multiple-files-from-command-line

Also see:

http://mywiki.wooledge.org/BashFAQ/030

Wednesday, 12 February 2014

@RetryOnFailure annotation using Aspect from JCABI library

Use this annotation to make repeated calls on failure for an idempotent method

http://www.jcabi.com/jcabi-aspects/annotation-retryonfailure.html

Validate user-inputted URLs using Google Safe Browsing API

Use this API to get a database of URLs from Google and verify them
locally. They also have an earlier version of the API but you need to
make a remote call. This is better as the client is the only one that
knows about the URLs it is verifying.

https://developers.google.com/safe-browsing/developers_guide_v2

Thursday, 6 February 2014

Notes: Example of image data embedded in HTML

This is a div that appears on the Onion above an article when you've
reached 5 articles already, asking you to subscribe. The image
information is embedded in base64 on the div itself.


<div style="background-image:
url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6RDhCQzBDREZCQ0ZGMTFFMEIxQkFCQ0MxOEQzMzc5NTEiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6RDhCQzBDRTBCQ0ZGMTFFMEIxQkFCQ0MxOEQzMzc5NTEiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDpEOEJDMENEREJDRkYxMUUwQjFCQUJDQzE4RDMzNzk1MSIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDpEOEJDMENERUJDRkYxMUUwQjFCQUJDQzE4RDMzNzk1MSIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PoOauxEAAAALSURBVAgdYzA+AwABNQEAGVbmvQAAAABJRU5ErkJggg%3D%3D);
z-index: 99998; position: absolute; top: 0px; left: 0px; width: 100%;
height: 3198px;"></div>

Tuesday, 4 February 2014

Java: (Spring) Getting subset of a list for paged display

PagedListHolder - one of those Spring classes that you wish you've
been using for the past 10 years

Given a
1. list of items
2. page number and
3. number of items per page

Return the part of the list to be displayed on the page


PagedListHolder<PopularSearch> pagedListHolder = new
PagedListHolder<PopularSearch>(sourceList);
pagedListHolder.setPage(pageNumber);
pagedListHolder.setPageSize(itemCountPerPage);

List<PopularSearch> tmp = pagedListHolder.getPageList();

Friday, 31 January 2014

Doubleclick for Publishers reference material

From: https://productforums.google.com/forum/#!category-topic/dfp/getting-started/208IT9kC6Xo

For Premium

DFP Premium Help Center
https://support.google.com/dfp_premium/

Please remember that are two main ways to contact DFP. There is a
Contact Us link on all DFP Premium support page (near the upper right
hand part of the page). There is also a Report a Bug link in your DFP
account (also in the upper right hand corner of your page).

Getting Started Guide (high level overview)
https://support.google.com/dfp_premium/answer/184981

Training
https://support.google.com/dfp_premium/answer/3419426

Partner Directory
https://support.google.com/dfp_premium/answer/2616938

DFP Training Videos:
http://www.youtube.com/dfp

Release Notes
https://support.google.com/dfp_premium/answer/179039


Other Resources

Google also provides additional tools to webmasters to help you track
traffic and improve your search rankings. If you have not signed up
already, you might want to at least take a quick look at them.

Google Analytics
http://www.google.com/analytics/

Google Webmaster Tools
http://www.google.com/webmasters/


Webinars

Google has been releasing quite a few webinars that people maybe
interested in. While not always specific to DFP, many of them are
helpful for webmasters and advertising in general.

You can register for Google Webinars here:
http://www.google.com/ads/experienced/webinars.html

Or use their Calendar to see upcoming Webinars
https://www.google.com/calendar/embed?src=google.com_fqpk56cl9ck36np1ip263m2hks%40group.calendar.google.com

Javascript: Finding the viewport width,

Useful for responsive design, determining same max-width used in @media queries

var windowWidth = window.innerWidth ||
document.documentElement.clientWidth || document.body.clientWidth;

Based on: http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript


"The responsive viewport breakpoint "staircase" in my opinion becomes:

320px, 480px, 603px, 768px, 966px, 1024px"

See: http://www.hardweb.com.au/nexus7viewportportrait603x797landscape966x444.html

Also read:

http://alistapart.com/article/vexing-viewports


Been working recently on Google DFP tag code, so keeping these references:

http://exisweb.net/how-to-use-google-adsense-on-a-responsive-website

https://productforums.google.com/forum/#!msg/dfp/gxHN91Z7PoI/yC14WsR0KoMJ

http://stackoverflow.com/questions/15423189/changing-ad-size-based-on-browser-width-in-dfp

http://stackoverflow.com/questions/15452905/adding-a-browser-width-component-to-ad-slots




<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style type='text/css'>
</style>


<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$("#showValue").click(function () {
var windowWidth = window.innerWidth ||
document.documentElement.clientWidth || document.body.clientWidth;

$("#widthLabel").text(windowWidth);
$("#windowinnerWidth").text(window.innerWidth);
$("#documentdocumentElementclientWidth").text(document.documentElement.clientWidth);
$("#documentbodyclientWidth").text(document.body.clientWidth);
$("#viewportHeight").text($(window).height());

});
});//]]>

</script>


</head>
<body>
<p>Width: <span id="widthLabel"></span>

<br/>window.innerWidth: <span id="windowinnerWidth"></span>

<br/>document.documentElement.clientWidth: <span
id="documentdocumentElementclientWidth"></span>

<br/>document.body.clientWidth: <span id="documentbodyclientWidth"></span>

<br/>$(window).height(): <span id="viewportHeight"></span>
</p>
<input id="showValue" type="button" value="Show Width" />
</body>


</html>

Wednesday, 15 January 2014

TIL: Hibernate JPA NamedNativeQuery declaration requires a "resultClass" even for update/delete operations

I was writing some JPA code (Hibernate implementation) that used
NamedNativeQuery

and kept getting this when I ran the webapp in Tomcat:

nested exception is org.hibernate.cfg.NotYetImplementedException: Pure
native scalar queries are not yet supported

The exception was from an unrelated class but I suspected that this
was caused by changes I made.

According to this:

http://jdevelopment.nl/hibernates-pure-native-scalar-queries-supported/

resultClass is required for all NamedNativeQuery, even the ones that
don't return them, because of a longstanding BUG in Hibernate.
Apparently the bug was fixed as of Hibernate 4.2 but we're still on
3.6 so upgrading is not really an option.

Thursday, 31 October 2013

Joda Time - handling of end of February when DateTime.plusMonths() is called

Overheard a colleague at work with an issue re: February and got
curious enough to see what Joda-Time does in handling this scenario:

// if current date is 31st, how will joda-time handle setting month to february?
dt = new DateTime();
println "now: " + dt.toString();
futureDt = dt.plusMonths(4);
println "4 mos in future: " + futureDt.toString();

OUTPUT

now: 2013-10-31T15:29:53.628+11:00
4 mos in future: 2014-02-28T15:29:53.628+11:00

Nice.

The Groovy Shell functionality within IntelliJ IDEA makes quick
playing around like this a breeze!

Friday, 25 October 2013

Phatch - Photo Batch Processor

Found myself having to generate different resolutions of images.
Originally planned to use ImageMagick from a ruby script but came
across something quicker and easier - Phatch on Linux.

http://photobatch.wikidot.com/

(TO UPDATE THIS POST)

Thursday, 4 July 2013

Building strings in javascript

Instead of StringBuilder, we use an array and just append to the end of array.

http://trephine.org/t/index.php?title=Efficient_JavaScript_string_building


http://dev.opera.com/articles/view/efficient-javascript/?page=2#primitiveoperator

/**
* Constructs an html string from any number of lists of data items.
*/
function buildList( /* list1, list2, ... */ ) {
var buf = [ "<ul>\n" ];
for (var i=0; i<arguments.length; i++) {
var list = arguments[i];
for (var j=0; j<list.length; j++) {
buf[buf.length] = "<li>"; // append <li> tags and
buf[buf.length] = list[j]; // item to array buffer, utilizing
buf[buf.length] = "</li>\n"; // primitive array assignment
}
}
buf[buf.length] = "</ul>";
return buf.join('');
}

Wednesday, 19 June 2013

Workaround for Ubuntu 12 bug with Chrome - can't rearrange tabs

I have started using OneTab plugin to do this, where I would collapse
all tabs into a OneTab page, then rearrange them, but this would also
work:

https://bugs.launchpad.net/ubuntu/+source/unity-2d/+bug/935713/comments/11

"Two workarounds for this:

To move tabs left or right in a single window you can use
Ctrl+Shift+PgUp and Ctrl+Shift+PgDown.

To move tabs between two Chromium windows you need to set the
destination window to be "Always on top". Then dragging a tab from the
source window to the destination would actually allow the tab to be
inserted in the destination window, as expected."

Monday, 10 June 2013

From an ex-friends' post on St Augustine.

"That is why the Philippines is one of, if not the most blest people
in this earth by God. Suffering is strongest in Love and it works both
ways, for God and us his people. That is the reason why life in the
Philippines is always difficult. No man or anybody is meant to make it
otherwise, because God harvests millions of souls from our country. He
gives our people suffering as a divine gift in purifying souls that is
a requirement for us to enter the kingdom of God. He loves us so much
that suffering is His blessing so that we will always love Him back
above all else, and never look elsewhere."


I can't believe it. This is from a guy named after a leader of the
Russian Revolution. What happened? What made him give up, to accept
that suffering is our lot in life and is a blessing? What twisted
logic! What kind of insecure 5-year old god is this, to make people
suffer so that they pay more attention to him? Accept your situation,
don't try to make it better, suffering makes you better for the
afterlife. Holy shit.


Oh wait, Sherlock. What if there is no afterlife? What if this is all
we have? What then? No second chances, no upgrades to first-class at
journey's end. What have you done with your life if all you did is
suffer, hoping that you get a better deal in the next one?

Monday, 27 May 2013

JQuery: Toggle display of a div

Javascript:

// show 'more-attributes' for ad if user clicks 'Show attributes' link
$('.show-hide-attributes').live('click', function () {
var $this = $(this);
$(this).closest('.meta-attrbt').find('.more-attributes').slideToggle(0,
function () {
$this.text($(this).is(':visible') ? "Hide attributes" :
"Show attributes");
});

return false;
});


HTML

<dd class="p-ads-dd meta-attrbt">
<a class="show-hide-attributes" href="#">Show attributes</a>
<div style="display: none;" class="more-attributes">
<p>Attribute 1: This is a detail</p>
<p>Attribute 2: This is another detail</p>
<p>Areas of expertise: Expertise 1</p>
<p>Areas of expertise: Expertise 2</p>
</div>

</dd>


JSFiddle:

http://jsfiddle.net/25QC7/



Attach function to click event of .show-hide-attributes

We are using classes ('more-attributes', 'show-hide-attributes)
because there are multiple instances of these on the page.

We are using .find() to attach the slideToggle() because there are
multiple rows containing these attributes. Each set of attributes is
specific to a single record.

Added function to slideToggle() so that label changes from 'Show
attributes' if attribute div is hidden, to 'Hide attributes' if the
div is displayed.

Tuesday, 14 May 2013

Java: Anti-pattern - make your method names as terse as possible.

Just came across a mock class used in unit tests that has:


cr()

b()

Of course there was no doco on the method names.

Took me a while to realise this was:

create()

build()


So why the fuck did they have to make them that short? Probably
because they were using vi.

Thursday, 9 May 2013

IntelliJ: recommended vmoptions settings to improve performance

Currently using these settings

-Xms512m
-Xmx2048m
-XX:MaxPermSize=512m
-XX:ReservedCodeCacheSize=256m
-XX:+UseCodeCacheFlushing
-XX:+UseConcMarkSweepGC
-ea
-Dsun.io.useCanonCaches=false
-Djava.net.preferIPv4Stack=true

If that doesn't work, I'll try and update bin/idea64.vmoptions to:

-server
-Xms1024m
-Xmx1024m
-XX:NewSize=128m
-XX:MaxNewSize=128m
-XX:PermSize=256m
-XX:MaxPermSize=256m
-XX:+UseParNewGC
-XX:ParallelGCThreads=4
-XX:MaxTenuringThreshold=1
-XX:SurvivorRatio=8
-XX:+UseCodeCacheFlushing
-XX:+UseConcMarkSweepGC
-XX:+AggressiveOpts
-XX:+CMSClassUnloadingEnabled
-XX:+CMSIncrementalMode
-XX:+CMSIncrementalPacing
-XX:+CMSParallelRemarkEnabled
-XX:CMSInitiatingOccupancyFraction=65
-XX:+CMSScavengeBeforeRemark
-XX:+UseCMSInitiatingOccupancyOnly
-XX:ReservedCodeCacheSize=64m
-XX:-TraceClassUnloading
-ea
-Dsun.io.useCanonCaches=false



REF: http://p7h.blogspot.com.au/2012/12/intellij-idea-tuning-parameters.html

Monday, 6 May 2013

UNIX: Copy a file to multiple subdirectories

I have a directory tree and in each of the lowest level directories, I
have a file called "attributes"

I've updated the contents of one of them, and want to copy it to the
other directories.

1. Find the files
find . -name attributes > target_directories.log

2. Edit "target_directories.log" and remove the "attributes" so it is
a list of subdirectories.

./building_trades/carpentry/attributes
./building_trades/concreting_paving/attributes
./party_catering/attributes

becomes

./building_trades/carpentry/
./building_trades/concreting_paving/
./party_catering/

Do this in vi using:

:%s/search_string/replacement_string/g

:%s/\/attributes/\//g

We have to use the / in our search and replace, as doing

:%s/attributes//g

didn't seem to work

The "\" has to be there to escape the "/" character

3. Copy our updated file to other subdirectories:

cat target_directories.log |xargs -n 1 cp attributes