Groovy / Spring / Spring Security / Gmock : Last method called on mock already has a non-fixed count set

I banged my head against the wall for quite a while trying to figure this error out in my Groovy Junit tests:

Request processing failed; nested exception is java.lang.IllegalStateException: Last method called on mock already has a non-fixed count set.

I’m using Spring 4.0 with Spring Security 3.2.5 and thought maybe it was something to do with my test setup.  It turns out to be pretty straightforward.  I hadn’t wrapped my test in a play closure.  So going from this:

def r = mockMvc.perform(get("/...")).andExpect(status().isOk()).andReturn()

to this:

play {
  def r = mockMvc.perform(get("/...")).andExpect(status().isOk()).andReturn()
}

solved my problem.

Down to one remote with HDMI-ARC and HDMI-CEC functionality

One of my biggest complaints about getting a home theatre system was the need for multiple remotes.  I know you can buy an all-in-one remote that will manage everything for you but for whatever reason I didn’t do that. I just bought a Samsung Smart TV and decided to look into the state of TV remotes again.

228550581

Last night I learned that I was just ignorant of some progress that had been made in this area by two standards: HDMI Audio Return Channel (HDMI-ARC) and HDMI Consumer Electronics Control (HDMI-CEC).  Using these two technologies, I have tied all of my devices together under one remote control.  I push one power button and everything turns on. I push the volume up button on the TV remote and my receiver volume increases.  I want to go to bed and I hit the power button on the TV and both the TV and receiver turn off.  This is the way things always should have been.

Let’s start with HDMI-ARC.  This is quite simple in concept but solves the persistent issue of how do you get audio from the TV back to your receiver.  Most setups I have done and seen involve Blu-ray players, cable STBs and PS3s sending their video and audio to your home cinema receiver then the receiver sends the video to the TV.  Basically, your TV is a dumb monitor.  This all changed when I got my Samsung Smart TV and tried to watch Netflix on it for the first time.  My picture quality was fine but all the audio was coming from my TV!  Enabling the HDMI-ARC on the TV and my Onkyo receiver solved that problem and now it makes no difference where the audio comes from, my receiver can find it.

Next is HDMI-CEC.  You will find this under various brand names like VIERA Link or Anynet+ but they are mostly compliant with the HDMI-CEC standard.  What it allows you to do is send commands over the HDMI cable so when your TV powers up and it’s on the cable input, it tells your cable box to power up too.  When you switch to Blu-ray, it turns that on for you.  My TV also automatically tied into my receiver’s volume control so when you change the volume on the TV it is actually changing the volume on my home cinema receiver.

Sounds like paradise, right?  Well, I have managed to get things gummed up a couple of times and had to reboot everything a couple of times already but I’m hoping that it’s because I don’t have it all hooked up correctly yet.  Also, you’ll probably still need your Tivo remote to do some Tivo things and your Blu-ray remote to do some BD things.  But I’m okay with that.  If I can use one remote 90% of the time then I’m happy.

Using CSS instead of XPath in Xopus config to set placeholder text in Xopus

We have URLs that are generated from the main topic’s title element and we wanted the authors to be aware of this when they created a page in Xopus. I thought it would be a simple update to the Xopus config.xml file but it turned out not to be. In the end, we added content using CSS instead of using the Xopus config like this:

/* This adds text to Xopus specifically on the first page title*/
#xopus-xsl-root div h1.topic-title:first-of-type .xopus-placeholder:after {
  content: ' (used for generating page URL)';
}

I believe this could do with a little more refinement but it’s worked in all the cases we’ve tested so we are happy.

If you are interested in what didn’t work, keep reading.

One of the limitations of the Xopus placeholder configuration option is that it is not really XPath so it only matches on the element name, attribute name or a class name.  This is documented on the Xopus Use of XPath in configuration FAQ page.

So we first tried:

<x:node match="class('topic/title')" preferElementsOnlyParent="true">
  <x:name xml:lang="en">Title (used to generate URL)</x:name>
  <x:placeholder xml:lang="en">{name}</x:placeholder>
</x:node>

But this added the message ‘ (used to generate URL)’ to the titles on figures, sections and other elements. My next thought was to do add the longer placeholder only to actual descendants of the topic element (and go back and add concept, etc. later) like this:

<x:node match="topic/title" preferElementsOnlyParent="true">
  <x:name xml:lang="en">Title (used to generate URL)</x:name>
  <x:placeholder xml:lang="en">{name}</x:placeholder>
</x:node>
<x:node match="class('topic/title')" preferElementsOnlyParent="true">
  <x:name xml:lang="en">Title</x:name>
  <x:placeholder xml:lang="en">{name}</x:placeholder>
