diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/SpotInstanceHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/SpotInstanceHandler.java index 53987849af..c4032fcfa1 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/SpotInstanceHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/SpotInstanceHandler.java @@ -20,8 +20,6 @@ package org.jclouds.aws.ec2.xml; import static org.jclouds.util.SaxUtils.*; -import java.util.Map; - import javax.inject.Inject; import org.jclouds.aws.ec2.domain.SpotInstanceRequest; diff --git a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/TagSetHandler.java b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/TagSetHandler.java index 82f55e47de..df91616a9b 100644 --- a/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/TagSetHandler.java +++ b/providers/aws-ec2/src/main/java/org/jclouds/aws/ec2/xml/TagSetHandler.java @@ -18,6 +18,8 @@ */ package org.jclouds.aws.ec2.xml; +import static org.jclouds.util.SaxUtils.*; + import java.util.Map; import org.jclouds.http.functions.ParseSax; @@ -40,7 +42,6 @@ public class TagSetHandler extends ParseSax.HandlerForGeneratedRequestWithResult public TagSetHandler() { super(); - this.result = ImmutableMap.builder(); } public Map getResult() { @@ -49,7 +50,9 @@ public class TagSetHandler extends ParseSax.HandlerForGeneratedRequestWithResult @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { - if (qName.equals("item")) { + if (equalsOrSuffix(qName, "tagSet")) { + result = ImmutableMap.builder(); + } else if (qName.equals("item")) { inItem = true; key = null; value = null; @@ -67,9 +70,9 @@ public class TagSetHandler extends ParseSax.HandlerForGeneratedRequestWithResult } if (inItem) { if (qName.equals("key")) { - key = currentText.toString().trim(); + key = currentOrNull(currentText); } else if (qName.equals("value")) { - value = currentText.toString().trim(); + value = currentOrNull(currentText); } } } diff --git a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java index 50c1facd8b..4bcbc54b80 100644 --- a/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java +++ b/providers/aws-ec2/src/test/java/org/jclouds/aws/ec2/xml/SpotInstancesHandlerTest.java @@ -18,12 +18,12 @@ */ package org.jclouds.aws.ec2.xml; -import static org.easymock.EasyMock.expect; -import static org.easymock.classextension.EasyMock.createMock; -import static org.easymock.classextension.EasyMock.replay; -import static org.testng.Assert.assertEquals; +import static org.easymock.EasyMock.*; +import static org.easymock.classextension.EasyMock.*; +import static org.testng.Assert.*; import java.io.InputStream; +import java.util.Map; import java.util.Set; import org.jclouds.aws.ec2.domain.SpotInstanceRequest; @@ -36,7 +36,12 @@ import org.jclouds.rest.internal.GeneratedHttpRequest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; +import com.google.common.base.Function; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import com.google.common.collect.Maps; import com.google.inject.AbstractModule; import com.google.inject.Guice; @@ -85,6 +90,27 @@ public class SpotInstancesHandlerTest extends BaseEC2HandlerTest { assertEquals(result.size(), 3); } + public void testParseTags() { + Set result = factory.create(injector.getInstance(SpotInstancesHandler.class)).parse( + getClass().getResourceAsStream("/describe_spot_instance_tags.xml")); + Iterable> tags = Iterables.transform(result, new Function>() { + @Override + public Map apply(SpotInstanceRequest input) { + return input.getTags(); + } + }); + + assertEquals( + Iterables.get(tags, 0), + ImmutableMap.of("One", "one", "Two", "one")); + assertEquals( + Iterables.get(tags, 1), + ImmutableMap.of("One", "two", "Two", "two")); + assertEquals( + Iterables.get(tags, 2), + ImmutableMap.of("Two", "three", "Three", "three", "Four", "")); + } + public void testParseNoNPE() { factory.create(injector.getInstance(SpotInstancesHandler.class)).parse( getClass().getResourceAsStream("/describe_spot_instances_1.xml")); diff --git a/providers/aws-ec2/src/test/resources/describe_spot_instance_tags.xml b/providers/aws-ec2/src/test/resources/describe_spot_instance_tags.xml new file mode 100644 index 0000000000..8c1a22e484 --- /dev/null +++ b/providers/aws-ec2/src/test/resources/describe_spot_instance_tags.xml @@ -0,0 +1,115 @@ + + + f2247378-7df0-4725-b55f-8ef58b557dcd + + + sir-f4d44212 + 0.300000 + one-time + cancelled + + ami-8e1fece7 + ec2-o + + + jclouds#ec2-o#us-east-1 + + + ec2-o + + + t1.micro + + + true + + + 2011-07-29T04:01:12.000Z + Linux/UNIX + + + One + one + + + Two + one + + + us-east-1b + + + sir-43408412 + 0.300000 + one-time + cancelled + + ami-8e1fece7 + jclouds#adriancole-ec2unssh + + + jclouds#adriancole-ec2unssh#us-east-1 + + + t1.micro + + + false + + + 2011-07-29T04:02:53.000Z + Linux/UNIX + + + One + two + + + Two + two + + + us-east-1b + + + sir-e4c3fa14 + 0.300000 + one-time + closed + + ami-8e1fece7 + ec2-o + + + jclouds#ec2-o#us-east-1 + + + ec2-o + + + t1.micro + + + true + + + 2011-07-29T05:25:49.000Z + Linux/UNIX + + + Two + three + + + Three + three + + + Four + + + + us-east-1c + + + \ No newline at end of file