Пример #1
0
        public void testToString()
        {
            DateTime CALENDAR = DateTime.Now;

            // we'll test that a few expected constructed TagURI's create the
            // expected strings

            // first, some tests with a null calendar
            for (int i = 0; i < 4; ++i)
            {
                for (int j = 0; j < 4; ++j)
                {
                    TagURI tag_uri  = new TagURI(AUTHORITIES[i], IDS[j], null);
                    String expected = "tag:" + AUTHORITIES[i] + ":" + IDS[j];
                    Assert.AreEqual(
                        expected,
                        tag_uri.ToString(), "Expected string did not match generated ToString()");
                }
            }

            // now some cases with date
            for (int i = 0; i < 4; ++i)
            {
                CALENDAR = new DateTime(2006, 6, 4);

                for (int j = 0; j < 4; ++j)
                {
                    TagURI tag_uri  = new TagURI(AUTHORITIES[i], IDS[j], CALENDAR);
                    String expected = "tag:" + AUTHORITIES[i] + "," +
                                      CALENDAR.Year + "-" +
                                      CALENDAR.Month + "-" +
                                      CALENDAR.Day + ":" + IDS[j];
                    Assert.AreEqual(
                        expected,
                        tag_uri.ToString(),
                        "Expected string did not match generated ToString()"
                        );
                }
            }

            // now some cases with date such that day is left out
            // (first of the month)
            for (int i = 0; i < 4; ++i)
            {
                CALENDAR = new DateTime(2006, 6, 1);


                for (int j = 0; j < 4; ++j)
                {
                    TagURI tag_uri  = new TagURI(AUTHORITIES[i], IDS[j], CALENDAR);
                    String expected = "tag:" + AUTHORITIES[i] + "," +
                                      CALENDAR.Year + "-" +
                                      CALENDAR.Month + ":" + IDS[j];
                    Assert.AreEqual(
                        expected,
                        tag_uri.ToString(),
                        "Expected string did not match generated ToString()"
                        );
                }
            }

            // now some cases with date such that day and month are left out
            // (jan-1)
            for (int i = 0; i < 4; ++i)
            {
                CALENDAR = new DateTime(2006, 1, 1);

                for (int j = 0; j < 4; ++j)
                {
                    TagURI tag_uri  = new TagURI(AUTHORITIES[i], IDS[j], CALENDAR);
                    String expected = "tag:" + AUTHORITIES[i] + "," +
                                      CALENDAR.Year + ":" + IDS[j];
                    Assert.AreEqual(
                        expected,
                        tag_uri.ToString(),
                        "Expected string did not match generated ToString()"
                        );
                }
            }
        }
