Using awk on the Command Line to Parse Files


I needed to format a file that contained a list of words with their accompanying definitions.  I didn't need the definitions and wanted to put an 'item' tag around each word.  awk to the rescue!

The Command:

awk '{print ""$1""}' < words.xml

I have this stored in the words.xml file:


BAD something bad
BAG to put into a bag
BAH intj. expressing disgust
BAL type of shoe (balmoral)
BAM to strike with a dull sound
BAN to prohibit/Rumanian coin

After running the command, I get:

<item>BAD</item>
<item>BAG</item>
<item>BAH</item>
<item>BAL</item>
<item>BAM</item>
<item>BAN</item>

Thanks awk!

Updated: Mocking Hibernate Create Criteria in Grails' Unit Test with GMock

Using the GMock Library I have updated my unit tests to mock out the create criteria methods the following way:

void testSomeFunction() {
    def results = []

    def mockCriteria = mock() {
      list(instanceOf(Closure)).returns(results)
    }

    mock(Book).static.createCriteria().returns(mockCriteria)

    play {
      assertEquals null, bookService.getDefaultBook(null)
    }
  }


You need to add the following to the top of the unit test file with the import statements:

import org.gmock.*
import static org.hamcrest.Matchers.*

@WithGMock

Also add the following to your BuildConfig.groovy:

dependencies {
    test "org.gmock:gmock:0.8.0"
    test "org.hamcrest:hamcrest-all:1.0"
}

I learned this from the following blog post: http://adhockery.blogspot.com/2010/01/using-gmock-to-complement-grails.html

Unit Testing a Android Activity which contains a Runnable

In order to unit test an Android Activity to verify that the Runnable is called you need to add a call to waitForIdle to the Instrumentation, which is called when all Runnables have been executed.

Here is the activity that we are testing:

public class SplashScreenActivity extends Activity {

public final int SPLASH_DISPLAY_LENGTH = 1200;
 
   @Override
   public void onCreate(Bundle icicle) {
 super.onCreate(icicle);
 setContentView(R.layout.splash);

 new Handler().postDelayed(new Runnable() {

  public void run() {    
   Intent mainIntent = new Intent(SplashScreenActivity.this, NextActivity.class);
   startActivity(mainIntent);
   finish();
  }

 }, SPLASH_DISPLAY_LENGTH);
   }
}

Here is the method in the Unit Test Class:

public void testSubLaunch() {
        SplashScreenActivity mActivity = startActivity(new Intent(), null, null);
        getInstrumentation().waitForIdle(new Runnable() {
  public void run() {
   assertNotNull(getStartedActivityIntent());
   assertTrue(isFinishCalled());
  }         
        });    
    }