@Override public Object encode(final Object source) throws EncoderException { if (!(source instanceof String)) { throw new EncoderException("BeiderMorseEncoder encode parameter is not of type String"); } return encode((String) source); }
/** * Encode the value using DoubleMetaphone. It will only work if * <code>obj</code> is a <code>String</code> (like <code>Metaphone</code>). * * @param obj Object to encode (should be of type String) * @return An encoded Object (will be of type String) * @throws EncoderException encode parameter is not of type String */ @Override public Object encode(final Object obj) throws EncoderException { if (!(obj instanceof String)) { throw new EncoderException("DoubleMetaphone encode parameter is not of type String"); } return doubleMetaphone((String) obj); }
/** * Converts an array of raw binary data into an array of ASCII 0 and 1 chars. * * @param raw * the raw binary data to convert * @return 0 and 1 ASCII character chars one for each bit of the argument * @throws EncoderException * if the argument is not a byte[] * @see org.apache.commons.codec.Encoder#encode(Object) */ @Override public Object encode(final Object raw) throws EncoderException { if (!(raw instanceof byte[])) { throw new EncoderException("argument not a byte array"); } return toAsciiChars((byte[]) raw); }
@Test public void testConstructorStringThrowable() { final DecoderException e = new DecoderException(MSG, t); assertEquals(MSG, e.getMessage()); assertEquals(t, e.getCause()); }
@Test public void testConstructorString() { final EncoderException e = new EncoderException(MSG); assertEquals(MSG, e.getMessage()); assertNull(e.getCause()); }
/** * Encodes an Object using the Base-N algorithm. This method is provided in order to satisfy the requirements of * the Encoder interface, and will throw an EncoderException if the supplied object is not of type byte[]. * * @param obj * Object to encode * @return An object (of type byte[]) containing the Base-N encoded data which corresponds to the byte[] supplied. * @throws EncoderException * if the parameter supplied is not of type byte[] */ @Override public Object encode(final Object obj) throws EncoderException { if (!(obj instanceof byte[])) { throw new EncoderException("Parameter supplied to Base-N encode is not a byte[]"); } return encode((byte[]) obj); }
@Test public void testEncodeNull() throws Exception { final StringEncoder encoder = this.getStringEncoder(); try { encoder.encode(null); } catch (final EncoderException ee) { // An exception should be thrown } }
/** * Returns the numeric value of the character <code>b</code> in radix 16. * * @param b * The byte to be converted. * @return The numeric value represented by the character in radix 16. * * @throws DecoderException * Thrown when the byte is not valid per {@link Character#digit(char,int)} */ static int digit16(final byte b) throws DecoderException { final int i = Character.digit((char) b, RADIX); if (i == -1) { throw new DecoderException("Invalid URL encoding: not a valid digit (radix " + RADIX + "): " + b); } return i; }
@Test public void testEncodeEmpty() throws Exception { final BinaryEncoder encoder = makeEncoder(); encoder.encode(new byte[0]); }
@Test public void testEncodeEmpty() throws Exception { final Encoder encoder = this.getStringEncoder(); encoder.encode(""); encoder.encode(" "); encoder.encode("\t"); }
protected void checkEncodingVariations(final String expected, final String data[]) throws EncoderException { for (final String element : data) { this.checkEncoding(expected, element); } }
/** * We could make the constructor private in the future, it's a matter a style. */ @Test public void testConstructor() { new CharEncoding(); }
@Test public void testConstructorString() { final DecoderException e = new DecoderException(MSG); assertEquals(MSG, e.getMessage()); assertNull(e.getCause()); }
@Test public void testConstructor0() { final EncoderException e = new EncoderException(); assertNull(e.getMessage()); assertNull(e.getCause()); }
public void checkEncoding(final String expected, final String source) throws EncoderException { Assert.assertEquals("Source: " + source, expected, this.getStringEncoder().encode(source)); }
@Test public void testEncodeNull() throws Exception { final BinaryEncoder encoder = makeEncoder(); try { encoder.encode(null); } catch (final EncoderException ee) { // An exception should be thrown } } }
protected void checkEncodings(final String[][] data) throws EncoderException { for (final String[] element : data) { this.checkEncoding(element[1], element[0]); } }
@Test public void testConstructor0() { final DecoderException e = new DecoderException(); assertNull(e.getMessage()); assertNull(e.getCause()); }
@Test public void testConstructorStringThrowable() { final EncoderException e = new EncoderException(MSG, t); assertEquals(MSG, e.getMessage()); assertEquals(t, e.getCause()); }
@Test public void testEncodeWithInvalidObject() throws Exception { boolean exceptionThrown = false; try { final StringEncoder encoder = this.getStringEncoder(); encoder.encode(new Float(3.4)); } catch (final Exception e) { exceptionThrown = true; } Assert.assertTrue("An exception was not thrown when we tried to encode " + "a Float object", exceptionThrown); }