1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/archive/archivetest.h
3 // Purpose: Test the archive classes
4 // Author: Mike Wetherell
5 // Copyright: (c) 2004 Mike Wetherell
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
9 #ifndef WX_ARCHIVETEST_INCLUDED
10 #define WX_ARCHIVETEST_INCLUDED 1
12 #define WX_TEST_ARCHIVE_ITERATOR
14 #include "wx/archive.h"
15 #include "wx/wfstream.h"
18 ///////////////////////////////////////////////////////////////////////////////
19 // Bit flags for options for the tests
23 PipeIn
= 0x01, // input streams are non-seekable
24 PipeOut
= 0x02, // output streams are non-seekable
25 Stub
= 0x04, // the archive should be appended to a stub
30 ///////////////////////////////////////////////////////////////////////////////
31 // TestOutputStream and TestInputStream are memory streams which can be
32 // seekable or non-seekable.
34 class TestOutputStream
: public wxOutputStream
37 TestOutputStream(int options
);
39 ~TestOutputStream() { delete [] m_data
; }
41 int GetOptions() const { return m_options
; }
42 wxFileOffset
GetLength() const { return m_size
; }
43 bool IsSeekable() const { return (m_options
& PipeOut
) == 0; }
45 // gives away the data, this stream is then empty, and can be reused
46 void GetData(char*& data
, size_t& size
);
51 wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
52 wxFileOffset
OnSysTell() const;
53 size_t OnSysWrite(const void *buffer
, size_t size
);
62 class TestInputStream
: public wxInputStream
65 // various streams have implemented eof differently, so check the archive
66 // stream works with all the possibilities (bit flags that can be ORed)
68 AtLast
= 0x01, // eof before an attempt to read past the last byte
69 WithError
= 0x02 // give an error instead of eof
72 // ctor takes the data from the output stream, which is then empty
73 TestInputStream(TestOutputStream
& out
, int eoftype
)
74 : m_data(NULL
), m_eoftype(eoftype
) { SetData(out
); }
76 TestInputStream(const TestInputStream
& in
);
77 ~TestInputStream() { delete [] m_data
; }
80 wxFileOffset
GetLength() const { return m_size
; }
81 bool IsSeekable() const { return (m_options
& PipeIn
) == 0; }
82 void SetData(TestOutputStream
& out
);
84 void Chop(size_t size
) { m_size
= size
; }
85 char& operator [](size_t pos
) { return m_data
[pos
]; }
88 wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
89 wxFileOffset
OnSysTell() const;
90 size_t OnSysRead(void *buffer
, size_t size
);
100 ///////////////////////////////////////////////////////////////////////////////
101 // wxFFile streams for piping to/from an external program
103 class PFileInputStream
: public wxFFileInputStream
106 PFileInputStream(const wxString
& cmd
);
110 class PFileOutputStream
: public wxFFileOutputStream
113 PFileOutputStream(const wxString
& cmd
);
114 ~PFileOutputStream();
118 ///////////////////////////////////////////////////////////////////////////////
119 // A class to hold a test entry
124 TestEntry(const wxDateTime
& dt
, int len
, const char *data
);
125 ~TestEntry() { delete [] m_data
; }
127 wxDateTime
GetDateTime() const { return m_dt
; }
128 wxFileOffset
GetLength() const { return m_len
; }
129 size_t GetSize() const { return m_len
; }
130 const char *GetData() const { return m_data
; }
131 wxString
GetComment() const { return m_comment
; }
132 bool IsText() const { return m_isText
; }
134 void SetComment(const wxString
& comment
) { m_comment
= comment
; }
135 void SetDateTime(const wxDateTime
& dt
) { m_dt
= dt
; }
146 ///////////////////////////////////////////////////////////////////////////////
149 template <class ClassFactoryT
>
150 class ArchiveTestCase
: public CppUnit::TestCase
153 ArchiveTestCase(std::string name
,
154 ClassFactoryT
*factory
,
156 const wxString
& archiver
= wxEmptyString
,
157 const wxString
& unarchiver
= wxEmptyString
);
162 // the classes to test
163 typedef typename
ClassFactoryT::entry_type EntryT
;
164 typedef typename
ClassFactoryT::instream_type InputStreamT
;
165 typedef typename
ClassFactoryT::outstream_type OutputStreamT
;
166 typedef typename
ClassFactoryT::notifier_type NotifierT
;
167 typedef typename
ClassFactoryT::iter_type IterT
;
168 typedef typename
ClassFactoryT::pairiter_type PairIterT
;
170 // the entry point for the test
173 // create the test data
174 void CreateTestData();
175 TestEntry
& Add(const char *name
, const char *data
, int len
= -1);
176 TestEntry
& Add(const char *name
, int len
= 0, int value
= EOF
);
178 // 'archive up' the test data
179 void CreateArchive(wxOutputStream
& out
);
180 void CreateArchive(wxOutputStream
& out
, const wxString
& archiver
);
182 // perform various modifications on the archive
183 void ModifyArchive(wxInputStream
& in
, wxOutputStream
& out
);
185 // extract the archive and verify its contents
186 void ExtractArchive(wxInputStream
& in
);
187 void ExtractArchive(wxInputStream
& in
, const wxString
& unarchiver
);
188 void VerifyDir(wxString
& path
, size_t rootlen
= 0);
190 // tests for the iterators
191 void TestIterator(wxInputStream
& in
);
192 void TestPairIterator(wxInputStream
& in
);
193 void TestSmartIterator(wxInputStream
& in
);
194 void TestSmartPairIterator(wxInputStream
& in
);
196 // try reading two entries at the same time
197 void ReadSimultaneous(TestInputStream
& in
);
200 virtual void OnCreateArchive(OutputStreamT
& WXUNUSED(arc
)) { }
201 virtual void OnSetNotifier(EntryT
& entry
);
203 virtual void OnArchiveExtracted(InputStreamT
& WXUNUSED(arc
),
204 int WXUNUSED(expectedTotal
)) { }
206 virtual void OnCreateEntry( OutputStreamT
& WXUNUSED(arc
),
207 TestEntry
& WXUNUSED(testEntry
),
208 EntryT
*entry
= NULL
) { (void)entry
; }
210 virtual void OnEntryExtracted( EntryT
& WXUNUSED(entry
),
211 const TestEntry
& WXUNUSED(testEntry
),
212 InputStreamT
*arc
= NULL
) { (void)arc
; }
214 typedef std::map
<wxString
, TestEntry
*> TestEntries
;
215 TestEntries m_testEntries
; // test data
216 std::auto_ptr
<ClassFactoryT
> m_factory
; // factory to make classes
217 int m_options
; // test options
218 wxDateTime m_timeStamp
; // timestamp to give test entries
219 int m_id
; // select between the possibilites
220 wxString m_archiver
; // external archiver
221 wxString m_unarchiver
; // external unarchiver
225 ///////////////////////////////////////////////////////////////////////////////
231 // make a new id and return it as a string
232 static std::string
MakeId();
233 // get the current id
234 static int GetId() { return m_seed
; }
236 // seed for generating the ids
241 ///////////////////////////////////////////////////////////////////////////////
242 // Base class for the archive test suites
244 class ArchiveTestSuite
: public CppUnit::TestSuite
247 ArchiveTestSuite(std::string name
);
250 virtual ArchiveTestSuite
*makeSuite();
252 virtual CppUnit::Test
*makeTest(std::string descr
,
254 bool genericInterface
,
255 const wxString
& archiver
,
256 const wxString
& unarchiver
);
258 void AddArchiver(const wxString
& cmd
) { AddCmd(m_archivers
, cmd
); }
259 void AddUnArchiver(const wxString
&cmd
) { AddCmd(m_unarchivers
, cmd
); }
260 bool IsInPath(const wxString
& cmd
);
262 std::string
Description(const wxString
& type
,
264 bool genericInterface
= false,
265 const wxString
& archiver
= wxEmptyString
,
266 const wxString
& unarchiver
= wxEmptyString
);
271 wxArrayString m_archivers
;
272 wxArrayString m_unarchivers
;
274 void AddCmd(wxArrayString
& cmdlist
, const wxString
& cmd
);