001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.forms;
021
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.Blob;
029import org.nuxeo.ecm.core.api.DocumentModel;
030
031/**
032 * Form instance to be used in unit tests.
033 *
034 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
035 */
036public class TestFormInstance implements FormInstance {
037
038    protected final Map<String, String[]> params;
039
040    protected final Map<String, Blob[]> blobs;
041
042    public TestFormInstance(Map<String, String[]> params, Map<String, Blob[]> blobs) {
043        if (params == null) {
044            this.params = new HashMap<String, String[]>();
045        } else {
046            this.params = params;
047        }
048        if (blobs == null) {
049            this.blobs = new HashMap<String, Blob[]>();
050        } else {
051            this.blobs = blobs;
052        }
053    }
054
055    public TestFormInstance() {
056        this(null, null);
057    }
058
059    public TestFormInstance(Map<String, String[]> params) {
060        this(params, null);
061    }
062
063    public void setField(String key, String... values) {
064        params.put(key, values);
065    }
066
067    public void addField(String key, String... values) {
068        String[] ar = params.get(key);
069        if (ar == null) {
070            params.put(key, values);
071        } else {
072            String[] tmp = new String[ar.length + values.length];
073            System.arraycopy(ar, 0, tmp, 0, ar.length);
074            System.arraycopy(values, 0, tmp, ar.length, values.length);
075            params.put(key, tmp);
076        }
077    }
078
079    public void setField(String key, Blob... values) {
080        blobs.put(key, values);
081    }
082
083    public void addField(String key, Blob... values) {
084        Blob[] ar = blobs.get(key);
085        if (blobs == null) {
086            blobs.put(key, values);
087        } else {
088            Blob[] tmp = new Blob[ar.length + values.length];
089            System.arraycopy(ar, 0, tmp, 0, ar.length);
090            System.arraycopy(values, 0, tmp, ar.length, values.length);
091            blobs.put(key, tmp);
092        }
093    }
094
095    public Collection<String> getKeys() {
096        List<String> result = new ArrayList<String>();
097        result.addAll(params.keySet());
098        result.addAll(blobs.keySet());
099        return result;
100    }
101
102    /**
103     * TODO XXX implement it
104     */
105    public void fillDocument(DocumentModel doc) {
106        throw new UnsupportedOperationException("Not yet implemented");
107    }
108
109    public Object[] get(String key) {
110        Object[] val = params.get(key);
111        if (val == null) {
112            val = blobs.get(key);
113        }
114        return val;
115    }
116
117    public Blob getBlob(String key) {
118        Blob[] blobs = this.blobs.get(key);
119        return blobs == null ? null : blobs[0];
120    }
121
122    public Blob[] getBlobs(String key) {
123        return blobs.get(key);
124    }
125
126    public Map<String, Blob[]> getBlobFields() {
127        return blobs;
128    }
129
130    public Map<String, String[]> getFormFields() {
131        return params;
132    }
133
134    public String[] getList(String key) {
135        return params.get(key);
136    }
137
138    public String getString(String key) {
139        String[] values = params.get(key);
140        return values == null ? null : values[0];
141    }
142
143}