Skip to content

Instantly share code, notes, and snippets.

View beargiles's full-sized avatar

Bear Giles beargiles

  • Boulder, CO
  • 21:10 (UTC -06:00)
View GitHub Profile
@beargiles
beargiles / H2withBrowser.java
Last active April 16, 2019 15:03
Create an embedded H2 instance and launch related web console.
/**
* Simple code to create an embedded H2 server and launch associated web console.
*/
import java.sql.*;
import org.h2.jdbcx.JdbcConnectionPool;
import org.h2.tools.Server;
private static final String URL = "jdbc:h2:~/test;AUTO_SERVER=TRUE";
private static final String USERNAME = "sa";
private static final String PASSWORD = "";
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
@beargiles
beargiles / AdvancedTestCase.java
Created August 3, 2015 00:50
How to add database logging to JUnit3 TestCase.
public class AdvancedTestCase extends TestCase {
public void run(TestResult result) {
AdvancedTestListener l = AdvancedTestListenerFactory.INSTANCE.newInstance();
result.addListener(l);
l.setName(getName());
// normally do this with reflection
MyTestedClass orig = ((MyAdvancedTest) this).obj;
MyFacadeClass facade = new MyFacadeClass(orig);
@beargiles
beargiles / DnsBlockingSecurityManager.java
Last active January 3, 2016 23:22
SecurityManager example
package com.invariantproperties.sandbox.securityManager;
import java.net.InetSocketAddress;
import java.net.SocketPermission;
import java.security.Permission;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@beargiles
beargiles / BasicKdcTest.java
Last active May 14, 2018 17:48
JAAS with Kerberos; Unit Test using Apache Hadoop Mini-KDC.
import java.io.File;
import javax.security.auth.kerberos.KerberosPrincipal;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@beargiles
beargiles / typesafe_encode.sql
Last active October 25, 2021 23:26
Type-safe PostgreSQL encode/decode
--
-- Define custom enum type containing supported encoding algorithms
--
CREATE TYPE encoding AS ENUM ('BASE64', 'ESCAPE', 'HEX');
--
-- Type-safe alternative to encode(bytea, text)
--
CREATE OR REPLACE FUNCTION encode (value bytea, alg encoding) RETURNS text AS $$
#print_strict_params on
@beargiles
beargiles / get_gluon.sql
Last active October 25, 2021 23:35
PostgreSQL function using user-defined enum type in case statement
CREATE OR REPLACE FUNCTION get_gluon(color color) RETURNS TEXT AS $$
#print_strict_params on
DECLARE
duck TEXT;
BEGIN
CASE color
WHEN 'RED'::color THEN
duck := 'Huey';
WHEN 'GREEN'::color THEN
duck := 'Dewey';
@beargiles
beargiles / color.sql
Last active October 25, 2021 23:38
Defining a PostgreSQL enum type
--
-- Create user-defined enum type.
-- (List details with '\dT+ color' in psql)
--
CREATE TYPE color AS ENUM ('RED', 'GREEN', 'BLUE');
--
-- Create a table using the user-defined enum type.
--
CREATE TABLE quark (
@beargiles
beargiles / color.sql
Created October 25, 2021 23:27
PostgreSQL table with check constraint
CREATE TABLE color (
color TEXT CHECK (color IN ('RED', 'GREEN', 'BLUE'))
);
@beargiles
beargiles / color_check.sql
Created October 25, 2021 23:32
PostgreSQL function checking whether a parameter is in a valid set of values
--
-- Function that needs to explicitly check whether the 'color' value is valid
--
CREATE OR REPLACE FUNCTION get_gluon(color text) RETURNS TEXT AS $$
#print_strict_params on
DECLARE
duck TEXT;
BEGIN
IF color not in ('red', 'green', 'blue') THEN
RAISE EXCEPTION 'unknown color %', color;