KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > portlet > MockPortletPreferences


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.mock.web.portlet;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.portlet.PortletPreferences;
27 import javax.portlet.PreferencesValidator;
28 import javax.portlet.ReadOnlyException;
29 import javax.portlet.ValidatorException;
30
31 import org.springframework.core.CollectionFactory;
32 import org.springframework.util.Assert;
33
34 /**
35  * Mock implementation of the {@link javax.portlet.PortletPreferences} interface.
36  *
37  * @author John A. Lewis
38  * @author Juergen Hoeller
39  * @since 2.0
40  */

41 public class MockPortletPreferences implements PortletPreferences {
42
43     private PreferencesValidator preferencesValidator;
44
45     private final Map JavaDoc preferences = CollectionFactory.createLinkedMapIfPossible(16);
46
47     private final Set JavaDoc readOnly = new HashSet JavaDoc();
48
49
50     public void setReadOnly(String JavaDoc key, boolean readOnly) {
51         Assert.notNull(key, "Key must not be null");
52         if (readOnly) {
53             this.readOnly.add(key);
54         }
55         else {
56             this.readOnly.remove(key);
57         }
58     }
59
60     public boolean isReadOnly(String JavaDoc key) {
61         Assert.notNull(key, "Key must not be null");
62         return this.readOnly.contains(key);
63     }
64
65     public String JavaDoc getValue(String JavaDoc key, String JavaDoc def) {
66         Assert.notNull(key, "Key must not be null");
67         String JavaDoc[] values = (String JavaDoc[]) this.preferences.get(key);
68         return (values != null && values.length > 0 ? values[0] : def);
69     }
70
71     public String JavaDoc[] getValues(String JavaDoc key, String JavaDoc[] def) {
72         Assert.notNull(key, "Key must not be null");
73         String JavaDoc[] values = (String JavaDoc[]) this.preferences.get(key);
74         return (values != null && values.length > 0 ? values : def);
75     }
76
77     public void setValue(String JavaDoc key, String JavaDoc value) throws ReadOnlyException {
78         setValues(key, new String JavaDoc[] {value});
79     }
80
81     public void setValues(String JavaDoc key, String JavaDoc[] values) throws ReadOnlyException {
82         Assert.notNull(key, "Key must not be null");
83         if (isReadOnly(key)) {
84             throw new ReadOnlyException("Preference '" + key + "' is read-only");
85         }
86         this.preferences.put(key, values);
87     }
88
89     public Enumeration JavaDoc getNames() {
90         return Collections.enumeration(this.preferences.keySet());
91     }
92
93     public Map JavaDoc getMap() {
94         return Collections.unmodifiableMap(this.preferences);
95     }
96
97     public void reset(String JavaDoc key) throws ReadOnlyException {
98         Assert.notNull(key, "Key must not be null");
99         if (isReadOnly(key)) {
100             throw new ReadOnlyException("Preference '" + key + "' is read-only");
101         }
102         this.preferences.remove(key);
103     }
104
105     public void setPreferencesValidator(PreferencesValidator preferencesValidator) {
106         this.preferencesValidator = preferencesValidator;
107     }
108
109     public void store() throws IOException JavaDoc, ValidatorException {
110         if (this.preferencesValidator != null) {
111             this.preferencesValidator.validate(this);
112         }
113     }
114
115 }
116
Popular Tags