KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > mock > MockSession


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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 package org.apache.cocoon.environment.mock;
17
18 import java.util.Enumeration JavaDoc;
19 import java.util.Hashtable JavaDoc;
20
21 import junit.framework.AssertionFailedError;
22
23 import org.apache.cocoon.environment.Session;
24
25 public class MockSession implements Session {
26
27     private long creationtime = System.currentTimeMillis();
28     private String JavaDoc id = "MockSession";
29     private long lastaccessedtime = System.currentTimeMillis();
30     private int maxinactiveinterval = -1;
31     private Hashtable JavaDoc attributes = new Hashtable JavaDoc();
32     private boolean valid = true;
33
34     public long getCreationTime() {
35         checkValid();
36         return creationtime;
37     }
38   
39     public void setId(String JavaDoc id) {
40         this.id = id;
41     }
42
43     public String JavaDoc getId() {
44         checkValid();
45         return id;
46     }
47
48     public long getLastAccessedTime() {
49         checkValid();
50         return lastaccessedtime;
51     }
52
53     public void setMaxInactiveInterval(int interval) {
54         checkValid();
55         this.maxinactiveinterval = interval;
56     }
57
58     public int getMaxInactiveInterval() {
59         checkValid();
60         return maxinactiveinterval;
61     }
62
63     public Object JavaDoc getAttribute(String JavaDoc name) {
64         checkValid();
65         return attributes.get(name);
66     }
67
68     public Enumeration JavaDoc getAttributeNames() {
69         checkValid();
70         return attributes.keys();
71     }
72
73     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
74         checkValid();
75         attributes.put(name, value);
76     }
77
78     public void removeAttribute(String JavaDoc name) {
79         checkValid();
80         attributes.remove(name);
81     }
82
83     public void invalidate() {
84         checkValid();
85         this.valid = false;
86     }
87
88     public boolean isNew() {
89         checkValid();
90         return false;
91     }
92
93     private void checkValid() throws IllegalStateException JavaDoc {
94         if (!valid)
95             throw new AssertionFailedError("session has been invalidated!");
96     }
97
98     public boolean isValid() {
99         return valid;
100     }
101 }
102
103
Popular Tags