</x:node>

This resulted in not showing the additional text message anywhere because the Xopus config doesn’t use full XPath, as I mentioned earlier so a match of topic/title matches all titles and since class(‘topic/title’) also matches the element and it comes later in the file, that is the version that was used.

Installing a SuperNexus ROM on Galaxy S3

I finally did it. I finally rooted my Galaxy S3 and installed a custom ROM. This usually happens when my phone becomes slow and I think “this phone was fast last year so what is wrong?”

I also despise hearing about new Android features but not being able to use them. My Google Now was horrendously slow on my Samsung TouchWiz ROM.  I just got a galaxy tab 10 too and I found that things were just different enough between the two to annoy me when switching back and forth.

So was it hard? Not at all. I’d explain but the instructions on the SuperNexus ROM announcement page are adequate. If you have problems, let me know and I’ll help. I’ve flashed a lot of ROMs on other devices but this is the first one on my S3.

http://forum.xda-developers.com/showthread.php?t=2219885

Now for what didn’t work very well.

Barclays mobile app wouldn’t run on my rooted Galaxy S3. It would start and take me through all the steps then bomb out saying that you cannot run the app on rooted devices. This is annoying because I’ve come to rely on this application’s mobile pinsentry functionality.  After a little research, I found that I could get the Barclays Mobile application to run on my rooted phone using the steps below. I basically followed the instructions I found here: http://forum.xda-developers.com/showthread.php?t=1710613

  1. Buy TriangleAway
  2. Tell it to reset your custom ROM counter
  3. Open the SuperSU application
  4. Click the button that says prepare for reinstallation via google play (or something similar…my app is gone now so I can’t go back and look!)
  5. Reboot
  6. Your phone now is not flagged as having a custom ROM and superSU is not installed
  7. Run the Barclays app and set it up
  8. Unverified yet: reinstall SuperSU from the play store. I’ve read that this will work but I’ll download it and use it when I need it next. For now things are in good shape on my phone.

I’ve heard that this will help you use SkyGo on a rooted Galaxy S3 too but I haven’t tried that either. Bonus: my Netflix application works now too!

I’ve been running the ROM for a day and am incredibly pleased with my phone again. I’d recommend flashing to anybody who is a little frustrated with lags and the overall performance of their Galaxy S3 at the moment. It’s not a hardware problem, it’s a software one!

Ultimaker first (few) print notes

There is a fairly steep learning curve on 3D printing. I spent the first day or two in a fog where I just pushed buttons and didn’t really know what was going to happen but that’s what hacking is all about, right?

Since I would have found this helpful, I’ll just give some tips that I’ve learned so far. Keep in mind that these are tips from a guy who’s been printing for just 24 hours.

  1. The Ultimaker build instructions end abruptly. I had a finished machine but nobody said, now print this and post your results to the forum so we can help you. But that’s a good idea. Also, I later found this page which is just what I was looking for after I finished assembly: http://wiki.ultimaker.com/Calibrate
  2. The blue tape and the build platform. When I got to the step where I was supposed to cover my lovely perspex platform in blue masking tape I shuddered. Why? Oh why? There are a couple of reasons. One is that perspex isn’t very grippy so it’s not good for your first print layer. The second is if something is really screwed up, you can just pull the tape up and put down a new layer. I’ve grown to like my lovely blue tape.
  3. What temperature do I use? How do I print? What is slicing? Do I need a support structure? Keep reading articles and take your time. The answers are out there and often it involves experimentation so just go for it.
  4. Are my belts tight enough? Probably not. Grab a paperclip or two: https://groups.google.com/forum/?fromgroups=#!topic/ultimaker/L5oH548RO8I Otherwise people suggest printing things to help with belt tension but you haven’t got that far yet, have you?!
  5. How tight should I tighten the thumb screw on the extruder? I made mine pretty tight. People recommend “just tight enough” but I’m finding that position hard to find. Since I’m getting better at printing every minute I’m probably going to be confident enough to print the http://www.thingiverse.com/thing:26094

Just think, in no time, you’ll have sloppy prints just like me:

Image

First Ultimaker 3D Print

I spent the last couple of months building an Ultimaker 3D printer. Ok, I didn’t actually spend three months building it but it took me three months.  The build process was daunting because I wasn’t sure what I was building. If you draw up some plans for a bunk bed and then proceed to build it then you kind of know what’s happening at every step. When you get a big kit and are following directions you aren’t always sure why you are doing something.

2013-01-20 21.53.01 2013-01-20 21.53.36 2013-01-20 22.31.30 2013-01-20 22.32.33 2013-01-20 22.33.59 2013-01-21 00.05.08

 

