Author: michael
Date: Sat Feb 25 15:55:48 2012
New Revision: 1293628

URL: http://svn.apache.org/viewvc?rev=1293628&view=rev
Log:
Implementation for WAVE-317.
Code Review: https://reviews.apache.org/r/3882/

Added:
    
incubator/wave/trunk/test/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantControllerTest.java
Modified:
    
incubator/wave/trunk/src/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantController.java

Modified: 
incubator/wave/trunk/src/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantController.java
URL: 
http://svn.apache.org/viewvc/incubator/wave/trunk/src/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantController.java?rev=1293628&r1=1293627&r2=1293628&view=diff
==============================================================================
--- 
incubator/wave/trunk/src/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantController.java
 (original)
+++ 
incubator/wave/trunk/src/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantController.java
 Sat Feb 25 15:55:48 2012
@@ -16,6 +16,8 @@
  */
 package org.waveprotocol.wave.client.wavepanel.impl.edit;
 
+import javax.annotation.Nullable;
+
 import com.google.gwt.dom.client.Element;
 import com.google.gwt.event.dom.client.ClickEvent;
 import com.google.gwt.event.dom.client.ClickHandler;
@@ -37,6 +39,7 @@ import org.waveprotocol.wave.client.widg
 import org.waveprotocol.wave.client.widget.profile.ProfilePopupView;
 import org.waveprotocol.wave.model.conversation.Conversation;
 import org.waveprotocol.wave.model.util.Pair;
+import org.waveprotocol.wave.model.util.Preconditions;
 import org.waveprotocol.wave.model.wave.InvalidParticipantAddress;
 import org.waveprotocol.wave.model.wave.ParticipantId;
 
