<commit_before>/*
 *   Copyright 2009 <NAME>
 * 
 *   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
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package org.jredis.ri.alphazero;

import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.jredis.ClientRuntimeException;
import org.jredis.JRedisFuture;
import org.jredis.RedisException;
import org.jredis.protocol.Command;
import org.jredis.protocol.ResponseStatus;
import org.jredis.ri.JRedisTestSuiteBase;
import org.jredis.ri.alphazero.support.Log;
import org.testng.annotations.Test;

/**
 * Provides the comprehensive set of tests of all {@link JRedisFuture} methods.
 *
 * @author  <NAME> (<EMAIL>)
 * @version alpha.0, Oct 10, 2009
 * @since   alpha.0
 * 
 */

public abstract class JRedisFutureProviderTestsBase extends JRedisTestSuiteBase<JRedisFuture> {

	// ------------------------------------------------------------------------
	// Properties
	// ------------------------------------------------------------------------

	/** JRedis Command being tested -- for log info */
	private String cmd;
	
	// ------------------------------------------------------------------------
	// The Tests
	// ======================================================= JRedisFuture ===
	/**
	 * We define and run provider agnostic tests here.  This means we run a set
	 * of JRedisFuture interface method tests that every connected JRedisFuture
	 * implementation should be able to support. 
	 * 
	 * The following commands are omitted:
	 * 1 - QUIT: since we may be testing a multi-connection provier
	 * 2 - SHUTDOWN: for the same reason as QUIT 
	 * 3 - MOVE and SELECT
	 */
	// ------------------------------------------------------------------------

	/**
	 * Test settting a key in a flushed db, and then checking
	 * exists, flushing again, and finally keys().  
	 * Will invoke commands asynchronously, and after 
	 * @throws InterruptedException 
	 */
	@Test
	public void testSetAndFlushdbAndExistsAndKeys() throws InterruptedException {
		cmd = 
			Command.FLUSHDB.code + " | " +
			Command.SET.code + " | " +
			Command.EXISTS.code + " | " +
			Command.FLUSHDB.code + " | " +
			Command.KEYS.code;
			
		Log.log("TEST: %s commands", cmd);
		try {
			key = "woof";
			Future<ResponseStatus> flushResp = provider.flushdb();
			Future<ResponseStatus> setResp = provider.set(key, "meow");
			Future<Boolean> existsResp = provider.exists(key);
			Future<ResponseStatus> flush2Resp = provider.flushdb();
			Future<List<String>>  keysResp = provider.keys();
			<commit_msg>Source update only: beefing up tests for asynch (cont)<commit_after>/*
 *   Copyright 2009 <NAME>
 * 
 *   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
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package org.jredis.ri.alphazero;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.jredis.ClientRuntimeException;
import org.jredis.JRedisFuture;
import org.jredis.RedisException;
import org.jredis.protocol.Command;
import org.jredis.protocol.ResponseStatus;
import org.jredis.ri.JRedisTestSuiteBase;
import org.jredis.ri.alphazero.support.Log;
import org.testng.annotations.Test;

/**
 * Provides the comprehensive set of tests of all {@link JRedisFuture} methods.
 *
 * @author  <NAME> (<EMAIL>)
 * @version alpha.0, Oct 10, 2009
 * @since   alpha.0
 * 
 */

public abstract class JRedisFutureProviderTestsBase extends JRedisTestSuiteBase<JRedisFuture> {

	// ------------------------------------------------------------------------
	// Properties
	// ------------------------------------------------------------------------

	/** JRedis Command being tested -- for log info */
	private String cmd;
	
	// ------------------------------------------------------------------------
	// The Tests
	// ======================================================= JRedisFuture ===
	/**
	 * We define and run provider agnostic tests here.  This means we run a set
	 * of JRedisFuture interface method tests that every connected JRedisFuture
	 * implementation should be able to support. 
	 * 
	 * The following commands are omitted:
	 * 1 - QUIT: since we may be testing a multi-connection provier
	 * 2 - SHUTDOWN: for the same reason as QUIT 
	 * 3 - MOVE and SELECT
	 */
	// ------------------------------------------------------------------------

	/**
	 * Test method for {@link org.jredis.ri.alphazero.JRedisSupport#rename(java.lang.String, java.lang.String)}.
	 * @throws InterruptedException 
	 */
	@Test
	public void testRename() throws InterruptedException {
		cmd = Command.RENAME.code;
		Log.log("TEST: %s command", cmd);
		try {
			
			String newkey = null;
			byte[] value = dataList.get(0);
			key = getRandomAsciiString (random.nextInt(24)+2);
			newkey = getRandomAsciiString (random.nextInt(24)+2);
			
			Future<ResponseStatus> 	flushResp = provider.flushdb();
			Future<ResponseStatus> 	setResp = provider.set (key, value);
			Future<byte[]> 			getOldResp = provider.get(key);
			Future<ResponseStatus> 	reanmeResp = provider.rename (key, newkey);
			Future<byte[]> 			getNewResp = provider.get(newkey);
			
			try {
				flushResp.get();
				setResp.get();
				assertEquals(value, getOldResp.get());
				reanmeResp.get();
				assertEquals(value, getNewResp.get());
			}
			catch(ExecutionException e){
				// errors in response 
				Throwable cause = e.getCause();
				fail(cmd + " ERROR => " + cause.getLocalizedMessage(), e); 
			}
		} 
		catch (ClientRuntimeException e) {  fail(cmd + " Runtime ERROR => " + e.getLocalizedMessage(), e);  }
	}

	/**
	 * Test settting a key in a flushed db, and then checking
	 * exists, flushing again, and finally keys().  
	 * Will invoke commands asynchronously, and after 
	 * @throws InterruptedException 
	 */
	@Test
	public void testSetAndFlushdbAndExistsAndKeys() throws InterruptedException {
		cmd = 
			Command.FLUSHDB.code + " | " +
			Command.SET.code + " | " +
			Command.EXISTS.code + " | " +
			Command.FLUSHDB.code + " | " +
			Command.KEYS.code;
			
		Log.log("TEST: %s commands", cmd);
		try {
			key = "woof";
			Future<ResponseStatus> flushResp = provider.flushdb();
			Future<ResponseStatus> setResp = provider.set(key, "meow");
			Future<Boolean> existsResp = provider.exists(key);
			Future<ResponseStatus> flush2Resp = provider.flushdb();
			Future<List<String>>  keysResp = provider.keys();
			
{'fraction_non_alphanumeric': 0.18998682476943346, 'fraction_numerical': 0.00619235836627141, 'mean_word_length': 2.619933237958989, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4444813', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2105, 'n_words': 629}