1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/archive/archivetest.h
3 // Purpose: Test the archive classes
4 // Author: Mike Wetherell
6 // Copyright: (c) 2004 Mike Wetherell
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #define WX_TEST_ARCHIVE_ITERATOR
12 #include "wx/archive.h"
13 #include "wx/wfstream.h"
16 ///////////////////////////////////////////////////////////////////////////////
17 // Bit flags for options for the tests
21 PipeIn
= 0x01, // input streams are non-seekable
22 PipeOut
= 0x02, // output streams are non-seekable
23 Stub
= 0x04, // the archive should be appended to a stub
28 ///////////////////////////////////////////////////////////////////////////////
29 // TestOutputStream and TestInputStream are memory streams which can be
30 // seekable or non-seekable.
32 class TestOutputStream
: public wxOutputStream
35 TestOutputStream(int options
);
37 ~TestOutputStream() { delete [] m_data
; }
39 int GetOptions() const { return m_options
; }
40 wxFileOffset
GetLength() const { return m_size
; }
41 bool IsSeekable() const { return (m_options
& PipeOut
) == 0; }
43 // gives away the data, this stream is then empty, and can be reused
44 void GetData(char*& data
, size_t& size
);
49 wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
50 wxFileOffset
OnSysTell() const;
51 size_t OnSysWrite(const void *buffer
, size_t size
);
60 class TestInputStream
: public wxInputStream
63 // ctor takes the data from the output stream, which is then empty
64 TestInputStream(TestOutputStream
& out
) : m_data(NULL
) { SetData(out
); }
66 TestInputStream(const TestInputStream
& in
);
67 ~TestInputStream() { delete [] m_data
; }
70 wxFileOffset
GetLength() const { return m_size
; }
71 bool IsSeekable() const { return (m_options
& PipeIn
) == 0; }
72 void SetData(TestOutputStream
& out
);
75 wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
76 wxFileOffset
OnSysTell() const;
77 size_t OnSysRead(void *buffer
, size_t size
);
86 ///////////////////////////////////////////////////////////////////////////////
87 // wxFFile streams for piping to/from an external program
89 class PFileInputStream
: public wxFFileInputStream
92 PFileInputStream(const wxString
& cmd
);
96 class PFileOutputStream
: public wxFFileOutputStream
99 PFileOutputStream(const wxString
& cmd
);
100 ~PFileOutputStream();
104 ///////////////////////////////////////////////////////////////////////////////
105 // A class to hold a test entry
110 TestEntry(const wxDateTime
& dt
, int len
, const char *data
);
111 ~TestEntry() { delete [] m_data
; }
113 wxDateTime
GetDateTime() const { return m_dt
; }
114 wxFileOffset
GetLength() const { return m_len
; }
115 size_t GetSize() const { return m_len
; }
116 const char *GetData() const { return m_data
; }
117 wxString
GetComment() const { return m_comment
; }
118 bool IsText() const { return m_isText
; }
120 void SetComment(const wxString
& comment
) { m_comment
= comment
; }
121 void SetDateTime(const wxDateTime
& dt
) { m_dt
= dt
; }
132 ///////////////////////////////////////////////////////////////////////////////
135 template <class ClassFactoryT
>
136 class ArchiveTestCase
: public CppUnit::TestCase
139 ArchiveTestCase(std::string name
,
141 ClassFactoryT
*factory
,
143 const wxString
& archiver
= wxEmptyString
,
144 const wxString
& unarchiver
= wxEmptyString
);
149 // the classes to test
150 typedef typename
ClassFactoryT::entry_type EntryT
;
151 typedef typename
ClassFactoryT::instream_type InputStreamT
;
152 typedef typename
ClassFactoryT::outstream_type OutputStreamT
;
153 typedef typename
ClassFactoryT::notifier_type NotifierT
;
154 typedef typename
ClassFactoryT::iter_type IterT
;
155 typedef typename
ClassFactoryT::pairiter_type PairIterT
;
157 // the entry point for the test
160 // create the test data
161 void CreateTestData();
162 TestEntry
& Add(const char *name
, const char *data
, int len
= -1);
163 TestEntry
& Add(const char *name
, int len
= 0, int value
= EOF
);
165 // 'archive up' the test data
166 void CreateArchive(wxOutputStream
& out
);
167 void CreateArchive(wxOutputStream
& out
, const wxString
& archiver
);
169 // perform various modifications on the archive
170 void ModifyArchive(wxInputStream
& in
, wxOutputStream
& out
);
172 // extract the archive and verify its contents
173 void ExtractArchive(wxInputStream
& in
);
174 void ExtractArchive(wxInputStream
& in
, const wxString
& unarchiver
);
175 void VerifyDir(wxString
& path
, size_t rootlen
= 0);
177 // tests for the iterators
178 void TestIterator(wxInputStream
& in
);
179 void TestPairIterator(wxInputStream
& in
);
180 void TestSmartIterator(wxInputStream
& in
);
181 void TestSmartPairIterator(wxInputStream
& in
);
183 // try reading two entries at the same time
184 void ReadSimultaneous(TestInputStream
& in
);
187 virtual void OnCreateArchive(OutputStreamT
& WXUNUSED(arc
)) { }
188 virtual void OnSetNotifier(EntryT
& entry
);
190 virtual void OnArchiveExtracted(InputStreamT
& WXUNUSED(arc
),
191 int WXUNUSED(expectedTotal
)) { }
193 virtual void OnCreateEntry( OutputStreamT
& WXUNUSED(arc
),
194 TestEntry
& WXUNUSED(testEntry
),
195 EntryT
*entry
= NULL
) { (void)entry
; }
197 virtual void OnEntryExtracted( EntryT
& WXUNUSED(entry
),
198 const TestEntry
& WXUNUSED(testEntry
),
199 InputStreamT
*arc
= NULL
) { (void)arc
; }
201 typedef std::map
<wxString
, TestEntry
*> TestEntries
;
202 TestEntries m_testEntries
; // test data
203 std::auto_ptr
<ClassFactoryT
> m_factory
; // factory to make classes
204 int m_options
; // test options
205 wxDateTime m_timeStamp
; // timestamp to give test entries
206 int m_id
; // select between the possibilites
207 wxString m_archiver
; // external archiver
208 wxString m_unarchiver
; // external unarchiver
212 ///////////////////////////////////////////////////////////////////////////////
213 // Base class for the archive test suites
215 class ArchiveTestSuite
: public CppUnit::TestSuite
218 ArchiveTestSuite(std::string name
);
223 virtual ArchiveTestSuite
*makeSuite();
225 virtual CppUnit::Test
*makeTest(std::string descr
,
228 bool genericInterface
,
229 const wxString
& archiver
,
230 const wxString
& unarchiver
);
232 void AddArchiver(const wxString
& cmd
) { AddCmd(m_archivers
, cmd
); }
233 void AddUnArchiver(const wxString
&cmd
) { AddCmd(m_unarchivers
, cmd
); }
234 bool IsInPath(const wxString
& cmd
);
236 std::string
Description(const wxString
& type
,
238 bool genericInterface
= false,
239 const wxString
& archiver
= wxEmptyString
,
240 const wxString
& unarchiver
= wxEmptyString
);
245 wxArrayString m_archivers
;
246 wxArrayString m_unarchivers
;
248 void AddCmd(wxArrayString
& cmdlist
, const wxString
& cmd
);