Author: lhazlewood
Date: Wed Dec 7 20:38:53 2011
New Revision: 1211626
URL: http://svn.apache.org/viewvc?rev=1211626&view=rev
Log:
SHIRO-279: added tests to bring the org.apache.shiro.crypto.hash.format package
to 100% class and line coverage. Also added 'ProvidedHashFormat' enum to
represent all out-of-the-box HashFormat implementations.
Added:
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/ProvidedHashFormat.java
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/Base64FormatTest.groovy
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactoryTest.groovy
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/HexFormatTest.groovy
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ProvidedHashFormatTest.groovy
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ToStringHashFormat.groovy
Modified:
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactory.java
Modified:
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactory.java
URL:
http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactory.java?rev=1211626&r1=1211625&r2=1211626&view=diff
==============================================================================
---
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactory.java
(original)
+++
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactory.java
Wed Dec 7 20:38:53 2011
@@ -19,11 +19,8 @@
package org.apache.shiro.crypto.hash.format;
import org.apache.shiro.util.ClassUtils;
-import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.util.UnknownClassException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.HashSet;
@@ -33,38 +30,83 @@ import java.util.Set;
/**
* This default {@code HashFormatFactory} implementation heuristically
determines a {@code HashFormat} class to
* instantiate based on the input argument and returns a new instance of the
discovered class. The heuristics are
- * detailed in the {@link #getInstance(String) getInstance} method
documentation.j
+ * detailed in the {@link #getInstance(String) getInstance} method
documentation.
*
* @since 1.2
*/
public class DefaultHashFormatFactory implements HashFormatFactory {
- private static final Logger log =
LoggerFactory.getLogger(DefaultHashFormatFactory.class);
-
- private static final String DEFAULT_HASH_FORMAT_PACKAGE_NAME =
HashFormat.class.getPackage().getName();
-
private Map<String, String> formatClassNames; //id - to - fully qualified
class name
- private Set<String> searchPackages;
+ private Set<String> searchPackages; //packages to search for HashFormat
implementations
public DefaultHashFormatFactory() {
this.searchPackages = new HashSet<String>();
- formatClassNames = new HashMap<String, String>();
- formatClassNames.put(Shiro1CryptFormat.ID,
Shiro1CryptFormat.class.getName());
+ this.formatClassNames = new HashMap<String, String>();
}
+ /**
+ * Returns a {@code
hashFormatAlias}-to-<code>fullyQualifiedHashFormatClassNameImplementation</code>
map.
+ * <p/>
+ * This map will be used by the {@link #getInstance(String) getInstance}
implementation: that method's argument
+ * will be used as a lookup key to this map. If the map returns a value,
that value will be used to instantiate
+ * and return a new {@code HashFormat} instance.
+ * <h3>Defaults</h3>
+ * Shiro's default HashFormat implementations (as listed by the {@link
ProvidedHashFormat} enum) will
+ * be searched automatically independently of this map. You only need to
populate this map with custom
+ * {@code HashFormat} implementations that are <em>not</em> already
represented by a {@code ProvidedHashFormat}.
+ * <h3>Efficiency</h3>
+ * Populating this map will be more efficient than configuring {@link
#getSearchPackages() searchPackages},
+ * but search packages may be more convenient depending on the number of
{@code HashFormat} implementations that
+ * need to be supported by this factory.
+ *
+ * @return a {@code
hashFormatAlias}-to-<code>fullyQualifiedHashFormatClassNameImplementation</code>
map.
+ */
public Map<String, String> getFormatClassNames() {
return formatClassNames;
}
+ /**
+ * Sets the {@code hash-format-alias}-to-{@code
fullyQualifiedHashFormatClassNameImplementation} map to be used in
+ * the {@link #getInstance(String)} implementation. See the {@link
#getFormatClassNames()} JavaDoc for more
+ * information.
+ * <h3>Efficiency</h3>
+ * Populating this map will be more efficient than configuring {@link
#getSearchPackages() searchPackages},
+ * but search packages may be more convenient depending on the number of
{@code HashFormat} implementations that
+ * need to be supported by this factory.
+ *
+ * @param formatClassNames the {@code hash-format-alias}-to-{@code
fullyQualifiedHashFormatClassNameImplementation}
+ * map to be used in the {@link
#getInstance(String)} implementation.
+ */
public void setFormatClassNames(Map<String, String> formatClassNames) {
this.formatClassNames = formatClassNames;
}
+ /**
+ * Returns a set of package names that can be searched for {@link
HashFormat} implementations according to
+ * heuristics defined in the {@link #getHashFormatClass(String, String)
getHashFormat(packageName, token)} JavaDoc.
+ * <h3>Efficiency</h3>
+ * Configuring this property is not as efficient as configuring a {@link
#getFormatClassNames() formatClassNames}
+ * map, but it may be more convenient depending on the number of {@code
HashFormat} implementations that
+ * need to be supported by this factory.
+ *
+ * @return a set of package names that can be searched for {@link
HashFormat} implementations
+ * @see #getHashFormatClass(String, String)
+ */
public Set<String> getSearchPackages() {
return searchPackages;
}
+ /**
+ * Sets a set of package names that can be searched for {@link HashFormat}
implementations according to
+ * heuristics defined in the {@link #getHashFormatClass(String, String)
getHashFormat(packageName, token)} JavaDoc.
+ * <h3>Efficiency</h3>
+ * Configuring this property is not as efficient as configuring a {@link
#getFormatClassNames() formatClassNames}
+ * map, but it may be more convenient depending on the number of {@code
HashFormat} implementations that
+ * need to be supported by this factory.
+ *
+ * @param searchPackages a set of package names that can be searched for
{@link HashFormat} implementations
+ */
public void setSearchPackages(Set<String> searchPackages) {
this.searchPackages = searchPackages;
}
@@ -75,13 +117,14 @@ public class DefaultHashFormatFactory im
}
HashFormat hashFormat = null;
+ Class clazz = null;
- Class clazz = getHashFormatClass(in);
-
- //The 'in' argument didn't result in a corresponding HashFormat class
using our heuristics.
- //As a fallback, check to see if the argument is an MCF-formatted
string. If it is, odds are very high
- //that the MCF ID id is the lookup token we can use to find a
corresponding HashFormat class:
- if (clazz == null &&
in.startsWith(ModularCryptFormat.TOKEN_DELIMITER)) {
+ //NOTE: this code block occurs BEFORE calling getHashFormatClass(in)
on purpose as a performance
+ //optimization. If the input arg is an MCF-formatted string, there
will be many unnecessary ClassLoader
+ //misses which can be slow. By checking the MCF-formatted option, we
can significantly improve performance
+ if (in.startsWith(ModularCryptFormat.TOKEN_DELIMITER)) {
+ //odds are high that the input argument is not a fully qualified
class name or a format key (e.g. 'hex',
+ //base64' or 'shiro1'). Try to find the key and lookup via that:
String test =
in.substring(ModularCryptFormat.TOKEN_DELIMITER.length());
String[] tokens = test.split("\\" +
ModularCryptFormat.TOKEN_DELIMITER);
//the MCF ID is always the first token in the delimited string:
@@ -92,28 +135,14 @@ public class DefaultHashFormatFactory im
}
}
+ if (clazz == null) {
+ //not an MCF-formatted string - use the unaltered input arg and go
through our heuristics:
+ clazz = getHashFormatClass(in);
+ }
+
if (clazz != null) {
//we found a HashFormat class - instantiate it:
hashFormat = newHashFormatInstance(clazz);
-
- //do further compatibility testing if we can:
- if (hashFormat instanceof ParsableHashFormat) {
- //This is not really an efficient way to test for format
compatibility, but
- //there is no other way that guarantees compatibility that I
can think of at the moment.
- //perhaps an isCompatible method can be introduced? The
struggle I have with this is how do you
- //determine compatibility without parsing it fully? If not
fully parsed, then it truly can't be
- //guaranteed compatible - at which point, you might as well
just parse the thing - L.H. 22 Nov 2011
- try {
- ParsableHashFormat phf = (ParsableHashFormat)hashFormat;
- phf.parse(in);
- // no exception - must be a match:
- return phf;
- } catch (RuntimeException re) {
- log.debug("Candidate format instance of type [{}] is
unable to " +
- "parse formatted String [{}]. Ignoring.", clazz,
in);
- log.trace("HashFormat parsing caused exception: ", re);
- }
- }
}
return hashFormat;
@@ -123,40 +152,63 @@ public class DefaultHashFormatFactory im
* Heuristically determine the fully qualified HashFormat implementation
class name based on the specified
* token.
* <p/>
- * This implementation functions as follows:
+ * This implementation functions as follows (in order):
+ * <ol>
+ * <li>See if the argument can be used as a lookup key in the {@link
#getFormatClassNames() formatClassNames}
+ * map. If a value (a fully qualified class name {@link HashFormat
HashFormat} implementation) is found,
+ * {@link ClassUtils#forName(String) lookup} the class and return it.</li>
+ * <li>
+ * Check to see if the token argument is a
+ * {@link ProvidedHashFormat} enum value. If so, acquire the
corresponding {@code HashFormat} class and
+ * return it.
+ * </li>
+ * <li>
+ * Check to see if the token argument is itself a fully qualified class
name. If so, try to load the class
+ * and return it.
+ * </li>
+ * <li>If the above options do not result in a discovered class, search
all all configured
+ * {@link #getSearchPackages() searchPackages} using heuristics defined in
the
+ * {@link #getHashFormatClass(String, String)
getHashFormatClass(packageName, token)} method documentation
+ * (relaying the {@code token} argument to that method for each configured
package).
+ * </li>
+ * </ol>
* <p/>
- * All configured {@link #getSearchPackages() searchPackages} will be
searched using heuristics defined in the
- * {@link #getHashFormatClass(String, String)
getHashFormatClass(packageName, token)} method documentation (relaying
- * the {@code token} argument to that method for each configured package).
- * <p/>
- * If the class was not found in any configured {@code searchPackages},
the default
- * {@code org.apache.shiro.crypto.hash.format} package will be attempted
as a final fallback.
- * </p>
- * If the class was not discovered in any of the {@code searchPackages} or
in Shiro's default fallback package,
- * {@code null} is returned to indicate the class could not be found.
+ * If a class is not discovered via any of the above means, {@code null}
is returned to indicate the class
+ * could not be found.
*
* @param token the string token from which a class name will be
heuristically determined.
* @return the discovered HashFormat class implementation or {@code null}
if no class could be heuristically determined.
*/
protected Class getHashFormatClass(String token) {
- //check to see if the token is a fully qualified class name:
- Class clazz = lookupHashFormatClass(token);
+ Class clazz = null;
+ //check to see if the token is a configured FQCN alias. This is
faster than searching packages,
+ //so we try this first:
+ if (this.formatClassNames != null) {
+ String value = this.formatClassNames.get(token);
+ if (value != null) {
+ //found an alias - see if the value is a class:
+ clazz = lookupHashFormatClass(value);
+ }
+ }
+
+ //check to see if the token is one of Shiro's provided FQCN aliases
(again, faster than searching):
if (clazz == null) {
- //check to see if the token is a FQCN alias:
- if (!CollectionUtils.isEmpty(this.formatClassNames)) {
- String value = this.formatClassNames.get(token);
- if (value != null) {
- //found an alias - see if the value is a class:
- clazz = lookupHashFormatClass(token);
- }
+ ProvidedHashFormat provided = ProvidedHashFormat.byId(token);
+ if (provided != null) {
+ clazz = provided.getHashFormatClass();
}
}
if (clazz == null) {
+ //check to see if 'token' was a FQCN itself:
+ clazz = lookupHashFormatClass(token);
+ }
+
+ if (clazz == null) {
//token wasn't a FQCN or a FQCN alias - try searching in
configured packages:
- if (!CollectionUtils.isEmpty(this.searchPackages)) {
+ if (this.searchPackages != null) {
for (String packageName : this.searchPackages) {
clazz = getHashFormatClass(packageName, token);
if (clazz != null) {
@@ -167,11 +219,6 @@ public class DefaultHashFormatFactory im
}
}
- if (clazz == null) {
- //couldn't find it in any configured search packages. Try Shiro's
default search package:
- clazz = getHashFormatClass(DEFAULT_HASH_FORMAT_PACKAGE_NAME,
token);
- }
-
if (clazz != null) {
assertHashFormatImpl(clazz);
}
@@ -302,6 +349,6 @@ public class DefaultHashFormatFactory im
protected final HashFormat newHashFormatInstance(Class clazz) {
assertHashFormatImpl(clazz);
- return (HashFormat)ClassUtils.newInstance(clazz);
+ return (HashFormat) ClassUtils.newInstance(clazz);
}
}
Added:
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/ProvidedHashFormat.java
URL:
http://svn.apache.org/viewvc/shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/ProvidedHashFormat.java?rev=1211626&view=auto
==============================================================================
---
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/ProvidedHashFormat.java
(added)
+++
shiro/trunk/core/src/main/java/org/apache/shiro/crypto/hash/format/ProvidedHashFormat.java
Wed Dec 7 20:38:53 2011
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shiro.crypto.hash.format;
+
+/**
+ * An enum representing Shiro's default provided {@link HashFormat}
implementations.
+ */
+public enum ProvidedHashFormat {
+
+ /**
+ * Value representing the {@link HexFormat} implementation.
+ */
+ HEX(HexFormat.class),
+
+ /**
+ * Value representing the {@link Base64Format} implementation.
+ */
+ BASE64(Base64Format.class),
+
+ /**
+ * Value representing the {@link Shiro1CryptFormat} implementation.
+ */
+ SHIRO1(Shiro1CryptFormat.class);
+
+ private final Class<? extends HashFormat> clazz;
+
+ private ProvidedHashFormat(Class<? extends HashFormat> clazz) {
+ this.clazz = clazz;
+ }
+
+ Class<? extends HashFormat> getHashFormatClass() {
+ return this.clazz;
+ }
+
+ public static ProvidedHashFormat byId(String id) {
+ if (id == null) {
+ return null;
+ }
+ try {
+ return valueOf(id.toUpperCase());
+ } catch (IllegalArgumentException ignored) {
+ return null;
+ }
+ }
+
+}
Added:
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/Base64FormatTest.groovy
URL:
http://svn.apache.org/viewvc/shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/Base64FormatTest.groovy?rev=1211626&view=auto
==============================================================================
---
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/Base64FormatTest.groovy
(added)
+++
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/Base64FormatTest.groovy
Wed Dec 7 20:38:53 2011
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shiro.crypto.hash.format
+
+import org.apache.shiro.crypto.hash.Hash
+import org.apache.shiro.crypto.hash.Sha1Hash
+
+/**
+ * Unit tests for the {@link Base64Format} implementation.
+ *
+ * @since 1.2
+ */
+class Base64FormatTest extends GroovyTestCase {
+
+ void testFormat() {
+ Hash hash = new Sha1Hash("hello");
+ Base64Format format = new Base64Format()
+ String base64 = format.format(hash)
+ assertEquals base64, hash.toBase64()
+ }
+
+ void testFormatWithNullArgument() {
+ Base64Format format = new Base64Format()
+ assertNull format.format(null)
+ }
+
+}
Added:
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactoryTest.groovy
URL:
http://svn.apache.org/viewvc/shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactoryTest.groovy?rev=1211626&view=auto
==============================================================================
---
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactoryTest.groovy
(added)
+++
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/DefaultHashFormatFactoryTest.groovy
Wed Dec 7 20:38:53 2011
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shiro.crypto.hash.format
+
+import org.apache.shiro.crypto.hash.Sha1Hash
+
+/**
+ * Unit tests for the {@link DefaultHashFormatFactory} implementation.
+ *
+ * @since 1.2
+ */
+class DefaultHashFormatFactoryTest extends GroovyTestCase {
+
+ void testDefaultInstance() {
+ def factory = new DefaultHashFormatFactory()
+ assertNotNull factory.formatClassNames
+ assertTrue factory.formatClassNames.isEmpty()
+ assertNotNull factory.searchPackages
+ assertTrue factory.formatClassNames.isEmpty()
+ }
+
+ void testNullArg() {
+ def factory = new DefaultHashFormatFactory()
+ assertNull factory.getInstance(null)
+ }
+
+ void testNotFound() {
+ def factory = new DefaultHashFormatFactory()
+ assertNull factory.getInstance('foo')
+ }
+
+ void testSetFormatClassNames() {
+ def classNames = ['hex': HexFormat.class.name]
+ def factory = new DefaultHashFormatFactory()
+ factory.formatClassNames = classNames
+ assertNotNull factory.formatClassNames
+ assertEquals 1, factory.formatClassNames.size()
+ assertEquals factory.formatClassNames['hex'], HexFormat.class.name
+ }
+
+ void testGetInstanceWithConfiguredFormatClassName() {
+ def classNames = ['anAlias': HexFormat.class.name]
+ def factory = new DefaultHashFormatFactory(formatClassNames:
classNames)
+ def instance = factory.getInstance('anAlias')
+ assertNotNull instance
+ assertTrue instance instanceof HexFormat
+ }
+
+ void testGetInstanceWithMcfFormattedString() {
+ Shiro1CryptFormat format = new Shiro1CryptFormat()
+ def formatted = format.format(new Sha1Hash("test"))
+
+ def factory = new DefaultHashFormatFactory()
+
+ def instance = factory.getInstance(formatted)
+
+ assertNotNull instance
+ assertTrue instance instanceof Shiro1CryptFormat
+ }
+
+ void testAbsentFQCN() {
+ def factory = new DefaultHashFormatFactory()
+ def instance =
factory.getInstance("com.foo.bar.some.random.MyHashFormat")
+ assertNull instance
+ }
+
+ void testPresentFQCN() {
+ def factory = new DefaultHashFormatFactory()
+ def instance = factory.getInstance(Shiro1CryptFormat.class.name)
+ assertNotNull instance
+ assertTrue instance instanceof Shiro1CryptFormat
+ }
+
+ void testMcfFormattedArgument() {
+ def factory = new DefaultHashFormatFactory()
+
+ def hash = new Sha1Hash("test")
+ def formatted = new Shiro1CryptFormat().format(hash)
+
+ def instance = factory.getInstance(formatted)
+
+ assertNotNull instance
+ assertTrue instance instanceof Shiro1CryptFormat
+ }
+
+ void testSearchPackages() {
+ def factory = new DefaultHashFormatFactory()
+ factory.searchPackages = ['org.apache.shiro.crypto.hash.format']
+
+ //find the test class 'ToStringHashFormat'
+ def instance = factory.getInstance('toString')
+
+ assertNotNull instance
+ assertTrue instance instanceof ToStringHashFormat
+ }
+
+ void testSearchPackagesWithoutMatch() {
+ def factory = new DefaultHashFormatFactory()
+ factory.searchPackages = ['com.foo']
+
+ assertNull factory.getInstance('bar')
+ }
+
+ void testWithInvalidHashFormatImplementation() {
+ def factory = new DefaultHashFormatFactory()
+ try {
+ factory.getInstance("java.lang.Integer")
+ fail "Call should have resulted in an IllegalArgumentException"
+ } catch (IllegalArgumentException expected) {
+ }
+
+ }
+}
Added:
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/HexFormatTest.groovy
URL:
http://svn.apache.org/viewvc/shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/HexFormatTest.groovy?rev=1211626&view=auto
==============================================================================
---
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/HexFormatTest.groovy
(added)
+++
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/HexFormatTest.groovy
Wed Dec 7 20:38:53 2011
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shiro.crypto.hash.format
+
+import org.apache.shiro.crypto.hash.Hash
+import org.apache.shiro.crypto.hash.Sha1Hash
+
+/**
+ * Unit tests for the {@link HexFormat} implementation.
+ *
+ * @since 1.2
+ */
+class HexFormatTest extends GroovyTestCase {
+
+ void testFormat() {
+ Hash hash = new Sha1Hash("hello");
+ HexFormat format = new HexFormat()
+ String hex = format.format(hash)
+ assertEquals hex, hash.toHex()
+ }
+
+ void testFormatWithNullArgument() {
+ HexFormat format = new HexFormat()
+ assertNull format.format(null)
+ }
+
+}
+
+
Added:
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ProvidedHashFormatTest.groovy
URL:
http://svn.apache.org/viewvc/shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ProvidedHashFormatTest.groovy?rev=1211626&view=auto
==============================================================================
---
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ProvidedHashFormatTest.groovy
(added)
+++
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ProvidedHashFormatTest.groovy
Wed Dec 7 20:38:53 2011
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shiro.crypto.hash.format
+
+/**
+ * Unit tests for the {@link ProvidedHashFormat} implementation.
+ */
+class ProvidedHashFormatTest extends GroovyTestCase {
+
+ void testDefaults() {
+ def set = ProvidedHashFormat.values() as Set
+ assertEquals 3, set.size()
+ assertTrue set.contains(ProvidedHashFormat.HEX)
+ assertTrue set.contains(ProvidedHashFormat.BASE64)
+ assertTrue set.contains(ProvidedHashFormat.SHIRO1)
+ }
+
+ void testByIdWithNullArg() {
+ assertNull ProvidedHashFormat.byId(null)
+ }
+
+}
Added:
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ToStringHashFormat.groovy
URL:
http://svn.apache.org/viewvc/shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ToStringHashFormat.groovy?rev=1211626&view=auto
==============================================================================
---
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ToStringHashFormat.groovy
(added)
+++
shiro/trunk/core/src/test/groovy/org/apache/shiro/crypto/hash/format/ToStringHashFormat.groovy
Wed Dec 7 20:38:53 2011
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.shiro.crypto.hash.format
+
+import org.apache.shiro.crypto.hash.Hash
+
+/**
+ * Simple {@code HashFormat} for testing that merely returns {@code
hash.toString()}.
+ *
+ * @since 1.2
+ */
+class ToStringHashFormat implements HashFormat {
+
+ String format(Hash hash) {
+ return hash.toString()
+ }
+}