sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1563691 - in /sis/branches/JDK7/core/sis-referencing/src: main/java/org/apache/sis/io/wkt/ test/java/org/apache/sis/io/wkt/ test/java/org/apache/sis/test/suite/
Date Sun, 02 Feb 2014 18:56:28 GMT
Author: desruisseaux
Date: Sun Feb  2 18:56:28 2014
New Revision: 1563691

URL: http://svn.apache.org/r1563691
Log:
Remove internal 'Immutable' subclasses. Make Colors and Symbols cloneable instead.

Added:
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ColorsTest.java
  (with props)
Modified:
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
    sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/FormatterTest.java
    sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java?rev=1563691&r1=1563690&r2=1563691&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Colors.java
[UTF-8] Sun Feb  2 18:56:28 2014
@@ -42,7 +42,7 @@ import org.apache.sis.util.resources.Err
  * @see WKTFormat#getColors()
  * @see WKTFormat#setColors(Colors)
  */
-public class Colors implements Serializable {
+public class Colors implements Cloneable, Serializable {
     /**
      * For cross-version compatibility.
      */
@@ -55,7 +55,7 @@ public class Colors implements Serializa
      *
      * @see FormattableObject#print()
      */
-    public static final Colors CONSOLE = new Immutable();
+    public static final Colors CONSOLE = new Colors();
     static {
         final EnumMap<ElementKind,X364> map = CONSOLE.map;
         map.put(ElementKind.NUMBER,     X364.FOREGROUND_YELLOW);
@@ -67,12 +67,19 @@ public class Colors implements Serializa
         map.put(ElementKind.METHOD,     X364.FOREGROUND_GREEN);
         map.put(ElementKind.DATUM,      X364.FOREGROUND_GREEN);
         map.put(ElementKind.ERROR,      X364.BACKGROUND_RED);
+        CONSOLE.isImmutable = true;
     }
 
     /**
      * The map of colors.
+     * Consider this field as final — it is modified only by {@link #clone()}.
      */
-    private final EnumMap<ElementKind,X364> map;
+    private EnumMap<ElementKind,X364> map;
+
+    /**
+     * {@code true} if this instance shall be considered as immutable.
+     */
+    private boolean isImmutable;
 
     /**
      * Creates a new, initially empty, set of colors.
@@ -101,6 +108,9 @@ public class Colors implements Serializa
      * @throws IllegalArgumentException If the given color name is not recognized.
      */
     public void setName(final ElementKind key, final String color) throws IllegalArgumentException
{
+        if (isImmutable) {
+            throw new UnsupportedOperationException(Errors.format(Errors.Keys.UnmodifiableObject_1,
"Colors"));
+        }
         if (color == null) {
             map.remove(key);
         } else {
@@ -114,7 +124,7 @@ public class Colors implements Serializa
      * @param key The syntactic element for which to get the color.
      * @return The color of the specified element, or {@code null} if none.
      */
-    public String getName(final ElementKind key) {
+    public final String getName(final ElementKind key) { // Declared final for consistency
with getAnsiSequence(…)
         final X364 color = map.get(key);
         return (color != null) ? color.color : null;
     }
@@ -130,55 +140,62 @@ public class Colors implements Serializa
     /**
      * Returns an immutable copy of this set of colors, or {@code this} if this instance
is already immutable.
      */
-    Colors immutable() {
-        return new Immutable(this);
+    final Colors immutable() {
+        if (isImmutable) {
+            return this;
+        }
+        final Colors clone = clone();
+        clone.isImmutable = true;
+        return clone;
     }
 
     /**
-     * An immutable subclass of {@link Colors} for the {@link Colors#CONSOLE} constant
-     * or for the object to be used by {@link WKTFormat}.
+     * Returns a clone of this {@code Colors}.
+     *
+     * @return A clone of this {@code Colors}.
      */
-    private static final class Immutable extends Colors {
-        /**
-         * For cross-version compatibility.
-         */
-        private static final long serialVersionUID = -2349530616334766576L;
-
-        /**
-         * Creates an initially empty set of colors.
-         * Callers must put colors directly in the {@link #map}.
-         */
-        Immutable() {
-        }
-
-        /**
-         * Creates a immutable copy of the given set of colors.
-         */
-        Immutable(final Colors colors) {
-            super(colors);
+    @Override
+    public Colors clone() {
+        final Colors clone;
+        try {
+            clone = (Colors) super.clone();
+        } catch (CloneNotSupportedException e) {
+            throw new AssertionError(e);
         }
+        clone.map = clone.map.clone();
+        clone.isImmutable = false;
+        return clone;
+    }
 
-        /**
-         * Returns {@code this} since this set of colors is already immutable.
-         */
-        @Override
-        Colors immutable() {
-            return this;
+    /**
+     * Compares this {@code Colors} with the given object for equality.
+     *
+     * @param  other The object to compare with this {@code Colors}.
+     * @return {@code true} if both objects are equal.
+     */
+    @Override
+    public boolean equals(final Object other) {
+        if (other instanceof Colors) {
+            final Colors that = (Colors) other;
+            return map.equals(that.map);
         }
+        return false;
+    }
 
-        /**
-         * Do not allow color changes.
-         */
-        @Override
-        public void setName(final ElementKind key, final String color) {
-            throw new UnsupportedOperationException(Errors.format(Errors.Keys.UnmodifiableObject_1,
"Colors"));
-        }
+    /**
+     * Returns a hash code value for this object.
+     *
+     * @return A hash code value.
+     */
+    @Override
+    public int hashCode() {
+        return map.hashCode() ^ (int) serialVersionUID;
+    }
 
-        /**
-         * Replaces the deserialized instance by {@link #CONSOLE} one if possible.
-         */
-        Object readResolve() {
-            return super.map.equals(CONSOLE.map) ? CONSOLE : this;
-        }
+    /**
+     * Replaces the deserialized instance by {@link #CONSOLE} one if possible.
+     */
+    final Object readResolve() {
+        return isImmutable && map.equals(CONSOLE.map) ? CONSOLE : this;
     }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java?rev=1563691&r1=1563690&r2=1563691&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] Sun Feb  2 18:56:28 2014
@@ -54,22 +54,18 @@ import org.apache.sis.referencing.operat
 
 
 /**
- * Formats {@linkplain FormattableObject formattable objects} as <cite>Well Known Text</cite>
(WKT).
- * Each {@code Formatter} instance is created for a given {@linkplain Convention convention},
- * {@linkplain Symbols set of symbols} and indentation.
+ * Formats an object as <cite>Well Known Text</cite> (WKT). {@code Formatter}
instances are created by
+ * {@link WKTFormat} and given to the {@link FormattableObject#formatTo(Formatter)} method
of the object
+ * to format. {@code Formatter} provides the following services:
  *
- * {@link Example}
- * For formatting an object with {@linkplain Symbols#CURLY_BRACKETS curly brackets} instead
of
- * square ones and the whole text on the same line (no indentation), one can use the following:
- *
- * {@preformat java
- *     Formatter formatter = new Formatter(Convention.DEFAULT, Symbols.CURLY_BRACKETS, WKTFormat.SINGLE_LINE);
- *     formatter.append(theObject);
- *     String wkt = formatter.toString();
- *
- *     // Following is needed only if you want to reuse the formatter again for other objects.
- *     formatter.clear();
- * }
+ * <ul>
+ *   <li>A series of {@code append(…)} methods to be invoked by the {@code formatTo(Formatter)}
implementations.</li>
+ *   <li>Information about the context: {@link #getConvention()}, {@link #getAngularUnit()},
{@link #getLinearUnit()}.
+ *       Some of those information (e.g. the angular units) depend on the enclosing WKT element.</li>
+ *   <li>Convenience methods for fetching relevant information from the object to format:
+ *       {@link #getName(IdentifiedObject)}, {@link #getIdentifier(IdentifiedObject)}.</li>
+ *   <li>A flag for declaring the object unformattable.</li>
+ * </ul>
  *
  * {@section Thread safety}
  * Formatters are not synchronized. It is recommended to create separated formatter instances
for each thread.
@@ -254,13 +250,13 @@ public class Formatter {
      * Constructor for private use by {@link WKTFormat#getFormatter()} only. This allows
to use the number
      * format created by {@link WKTFormat#createFormat(Class)}, which may be overridden by
the user.
      */
-    Formatter(final Symbols symbols, final NumberFormat numberFormat) {
+    Formatter(final Symbols symbols, final NumberFormat numberFormat, final UnitFormat unitFormat)
{
         this.convention   = Convention.DEFAULT;
         this.authority    = Convention.DEFAULT.getNameAuthority();
         this.symbols      = symbols;
         this.indentation  = WKTFormat.DEFAULT_INDENTATION;
         this.numberFormat = numberFormat; // No clone needed.
-        this.unitFormat   = UnitFormat.getInstance(symbols.getLocale());
+        this.unitFormat   = unitFormat;   // No clone needed.
         // Do not set the buffer. It will be set by WKTFormat.format(…).
     }
 
@@ -940,7 +936,7 @@ public class Formatter {
      * Clears this formatter before formatting a new object. This method clears the
      * {@linkplain #getLinearUnit() linear unit} and {@linkplain #isInvalidWKT() WKT validity
flag}.
      */
-    public void clear() {
+    final void clear() {
         /*
          * Configuration options (indentation, colors, conventions) are left unchanged.
          * We do not mention that fact in the Javadoc because those options do not appear

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java?rev=1563691&r1=1563690&r2=1563691&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/Symbols.java
[UTF-8] Sun Feb  2 18:56:28 2014
@@ -18,8 +18,6 @@ package org.apache.sis.io.wkt;
 
 import java.util.Arrays;
 import java.util.Locale;
-import java.io.IOException;
-import java.io.ObjectInputStream;
 import java.io.Serializable;
 import java.text.NumberFormat;
 import org.apache.sis.util.Localized;
@@ -53,7 +51,7 @@ import static org.apache.sis.util.Argume
  * @see WKTFormat#getSymbols()
  * @see WKTFormat#setSymbols(Symbols)
  */
-public class Symbols implements Localized, Serializable {
+public class Symbols implements Localized, Cloneable, Serializable {
     /**
      * For cross-version compatibility.
      */
@@ -63,14 +61,14 @@ public class Symbols implements Localize
      * A set of symbols with values between square brackets, like {@code DATUM["WGS84"]}.
      * This is the most frequently used WKT format.
      */
-    public static final Symbols SQUARE_BRACKETS = new Immutable(
+    public static final Symbols SQUARE_BRACKETS = new Symbols(
             new int[] {'[', ']', '(', ')'}, new int[] {'"', '"', '“', '”'});
 
     /**
      * A set of symbols with values between parentheses, like {@code DATUM("WGS84")}.
      * This is a less frequently used but legal WKT format.
      */
-    public static final Symbols CURLY_BRACKETS = new Immutable(
+    public static final Symbols CURLY_BRACKETS = new Symbols(
             new int[] {'(', ')', '[', ']'}, SQUARE_BRACKETS.quotes);
 
     /**
@@ -113,7 +111,7 @@ public class Symbols implements Localize
      * will look for determining the text end.
      *
      * @see #getQuote()
-     * @see #readObject(ObjectInputStream)
+     * @see #readResolve()
      */
     private transient String quote;
 
@@ -130,6 +128,11 @@ public class Symbols implements Localize
     private String separator;
 
     /**
+     * {@code true} if this instance shall be considered as immutable.
+     */
+    private boolean isImmutable;
+
+    /**
      * Creates a new set of WKT symbols initialized to the default values.
      * Newly created {@code Symbols} instances use the following defaults:
      *
@@ -173,70 +176,16 @@ public class Symbols implements Localize
         this.openSequence  = '{';
         this.closeSequence = '}';
         this.separator     = ", ";
+        this.isImmutable   = true;
     }
 
     /**
-     * An immutable set of symbols.
+     * Throws an exception if this set of symbols is immutable.
      */
-    private static final class Immutable extends Symbols {
-        /**
-         * For cross-version compatibility.
-         */
-        private static final long serialVersionUID = -3252233734797811448L;
-
-        /**
-         * Constructor reserved to {@link Symbols#SQUARE_BRACKETS} and {@link Symbols#CURLY_BRACKETS}
constants.
-         * The given arrays are stored by reference - they are not cloned.
-         */
-        Immutable(final int[] brackets, final int[] quotes) {
-            super(brackets, quotes);
-        }
-
-        /**
-         * Creates an immutable copy of the given set of symbols.
-         */
-        Immutable(final Symbols symbols) {
-            super(symbols);
-        }
-
-        /**
-         * Returns {@code this} since this set of symbols is already immutable.
-         */
-        @Override
-        Symbols immutable() {
-            return this;
-        }
-
-        /**
-         * Unconditionally throws an exception since instance of this class are immutable.
-         */
-        @Override
-        void checkWritePermission() throws UnsupportedOperationException {
+    final void checkWritePermission() throws UnsupportedOperationException {
+        if (isImmutable) {
             throw new UnsupportedOperationException(Errors.format(Errors.Keys.UnmodifiableObject_1,
"Symbols"));
         }
-
-        /**
-         * Invoked on deserialization for replacing the deserialized instance by the constant
instance.
-         */
-        Object readResolve() {
-            if (equals(SQUARE_BRACKETS)) return SQUARE_BRACKETS;
-            if (equals(CURLY_BRACKETS))  return CURLY_BRACKETS;
-            return this;
-        }
-    }
-
-    /**
-     * Throws an exception if this set of symbols is immutable.
-     * To be overridden by the {@link Immutable} subclass only.
-     */
-    void checkWritePermission() throws UnsupportedOperationException {
-    }
-
-    /**
-     * Returns an immutable copy of this set of symbols, or {@code this} if this instance
is already immutable.
-     */
-    Symbols immutable() {
-        return new Immutable(this);
     }
 
     /**
@@ -630,6 +579,39 @@ public class Symbols implements Localize
     }
 
     /**
+     * Returns an immutable copy of this set of symbols, or {@code this} if this instance
is already immutable.
+     */
+    final Symbols immutable() {
+        if (isImmutable) {
+            return this;
+        }
+        final Symbols clone = clone();
+        clone.isImmutable = true;
+        return clone;
+    }
+
+    /**
+     * Returns a clone of this {@code Symbols}.
+     *
+     * @return A clone of this {@code Symbols}.
+     */
+    @Override
+    public Symbols clone() {
+        final Symbols clone;
+        try {
+            clone = (Symbols) super.clone();
+        } catch (CloneNotSupportedException e) {
+            throw new AssertionError(e);
+        }
+        /*
+         * No needs to copy the arrays, because their content are never modified.
+         * Instead, the setter methods create new arrays.
+         */
+        clone.isImmutable = false;
+        return clone;
+    }
+
+    /**
      * Compares this {@code Symbols} with the given object for equality.
      *
      * @param  other The object to compare with this {@code Symbols}.
@@ -661,10 +643,15 @@ public class Symbols implements Localize
     }
 
     /**
-     * Invoked on deserialization for recomputing the {@link #quote} field.
+     * Invoked on deserialization for replacing the deserialized instance by the constant
instance.
+     * This method also opportunistically recompute the {@link #quote} field if no replacement
is done.
      */
-    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException
{
-        in.defaultReadObject();
+    final Object readResolve() {
+        if (isImmutable) {
+            if (equals(SQUARE_BRACKETS)) return SQUARE_BRACKETS;
+            if (equals(CURLY_BRACKETS))  return CURLY_BRACKETS;
+        }
         quote = String.valueOf(Character.toChars(quotes[1]));
+        return this;
     }
 }

Modified: sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java?rev=1563691&r1=1563690&r2=1563691&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
[UTF-8] Sun Feb  2 18:56:28 2014
@@ -16,12 +16,17 @@
  */
 package org.apache.sis.io.wkt;
 
+import java.util.Date;
 import java.util.Locale;
 import java.util.TimeZone;
 import java.text.Format;
 import java.text.NumberFormat;
 import java.text.ParsePosition;
 import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import javax.measure.unit.Unit;
+import javax.measure.unit.UnitFormat;
 import org.opengis.metadata.citation.Citation;
 import org.opengis.parameter.GeneralParameterValue;
 import org.opengis.referencing.IdentifiedObject;
@@ -39,11 +44,11 @@ import org.apache.sis.util.resources.Err
  * {@code WKTFormat} objects allow the following configuration:
  *
  * <ul>
- *   <li>The {@linkplain Symbols symbols} to use (curly braces or brackets, <i>etc.</i>)</li>
+ *   <li>The {@linkplain Symbols symbols} to use (curly braces or brackets, <i>etc</i>).</li>
  *   <li>The preferred authority of {@linkplain IdentifiedObject#getName() object name}
to
- *       format (see {@link Formatter#getName(IdentifiedObject)} for more information)</li>
- *   <li>Whatever ANSI X3.64 colors are allowed or not (default is not)</li>
- *   <li>The indentation</li>
+ *       format (see {@link Formatter#getName(IdentifiedObject)} for more information).</li>
+ *   <li>Whatever ANSI X3.64 colors are allowed or not (default is not).</li>
+ *   <li>The indentation.</li>
  * </ul>
  *
  * {@section String expansion}
@@ -198,7 +203,7 @@ public class WKTFormat extends CompoundF
     public void setSymbols(final Symbols symbols) {
         ArgumentChecks.ensureNonNull("symbols", symbols);
         if (!symbols.equals(this.symbols)) {
-            this.symbols = symbols;
+            this.symbols = symbols.immutable();
             formatter = null;
         }
     }
@@ -333,19 +338,6 @@ public class WKTFormat extends CompoundF
     }
 
     /**
-     * Returns the formatter, creating it if needed.
-     */
-    private Formatter getFormatter() {
-        Formatter formatter = this.formatter;
-        if (formatter == null) {
-            formatter = new Formatter(symbols, (NumberFormat) createFormat(Number.class));
-            updateFormatter(formatter);
-            this.formatter = formatter;
-        }
-        return formatter;
-    }
-
-    /**
      * Formats the specified object as a Well Know Text. The given object shall be an instance
of one of
      * {@link FormattableObject}, {@link IdentifiedObject}, {@link MathTransform}, {@link
GeneralParameterValue}
      * or {@link Matrix}.
@@ -358,15 +350,30 @@ public class WKTFormat extends CompoundF
      */
     @Override
     public void format(final Object object, final Appendable toAppendTo) throws IOException
{
+        /*
+         * If the given Appendable is not a StringBuffer, creates a temporary StringBuffer.
+         * We can not write directly in an arbitrary Appendable because Formatter needs the
+         * ability to go backward ("append only" is not sufficient), and because it passes
+         * the buffer to other java.text.Format instances which work only with StringBuffer.
+         */
         final StringBuffer buffer;
         if (toAppendTo instanceof StringBuffer) {
             buffer = (StringBuffer) toAppendTo;
         } else {
             buffer = new StringBuffer(500);
         }
-        final Formatter formatter = getFormatter();
+        /*
+         * Creates the Formatter when first needed.
+         */
+        Formatter formatter = this.formatter;
+        if (formatter == null) {
+            formatter = new Formatter(symbols,
+                    (NumberFormat) getFormat(Number.class),
+                    (UnitFormat)   getFormat(Unit.class));
+            updateFormatter(formatter);
+            this.formatter = formatter;
+        }
         try {
-            formatter.clear();
             formatter.setBuffer(buffer);
             if (object instanceof FormattableObject) {
                 formatter.append((FormattableObject) object);
@@ -388,6 +395,7 @@ public class WKTFormat extends CompoundF
             }
         } finally {
             formatter.setBuffer(null);
+            formatter.clear();
         }
         if (buffer != toAppendTo) {
             toAppendTo.append(buffer);
@@ -403,7 +411,7 @@ public class WKTFormat extends CompoundF
      *   <tr><th>Value type</th>     <th>Format to create</th></tr>
      *   <tr><td>{@link Number}</td> <td>{@link NumberFormat}</td></tr>
      *   <tr><td>{@link Date}</td>   <td>{@link DateFormat}</td></tr>
-     *   <tr><td>{@link Angle}</td>  <td>{@link AngleFormat}</td></tr>
+     *   <tr><td>{@link Unit}</td>   <td>{@link UnitFormat}</td></tr>
      * </table>
      *
      * @param  valueType The base type of values to parse or format.
@@ -414,6 +422,14 @@ public class WKTFormat extends CompoundF
         if (valueType == Number.class) {
             return symbols.createNumberFormat();
         }
+        if (valueType == Unit.class) {
+            return UnitFormat.getInstance(symbols.getLocale());
+        }
+        if (valueType == Date.class) {
+            final DateFormat format = new SimpleDateFormat("yyyy-MM-dd", symbols.getLocale());
+            format.setTimeZone(getTimeZone());
+            return format;
+        }
         return super.createFormat(valueType);
     }
 

Added: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ColorsTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ColorsTest.java?rev=1563691&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ColorsTest.java
(added)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ColorsTest.java
[UTF-8] Sun Feb  2 18:56:28 2014
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.io.wkt;
+
+import org.apache.sis.test.TestCase;
+import org.apache.sis.test.DependsOnMethod;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+
+
+/**
+ * Tests the {@link Colors} class.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+public final strictfp class ColorsTest extends TestCase {
+    /**
+     * Tests {@link Colors#getName(ElementKind)}.
+     */
+    @Test
+    public void testGetName() {
+        final Colors colors = Colors.CONSOLE;
+        assertEquals("cyan",  colors.getName(ElementKind.CODE_LIST));
+        assertEquals("green", colors.getName(ElementKind.DATUM));
+        assertEquals("red",   colors.getName(ElementKind.ERROR));
+    }
+
+    /**
+     * Tests {@link Colors#setName(ElementKind, String)}.
+     */
+    @Test
+    @DependsOnMethod("testGetName")
+    public void testSetName() {
+        final Colors colors = new Colors(Colors.CONSOLE);
+        assertEquals("green", colors.getName(ElementKind.DATUM));
+        colors.setName(ElementKind.DATUM, "blue");
+        assertEquals("blue", colors.getName(ElementKind.DATUM));
+    }
+
+    /**
+     * Ensures that the static constant is immutable.
+     */
+    @Test
+    public void testImmutability() {
+        try {
+            Colors.CONSOLE.setName(ElementKind.DATUM, "blue");
+            fail("Constant shall be immutable.");
+        } catch (UnsupportedOperationException e) {
+            // This is the expected exception.
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("Colors"));
+        }
+    }
+
+    /**
+     * Tests {@link Colors} serialization.
+     */
+    @Test
+    public void testSerialization() {
+        assertSame(Colors.CONSOLE, assertSerializedEquals(Colors.CONSOLE));
+        final Colors colors = new Colors(Colors.CONSOLE);
+        colors.setName(ElementKind.DATUM, "blue");
+        final Colors c = assertSerializedEquals(colors);
+        assertNotSame(colors, c); // Expect a new instance.
+        assertEquals("blue", c.getName(ElementKind.DATUM));
+    }
+}

Propchange: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ColorsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/ColorsTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/FormatterTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/FormatterTest.java?rev=1563691&r1=1563690&r2=1563691&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/FormatterTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/FormatterTest.java
[UTF-8] Sun Feb  2 18:56:28 2014
@@ -37,7 +37,7 @@ import static org.apache.sis.referencing
  * @version 0.4
  * @module
  */
-@DependsOn({ConventionTest.class, SymbolsTest.class})
+@DependsOn({ConventionTest.class, SymbolsTest.class, ColorsTest.class})
 public final strictfp class FormatterTest extends TestCase {
     /**
      * Verifies the ANSI escape sequences hard-coded in {@link Formatter}.

Modified: sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java?rev=1563691&r1=1563690&r2=1563691&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/test/suite/ReferencingTestSuite.java
[UTF-8] Sun Feb  2 18:56:28 2014
@@ -47,6 +47,7 @@ import org.junit.BeforeClass;
     org.apache.sis.internal.referencing.ReferencingUtilitiesTest.class,
     org.apache.sis.io.wkt.ConventionTest.class,
     org.apache.sis.io.wkt.SymbolsTest.class,
+    org.apache.sis.io.wkt.ColorsTest.class,
     org.apache.sis.io.wkt.FormatterTest.class,
     org.apache.sis.internal.jaxb.referencing.CodeTest.class,
     org.apache.sis.internal.jaxb.referencing.SecondDefiningParameterTest.class,



Mime
View raw message