Author: justin Date: Tue Feb 23 19:00:50 2010 New Revision: 915489 URL: http://svn.apache.org/viewvc?rev=915489&view=rev Log: SLING-1407 - creating NameValuePairList utility class and adding method to SlingIntegrationTestClient. This created one ambiguous method call in PostRedirectTest.
Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePair.java sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePairList.java Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/PostRedirectTest.java Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePair.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePair.java?rev=915489&view=auto ============================================================================== --- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePair.java (added) +++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePair.java Tue Feb 23 19:00:50 2010 @@ -0,0 +1,59 @@ +/* + * 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.sling.commons.testing.integration; + + +/** + * A basic name-value pair class. + */ +public class NameValuePair { + + public NameValuePair() { + this(null, null); + } + + public NameValuePair(String name, String value) { + this.name = name; + this.value = value; + } + + private String name = null; + + private String value = null; + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setValue(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + @Override + public String toString() { + return String.format("name=%s, value=%s", name, value); + } + +} Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePairList.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePairList.java?rev=915489&view=auto ============================================================================== --- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePairList.java (added) +++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/NameValuePairList.java Tue Feb 23 19:00:50 2010 @@ -0,0 +1,101 @@ +/* + * 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.sling.commons.testing.integration; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.Map; + +/** + * A list of name-value pairs. + */ +public class NameValuePairList implements Iterable<NameValuePair> { + + private final List<NameValuePair> delegate; + + public NameValuePairList() { + delegate = new ArrayList<NameValuePair>(); + } + + public NameValuePairList(List<NameValuePair> init) { + delegate = new ArrayList<NameValuePair>(init); + } + + public NameValuePairList(NameValuePairList clientNodeProperties) { + this(clientNodeProperties.delegate); + } + + public NameValuePairList(Map<String, String> clientNodeProperties) { + this(); + for (Map.Entry<String,String> e : clientNodeProperties.entrySet()) { + add(e.getKey(), e.getValue()); + } + } + + public void add(String name, String value) { + delegate.add(new NameValuePair(name, value)); + } + + public void addIfNew(String name, String value) { + boolean found = false; + for (ListIterator<NameValuePair> li = delegate.listIterator(); li.hasNext();) { + NameValuePair current = li.next(); + if (current.getName().equals(name)) { + found = true; + break; + } + } + + if (!found) { + delegate.add(new NameValuePair(name, value)); + } + + } + + public void addOrReplace(String name, String value) { + boolean replaced = false; + for (ListIterator<NameValuePair> li = delegate.listIterator(); li.hasNext();) { + NameValuePair current = li.next(); + if (current.getName().equals(name)) { + if (!replaced) { + current.setValue(value); + replaced = true; + } else { + li.remove(); + } + } + } + + if (!replaced) { + delegate.add(new NameValuePair(name, value)); + } + } + + public void clear() { + delegate.clear(); + } + + public Iterator<NameValuePair> iterator() { + return delegate.iterator(); + } + + public int size() { + return delegate.size(); + } +} Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java?rev=915489&r1=915488&r2=915489&view=diff ============================================================================== --- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java (original) +++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/SlingIntegrationTestClient.java Tue Feb 23 19:00:50 2010 @@ -107,43 +107,48 @@ */ public String createNode(String url, Map<String,String> clientNodeProperties, Map<String,String> requestHeaders,boolean multiPart) throws IOException { + return createNode(url, new NameValuePairList(clientNodeProperties), requestHeaders, multiPart); + } + + /** Create a node under given path, using a POST to Sling + * @param url under which node is created + * @param multiPart if true, does a multipart POST + * @return the URL that Sling provides to display the node + */ + public String createNode(String url, NameValuePairList clientNodeProperties, Map<String,String> requestHeaders, boolean multiPart) + throws IOException { final PostMethod post = new PostMethod(url); post.setFollowRedirects(false); // create a private copy of the properties to not tamper with // the properties of the client - Map<String, String> nodeProperties = new HashMap<String, String>(); + NameValuePairList nodeProperties = new NameValuePairList(clientNodeProperties); // add sling specific properties - nodeProperties.put(":redirect", "*"); - nodeProperties.put(":displayExtension", ""); - nodeProperties.put(":status", "browser"); - - // take over any client provided properties - if (clientNodeProperties != null) { - nodeProperties.putAll(clientNodeProperties); - } else { - // add fake property - otherwise the node is not created - nodeProperties.put("jcr:created", ""); - } + nodeProperties.addOrReplace(":redirect", "*"); + nodeProperties.addOrReplace(":displayExtension", ""); + nodeProperties.addOrReplace(":status", "browser"); + + // add fake property - otherwise the node is not created + nodeProperties.addIfNew("jcr:created", ""); // force form encoding to UTF-8, which is what we use to convert the // string parts into stream data - nodeProperties.put("_charset_", "UTF-8"); + nodeProperties.addOrReplace("_charset_", "UTF-8"); if( nodeProperties.size() > 0) { if(multiPart) { final List<Part> partList = new ArrayList<Part>(); - for(Map.Entry<String,String> e : nodeProperties.entrySet()) { + for(NameValuePair e : nodeProperties) { if (e.getValue() != null) { - partList.add(new StringPart(e.getKey().toString(), e.getValue().toString(), "UTF-8")); + partList.add(new StringPart(e.getName(), e.getValue(), "UTF-8")); } } final Part [] parts = partList.toArray(new Part[partList.size()]); post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); } else { - for(Map.Entry<String,String> e : nodeProperties.entrySet()) { - post.addParameter(e.getKey(),e.getValue()); + for(NameValuePair e : nodeProperties) { + post.addParameter(e.getName(),e.getValue()); } } } @@ -204,7 +209,7 @@ partsList.add(typeHintPart); } } - + final Part[] parts = partsList.toArray(new Part[partsList.size()]); final PostMethod post = new PostMethod(url); post.setFollowRedirects(false); Modified: sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/PostRedirectTest.java URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/PostRedirectTest.java?rev=915489&r1=915488&r2=915489&view=diff ============================================================================== --- sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/PostRedirectTest.java (original) +++ sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/PostRedirectTest.java Tue Feb 23 19:00:50 2010 @@ -47,8 +47,7 @@ public void testDefaultRedirect() throws IOException { final Map<String, String> params = new HashMap<String, String>(); params.put(":redirect", null); - final String location = testClient.createNode(postUrl, null, null, - false); + final String location = testClient.createNode(postUrl, null); assertTrue("With no headers or parameters, redirect (" + location + ") must point to created node (path=" + postPath + ")", location.contains(postPath));