MyAttributeEncodingFactory { public static MyAttributeValue parseValueFromEncoding(URI encoding, Object attributeValue, EnumAllowedValues eav) { MyAttributeValue ret = parseValueFromEncodingNoCheck(encoding, attributeValue, eav); if(! encoding.equals(ret.getEncodingOrNull())) { throw new RuntimeException("Problem with encoding: " + encoding + "/" + ret.getEncodingOrNull()); } return ret; }  static MyAttributeValue parseValueFromEncoding(URI encoding, Object attributeValue, /*Nullable*/ EnumAllowedValues eav); static Map<String, BigInteger> getEncodingForEachAllowedValue(URI encoding, EnumAllowedValues eav); static MyAttributeValue recoverValueFromBigInteger(URI encoding, BigInteger attributeValue, /*Nullable*/ EnumAllowedValues eav); static URI getDatatypeFromEncoding(URI encoding); static BigInteger byteArrayToInteger(byte[] arr); static BigInteger byteArrayToIntegerWithLength(byte[] arr); static byte[] decodeByteArrayWithLength(BigInteger integerValue); static final BigInteger SIGNED_OFFSET; static final BigInteger MAX_VALUE; }
@Test public void testStringSha256() { final BigInteger expectedHex = new BigInteger(HASH_OF_TEST_STRING, 16); final BigInteger expectedDec = new BigInteger(HASH_OF_TEST_STRING_DECIMAL); assertEquals(expectedHex, expectedDec); MyAttributeValue v = MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_STRING_SHA, TEST_STRING, null); assertEquals(v.getIntegerValueOrNull(), expectedHex); }
@Test public void testStringDifferentEncodings() { final URI stringDatatype = URI.create("xs:string"); final BigInteger expectedUtf8 = new BigInteger("68C3A96C6CC3B807", 16); final BigInteger expectedSha = new BigInteger(HASH_OF_TEST_STRING, 16); List<MyAttributeValue> val = new ArrayList<MyAttributeValue>(); val.add(MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_STRING_UTF_8, TEST_STRING, null)); val.add(MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_STRING_SHA, TEST_STRING, null)); val.add(MyAttributeValueFactory.parseValue(stringDatatype, TEST_STRING, null)); for(MyAttributeValue v: val) { assertEquals(expectedUtf8, v.getIntegerValueUnderEncoding(ENCODING_STRING_UTF_8)); assertEquals(expectedSha, v.getIntegerValueUnderEncoding(ENCODING_STRING_SHA)); } }
@Test public void testGetEncodings() { final URI stringDatatype = URI.create("xs:string"); final BigInteger expectedUtf8 = new BigInteger("68C3A96C6CC3B807", 16); final BigInteger expectedSha = new BigInteger(HASH_OF_TEST_STRING, 16); MyAttributeValue v1 = MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_STRING_UTF_8, TEST_STRING, null); MyAttributeValue v2 = MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_STRING_SHA, TEST_STRING, null); MyAttributeValue v3 = MyAttributeValueFactory.parseValue(stringDatatype, TEST_STRING, null); Set<MyAttributeValue> set1 = new HashSet<MyAttributeValue>(); set1.add(v1); set1.add(v3); assertEquals(expectedUtf8, v3.getCompatibleIntegerValue(set1)); Set<MyAttributeValue> set2 = new HashSet<MyAttributeValue>(); set2.add(v2); set2.add(v3); assertEquals(expectedSha, v3.getCompatibleIntegerValue(set2)); Set<MyAttributeValue> set3 = new HashSet<MyAttributeValue>(); set3.add(v3); try { v3.getCompatibleIntegerValue(set3); fail(); } catch(RuntimeException e) { assertTrue(e.getMessage().contains("No encoding")); } Set<MyAttributeValue> set4 = new HashSet<MyAttributeValue>(); set4.add(v1); set4.add(v2); set4.add(v3); try { v3.getCompatibleIntegerValue(set4); fail(); } catch(RuntimeException e) { assertTrue(e.getMessage().contains("Too many encodings")); } assertEquals(v1.getEncodingOrNull(), ENCODING_STRING_UTF_8); assertEquals(v2.getEncodingOrNull(), ENCODING_STRING_SHA); assertEquals(v3.getEncodingOrNull(), null); }
@Test public void overlongString() { final String maxlengthString = "1234567890123456789012345678901"; final String overlongString = maxlengthString + "X"; MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_STRING_UTF_8, maxlengthString, null); try { MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_STRING_UTF_8, overlongString, null); fail("Expected an exception due to overlong string"); } catch(RuntimeException e) { assertTrue(e.getMessage().contains("too long")); } }
@Test public void overlongUri() { final URI maxlengthString = URI.create("1234567890123456789012345678901"); final URI overlongString = URI.create(maxlengthString + "X"); MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_URI_UTF_8, maxlengthString, null); try { MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_URI_UTF_8, overlongString, null); fail("Expected an exception due to overlong string"); } catch(RuntimeException e) { assertTrue(e.getMessage().contains("too long")); } }
@Test public void testUriSha256() { final BigInteger expectedHex = new BigInteger(HASH_OF_TEST_STRING, 16); final BigInteger expectedDec = new BigInteger(HASH_OF_TEST_STRING_DECIMAL); assertEquals(expectedHex, expectedDec); MyAttributeValue v = MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_URI_SHA, TEST_STRING_URI, null); assertEquals(v.getIntegerValueOrNull(), expectedHex); }
@Test public void testUriDifferentEncodings() { final URI uriDatatype = URI.create("xs:anyURI"); final BigInteger expectedUtf8 = new BigInteger("68C3A96C6CC3B807", 16); final BigInteger expectedSha = new BigInteger(HASH_OF_TEST_STRING, 16); List<MyAttributeValue> val = new ArrayList<MyAttributeValue>(); val.add(MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_URI_UTF_8, TEST_STRING_URI, null)); val.add(MyAttributeEncodingFactory.parseValueFromEncoding(ENCODING_URI_SHA, TEST_STRING_URI, null)); val.add(MyAttributeValueFactory.parseValue(uriDatatype, TEST_STRING_URI, null)); for(MyAttributeValue v: val) { assertEquals(expectedUtf8, v.getIntegerValueUnderEncoding(ENCODING_URI_UTF_8)); assertEquals(expectedSha, v.getIntegerValueUnderEncoding(ENCODING_URI_SHA)); } }