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 // various streams have implemented eof differently, so check the archive
64 // stream works with all the possibilities (bit flags that can be ORed)
66 AtLast
= 0x01, // eof before an attempt to read past the last byte
67 WithError
= 0x02 // give an error instead of eof
70 // ctor takes the data from the output stream, which is then empty
71 TestInputStream(TestOutputStream
& out
, int eoftype
)
72 : m_data(NULL
), m_eoftype(eoftype
) { SetData(out
); }
74 TestInputStream(const TestInputStream
& in
);
75 ~TestInputStream() { delete [] m_data
; }
78 wxFileOffset
GetLength() const { return m_size
; }
79 bool IsSeekable() const { return (m_options
& PipeIn
) == 0; }
80 void SetData(TestOutputStream
& out
);
82 void Chop(size_t size
) { m_size
= size
; }
83 char& operator [](size_t pos
) { return m_data
[pos
]; }
86 wxFileOffset
OnSysSeek(wxFileOffset pos
, wxSeekMode mode
);
87 wxFileOffset
OnSysTell() const;
88 size_t OnSysRead(void *buffer
, size_t size
);
98 ///////////////////////////////////////////////////////////////////////////////
99 // wxFFile streams for piping to/from an external program
101 class PFileInputStream
: public wxFFileInputStream
104 PFileInputStream(const wxString
& cmd
);
108 class PFileOutputStream
: public wxFFileOutputStream
111 PFileOutputStream(const wxString
& cmd
);
112 ~PFileOutputStream();
116 ///////////////////////////////////////////////////////////////////////////////
117 // A class to hold a test entry
122 TestEntry(const wxDateTime
& dt
, int len
, const char *data
);
123 ~TestEntry() { delete [] m_data
; }
125 wxDateTime
GetDateTime() const { return m_dt
; }
126 wxFileOffset
GetLength() const { return m_len
; }
127 size_t GetSize() const { return m_len
; }
128 const char *GetData() const { return m_data
; }
129 wxString
GetComment() const { return m_comment
; }
130 bool IsText() const { return m_isText
; }
132 void SetComment(const wxString
& comment
) { m_comment
= comment
; }
133 void SetDateTime(const wxDateTime
& dt
) { m_dt
= dt
; }
144 ///////////////////////////////////////////////////////////////////////////////
147 template <class ClassFactoryT
>
148 class ArchiveTestCase
: public CppUnit::TestCase
151 ArchiveTestCase(std::string name
,
152 ClassFactoryT
*factory
,
154 const wxString
& archiver
= wxEmptyString
,
155 const wxString
& unarchiver
= wxEmptyString
);
160 // the classes to test
161 typedef typename
ClassFactoryT::entry_type EntryT
;
162 typedef typename
ClassFactoryT::instream_type InputStreamT
;
163 typedef typename
ClassFactoryT::outstream_type OutputStreamT
;
164 typedef typename
ClassFactoryT::notifier_type NotifierT
;
165 typedef typename
ClassFactoryT::iter_type IterT
;
166 typedef typename
ClassFactoryT::pairiter_type PairIterT
;
168 // the entry point for the test
171 // create the test data
172 void CreateTestData();
173 TestEntry
& Add(const char *name
, const char *data
, int len
= -1);
174 TestEntry
& Add(const char *name
, int len
= 0, int value
= EOF
);
176 // 'archive up' the test data
177 void CreateArchive(wxOutputStream
& out
);
178 void CreateArchive(wxOutputStream
& out
, const wxString
& archiver
);
180 // perform various modifications on the archive
181 void ModifyArchive(wxInputStream
& in
, wxOutputStream
& out
);
183 // extract the archive and verify its contents
184 void ExtractArchive(wxInputStream
& in
);
185 void ExtractArchive(wxInputStream
& in
, const wxString
& unarchiver
);
186 void VerifyDir(wxString
& path
, size_t rootlen
= 0);
188 // tests for the iterators
189 void TestIterator(wxInputStream
& in
);
190 void TestPairIterator(wxInputStream
& in
);
191 void TestSmartIterator(wxInputStream
& in
);
192 void TestSmartPairIterator(wxInputStream
& in
);
194 // try reading two entries at the same time
195 void ReadSimultaneous(TestInputStream
& in
);
198 virtual void OnCreateArchive(OutputStreamT
& WXUNUSED(arc
)) { }
199 virtual void OnSetNotifier(EntryT
& entry
);
201 virtual void OnArchiveExtracted(InputStreamT
& WXUNUSED(arc
),
202 int WXUNUSED(expectedTotal
)) { }
204 virtual void OnCreateEntry( OutputStreamT
& WXUNUSED(arc
),
205 TestEntry
& WXUNUSED(testEntry
),
206 EntryT
*entry
= NULL
) { (void)entry
; }
208 virtual void OnEntryExtracted( EntryT
& WXUNUSED(entry
),
209 const TestEntry
& WXUNUSED(testEntry
),
210 InputStreamT
*arc
= NULL
) { (void)arc
; }
212 typedef std::map
<wxString
, TestEntry
*> TestEntries
;
213 TestEntries m_testEntries
; // test data
214 std::auto_ptr
<ClassFactoryT
> m_factory
; // factory to make classes
215 int m_options
; // test options
216 wxDateTime m_timeStamp
; // timestamp to give test entries
217 int m_id
; // select between the possibilites
218 wxString m_archiver
; // external archiver
219 wxString m_unarchiver
; // external unarchiver
223 ///////////////////////////////////////////////////////////////////////////////
229 // make a new id and return it as a string
230 static std::string
MakeId();
231 // get the current id
232 static int GetId() { return m_seed
; }
234 // seed for generating the ids
239 ///////////////////////////////////////////////////////////////////////////////
240 // Base class for the archive test suites
242 class ArchiveTestSuite
: public CppUnit::TestSuite
245 ArchiveTestSuite(std::string name
);
248 virtual ArchiveTestSuite
*makeSuite();
250 virtual CppUnit::Test
*makeTest(std::string descr
,
252 bool genericInterface
,
253 const wxString
& archiver
,
254 const wxString
& unarchiver
);
256 void AddArchiver(const wxString
& cmd
) { AddCmd(m_archivers
, cmd
); }
257 void AddUnArchiver(const wxString
&cmd
) { AddCmd(m_unarchivers
, cmd
); }
258 bool IsInPath(const wxString
& cmd
);
260 std::string
Description(const wxString
& type
,
262 bool genericInterface
= false,
263 const wxString
& archiver
= wxEmptyString
,
264 const wxString
& unarchiver
= wxEmptyString
);
269 wxArrayString m_archivers
;
270 wxArrayString m_unarchivers
;
272 void AddCmd(wxArrayString
& cmdlist
, const wxString
& cmd
);