Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.splout.db.common;
  
  /*
   * #%L
   * Splout SQL Server
   * %%
   * Copyright (C) 2012 Datasalt Systems S.L.
   * %%
   * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  * 
  * You should have received a copy of the GNU Affero General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  * #L%
  */
 import java.io.File;
 
 import  org.apache.thrift.transport.TTransportException;
 
Things that are used extensively in unit / integration tests.
 
 public class TestUtils {

Creates a simple database with two columns: one integer (a) and one string (b). It also insertes one default row from parameters a, b.
 
 	public static void createFooDatabase(String whereint aString bthrows SQLExceptionJSONSerDeException,
 		File dbFolder = new File(where);
 		dbFolder.mkdir();
 		final SQLiteJDBCManager manager = new SQLiteJDBCManager(where + "/" + "foo.db", 10);
 		manager.query("DROP TABLE IF EXISTS t;", 100);
 		manager.query("CREATE TABLE t (a INT, b TEXT);", 100);
 		manager.query("INSERT INTO t (a, b) VALUES (" + a + ", \"" + b + "\")", 100);
 		manager.close();
 	}

Use this method to get a high-level client for Hazelcast in unit tests.

Throws:
HazelcastConfigBuilderException
 
 		HazelcastInstance hz = Hazelcast.newHazelcastInstance(HazelcastConfigBuilder.build(testConfig));
 		return coord;
 	}

Use this class for waiting on a certain condition up to some time.
 
 	public static abstract class NotWaitingForeverCondition {
 		
 		public abstract boolean endCondition();
 		
 		public void waitAtMost(long patiencethrows InterruptedException {
 			long waitedSoFar = 0;
 			while(!endCondition()) {
 				Thread.sleep(200);
 				waitedSoFar += 200;
 				if(waitedSoFar > patience) {
 					throw new AssertionError("Waited more than " + patience + " on a test condition.");
 				}
 			}
 		}
 	}

Utility class that can be used for implementing things that can be retried up to a maximum number of times.
 
 	public static abstract class CatchAndRetry {
 
 		private int maxRetrials;
		private Class<? extends Throwableexception;
		public CatchAndRetry(Class<? extends Throwableexceptionint maxRetrials) {
			this. = exception;
			this. = maxRetrials;
		}
		public abstract void businessLogic() throws Throwable;
		public abstract void retryLogic();
		public void catchAndRetry() throws Throwable {
			int trial = 0;
			boolean succeeded = false;
			do {
				try {
					succeeded = true;
catch(Throwable t) {
						trial++;
else {
						throw t;
					}
				}
while(!succeeded && trial < );
		}
	}

Returns a QNode instance if, after a maximum of X trials, we can find a port to bind it to. The configuration passed by instance might have been modified accordingly.
	public static QNode getTestQNode(final SploutConfiguration testConfigfinal IQNodeHandler handlerthrows Throwable {
		final AtomicReference<QNodereference = new AtomicReference<QNode>();
		CatchAndRetry qNodeInit = new CatchAndRetry(java.net.BindException.class, 50) {
      public void businessLogic() throws Throwable {
				QNode qNode = new QNode();
				qNode.start(testConfighandler);
				reference.set(qNode);
			}
      public void retryLogic() {
				testConfig.setProperty(.testConfig.getInt(.) + 1);
			}
		};
		qNodeInit.catchAndRetry();
		return reference.get();
	}
	public static DNode getTestDNode(final SploutConfiguration testConfigfinal IDNodeHandler handlerfinal String dataFolderthrows Throwable {
		return getTestDNode(testConfighandlerdataFoldertrue);
	}

Returns a DNode instance if, after a maximum of X trials, we can find a port to bind it to. The configuration passed by instance might have been modified accordingly.
	public static DNode getTestDNode(final SploutConfiguration testConfigfinal IDNodeHandler handlerfinal String dataFolderboolean deleteDataFolderthrows Throwable {
		final AtomicReference<DNodereference = new AtomicReference<DNode>();
		testConfig.setProperty(.dataFolder);
		if(deleteDataFolder) {
			File file = new File(dataFolder);
			if(file.exists()) {
				FileUtils.deleteDirectory(file);
			}
			file.mkdir();
		}
		testConfig.setProperty(."fetcher-" + dataFolder);
		File fetcherTmp = new File("fetcher-" + dataFolder);
		if(fetcherTmp.exists()) {
			FileUtils.deleteDirectory(fetcherTmp);
		}
		fetcherTmp.mkdir();
		CatchAndRetry dNodeInit = new CatchAndRetry(TTransportException.class, 50) {
      public void businessLogic() throws Throwable {
				DNode dNode = new DNode(testConfighandler);
				dNode.init();
				reference.set(dNode);
			}
			public void retryLogic() {
				testConfig.setProperty(.testConfig.getInt(.) + 1);
			}
		};
		dNodeInit.catchAndRetry();
		return reference.get();
	}

Delete folders that *might* have been created by DNodes & ZooKeepers from a certain test class. The number of instances is the number of different methods (and therefore unique namespaces) in the test class. The convention is: dnode-ClassName-i for Dnode data folder of test class "className" and method "i". Same follows for ZooKeeper data folders "zk-" and Fetcher TMP folders "fetcher-dnode-" .
	public static void cleanUpTmpFolders(String classNameint nInstancesthrows IOException {
		File file = new File("dnode-" + className);
		if(file.exists()) {
			FileUtils.deleteDirectory(file);
		}
		file = new File("zk-" + className);
		if(file.exists()) {
			FileUtils.deleteDirectory(file);
		}
		file = new File("fetcher-dnode-" + className);
		if(file.exists()) {
			FileUtils.deleteDirectory(file);
		}
		for(int i = 0; i <= nInstancesi++) {
			file = new File("dnode-" + className + "-" + i);
			if(file.exists()) {
				FileUtils.deleteDirectory(file);
			}
			file = new File("zk-" + className + "-" + i);
			if(file.exists()) {
				FileUtils.deleteDirectory(file);
			}
			file = new File("fetcher-dnode-" + className + "-" + i);
			if(file.exists()) {
				FileUtils.deleteDirectory(file);
			}
		}
	}
New to GrepCode? Check out our FAQ X