// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.document; import com.yahoo.document.idstring.IdIdString; import com.yahoo.document.idstring.IdString; import com.yahoo.vespa.objects.BufferSerializer; import org.junit.Before; import org.junit.Test; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.regex.Pattern; import java.util.Arrays; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; public class DocumentIdTestCase { DocumentTypeManager manager = new DocumentTypeManager(); @Before public void setUp() { DocumentType testDocType = new DocumentType("testdoc"); testDocType.addField("intattr", DataType.INT); testDocType.addField("rawattr", DataType.RAW); testDocType.addField("floatattr", DataType.FLOAT); testDocType.addField("stringattr", DataType.STRING); testDocType.addField("Minattr", DataType.INT); manager.registerDocumentType(testDocType); } @Test public void testCompareTo() { DocumentId docId1 = new Document(manager.getDocumentType("testdoc"), new DocumentId("id:ns:testdoc::http://www.uio.no/")).getId(); DocumentId docId2 = new Document(manager.getDocumentType("testdoc"), new DocumentId("id:ns:testdoc::http://www.uio.no/")).getId(); DocumentId docId3 = new Document(manager.getDocumentType("testdoc"), new DocumentId("id:ns:testdoc::http://www.ntnu.no/")).getId(); assertTrue(docId1.equals(docId2)); assertTrue(!docId1.equals(docId3)); assertTrue(docId1.compareTo(docId3) > 0); assertTrue(docId3.compareTo(docId1) < 0); assertEquals(docId1.hashCode(), docId2.hashCode()); } private void checkInvalidUri(String uri) { try { //invalid URI new DocumentId(uri); fail(); } catch (IllegalArgumentException iae) { } } @Test public void testValidInvalidUriSchemes() { try { //valid URIs new DocumentId("id:namespace:type:n=42:whatever"); new DocumentId("id:namespace:type::whatever"); } catch (IllegalArgumentException iae) { fail(iae.getMessage()); } checkInvalidUri("foobar:"); checkInvalidUri("ballooo:blabla/something/"); checkInvalidUri("id:namespace:type"); checkInvalidUri("id:namespace:type:key-values"); checkInvalidUri("id:namespace:type:n=0,n=1:foo"); checkInvalidUri("id:namespace:type:g=foo,g=bar:foo"); checkInvalidUri("id:namespace:type:n=0,g=foo:foo"); } @Test public void empty_user_location_value_throws_exception() { try { new DocumentId("id:namespace:type:n=:foo"); fail(); } catch (IllegalArgumentException e) { assertEquals("ID location value for 'n=' key is empty", e.getMessage()); } } @Test public void empty_group_location_value_throws_exception() { try { new DocumentId("id:namespace:type:g=:foo"); fail(); } catch (IllegalArgumentException e) { assertEquals("ID location value for 'g=' key is empty", e.getMessage()); } } //Compares globalId with C++ implementation located in // ~document-HEAD/document/src/tests/cpp-globalidbucketids.txt @Test public void testCalculateGlobalId() throws IOException { String file = "src/tests/cpp-globalidbucketids.txt"; BufferedReader fr = new BufferedReader(new FileReader(file)); String line; String[] split_line; String[] split_gid; byte[] b; // reads from file while ((line = fr.readLine()) != null) { split_line = line.split(" - "); DocumentId mydoc = new DocumentId(split_line[0]); b = mydoc.getGlobalId(); split_gid = Pattern.compile("\\(|\\)").split(split_line[1]); compareStringByte(split_gid[1],b); } fr.close(); } private void compareStringByte(String s, byte[] b){ /* System.out.println("-- "+s+" --"); System.out.print("++ 0x"); for (int i=0; i