Skip to content

Instantly share code, notes, and snippets.

View greghelton's full-sized avatar

Greg Helton greghelton

View GitHub Profile
@greghelton
greghelton / StreamReaderHelper.java
Created December 26, 2011 18:15
Java: Readers, Writers, Buffers and Streams - seems like a lot to deal with
package us.greg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
@greghelton
greghelton / StreamReaderHelperTest.java
Created December 26, 2011 18:19
Java: JUnit test for StringReaderHelper.java
package us.greg;
import java.io.ByteArrayInputStream;
import java.net.URLEncoder;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
public class StreamReaderHelperTest {
@greghelton
greghelton / TestStoreForward.java
Created December 26, 2011 19:17
Java: JUnit test Using Apache HTTPComponents
package us.greg;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
@greghelton
greghelton / using_ruby_hash.rb
Created December 26, 2011 20:04
Using a Ruby Hash
inst_section = {
:collie => 'furry',
:parakeet => 'feathered',
:cow => 'bovine',
:ant => 'stinging',
:snake => 'slithering',
:elephant => 'large'
}
puts inst_section[:ant]
puts inst_section[:cow]
@greghelton
greghelton / ProgrammableTest.java
Created December 26, 2011 20:22
Java: Using Mockito for Unit Testing
package us.home.electronics;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
public class ProgrammableTest {
@greghelton
greghelton / Programmable.java
Created December 26, 2011 20:24
Java: Dumb class only to demonstrate Mockito (see unit test)
package us.home.electronics;
import java.io.Console;
import java.io.IOException;
/**
* For conciseness, throw classes into just one source file
*/
public interface Programmable {
@greghelton
greghelton / build.xml
Created December 26, 2011 20:28
ANT build file for the Mockito Example
<project name="programmable" basedir="." default="test">
<property name="src.dir" location="src"/>
<property name="bin.dir" location="bin"/>
<property name="lib.dir" location="lib"/>
<property name="junit" value="${lib.dir}/junit"/>
<property name="mockito" value="${lib.dir}/mockito"/>
<path id="classpath">
<pathelement location="${bin.dir}"/>
@greghelton
greghelton / OldSchoolJDBC.java
Created December 26, 2011 21:15
Java: code to verify Java can connect to 1995 SQL Server Database
package old.school;
import java.sql.*;
/**
* run as follows:
* 1. javac OldSchoolJDBC.java
* 2. java OldSchoolJDBC
*/
public class OldSchoolJDBC {
@greghelton
greghelton / loop-thru-maps.clj
Created December 27, 2011 00:06
Clojure: add several rows or records or structs then print singly then in loop
(def account-info (hash-set
{:username "zak"
:balance 13.59
:member-since "2009-02-01"}
{:username "mak"
:balance 12.59
:member-since "2011-02-02"}
{:username "jak"
:balance 11.59
:member-since "2009-12-02"}))
@greghelton
greghelton / do_sync.clj
Created December 27, 2011 00:08
Clojure: use recur & use Clojure transactiopn
; refs manage synchronous, coordinated state
; agents manage asynchronous, independent state
; atoms manage synchronous, independent state
; ref-set must be called within a transaction
; alter does the same thing as ref-set but alter implies the new
; value is a function of the old. ref-set implies old value is
; being obliterated and the ref will point to a new value.
; If a Transaction is initiated inside another transaction
; the inner transaction becomes part of the outer and commits
; when the outer does