KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > monitor > test > MockResource


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package org.apache.avalon.excalibur.monitor.test;
20
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import java.io.Reader JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 import org.apache.avalon.excalibur.monitor.ResourceOutputStream;
31 import org.apache.avalon.excalibur.monitor.ResourceWriter;
32 import org.apache.avalon.excalibur.monitor.StreamResource;
33
34 /**
35  * The MockResource object so that we can enable the tests.
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  */

39 public class MockResource extends StreamResource
40 {
41     private final Mock m_mock;
42
43     /**
44      * Instantiate the FileResource
45      */

46     public MockResource( final String JavaDoc resource )
47         throws Exception JavaDoc
48     {
49         this( new Mock( resource ) );
50     }
51
52     public MockResource( final Mock resource )
53         throws Exception JavaDoc
54     {
55         super( resource.getName() );
56         m_mock = resource;
57         setPreviousModified( m_mock.lastModified() );
58     }
59
60     /**
61      * Determines the last time this resource was modified
62      */

63     public long lastModified()
64     {
65         return m_mock.lastModified();
66     }
67
68     /**
69      * Sets the resource value with an OutputStream
70      */

71     public InputStream JavaDoc getResourceAsStream()
72         throws IOException JavaDoc
73     {
74         return new ByteArrayInputStream JavaDoc( m_mock.getContent().getBytes() );
75     }
76
77     /**
78      * Sets the resource value with a Writer
79      */

80     public Reader JavaDoc getResourceAsReader()
81         throws IOException JavaDoc
82     {
83         return new InputStreamReader JavaDoc( getResourceAsStream() );
84     }
85
86     /**
87      * Sets the resource value with an OutputStream
88      */

89     public OutputStream JavaDoc setResourceAsStream()
90         throws IOException JavaDoc
91     {
92         return new ResourceOutputStream( new MockOutputStream( m_mock ), this );
93     }
94
95     /**
96      * Sets the resource value with a Writer
97      */

98     public Writer JavaDoc setResourceAsWriter()
99         throws IOException JavaDoc
100     {
101         return new ResourceWriter( new OutputStreamWriter JavaDoc( new MockOutputStream( m_mock ) ), this );
102     }
103 }
104
Popular Tags