Пример #2
0
        public void testGenerateTagURIBasedUUIDWithMessageDigest()
        {
            String TEST_AUTHORITY = "www.safehaus.org";
            MD5    MESSAGE_DIGEST = MD5.Create();

            // this test will attempt to check for reasonable behavior of the
            // generateTagURIBasedUUID method

            // we need a instance to use
            UUIDGenerator uuid_gen = UUIDGenerator.Current;

            // first, check that a null passed in causes the appropriate exception
            try
            {
                UUID uuid = uuid_gen.GenerateTagURIBasedUUID(null, MESSAGE_DIGEST);
                fail("Expected exception not thrown");
            }
            catch (NullReferenceException)
            {
                // expected exception caught, do nothing
            }
            catch (Exception ex)
            {
                fail("unexpected exception caught: " + ex);
            }

            try
            {
                TagURI test_tag =
                    new TagURI(TEST_AUTHORITY, "test id", DateTime.Now);
                UUID uuid = uuid_gen.GenerateTagURIBasedUUID(test_tag, null);
                fail("Expected exception not thrown");
            }
            catch (NullReferenceException)
            {
                // expected exception caught, do nothing
            }
            catch (Exception ex)
            {
                fail("unexpected exception caught: " + ex);
            }

            try
            {
                UUID uuid = uuid_gen.GenerateTagURIBasedUUID(null, null);
                fail("Expected exception not thrown");
            }
            catch (NullReferenceException)
            {
                // expected exception caught, do nothing
            }
            catch (Exception ex)
            {
                fail("unexpected exception caught: " + ex);
            }

            UUID[] uuid_array = new UUID[SIZE_OF_TEST_ARRAY];

            // now create the array of uuids
            for (int i = 0; i < uuid_array.Length; i++)
            {
                TagURI test_tag =
                    new TagURI(TEST_AUTHORITY, "test id" + i,
                               DateTime.Now);
                uuid_array[i] =
                    uuid_gen.GenerateTagURIBasedUUID(test_tag, MESSAGE_DIGEST);
            }

            // check that none of the UUIDs are null
            checkUUIDArrayForNonNullUUIDs(uuid_array);

            // check that all the uuids were correct variant and version
            checkUUIDArrayForCorrectVariantAndVersion(
                uuid_array, UUID.TYPE_NAME_BASED);

            // check that all uuids were unique
            checkUUIDArrayForUniqueness(uuid_array);

            // now, lets make sure generating two sets of tag based uuid with the
            // same args always gives the same result
            uuid_array = new UUID[SIZE_OF_TEST_ARRAY];
            UUID[] uuid_array2 = new UUID[SIZE_OF_TEST_ARRAY];

            // now create the array of uuids
            for (int i = 0; i < uuid_array.Length; i++)
            {
                TagURI test_tag =
                    new TagURI(TEST_AUTHORITY, "test id" + i,
                               DateTime.Now);
                uuid_array[i] =
                    uuid_gen.GenerateTagURIBasedUUID(test_tag, MESSAGE_DIGEST);
                uuid_array2[i] =
                    uuid_gen.GenerateTagURIBasedUUID(test_tag, MESSAGE_DIGEST);
            }

            // check that none of the UUIDs are null
            checkUUIDArrayForNonNullUUIDs(uuid_array);
            checkUUIDArrayForNonNullUUIDs(uuid_array2);

            // check that all the uuids were correct variant and version
            checkUUIDArrayForCorrectVariantAndVersion(
                uuid_array, UUID.TYPE_NAME_BASED);
            checkUUIDArrayForCorrectVariantAndVersion(
                uuid_array2, UUID.TYPE_NAME_BASED);

            // check that all uuids were unique
            checkUUIDArrayForUniqueness(uuid_array);
            checkUUIDArrayForUniqueness(uuid_array2);

            // check that both arrays are equal to one another
            assertEquals("expected both arrays to be equal, they were not!",
                         uuid_array, uuid_array2);
        }
Пример #3
0
        public void testEquals()
        {
            // test passing null to Equals returns false
            // (as specified in the JDK docs for Object)
            TagURI x = new TagURI(AUTHORITIES[1], IDS[2], null);

            assertFalse("Equals(null) didn't return false",
                        x.Equals((Object)null));

            // test that passing an object which is not a TagURI returns false
            assertFalse("x.Equals(non_TagURI_object) didn't return false",
                        x.Equals(new Object()));

            // test a case where two TagURIs are definitly not equal
            TagURI w = new TagURI(AUTHORITIES[2], IDS[0], DateTime.Now);

            assertFalse("x == w didn't return false",
                        x == w);
            assertFalse("x.Equals(w) didn't return false",
                        x.Equals(w));

            // test refelexivity
            assertTrue("x.Equals(x) didn't return true",
                       x.Equals(x));

            // test symmetry
            TagURI y = new TagURI(AUTHORITIES[1], IDS[2], null);

            assertFalse("x == y didn't return false",
                        x == y);
            assertTrue("y.Equals(x) didn't return true",
                       y.Equals(x));
            assertTrue("x.Equals(y) didn't return true",
                       x.Equals(y));

            // now we'll test transitivity
            TagURI z = new TagURI(AUTHORITIES[1], IDS[2], null);

            assertFalse("x == y didn't return false",
                        x == y);
            assertFalse("x == y didn't return false",
                        y == z);
            assertFalse("x == y didn't return false",
                        x == z);
            assertTrue("x.Equals(y) didn't return true",
                       x.Equals(y));
            assertTrue("y.Equals(z) didn't return true",
                       y.Equals(z));
            assertTrue("x.Equals(z) didn't return true",
                       x.Equals(z));

            // test consistancy (this test is just calling Equals multiple times)
            assertFalse("x == y didn't return false",
                        x == y);
            assertTrue("x.Equals(y) didn't return true",
                       x.Equals(y));
            assertTrue("x.Equals(y) didn't return true",
                       x.Equals(y));
            assertTrue("x.Equals(y) didn't return true",
                       x.Equals(y));
        }