@@ -90,35 +93,65 @@ public final class ParticipantController
   }
 
   /**
+   * Constructs a list of {@link ParticipantId} with the supplied string with 
comma
+   * separated participant addresses. The method will only succeed if all 
addresses
+   * is valid.
+   *
+   * @param localDomain if provided, automatic suffixing will occur.
+   * @param addresses string with comma separated participant addresses
+   * @return the array of {@link ParticipantId} instances constructed using 
the given
+   *         addresses string
+   * @throws InvalidParticipantAddress if at least one of the addresses failed 
validation.
+   */
+  public static ParticipantId[] buildParticipantList(
+      @Nullable String localDomain, String addresses) throws 
InvalidParticipantAddress {
+    Preconditions.checkNotNull(addresses, "Expected non-null address");
+
+    String[] addressList = addresses.split(",");
+    ParticipantId[] participants = new ParticipantId[addressList.length];
+
+    for (int i = 0; i < addressList.length; i++) {
+      String address = addressList[i].trim();
+
+      if (localDomain != null) {
+        if (!address.isEmpty() && address.indexOf("@") == -1) {
+          // If no domain was specified, assume that the participant is from 
the local domain.
+          address = address + "@" + localDomain;
+        } else if (address.equals("@")) {
+          // "@" is a shortcut for the shared domain participant.
+          address = address + localDomain;
+        }
+      }
+
+      // Will throw InvalidParticipantAddress if address is not valid
+      participants[i] = ParticipantId.of(address);
+    }
+    return participants;
+  }
+
+  /**
    * Shows an add-participant popup.
    */
   private void handleAddButtonClicked(Element context) {
-    ParticipantId p;
-    String address = Window.prompt("Add a participant: ", "");
-    if (address == null) {
+    String addressString = Window.prompt("Add a participant(s) (separate with 
comma ','): ", "");
+    if (addressString == null) {
       return;
     }
-    address = address.trim();
-    if (localDomain != null) {
-      if (!address.isEmpty() &&  address.indexOf("@") == -1) {
-        // If no domain was specified, assume that the participant is from the 
local domain.
-        address = address + "@" + localDomain;
-      } else if (address.equals("@")) {
-        // "@" is a shortcut for the shared domain participant.
-        address = address + localDomain;
-      }
-    }
+
+    ParticipantId[] participants;
 
     try {
-      p = ParticipantId.of(address);
+      participants = buildParticipantList(localDomain, addressString);
     } catch (InvalidParticipantAddress e) {
-      Window.alert("Invalid address: " + address);
+      Window.alert(e.getMessage());
       return;
     }
 
     ParticipantsView participantsUi = views.fromAddButton(context);
     Conversation conversation = models.getParticipants(participantsUi);
-    conversation.addParticipant(p);
+    for (ParticipantId participant : participants) {
+      conversation.addParticipant(participant);
+    }
   }
 
   /**

Added: 
incubator/wave/trunk/test/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantControllerTest.java
URL: 
http://svn.apache.org/viewvc/incubator/wave/trunk/test/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantControllerTest.java?rev=1293628&view=auto
==============================================================================
--- 
incubator/wave/trunk/test/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantControllerTest.java
 (added)
+++ 
incubator/wave/trunk/test/org/waveprotocol/wave/client/wavepanel/impl/edit/ParticipantControllerTest.java
 Sat Feb 25 15:55:48 2012
@@ -0,0 +1,144 @@
+/**
+ * Copyright 2012 Apache Wave
+ *
+ * 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 org.waveprotocol.wave.client.wavepanel.impl.edit;
+
+import junit.framework.TestCase;
+
+import org.waveprotocol.wave.model.wave.InvalidParticipantAddress;
+import org.waveprotocol.wave.model.wave.ParticipantId;
+
+/**
+ * Tests for the {@link ParticipantController} class.
+ *
+ */
+public class ParticipantControllerTest extends TestCase {
+
+  // Valid
+  private static final String DOMAIN_ONLY_ADDRESS = "@example.com";
+  private static final String TYPICAL_ADDRESS = "[email protected]";
+
+  // Invalid.
+  private static final String EMPTY_ADDRESS = "";
+  private static final String NO_DOMAIN_PREFIX = "test";
+  private static final String NO_DOMAIN_ADDRESS = "test@";
+  private static final String PREFIX_ONLY_ADDRESS = "@";
+
+  /**
+  * Tests for ParticipantController.buildParticipantList() method that creates 
a participant
+  * list from a comma separated string
+  */
+  public void testOneValidAddressInAddressList() throws Exception {
+    assertAddressListValid(TYPICAL_ADDRESS);
+    assertAddressListValid(DOMAIN_ONLY_ADDRESS);
+    assertLocalAddressListValid(NO_DOMAIN_PREFIX);
+  }
+
+  public void testOneInvalidAddressInAddressList() throws Exception {
+    assertAddressListInvalid(EMPTY_ADDRESS);
+    assertAddressListInvalid(NO_DOMAIN_PREFIX);
+    assertAddressListInvalid(NO_DOMAIN_ADDRESS);
+    assertAddressListInvalid(PREFIX_ONLY_ADDRESS);
+
+    try {
+      ParticipantController.buildParticipantList(null, null);
+      fail("Expected NullPointerException");
+    } catch (NullPointerException e) {
+      // Expected.
+    }
+  }
+
+  public void testMultipleVaildAddresses()  throws Exception {
+    String stringOne = "[email protected]";
+    String stringTwo = "[email protected]";
+    String stringThree = "[email protected]";
+    ParticipantId idOne = ParticipantId.ofUnsafe(stringOne);
+    ParticipantId idTwo = ParticipantId.ofUnsafe(stringTwo);
+    ParticipantId idThree = ParticipantId.ofUnsafe(stringThree);
+
+    ParticipantId[] participants = ParticipantController.buildParticipantList(
+        null, stringOne + "," + stringTwo + "," + stringThree);
+
+    assertEquals("Wrong number of participants created", 3, 
participants.length);
+    assertEquals("Participant one not the same", idOne, participants[0]);
+    assertEquals("Participant two not the same", idTwo, participants[1]);
+    assertEquals("Participant three not the same", idThree, participants[2]);
+  }
+
+  public void testMultipleLocalVaildAddresses() throws Exception {
+    String stringOne = "test1";
+    String stringTwo = "test2";
+    String stringThree = "test3";
+    ParticipantId idOne = ParticipantId.ofUnsafe(stringOne + "@localhost");
+    ParticipantId idTwo = ParticipantId.ofUnsafe(stringTwo + "@localhost");
+    ParticipantId idThree = ParticipantId.ofUnsafe(stringThree + "@localhost");
+
+    ParticipantId[] participants = ParticipantController.buildParticipantList(
+        "localhost", stringOne + "," + stringTwo + "," + stringThree);
+
+    assertEquals("Wrong number of participants created", 3, 
participants.length);
+    assertEquals("Participant one not the same", idOne, participants[0]);
+    assertEquals("Participant two not the same", idTwo, participants[1]);
+    assertEquals("Participant three not the same", idThree, participants[2]);
+  }
+
+  public void testMultipleAddressesWithOneInvalid() {
+    assertAddressListInvalid(NO_DOMAIN_ADDRESS + "," + TYPICAL_ADDRESS);
+    assertAddressListInvalid(NO_DOMAIN_PREFIX + "," + TYPICAL_ADDRESS);
+    assertAddressListInvalid(TYPICAL_ADDRESS + "," + NO_DOMAIN_PREFIX + "," + 
TYPICAL_ADDRESS);
+    assertLocalAddressListInvalid(TYPICAL_ADDRESS + "," + NO_DOMAIN_PREFIX + 
PREFIX_ONLY_ADDRESS);
+  }
+
+  /**
+   * Checks that a comma separated address list is valid (by throwing an 
exception if it is not)
+   */
+  private static void assertAddressListValid(String addresses) throws 
InvalidParticipantAddress {
+    ParticipantController.buildParticipantList(null, addresses);
+  }
+
+  /**
+   * Checks that a comma separated address list is valid (by throwing an 
exception if it is not)
+   * when local domain is provided
+   */
+  private static void assertLocalAddressListValid(String addresses) throws 
InvalidParticipantAddress {
+    ParticipantController.buildParticipantList("localhost", addresses);
+  }
+
+  /**
+   * Checks that an comma separated address list is not valid.
+   */
+  private static void assertAddressListInvalid(String addresses) {
+    try {
+      ParticipantController.buildParticipantList(null, addresses);
+      fail("Expected InvalidParticipantAddress Exception");
+    } catch (InvalidParticipantAddress e) {
+      // Expected.
+    }
+  }
+
+  /**
+   * Checks that an comma separated address list is not valid when local domain
+   * is provided
+   */
+  private static void assertLocalAddressListInvalid(String addresses) {
+    try {
+      ParticipantController.buildParticipantList("localhost", addresses);
+      fail("Expected InvalidParticipantAddress Exception");
+    } catch (InvalidParticipantAddress e) {
+      // Expected.
+    }
+  }
+}


Reply via email to