001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.query.algebra.evaluation.util;
007:
008: import java.util.Arrays;
009: import java.util.Collections;
010: import java.util.List;
011:
012: import junit.framework.TestCase;
013:
014: import org.openrdf.model.BNode;
015: import org.openrdf.model.Literal;
016: import org.openrdf.model.URI;
017: import org.openrdf.model.ValueFactory;
018: import org.openrdf.model.impl.ValueFactoryImpl;
019: import org.openrdf.model.vocabulary.XMLSchema;
020:
021: /**
022: *
023: * @author james
024: *
025: */
026: public class ValueComparatorTest extends TestCase {
027:
028: private ValueFactory vf = ValueFactoryImpl.getInstance();
029:
030: private BNode bnode1 = vf.createBNode();
031:
032: private BNode bnode2 = vf.createBNode();
033:
034: private URI uri1 = vf.createURI("http://script.example/Latin");
035:
036: private URI uri2 = vf
037: .createURI("http://script.example/Кириллица");
038:
039: private URI uri3 = vf.createURI("http://script.example/日本語");
040:
041: private Literal typed1 = vf.createLiteral(
042: "http://script.example/Latin", XMLSchema.STRING);
043:
044: private ValueComparator cmp = new ValueComparator();
045:
046: public void testBothNull() throws Exception {
047: assertTrue(cmp.compare(null, null) == 0);
048: }
049:
050: public void testLeftNull() throws Exception {
051: assertTrue(cmp.compare(null, typed1) < 0);
052: }
053:
054: public void testRightNull() throws Exception {
055: assertTrue(cmp.compare(typed1, null) > 0);
056: }
057:
058: public void testBothBnode() throws Exception {
059: assertTrue(cmp.compare(bnode1, bnode2) == 0);
060: }
061:
062: public void testLeftBnode() throws Exception {
063: assertTrue(cmp.compare(bnode1, typed1) < 0);
064: }
065:
066: public void testRightBnode() throws Exception {
067: assertTrue(cmp.compare(typed1, bnode1) > 0);
068: }
069:
070: public void testBothURI() throws Exception {
071: assertTrue(cmp.compare(uri1, uri1) == 0);
072: assertTrue(cmp.compare(uri1, uri2) < 0);
073: assertTrue(cmp.compare(uri1, uri3) < 0);
074: assertTrue(cmp.compare(uri2, uri1) > 0);
075: assertTrue(cmp.compare(uri2, uri2) == 0);
076: assertTrue(cmp.compare(uri2, uri3) < 0);
077: assertTrue(cmp.compare(uri3, uri1) > 0);
078: assertTrue(cmp.compare(uri3, uri2) > 0);
079: assertTrue(cmp.compare(uri3, uri3) == 0);
080: }
081:
082: public void testLeftURI() throws Exception {
083: assertTrue(cmp.compare(uri1, typed1) < 0);
084: }
085:
086: public void testRightURI() throws Exception {
087: assertTrue(cmp.compare(typed1, uri1) > 0);
088: }
089:
090: /**
091: * Tests whether xsd:int's are properly sorted in a list with mixed value
092: * types.
093: */
094: public void testOrder1() throws Exception {
095: Literal en4 = vf.createLiteral("4", "en");
096: Literal int10 = vf.createLiteral(10);
097: Literal int9 = vf.createLiteral(9);
098:
099: List<Literal> valueList = Arrays.asList(en4, int10, int9);
100: Collections.sort(valueList, cmp);
101:
102: assertTrue(valueList.indexOf(int9) < valueList.indexOf(int10));
103: }
104:
105: /**
106: * Tests whether various numerics are properly sorted in a list with mixed
107: * value types.
108: */
109: public void testOrder2() throws Exception {
110: Literal en4 = vf.createLiteral("4", "en");
111: Literal int10 = vf.createLiteral(10);
112: Literal int9 = vf.createLiteral(9);
113: Literal plain9 = vf.createLiteral("9");
114: Literal integer5 = vf.createLiteral("5", XMLSchema.INTEGER);
115: Literal float9 = vf.createLiteral(9f);
116: Literal plain4 = vf.createLiteral("4");
117: Literal plain10 = vf.createLiteral("10");
118:
119: List<Literal> valueList = Arrays.asList(en4, int10, int9,
120: plain9, integer5, float9, plain4, plain10);
121: Collections.sort(valueList, cmp);
122:
123: assertTrue(valueList.indexOf(integer5) < valueList
124: .indexOf(float9));
125: assertTrue(valueList.indexOf(integer5) < valueList
126: .indexOf(int9));
127: assertTrue(valueList.indexOf(integer5) < valueList
128: .indexOf(int10));
129: assertTrue(valueList.indexOf(float9) < valueList.indexOf(int10));
130: assertTrue(valueList.indexOf(int9) < valueList.indexOf(int10));
131: assertTrue(valueList.indexOf(int9) < valueList.indexOf(int10));
132: }
133:
134: /**
135: * Tests whether numerics of different types are properly sorted. The list
136: * also contains a datatype that would be sorted between the numerics if the
137: * datatypes were to be sorted alphabetically.
138: */
139: public void testOrder3() throws Exception {
140: Literal year1234 = vf.createLiteral("1234", XMLSchema.GYEAR);
141: Literal float2000 = vf.createLiteral(2000f);
142: Literal int1000 = vf.createLiteral(1000);
143:
144: List<Literal> valueList = Arrays.asList(year1234, float2000,
145: int1000);
146: Collections.sort(valueList, cmp);
147: assertTrue(valueList.indexOf(int1000) < valueList
148: .indexOf(float2000));
149: }
150: }
|