As you can see my bed levelling went a bit wonky. Those are supposed to be squares. The y-axis was working well but the x-axis wasn’t and seemed to cause movement in the y-axis instead. After untangling my wires because I thought I had crosstalk I realised that I hadn’t properly tightened the pulleys on the actual stepper motors. A twist of the screwdriver and things became much better.

I find this all rather exciting and daunting at the same time. I’ve been stuck in the land of digital for so long that worrying about crosstalk, friction and belt tensions is slightly bewildering. I’d recommend it to any developer!

Looking for XML elements in Javascript with e4x

If you are manipulating XML in Javascript then E4X is one of the options available to you. The problem is that the documentation is a little thin and the examples only show you how to find things that definitely exist. So here’s some help in trying to see if elements exist or not.

In this example, I’m processing DITA and trying to find the “title” of the document. This can be in a variety of locations in DITA and can be a <title> in a topic, concept, etc. or a <mainbooktitle> element in a bookmap. So how do you see if you have a mainbooktitle in e4x?

var concept = new XML("...my dita concept xml...");
var mainbooktitle = concept..mainbooktitle;
if (mainbooktitle.length() > 0) {
    // we have a mainbooktitle
}

The concept..mainbooktitle construct finds all children elements in the document named mainbooktitle and returns them in an XMLList. Crucially the documentation does not tell you that an XMLList is always returned and can either be:

  1. A list of length zero meaning no elements named mainbooktitle were found
  2. A list of length one meaning only one element was found. When this happens, you can refer to the item as if it were not in a list. For example, to get the id attribute you could just say mainbooktitle.@id instead of mainbooktitle[0].@id.
  3. A list of length greater than one. You should then use a for each loop or access items by index like this mainbooktitle[1].@id

I hope this saves somebody some time because I had to figure it out by trial and error!

“Flash the screen when an alert sound occurs” disabled but my screen still flashes!

I’m not sure when it started but I recently noticed that my screen was flashing all of the time when I was using the terminal instead of sounding a tone like it should. I dug around in Terminal’s preferences and in Universal Access but everything seemed to be in order.

A couple of Google searches later and I came across a post from Erik Phansen that solved my problem. It boils down to running one command in the terminal:

sudo killall coreaudiod

http://blog.erikphansen.com/weird-os-10-7-lion-audiovideo-problems/

Note that I had some strange feedback echoes after killing coreaudiod that I fixed by alternating between starting iTunes and playing some music and killing coreaudiod. Unfortunately I can’t say what combination stopped the echoes.

How long are my URLs?

We are trying to reduce the allowed URL length in one of our applications for security reasons. The first question that you need to answer when doing an optimisation like this is “how many characters do I need to keep my application running?” You can look through your apache log files to find out. Here is a command-line script to help you find out how long your URLs are:

cat access.log* | cut -d" " -f7 | awk '{ print length($0),$0 }' | sort -nu

One thing I noticed is that apache logs URL-encoded strings so the line lengths are too high if you have multibyte or special characters in your URLs. I was able to read the results  and do the maths manually this time but I will probably need to sort that out for the next round.

Managing window.onbeforeunload in Javascript

I struggled with this quite a bit because there is a lot of information on the web that is conflicting. I wanted a way to tell the user that they had unsaved changes when they tried to leave an editing page but not get the warning when they hit the save button. This seems like a logical user experience to me!  The trick I found was to create a flag that the event handler checks to know whether you are in the middle of a save or not. To be slightly more generic, I called it showUnsavedChangesWarning and defaulted it to true. Here is the code I ended up with:


top.showUnsavedChangesWarning = true;
window.onbeforeunload = function (event)
{
if (top.showUnsavedChangesWarning) {
var e = event || window.event;

// If we have unsaved changes, tell the browser to show a confirmation window
var message = null;
var document = Editor.getActiveDocument();
if (document != null && document.hasUnsavedChanges()) {
message = 'You have not saved changes in your file.';
// Firefox 4 doesn't let you specify the text but does popup a warning.
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = message;
}
// For Safari
return message;
}
}

// otherwise just fall through and return nothing
};

You won’t have the Editor object unless you’re also working with Xopus but you get the idea. The interesting details here are:

  1. Don’t return anything if you want the browser to just continue on. Some sites recommended null, some the empty string. The only thing that works consistently for me is not returning anything.
  2. Safari seems to have different handling from everyone else
  3. Firefox4 doesn’t let you display a custom message anymore. You can of course display your own confirm dialogue but they’ve removed the ability for you to stick custom text into a Firefox application dialogue. I think this is a good decision.

Hopefully this helps you too.