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
; }
42 // gives away the data, this stream is then empty, and can be reused
43 void GetData(char*& data
, size_t& size
);
48 wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
49 wxFileOffset
OnSysTell() const;
50 size_t OnSysWrite(const void *buffer
, size_t size
);
59 class TestInputStream
: public wxInputStream
62 // ctor takes the data from the output stream, which is then empty
63 TestInputStream(TestOutputStream
& out
) : m_data(NULL
) { SetData(out
); }
65 TestInputStream(const TestInputStream
& in
);
66 ~TestInputStream() { delete [] m_data
; }
69 wxFileOffset
GetLength() const { return m_size
; }
70 void SetData(TestOutputStream
& out
);
73 wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
74 wxFileOffset
OnSysTell() const;
75 size_t OnSysRead(void *buffer
, size_t size
);
84 ///////////////////////////////////////////////////////////////////////////////
85 // wxFFile streams for piping to/from an external program
87 class PFileInputStream
: public wxFFileInputStream
90 PFileInputStream(const wxString
& cmd
);
94 class PFileOutputStream
: public wxFFileOutputStream
97 PFileOutputStream(const wxString
& cmd
);
102 ///////////////////////////////////////////////////////////////////////////////
103 // A class to hold a test entry
108 TestEntry(const wxDateTime
& dt
, int len
, const char *data
);
109 ~TestEntry() { delete [] m_data
; }
111 wxDateTime
GetDateTime() const { return m_dt
; }
112 wxFileOffset
GetLength() const { return m_len
; }
113 size_t GetSize() const { return m_len
; }
114 const char *GetData() const { return m_data
; }
115 wxString
GetComment() const { return m_comment
; }
116 bool IsText() const { return m_isText
; }
118 void SetComment(const wxString
& comment
) { m_comment
= comment
; }
119 void SetDateTime(const wxDateTime
& dt
) { m_dt
= dt
; }
130 ///////////////////////////////////////////////////////////////////////////////
133 template <class ClassFactoryT
>
134 class ArchiveTestCase
: public CppUnit::TestCase
137 ArchiveTestCase(std::string name
,
139 ClassFactoryT
*factory
,
141 const wxString
& archiver
= wxEmptyString
,
142 const wxString
& unarchiver
= wxEmptyString
);
147 // the classes to test
148 typedef typename
ClassFactoryT::entry_type EntryT
;
149 typedef typename
ClassFactoryT::instream_type InputStreamT
;
150 typedef typename
ClassFactoryT::outstream_type OutputStreamT
;
151 typedef typename
ClassFactoryT::notifier_type NotifierT
;
152 typedef typename
ClassFactoryT::iter_type IterT
;
153 typedef typename
ClassFactoryT::pairiter_type PairIterT
;
155 // the entry point for the test
158 // create the test data
159 void CreateTestData();
160 TestEntry
& Add(const char *name
, const char *data
, int len
= -1);
161 TestEntry
& Add(const char *name
, int len
= 0, int value
= EOF
);
163 // 'archive up' the test data
164 void CreateArchive(wxOutputStream
& out
);
165 void CreateArchive(wxOutputStream
& out
, const wxString
& archiver
);
167 // perform various modifications on the archive
168 void ModifyArchive(wxInputStream
& in
, wxOutputStream
& out
);
170 // extract the archive and verify its contents
171 void ExtractArchive(wxInputStream
& in
);
172 void ExtractArchive(wxInputStream
& in
, const wxString
& unarchiver
);
173 void VerifyDir(wxString
& path
, size_t rootlen
= 0);
175 // tests for the iterators
176 void TestIterator(wxInputStream
& in
);
177 void TestPairIterator(wxInputStream
& in
);
178 void TestSmartIterator(wxInputStream
& in
);
179 void TestSmartPairIterator(wxInputStream
& in
);
181 // try reading two entries at the same time
182 void ReadSimultaneous(TestInputStream
& in
);
185 virtual void OnCreateArchive(OutputStreamT
& WXUNUSED(arc
)) { }
186 virtual void OnSetNotifier(EntryT
& entry
);
188 virtual void OnArchiveExtracted(InputStreamT
& WXUNUSED(arc
),
189 int WXUNUSED(expectedTotal
)) { }
191 virtual void OnCreateEntry( OutputStreamT
& WXUNUSED(arc
),
192 TestEntry
& WXUNUSED(testEntry
),
193 EntryT
*entry
= NULL
) { (void)entry
; }
195 virtual void OnEntryExtracted( EntryT
& WXUNUSED(entry
),
196 const TestEntry
& WXUNUSED(testEntry
),
197 InputStreamT
*arc
= NULL
) { (void)arc
; }
199 typedef std::map
<wxString
, TestEntry
*> TestEntries
;
200 TestEntries m_testEntries
; // test data
201 std::auto_ptr
<ClassFactoryT
> m_factory
; // factory to make classes
202 int m_options
; // test options
203 wxDateTime m_timeStamp
; // timestamp to give test entries
204 int m_id
; // select between the possibilites
205 wxString m_archiver
; // external archiver
206 wxString m_unarchiver
; // external unarchiver
210 ///////////////////////////////////////////////////////////////////////////////
211 // Base class for the archive test suites
213 class ArchiveTestSuite
: public CppUnit::TestSuite
216 ArchiveTestSuite(std::string name
);
221 virtual ArchiveTestSuite
*makeSuite();
223 virtual CppUnit::Test
*makeTest(std::string descr
,
226 bool genericInterface
,
227 const wxString
& archiver
,
228 const wxString
& unarchiver
);
230 void AddArchiver(const wxString
& cmd
) { AddCmd(m_archivers
, cmd
); }
231 void AddUnArchiver(const wxString
&cmd
) { AddCmd(m_unarchivers
, cmd
); }
232 bool IsInPath(const wxString
& cmd
);
234 std::string
Description(const wxString
& type
,
236 bool genericInterface
= false,
237 const wxString
& archiver
= wxEmptyString
,
238 const wxString
& unarchiver
= wxEmptyString
);
243 wxArrayString m_archivers
;
244 wxArrayString m_unarchivers
;
246 void AddCmd(wxArrayString
& cmdlist
, const wxString
& cmd
);