Author: desruisseaux
Date: Fri Jun 7 14:18:22 2013
New Revision: 1490660
URL: http://svn.apache.org/r1490660
Log:
Metadata.toString() is now localized, as decided by email two months ago.
Modified:
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java
sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeTableViewTest.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java
Modified:
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java?rev=1490660&r1=1490659&r2=1490660&view=diff
==============================================================================
---
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java
[UTF-8] (original)
+++
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/TreeTableView.java
[UTF-8] Fri Jun 7 14:18:22 2013
@@ -17,6 +17,8 @@
package org.apache.sis.metadata;
import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
import java.text.Format;
import java.io.Serializable;
import java.io.IOException;
@@ -127,7 +129,8 @@ final class TreeTableView implements Tre
public String toString() {
synchronized (TreeTableView.class) {
if (format == null) {
- final TreeTableFormat f = new TreeTableFormat(null, null);
+ final TreeTableFormat f = new TreeTableFormat(
+ Locale.getDefault(Locale.Category.FORMAT),
TimeZone.getDefault());
f.setColumns(TableColumn.NAME, TableColumn.VALUE);
format = f;
}
Modified:
sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeTableViewTest.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeTableViewTest.java?rev=1490660&r1=1490659&r2=1490660&view=diff
==============================================================================
---
sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeTableViewTest.java
[UTF-8] (original)
+++
sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/TreeTableViewTest.java
[UTF-8] Fri Jun 7 14:18:22 2013
@@ -26,6 +26,8 @@ import org.apache.sis.test.TestCase;
import org.junit.Test;
import static org.apache.sis.test.Assert.*;
+import static org.apache.sis.test.TestUtilities.toTreeStructure;
+import static org.apache.sis.test.TestUtilities.formatNameAndValue;
/**
@@ -47,10 +49,10 @@ public final strictfp class TreeTableVie
}
/**
- * Asserts that the given metadata object has the expected string
representation.
+ * The expected string representation of the tree created by {@link
#create(ValueExistencePolicy)}
+ * with {@link ValueExistencePolicy#NON_EMPTY}.
*/
- private static void assertExpectedString(final TreeTableView metadata) {
- assertMultilinesEquals("toString()",
+ private static final String EXPECTED =
"DefaultCitation\n" +
" ├─Title…………………………………………………………………………………… Some title\n" +
" ├─Alternate title (1 of 2)………………………………… First alternate
title\n" +
@@ -67,16 +69,18 @@ public final strictfp class TreeTableVie
" │ └─Role…………………………………………………………………………… Point of contact\n"
+
" ├─Presentation form (1 of 2)…………………………… Map digital\n" +
" ├─Presentation form (2 of 2)…………………………… Map hardcopy\n" +
- " └─Other citation details……………………………………… Some other
details\n",
- metadata.toString());
- }
+ " └─Other citation details……………………………………… Some other
details\n";
/**
* Tests {@link TreeTableView#toString()}.
+ * Since the result is locale-dependant, we can not compare against an
exact string.
+ * We will only compare the beginning of each line.
*/
@Test
public void testToString() {
- assertExpectedString(create(ValueExistencePolicy.NON_EMPTY));
+ final TreeTableView metadata = create(ValueExistencePolicy.NON_EMPTY);
+ assertMultilinesEquals(EXPECTED, formatNameAndValue(metadata)); //
Locale-independent
+ assertArrayEquals(toTreeStructure(EXPECTED),
toTreeStructure(metadata.toString())); // Locale-dependent.
}
/**
@@ -98,6 +102,6 @@ public final strictfp class TreeTableVie
try (ObjectInputStream in = new ObjectInputStream(new
ByteArrayInputStream(data))) {
deserialized = in.readObject();
}
- assertExpectedString((TreeTableView) deserialized);
+ assertMultilinesEquals(EXPECTED, formatNameAndValue((TreeTableView)
deserialized));
}
}
Modified:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java?rev=1490660&r1=1490659&r2=1490660&view=diff
==============================================================================
---
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
[UTF-8] (original)
+++
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/io/CompoundFormat.java
[UTF-8] Fri Jun 7 14:18:22 2013
@@ -83,13 +83,17 @@ public abstract class CompoundFormat<T>
/**
* The locale given at construction time, or {@link Locale#ROOT} (never
{@code null}) for
* unlocalized format. See {@link #getLocale()} for more information on
{@code ROOT} locale.
+ *
+ * @see #getLocale()
*/
- protected final Locale locale;
+ private final Locale locale;
/**
* The timezone given at construction time, or {@code null} for UTC.
+ *
+ * @see #getTimeZone()
*/
- protected final TimeZone timezone;
+ private final TimeZone timezone;
/**
* The formats for smaller unit of information.
@@ -107,12 +111,12 @@ public abstract class CompoundFormat<T>
* @param timezone The timezone, or {@code null} for UTC.
*/
protected CompoundFormat(final Locale locale, final TimeZone timezone) {
- this.locale = (locale != null) ? locale : Locale.ROOT;
+ this.locale = (locale != null) ? locale : Locale.ROOT;
this.timezone = timezone;
}
/**
- * Returns the locale given at construction time. The returned value may
be {@link Locale#ROOT}
+ * Returns the locale used by this format. The returned value may be
{@link Locale#ROOT}
* if this format does not apply any localization. The definition of
"unlocalized string"
* is implementation-dependent, but some typical examples are:
*
@@ -129,6 +133,15 @@ public abstract class CompoundFormat<T>
}
/**
+ * Returns the timezone used by this format.
+ *
+ * @return The timezone used for this format, or UTC for unlocalized
format.
+ */
+ public TimeZone getTimeZone() {
+ return timezone != null ? (TimeZone) timezone.clone() :
TimeZone.getTimeZone("UTC");
+ }
+
+ /**
* Returns the base type of values parsed and formatted by this {@code
Format} instance.
* The returned type may be a subclass of {@code <T>} if the format is
configured in a way
* that restrict the kind value to be parsed.
@@ -246,7 +259,7 @@ public abstract class CompoundFormat<T>
} while (Character.isSpaceChar(c) || Character.isISOControl(c));
pos.setErrorIndex(i);
}
- throw new LocalizedParseException(locale, getValueType(), text, pos);
+ throw new LocalizedParseException(getLocale(), getValueType(), text,
pos);
}
/**
@@ -368,6 +381,7 @@ public abstract class CompoundFormat<T>
* documented in this method javadoc. But actually it is not, since
the call to
* DefaultFormat.getInstance(…) will indirectly perform this kind of
comparison.
*/
+ final Locale locale = getLocale();
if (Number.class.isAssignableFrom(valueType)) {
if (Locale.ROOT.equals(locale)) {
return DefaultFormat.getInstance(valueType);
@@ -381,7 +395,7 @@ public abstract class CompoundFormat<T>
} else {
format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",
Locale.ROOT);
}
- format.setTimeZone(timezone != null ? timezone :
TimeZone.getTimeZone("UTC"));
+ format.setTimeZone(getTimeZone());
return format;
} else if (valueType == Angle.class) {
return AngleFormat.getInstance(locale);
Modified:
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java?rev=1490660&r1=1490659&r2=1490660&view=diff
==============================================================================
---
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
[UTF-8] (original)
+++
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/collection/TreeTableFormat.java
[UTF-8] Fri Jun 7 14:18:22 2013
@@ -404,7 +404,7 @@ public class TreeTableFormat extends Tab
*/
if (--indentationLevel < 0) {
pos.setErrorIndex(indexOfLineStart);
- throw new LocalizedParseException(locale,
+ throw new LocalizedParseException(getLocale(),
Errors.Keys.NodeHasNoParent_1, new Object[]
{node}, 0);
}
lastNode = lastNode.getParent();
@@ -418,7 +418,7 @@ public class TreeTableFormat extends Tab
final TreeTable.Node parent = lastNode.getParent();
if (parent == null) {
pos.setErrorIndex(indexOfLineStart);
- throw new LocalizedParseException(locale,
+ throw new LocalizedParseException(getLocale(),
Errors.Keys.NodeHasNoParent_1, new Object[]
{node}, 0);
}
parent.getChildren().add(node);
@@ -583,9 +583,9 @@ public class TreeTableFormat extends Tab
}
text = format.format(value);
} else if (value instanceof InternationalString) {
- text = ((InternationalString) value).toString(locale);
+ text = ((InternationalString) value).toString(getLocale());
} else if (value instanceof CodeList<?>) {
- text = Types.getCodeTitle((CodeList<?>)
value).toString(locale);
+ text = Types.getCodeTitle((CodeList<?>)
value).toString(getLocale());
} else if (value instanceof Enum<?>) {
text = CharSequences.upperCaseToSentence(((Enum<?>)
value).name());
} else {
Modified:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java?rev=1490660&r1=1490659&r2=1490660&view=diff
==============================================================================
---
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
[UTF-8] (original)
+++
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/TestUtilities.java
[UTF-8] Fri Jun 7 14:18:22 2013
@@ -29,7 +29,11 @@ import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.sis.util.Static;
+import org.apache.sis.util.CharSequences;
import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.collection.TreeTable;
+import org.apache.sis.util.collection.TableColumn;
+import org.apache.sis.util.collection.TreeTableFormat;
import org.apache.sis.internal.util.X364;
import static org.junit.Assert.*;
@@ -69,6 +73,12 @@ public final strictfp class TestUtilitie
};
/**
+ * The {@link TreeTableFormat} to use for unlocalized string
representations.
+ * Created when first needed.
+ */
+ private static Format tableFormat;
+
+ /**
* The thread group for every threads created for testing purpose.
*/
public static final ThreadGroup THREADS = new ThreadGroup("SIS-Tests");
@@ -200,6 +210,68 @@ public final strictfp class TestUtilitie
}
/**
+ * Returns a unlocalized string representation of {@code NAME} and {@code
VALUE} columns of the given tree table.
+ * This method is used mostly as a convenient way to verify the content of
an ISO 19115 metadata object.
+ *
+ * @param table The table for which to get a string representation.
+ * @return A unlocalized string representation of the given tree table.
+ */
+ public static String formatNameAndValue(final TreeTable table) {
+ synchronized (TestUtilities.class) {
+ if (tableFormat == null) {
+ final TreeTableFormat f = new TreeTableFormat(null, null);
+ f.setColumns(TableColumn.NAME, TableColumn.VALUE);
+ tableFormat = f;
+ }
+ return tableFormat.format(table);
+ }
+ }
+
+ /**
+ * Returns the tree structure of the given string representation, without
the localized text.
+ * For example given the following string:
+ *
+ * {@preformat
+ * Citation
+ * ├─Title…………………………………………………… Some title
+ * └─Cited responsible party
+ * └─Individual name……………… Some person of contact
+ * }
+ *
+ * this method returns an array containing the following elements:
+ *
+ * {@preformat
+ * "",
+ * " ├─",
+ * " └─",
+ * " └─"
+ * }
+ *
+ * This method is used for comparing two tree having string representation
in different locales.
+ * In such case, we can not compare the actual text content. The best we
can do is to compare
+ * the tree structure.
+ *
+ * @param tree The string representation of a tree.
+ * @return The structure of the given tree, without text.
+ */
+ public static CharSequence[] toTreeStructure(final CharSequence tree) {
+ final CharSequence[] lines = CharSequences.split(tree, '\n');
+ for (int i=0; i<lines.length; i++) {
+ final CharSequence line = lines[i];
+ final int length = line.length();
+ for (int j=0; j<length;) {
+ final int c = Character.codePointAt(line, j);
+ if (Character.isLetterOrDigit(c)) {
+ lines[i] = line.subSequence(0, j);
+ break;
+ }
+ j += Character.charCount(c);
+ }
+ }
+ return lines;
+ }
+
+ /**
* Returns the single element from the given array. If the given array is
null or
* does not contains exactly one element, then an {@link AssertionError}
is thrown.
*
Modified:
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1490660&r1=1490659&r2=1490660&view=diff
==============================================================================
---
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] (original)
+++
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] Fri Jun 7 14:18:22 2013
@@ -32,6 +32,7 @@ import org.junit.BeforeClass;
@Suite.SuiteClasses({
// Following are testing the test tools.
org.apache.sis.internal.test.AssertTest.class,
+ org.apache.sis.internal.test.TestUtilitiesTest.class,
org.apache.sis.internal.test.XMLComparatorTest.class,
// Most basic functions of SIS library.
Modified:
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java?rev=1490660&r1=1490659&r2=1490660&view=diff
==============================================================================
---
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
[UTF-8] (original)
+++
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
[UTF-8] Fri Jun 7 14:18:22 2013
@@ -86,6 +86,8 @@ public class NetcdfStore extends Abstrac
/**
* Closes this NetCDF store and releases any underlying resources.
+ *
+ * @throws DataStoreException If an error occurred while closing the
NetCDF file.
*/
@Override
public void close() throws DataStoreException {
Modified:
sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java?rev=1490660&r1=1490659&r2=1490660&view=diff
==============================================================================
---
sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java
[UTF-8] (original)
+++
sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/storage/netcdf/MetadataReaderTest.java
[UTF-8] Fri Jun 7 14:18:22 2013
@@ -24,10 +24,12 @@ import org.apache.sis.internal.netcdf.Te
import org.apache.sis.internal.netcdf.Decoder;
import org.apache.sis.internal.netcdf.ucar.DecoderWrapper;
import org.apache.sis.internal.netcdf.impl.ChannelDecoderTest;
+import org.apache.sis.metadata.iso.DefaultMetadata;
import org.apache.sis.test.DependsOn;
import org.junit.Test;
import static org.apache.sis.test.Assert.*;
+import static org.apache.sis.test.TestUtilities.formatNameAndValue;
/**
@@ -144,6 +146,6 @@ public final strictfp class MetadataRead
" └─Lineage\n" +
" └─Statement…………………………………………………………………… 2003-04-07
12:12:50 - created by gribtocdl" +
" 2005-09-26T21:50:00 - edavis - add attributes for
dataset discovery\n",
- actual.toString());
+ formatNameAndValue(DefaultMetadata.castOrCopy(actual).asTreeTable()));
}
}