001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.forms;
023
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029
030import org.nuxeo.ecm.core.api.Blob;
031import org.nuxeo.ecm.core.api.DocumentModel;
032
033/**
034 * Form instance to be used in unit tests.
035 *
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038public class TestFormInstance implements FormInstance {
039
040    protected final Map<String, String[]> params;
041
042    protected final Map<String, Blob[]> blobs;
043
044    public TestFormInstance(Map<String, String[]> params, Map<String, Blob[]> blobs) {
045        if (params == null) {
046            this.params = new HashMap<String, String[]>();
047        } else {
048            this.params = params;
049        }
050        if (blobs == null) {
051            this.blobs = new HashMap<String, Blob[]>();
052        } else {
053            this.blobs = blobs;
054        }
055    }
056
057    public TestFormInstance() {
058        this(null, null);
059    }
060
061    public TestFormInstance(Map<String, String[]> params) {
062        this(params, null);
063    }
064
065    public void setField(String key, String... values) {
066        params.put(key, values);
067    }
068
069    public void addField(String key, String... values) {
070        String[] ar = params.get(key);
071        if (ar == null) {
072            params.put(key, values);
073        } else {
074            String[] tmp = new String[ar.length + values.length];
075            System.arraycopy(ar, 0, tmp, 0, ar.length);
076            System.arraycopy(values, 0, tmp, ar.length, values.length);
077            params.put(key, tmp);
078        }
079    }
080
081    public void setField(String key, Blob... values) {
082        blobs.put(key, values);
083    }
084
085    public void addField(String key, Blob... values) {
086        Blob[] ar = blobs.get(key);
087        if (blobs == null) {
088            blobs.put(key, values);
089        } else {
090            Blob[] tmp = new Blob[ar.length + values.length];
091            System.arraycopy(ar, 0, tmp, 0, ar.length);
092            System.arraycopy(values, 0, tmp, ar.length, values.length);
093            blobs.put(key, tmp);
094        }
095    }
096
097    public Collection<String> getKeys() {
098        List<String> result = new ArrayList<String>();
099        result.addAll(params.keySet());
100        result.addAll(blobs.keySet());
101        return result;
102    }
103
104    /**
105     * TODO XXX implement it
106     */
107    public void fillDocument(DocumentModel doc) {
108        throw new UnsupportedOperationException("Not yet implemented");
109    }
110
111    public Object[] get(String key) {
112        Object[] val = params.get(key);
113        if (val == null) {
114            val = blobs.get(key);
115        }
116        return val;
117    }
118
119    public Blob getBlob(String key) {
120        Blob[] blobs = this.blobs.get(key);
121        return blobs == null ? null : blobs[0];
122    }
123
124    public Blob[] getBlobs(String key) {
125        return blobs.get(key);
126    }
127
128    public Map<String, Blob[]> getBlobFields() {
129        return blobs;
130    }
131
132    public Map<String, String[]> getFormFields() {
133        return params;
134    }
135
136    public String[] getList(String key) {
137        return params.get(key);
138    }
139
140    public String getString(String key) {
141        String[] values = params.get(key);
142        return values == null ? null : values[0];
143    }
144
145}