Author: nick Date: Thu May 17 19:22:04 2012 New Revision: 1339811 URL: http://svn.apache.org/viewvc?rev=1339811&view=rev Log: Make the composite test more explicit in what it does, fix up some deprecated warnings, and fix the typed getters for composites
Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java Modified: tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java?rev=1339811&r1=1339810&r2=1339811&view=diff ============================================================================== --- tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java (original) +++ tika/trunk/tika-core/src/main/java/org/apache/tika/metadata/Metadata.java Thu May 17 19:22:04 2012 @@ -242,10 +242,10 @@ public class Metadata implements Creativ * @return property value as a Integer, or <code>null</code> if the property is not set, or not a valid Integer */ public Integer getInt(Property property) { - if(property.getPropertyType() != Property.PropertyType.SIMPLE) { + if(property.getPrimaryProperty().getPropertyType() != Property.PropertyType.SIMPLE) { return null; } - if(property.getValueType() != Property.ValueType.INTEGER) { + if(property.getPrimaryProperty().getValueType() != Property.ValueType.INTEGER) { return null; } @@ -268,10 +268,10 @@ public class Metadata implements Creativ * @return property value as a Date, or <code>null</code> if the property is not set, or not a valid Date */ public Date getDate(Property property) { - if(property.getPropertyType() != Property.PropertyType.SIMPLE) { + if(property.getPrimaryProperty().getPropertyType() != Property.PropertyType.SIMPLE) { return null; } - if(property.getValueType() != Property.ValueType.DATE) { + if(property.getPrimaryProperty().getValueType() != Property.ValueType.DATE) { return null; } Modified: tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java URL: http://svn.apache.org/viewvc/tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java?rev=1339811&r1=1339810&r2=1339811&view=diff ============================================================================== --- tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java (original) +++ tika/trunk/tika-core/src/test/java/org/apache/tika/metadata/TestMetadata.java Thu May 17 19:22:04 2012 @@ -234,7 +234,7 @@ public class TestMetadata extends TestCa fail("Shouldn't be able to set a multi valued property as an int"); } catch(PropertyTypeException e) {} try { - meta.set(Metadata.CREATION_DATE, 1); + meta.set(TikaCoreProperties.CREATION_DATE, 1); fail("Shouldn't be able to set a date property as an int"); } catch(PropertyTypeException e) {} @@ -252,7 +252,7 @@ public class TestMetadata extends TestCa meta.set(Metadata.IMAGE_WIDTH, 22); assertEquals(22, meta.getInt(Metadata.IMAGE_WIDTH).intValue()); assertEquals(null, meta.getInt(Metadata.BITS_PER_SAMPLE)); - assertEquals(null, meta.getInt(Metadata.CREATION_DATE)); + assertEquals(null, meta.getInt(TikaCoreProperties.CREATION_DATE)); } /** @@ -264,8 +264,8 @@ public class TestMetadata extends TestCa long hour = 60 * 60 * 1000; // Isn't initially set, will get null back - assertEquals(null, meta.get(Metadata.CREATION_DATE)); - assertEquals(null, meta.getInt(Metadata.CREATION_DATE)); + assertEquals(null, meta.get(TikaCoreProperties.CREATION_DATE)); + assertEquals(null, meta.getInt(TikaCoreProperties.CREATION_DATE)); // Can only set as a single valued date try { @@ -278,52 +278,52 @@ public class TestMetadata extends TestCa } catch(PropertyTypeException e) {} // Can set it and retrieve it - meta.set(Metadata.CREATION_DATE, new Date(1000)); - assertEquals("1970-01-01T00:00:01Z", meta.get(Metadata.CREATION_DATE)); - assertEquals(1000, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, new Date(1000)); + assertEquals("1970-01-01T00:00:01Z", meta.get(TikaCoreProperties.CREATION_DATE)); + assertEquals(1000, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); // If you save a non date value, you get null - meta.set(Metadata.CREATION_DATE, "INVALID"); - assertEquals("INVALID", meta.get(Metadata.CREATION_DATE)); - assertEquals(null, meta.getDate(Metadata.CREATION_DATE)); + meta.set(TikaCoreProperties.CREATION_DATE, "INVALID"); + assertEquals("INVALID", meta.get(TikaCoreProperties.CREATION_DATE)); + assertEquals(null, meta.getDate(TikaCoreProperties.CREATION_DATE)); // If you try to retrieve a non simple date value, you get null - meta.set(Metadata.CREATION_DATE, new Date(1000)); - assertEquals(1000, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, new Date(1000)); + assertEquals(1000, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); assertEquals(null, meta.getInt(Metadata.BITS_PER_SAMPLE)); - assertEquals(null, meta.getInt(Metadata.CREATION_DATE)); + assertEquals(null, meta.getInt(TikaCoreProperties.CREATION_DATE)); // Our format doesn't include milliseconds // This means things get rounded - meta.set(Metadata.CREATION_DATE, new Date(1050)); - assertEquals("1970-01-01T00:00:01Z", meta.get(Metadata.CREATION_DATE)); - assertEquals(1000, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, new Date(1050)); + assertEquals("1970-01-01T00:00:01Z", meta.get(TikaCoreProperties.CREATION_DATE)); + assertEquals(1000, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); // We can accept a number of different ISO-8601 variants - meta.set(Metadata.CREATION_DATE, "1970-01-01T00:00:01Z"); - assertEquals(1000, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, "1970-01-01T00:00:01Z"); + assertEquals(1000, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); - meta.set(Metadata.CREATION_DATE, "1970-01-01 00:00:01Z"); - assertEquals(1000, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, "1970-01-01 00:00:01Z"); + assertEquals(1000, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); - meta.set(Metadata.CREATION_DATE, "1970-01-01T01:00:01+01:00"); - assertEquals(1000, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, "1970-01-01T01:00:01+01:00"); + assertEquals(1000, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); - meta.set(Metadata.CREATION_DATE, "1970-01-01 01:00:01+01:00"); - assertEquals(1000, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, "1970-01-01 01:00:01+01:00"); + assertEquals(1000, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); - meta.set(Metadata.CREATION_DATE, "1970-01-01T12:00:01+12:00"); - assertEquals(1000, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, "1970-01-01T12:00:01+12:00"); + assertEquals(1000, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); - meta.set(Metadata.CREATION_DATE, "1969-12-31T12:00:01-12:00"); - assertEquals(1000, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, "1969-12-31T12:00:01-12:00"); + assertEquals(1000, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); // Dates without times, come in at midday UTC - meta.set(Metadata.CREATION_DATE, "1970-01-01"); - assertEquals(12*hour, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, "1970-01-01"); + assertEquals(12*hour, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); - meta.set(Metadata.CREATION_DATE, "1970:01:01"); - assertEquals(12*hour, meta.getDate(Metadata.CREATION_DATE).getTime()); + meta.set(TikaCoreProperties.CREATION_DATE, "1970:01:01"); + assertEquals(12*hour, meta.getDate(TikaCoreProperties.CREATION_DATE).getTime()); } /** @@ -338,14 +338,27 @@ public class TestMetadata extends TestCa "1970-01-01T00:00:01", meta.get(TikaCoreProperties.DATE)); } + /** + * Defines a composite property, then checks that when set as the + * composite the value can be retrieved with the property or the aliases + */ + @SuppressWarnings("deprecation") public void testCompositeProperty() { - Metadata meta = new Metadata(); - Property compositeProperty = Property.composite( - DublinCore.DESCRIPTION, new Property[] { Property.internalText(Metadata.DESCRIPTION)}); - String message = "composite description"; - meta.set(compositeProperty, message); - assertEquals(message, meta.get(DublinCore.DESCRIPTION)); - assertEquals(message, meta.get(Metadata.DESCRIPTION)); - } - + Metadata meta = new Metadata(); + Property compositeProperty = Property.composite( + DublinCore.DESCRIPTION, new Property[] { + Property.internalText(Metadata.DESCRIPTION), + Property.internalText("testDescriptionAlt") + }); + String message = "composite description"; + meta.set(compositeProperty, message); + + // Fetch as the composite + assertEquals(message, meta.get(compositeProperty)); + // Fetch as the primary property on the composite + assertEquals(message, meta.get(DublinCore.DESCRIPTION)); + // Fetch as the aliases + assertEquals(message, meta.get(Metadata.DESCRIPTION)); + assertEquals(message, meta.get("testDescriptionAlt")); + } }