Skip to content

Instantly share code, notes, and snippets.

View tonypiazza's full-sized avatar

Tony Piazza tonypiazza

View GitHub Profile
@tonypiazza
tonypiazza / JunitIgnoreAnnotationExample.java
Last active September 27, 2015 10:28
Example usage of the org.junit.Ignore annotation
@Ignore @Test
public void canCreateIntegerPropertyAndValidate() {
Property prop =
new IntegerProperty("productRating", "Rating", false, 1, 10);
/***************************************************************
* The following assertions will fail if executed. Remove this *
* comment and the @Ignore annotation once refactoring of the *
* PropertyFactory is complete. *
***************************************************************/
assertFalse("Property value should be invalid", prop.isValid(0));
@tonypiazza
tonypiazza / JunitTestAnnotationWithTimeoutExample.java
Last active September 27, 2015 10:28
Example usage of the timeout attribute of the org.junit.Test annotation
@Test(timeout=3000)
public void canGetListOfAllPastAuctions() throws ServiceException {
AuctionService service = new BaseAuctionService();
service.loadAuctionHistory();
Collection<Auction> pastAuctions = service.getPastAuctions();
assertNotNull("unable to get past auctions", pastAuctions);
assertTrue("no past auctions", pastAuctions.size() > 0);
}
@tonypiazza
tonypiazza / JunitTestAnnotationWithExpectedExample.java
Last active September 27, 2015 10:28
Example usage of the expected attribute of the org.junit.Test annotation
@Test(expected=ServiceException.class)
public void cannotCreateUserWithSameUsername() throws ServiceException {
User user1 = new User("Jane", "Smith", "jsmith", "venus");
User user2 = new User("John", "Smith", "jsmith", "mars");
AuctionService service = null;
try {
service = new BaseAuctionService();
service.saveUser(user1);
} catch (ServiceException e) {
fail( e.getMessage() );
@tonypiazza
tonypiazza / JunitAssertThatExample.java
Last active September 27, 2015 10:28
Example usage of the assertThat method of the org.junit.Assert class
@Test
public void canGetAuctionsForUsername() throws ServiceException {
String username = "tpiazza";
Auction[] auctions = new Auction[5 + new Random().nextInt(16)];
for(int i = 0; i < auctions.length; i++) {
Auction auction = createDummyAuction( username );
auctions[i] = auction;
service.saveAuction( auction );
}
assertThat( service.getAuctions(username), hasItems(auctions) );
@tonypiazza
tonypiazza / JunitSuiteExample.java
Last active September 27, 2015 10:28
Example usage of the org.junit.runner.RunWith and org.junit.runners.Suite.SuiteClasses annotations
@RunWith(Suite.class)
@Suite.SuiteClasses({AuctionTest.class, BidTest.class, UserTest.class})
public class CoreAuctionDomainSuite {
// empty class
}
@tonypiazza
tonypiazza / LoggingAspect.aj
Created September 25, 2012 20:36
This aspect demonstrates how to implement logging of method and constructor entry/exit. This is something I created recently for a client to replace more than 10,000 lines of logging code.
package com.piazzaconsulting.aspect;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MemberSignature;
public aspect LoggingAspect {
private static final String POINTCUT =
@tonypiazza
tonypiazza / setup-cassandra-dev.sh
Last active December 16, 2015 04:59
A BASH script to setup everything we need for a Cassandra development machine with Ubuntu Desktop.
#!/bin/bash
# Script written by Tony Piazza (https://gist.github.com/tonypiazza)
# make sure we have sufficient privileges
if [ $UID != 0 ]
then
echo -e "Insufficient privileges!"
exit 1
fi
@tonypiazza
tonypiazza / setup-couchbase-dev.sh
Last active September 5, 2016 11:33
A BASH script to setup everything we need for a Couchbase development machine with Xubuntu Desktop. You will need a machine with at least 4348 MB of RAM and 4 cores.
#!/bin/bash
# Script written by Tony Piazza (https://gist.github.com/tonypiazza)
# Privileges check
if [ $UID != 0 ]
then
echo -e "Insufficient privileges!"
exit 1
fi
@tonypiazza
tonypiazza / findByUserName.java
Last active August 29, 2015 14:17
Here is an example of using RxJava via the Couchbase Java SDK.
public Iterable<Playlist> findByUserName(String userName) {
return getBucketFactory()
.getAsyncBucket(Playlist.class)
.flatMap(bucket -> bucket
.get(USER_PREFIX + userName)
.flatMap(doc ->
Observable.from(doc.content().getArray(PLAYLISTS_PROPERTY))
)
.flatMap(id -> bucket.get(id.toString()))
.map(doc -> fromJsonObject(doc.content(), Playlist.class))
@tonypiazza
tonypiazza / CheckAndSet.java
Last active August 29, 2015 14:25
Implementation of CAS using AspectJ
/*
* Copyright 2015 Piazza Software Consulting, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software