Skip to content

Commit

Permalink
Really tested.
Browse files Browse the repository at this point in the history
wip.  Added convenience method for setting credentials.  Tested.
  • Loading branch information
Joubin Houshyar committed Sep 14, 2010
1 parent ce5b15c commit 5a7bdc4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,12 @@ final public ConnectionSpec setCredentials (byte[] credentials) {
/* (non-Javadoc) @see org.jredis.connector.ConnectionSpec#setCredentials(java.lang.String) */
// @Override
final public ConnectionSpec setCredentials (String credentials) {
return setCredentials(credentials.getBytes());
byte[] bytes;
if(credentials == null || credentials.length() == 0)
bytes = null;
else
bytes = credentials.getBytes();
return setCredentials(bytes);
}
/* (non-Javadoc) @see org.jredis.connector.ConnectionSpec#setDatabase(int) */
// @Override
Expand Down
18 changes: 13 additions & 5 deletions core/api/src/test/java/org/jredis/connector/TestSpecElements.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testConnectionFlags() {

bitset = Flag.bitclear(bitset, flags[1]);
assertFalse(bitset == oldbitset, "clearing flag should have changed bitset");
assertFalse(Flag.isSet(bitset, flags[1]), "%s should have been cleared!\n", flags[1].name());
assertFalse(Flag.isSet(bitset, flags[1]), "%s should have been cleared!\n", flags[1].name() );

int bitset2 = 0x0000;
bitset2 = Flag.bitset(bitset2, flags);
Expand All @@ -62,13 +62,14 @@ public void testCredentialsOverloads () {
String property = Connection.Property.CREDENTIAL.name();
log.info(String.format("TEST:CONNECTOR spec sematics - Credentials", property));

// check with actual passwords
//
String password = "jredis";
byte[] credentials = password.getBytes();

ConnectionSpec spec = new ConnectionSpec.RefImpl();
assertNull(spec.getCredentials(), "RefImpl should not have defined: %s", property);

String password = "jredis";

// use the byte[] variant
//
spec.setCredentials(password.getBytes());
byte[] credentials_1 = spec.getCredentials();
assertNotNull(credentials_1, "RefImpl.setCredentials (byte[]) did not set: %s", property);
Expand All @@ -81,5 +82,12 @@ public void testCredentialsOverloads () {

// compare them
assertEquals(credentials_2, credentials_1, String.format("Overloaded methods for %s setter are not equivalent.", property));

// check for treatment of empty strings -- should be equivalent to null byte[]
//
spec = new ConnectionSpec.RefImpl(); // ASSUMPTION: tested above for null default value.
password = "";
spec.setCredentials(password);
assertNull(spec.getCredentials(), "RefImpl should not have set \"\" (empty) credential to non-null value: %s", property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jredis.connector.ConnectionSpec;
import org.jredis.connector.Connection.Modality;
import org.jredis.ri.alphazero.support.Assert;
import org.jredis.ri.alphazero.support.Log;

/**
* Default connection spec provides the following default values for a connection. See
Expand All @@ -40,10 +41,16 @@
* @since alpha.0
*
*/
public class DefaultConnectionSpec extends ConnectionSpec.RefImpl {
final public class DefaultConnectionSpec extends ConnectionSpec.RefImpl {
// ------------------------------------------------------------------------
// Consts
// ------------------------------------------------------------------------

static final int DEFAULT_REDIS_PORT = 6379;
static final String DEFAULT_REDIS_HOST_NAME = "localhost";
static final int DEFAULT_REDIS_DB = 0;
static final byte[] DEFAULT_REDIS_PASSWORD = null;

/** defaults to 3 */
static final int DEFAULT_RECONNECT_CNT = 3;
/** defautls to 48KB */
Expand Down Expand Up @@ -86,6 +93,17 @@ public class DefaultConnectionSpec extends ConnectionSpec.RefImpl {
* @param port
* @throws ClientRuntimeException for invalid port, or null address values
*/
public DefaultConnectionSpec () throws ClientRuntimeException {
Log.debug("Yo!");
setDefaultProperties();
}
/**
* Instantiates a default connection spec for the given host and port.
* @param address
* @param port
* @throws ClientRuntimeException for invalid port, or null address values
*/
@Deprecated
public DefaultConnectionSpec (InetAddress address, int port) throws ClientRuntimeException {
this (address, port, 0, null);
}
Expand All @@ -97,13 +115,13 @@ public DefaultConnectionSpec (InetAddress address, int port) throws ClientRuntim
* @param credentials
* @throws ClientRuntimeException for invalid port, or null address values
*/
@Deprecated
public DefaultConnectionSpec (InetAddress address, int port, int database, byte[] credentials) throws ClientRuntimeException {
this();
setPort(Assert.inRange (port, 1, 65534, "port init parameter for DefaultConnectionSpec", ClientRuntimeException.class));
setAddress(Assert.notNull(address, "address init parameter for DefaultConnectionSpec", ClientRuntimeException.class));
setDatabase(database);
setCredentials(credentials);

setDefaultProperties();
}
/**
* Set the default values for the {@link SocketFlag}s and {@link SocketProperty}s and various
Expand Down Expand Up @@ -145,7 +163,7 @@ private void setDefaultProperties () {
public static final ConnectionSpec newSpec ()
throws ClientRuntimeException
{
return newSpec ("localhost", 6379, 0, null);
return newSpec (DEFAULT_REDIS_HOST_NAME, DEFAULT_REDIS_PORT, DEFAULT_REDIS_DB, DEFAULT_REDIS_PASSWORD);
}

/**
Expand Down Expand Up @@ -196,6 +214,9 @@ public static final ConnectionSpec newSpec (
)
throws ClientRuntimeException
{
return new DefaultConnectionSpec(address, port, database, credentials);
// return new DefaultConnectionSpec(address, port, database, credentials);
ConnectionSpec spec = new DefaultConnectionSpec();
return spec.setAddress(address).setPort(port).setDatabase(database).setCredentials(credentials);

}
}
8 changes: 6 additions & 2 deletions examples/src/main/java/org/jredis/examples/HelloAgain.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import org.jredis.JRedis;
import org.jredis.ProviderException;
import org.jredis.RedisException;
import org.jredis.connector.ConnectionSpec;
import org.jredis.protocol.Command;
import org.jredis.ri.alphazero.JRedisClient;
import org.jredis.ri.alphazero.connection.DefaultConnectionSpec;
import static org.jredis.ri.alphazero.support.DefaultCodec.*;

/**
Expand All @@ -36,14 +38,16 @@
public class HelloAgain {
public static final String key = "jredis::examples::HelloAgain::message";
public static void main(String[] args) {
String password = "";
String password = "jredis";
if(args.length > 0) password = args[0];
new HelloAgain().run(password);
}

private void run(String password) {
try {
JRedis jredis = new JRedisClient("localhost", 6379, "jredis", 0);
ConnectionSpec spec = DefaultConnectionSpec.newSpec().setCredentials(password);
// JRedis jredis = new JRedisClient("localhost", 6379, "jredis", 0);
JRedis jredis = new JRedisClient(spec);
jredis.ping();

if(!jredis.exists(key)) {
Expand Down

0 comments on commit 5a7bdc4

Please sign in to comment.