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.ArrayList;
009: import java.util.Collections;
010: import java.util.List;
011:
012: import javax.xml.datatype.DatatypeFactory;
013: import javax.xml.datatype.XMLGregorianCalendar;
014:
015: import junit.framework.TestCase;
016:
017: import org.openrdf.model.Literal;
018: import org.openrdf.model.ValueFactory;
019: import org.openrdf.model.impl.ValueFactoryImpl;
020: import org.openrdf.model.vocabulary.XMLSchema;
021:
022: /**
023: *
024: * @author james
025: *
026: */
027: public class LiteralComparatorTest extends TestCase {
028:
029: private ValueFactory vf = new ValueFactoryImpl();
030:
031: private Literal one = vf.createLiteral(1);
032:
033: private Literal ten = vf.createLiteral(10);
034:
035: private Literal a = vf.createLiteral("a");
036:
037: private Literal b = vf.createLiteral("b");
038:
039: private Literal la = vf.createLiteral("a", "en");
040:
041: private Literal lb = vf.createLiteral("b", "en");
042:
043: private Literal lf = vf.createLiteral("a", "fr");
044:
045: private Literal f = vf.createLiteral(false);
046:
047: private Literal t = vf.createLiteral(true);
048:
049: private Literal date1;
050:
051: private Literal date2;
052:
053: private Literal simple1 = vf
054: .createLiteral("http://script.example/Latin");
055:
056: private Literal simple2 = vf
057: .createLiteral("http://script.example/Кириллица");
058:
059: private Literal typed1 = vf.createLiteral(
060: "http://script.example/Latin", XMLSchema.STRING);
061:
062: private ValueComparator cmp = new ValueComparator();
063:
064: public void testNumeric() throws Exception {
065: assertTrue(cmp.compare(one, one) == 0);
066: assertTrue(cmp.compare(one, ten) < 0);
067: assertTrue(cmp.compare(ten, one) > 0);
068: assertTrue(cmp.compare(ten, ten) == 0);
069: }
070:
071: public void testString() throws Exception {
072: assertTrue(cmp.compare(a, a) == 0);
073: assertTrue(cmp.compare(a, b) < 0);
074: assertTrue(cmp.compare(b, a) > 0);
075: assertTrue(cmp.compare(b, b) == 0);
076: }
077:
078: public void testSameLanguage() throws Exception {
079: assertTrue(cmp.compare(la, la) == 0);
080: assertTrue(cmp.compare(la, lb) < 0);
081: assertTrue(cmp.compare(lb, la) > 0);
082: assertTrue(cmp.compare(lb, lb) == 0);
083: }
084:
085: public void testDifferentLanguage() throws Exception {
086: cmp.compare(la, lf);
087: }
088:
089: public void testBoolean() throws Exception {
090: assertTrue(cmp.compare(f, f) == 0);
091: assertTrue(cmp.compare(f, t) < 0);
092: assertTrue(cmp.compare(t, f) > 0);
093: assertTrue(cmp.compare(t, t) == 0);
094: }
095:
096: public void testDateTime() throws Exception {
097: assertTrue(cmp.compare(date1, date1) == 0);
098: assertTrue(cmp.compare(date1, date2) < 0);
099: assertTrue(cmp.compare(date2, date1) > 0);
100: assertTrue(cmp.compare(date2, date2) == 0);
101: }
102:
103: public void testBothSimple() throws Exception {
104: assertTrue(cmp.compare(simple1, simple1) == 0);
105: assertTrue(cmp.compare(simple1, simple2) < 0);
106: assertTrue(cmp.compare(simple2, simple1) > 0);
107: assertTrue(cmp.compare(simple2, simple2) == 0);
108: }
109:
110: public void testLeftSimple() throws Exception {
111: assertTrue(cmp.compare(simple1, typed1) < 0);
112: }
113:
114: public void testRightSimple() throws Exception {
115: assertTrue(cmp.compare(typed1, simple1) > 0);
116: }
117:
118: public void testOrder() throws Exception {
119: Literal en4 = vf.createLiteral("4", "en");
120: Literal nine = vf.createLiteral(9);
121: List<Literal> list = new ArrayList<Literal>();
122: list.add(ten);
123: list.add(en4);
124: list.add(nine);
125: Collections.sort(list, cmp);
126: assertTrue(list.indexOf(nine) < list.indexOf(ten));
127: }
128:
129: @Override
130: protected void setUp() throws Exception {
131: DatatypeFactory factory = DatatypeFactory.newInstance();
132: XMLGregorianCalendar mar = factory
133: .newXMLGregorianCalendar("2000-03-04T20:00:00Z");
134: XMLGregorianCalendar oct = factory
135: .newXMLGregorianCalendar("2002-10-10T12:00:00-05:00");
136: date1 = vf.createLiteral(mar);
137: date2 = vf.createLiteral(oct);
138: }
139: }
|