Fixes Style/Script references to preserve module declaration order in Linker.

Review by: bobv (TBR)


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@2865 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/core/ext/linker/ArtifactSet.java b/dev/core/src/com/google/gwt/core/ext/linker/ArtifactSet.java
index 747899f..c04ad6d 100644
--- a/dev/core/src/com/google/gwt/core/ext/linker/ArtifactSet.java
+++ b/dev/core/src/com/google/gwt/core/ext/linker/ArtifactSet.java
@@ -87,7 +87,7 @@
    */
   public <A extends Artifact<?>, T extends A> SortedSet<A> find(
       Class<T> artifactType) {
-    // TODO make this sub-linear
+    // TODO make this sub-linear (but must retain order for styles/scripts!)
     SortedSet<A> toReturn = new TreeSet<A>();
     for (Artifact<?> artifact : this) {
       if (artifactType.isInstance(artifact)) {
diff --git a/dev/core/src/com/google/gwt/core/ext/linker/ScriptReference.java b/dev/core/src/com/google/gwt/core/ext/linker/ScriptReference.java
index 789397f..1e28710 100644
--- a/dev/core/src/com/google/gwt/core/ext/linker/ScriptReference.java
+++ b/dev/core/src/com/google/gwt/core/ext/linker/ScriptReference.java
@@ -18,14 +18,18 @@
 import com.google.gwt.core.ext.Linker;
 
 /**
- * An external script file referenced in the module manifest.
+ * An external script file referenced in the module manifest. The index is
+ * important because output order must match module declaration order.
  */
 public abstract class ScriptReference extends Artifact<ScriptReference> {
+  private final int index;
   private final String src;
 
-  protected ScriptReference(Class<? extends Linker> linkerType, String src) {
+  protected ScriptReference(Class<? extends Linker> linkerType, String src,
+      int index) {
     super(linkerType);
     this.src = src;
+    this.index = index;
   }
 
   /**
@@ -48,7 +52,7 @@
 
   @Override
   protected final int compareToComparableArtifact(ScriptReference o) {
-    return getSrc().compareTo(o.getSrc());
+    return index - o.index;
   }
 
   @Override
diff --git a/dev/core/src/com/google/gwt/core/ext/linker/StylesheetReference.java b/dev/core/src/com/google/gwt/core/ext/linker/StylesheetReference.java
index a35b4ac..6d3aeb0 100644
--- a/dev/core/src/com/google/gwt/core/ext/linker/StylesheetReference.java
+++ b/dev/core/src/com/google/gwt/core/ext/linker/StylesheetReference.java
@@ -18,14 +18,18 @@
 import com.google.gwt.core.ext.Linker;
 
 /**
- * An external stylesheet referenced in the module manifest.
+ * An external stylesheet referenced in the module manifest. The index is
+ * important because output order must match module declaration order.
  */
 public abstract class StylesheetReference extends Artifact<StylesheetReference> {
   private final String src;
+  private final int index;
 
-  protected StylesheetReference(Class<? extends Linker> linkerType, String src) {
+  protected StylesheetReference(Class<? extends Linker> linkerType, String src,
+      int index) {
     super(linkerType);
     this.src = src;
+    this.index = index;
   }
 
   /**
@@ -48,7 +52,7 @@
 
   @Override
   protected final int compareToComparableArtifact(StylesheetReference o) {
-    return getSrc().compareTo(o.getSrc());
+    return index - o.index;
   }
 
   @Override
diff --git a/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardLinkerContext.java b/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardLinkerContext.java
index a6dee8e..eff8325 100644
--- a/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardLinkerContext.java
+++ b/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardLinkerContext.java
@@ -175,14 +175,20 @@
     }
     properties = Collections.unmodifiableSortedSet(mutableProperties);
 
-    for (Script script : module.getScripts()) {
-      artifacts.add(new StandardScriptReference(script.getSrc()));
-      logger.log(TreeLogger.SPAM, "Added script " + script.getSrc(), null);
+    {
+      int index = 0;
+      for (Script script : module.getScripts()) {
+        artifacts.add(new StandardScriptReference(script.getSrc(), index++));
+        logger.log(TreeLogger.SPAM, "Added script " + script.getSrc(), null);
+      }
     }
 
-    for (String style : module.getStyles()) {
-      artifacts.add(new StandardStylesheetReference(style));
-      logger.log(TreeLogger.SPAM, "Added style " + style, null);
+    {
+      int index = 0;
+      for (String style : module.getStyles()) {
+        artifacts.add(new StandardStylesheetReference(style, index++));
+        logger.log(TreeLogger.SPAM, "Added style " + style, null);
+      }
     }
 
     // Generated files should be passed in via addArtifacts()
diff --git a/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardScriptReference.java b/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardScriptReference.java
index 79cc00a..456d381 100644
--- a/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardScriptReference.java
+++ b/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardScriptReference.java
@@ -22,7 +22,7 @@
  */
 public class StandardScriptReference extends ScriptReference {
 
-  public StandardScriptReference(String src) {
-    super(StandardLinkerContext.class, src);
+  public StandardScriptReference(String src, int index) {
+    super(StandardLinkerContext.class, src, index);
   }
 }
diff --git a/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardStylesheetReference.java b/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardStylesheetReference.java
index 49211fa..3534663 100644
--- a/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardStylesheetReference.java
+++ b/dev/core/src/com/google/gwt/core/ext/linker/impl/StandardStylesheetReference.java
@@ -22,7 +22,7 @@
  */
 public class StandardStylesheetReference extends StylesheetReference {
 
-  public StandardStylesheetReference(String src) {
-    super(StandardLinkerContext.class, src);
+  public StandardStylesheetReference(String src, int index) {
+    super(StandardLinkerContext.class, src, index);
   }
 }
diff --git a/dev/core/test/com/google/gwt/core/ext/linker/ArtifactSetTest.java b/dev/core/test/com/google/gwt/core/ext/linker/ArtifactSetTest.java
new file mode 100644
index 0000000..0121abf
--- /dev/null
+++ b/dev/core/test/com/google/gwt/core/ext/linker/ArtifactSetTest.java
@@ -0,0 +1,109 @@
+/*
+ * Copyright 2008 Google Inc.
+ * 
+ * Licensed 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 com.google.gwt.core.ext.linker;
+
+import com.google.gwt.core.ext.linker.impl.StandardScriptReference;
+import com.google.gwt.core.ext.linker.impl.StandardStylesheetReference;
+
+import junit.framework.TestCase;
+
+import java.util.SortedSet;
+
+/**
+ * Tests for {@link ArtifactSet}.
+ */
+public class ArtifactSetTest extends TestCase {
+
+  public void testScriptOrder() {
+    StandardScriptReference fooScript = new StandardScriptReference("foo", 0);
+    StandardScriptReference barScript = new StandardScriptReference("bar", 1);
+    assertTrue(fooScript.compareTo(barScript) < 0);
+    assertTrue(barScript.compareTo(fooScript) > 0);
+    assertTrue(fooScript.compareTo(fooScript) == 0);
+    assertTrue(barScript.compareTo(barScript) == 0);
+
+    {
+      ArtifactSet set = new ArtifactSet();
+      // Add in order.
+      set.add(fooScript);
+      set.add(barScript);
+      assertEquals(2, set.size());
+      assertSame(fooScript, set.first());
+      assertSame(barScript, set.last());
+
+      SortedSet<StandardScriptReference> found = set.find(StandardScriptReference.class);
+      assertEquals(2, found.size());
+      assertSame(fooScript, found.first());
+      assertSame(barScript, found.last());
+    }
+    {
+      ArtifactSet set = new ArtifactSet();
+      // Reversed add order.
+      set.add(barScript);
+      set.add(fooScript);
+      assertEquals(2, set.size());
+      assertSame(fooScript, set.first());
+      assertSame(barScript, set.last());
+
+      SortedSet<StandardScriptReference> found = set.find(StandardScriptReference.class);
+      assertEquals(2, found.size());
+      assertSame(fooScript, found.first());
+      assertSame(barScript, found.last());
+    }
+
+  }
+
+  public void testStyleOrder() {
+    StandardStylesheetReference fooStyle = new StandardStylesheetReference(
+        "foo", 0);
+    StandardStylesheetReference barStyle = new StandardStylesheetReference(
+        "bar", 1);
+    assertTrue(fooStyle.compareTo(barStyle) < 0);
+    assertTrue(barStyle.compareTo(fooStyle) > 0);
+    assertTrue(fooStyle.compareTo(fooStyle) == 0);
+    assertTrue(barStyle.compareTo(barStyle) == 0);
+
+    {
+      ArtifactSet set = new ArtifactSet();
+      // Add in order.
+      set.add(fooStyle);
+      set.add(barStyle);
+      assertEquals(2, set.size());
+      assertSame(fooStyle, set.first());
+      assertSame(barStyle, set.last());
+
+      SortedSet<StandardStylesheetReference> found = set.find(StandardStylesheetReference.class);
+      assertEquals(2, found.size());
+      assertSame(fooStyle, found.first());
+      assertSame(barStyle, found.last());
+    }
+    {
+      ArtifactSet set = new ArtifactSet();
+      // Reversed add order.
+      set.add(barStyle);
+      set.add(fooStyle);
+      assertEquals(2, set.size());
+      assertSame(fooStyle, set.first());
+      assertSame(barStyle, set.last());
+
+      SortedSet<StandardStylesheetReference> found = set.find(StandardStylesheetReference.class);
+      assertEquals(2, found.size());
+      assertSame(fooStyle, found.first());
+      assertSame(barStyle, found.last());
+    }
+
+  }
+}