FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
test_regexConfProvider.cc
1 /*
2  * Copyright (C) 2015, Siemens AG
3  * Author: Maximilian Huber
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include <cppunit/TestFixture.h>
20 #include <cppunit/extensions/HelperMacros.h>
21 
22 #include "regexConfProvider.hpp"
23 #include <sstream>
24 
25 using namespace std;
26 
27 class regexConfProviderTestSuite : public CPPUNIT_NS :: TestFixture {
28  CPPUNIT_TEST_SUITE (regexConfProviderTestSuite);
29  CPPUNIT_TEST (simpleTest);
30  CPPUNIT_TEST (simpleReplacementTest);
31  CPPUNIT_TEST (multipleReplacementTest);
32  CPPUNIT_TEST (testForInfiniteRecursion);
33 
34  CPPUNIT_TEST_SUITE_END ();
35 
36 private:
47  void regexConfProviderTest (istringstream& testStream,
48  const string& testString,
49  const string& testKey)
50  {
51  string testIdentity("testIdentity");
52 
54 
55  // load parse test-stream
56  rcp.maybeLoad(testIdentity,testStream);
57 
58  // test RegexConfProvider
59  CPPUNIT_ASSERT_MESSAGE("The generated string should match the expected string",
60  0 == strcmp(testString.c_str(),
61  rcp.getRegexValue(testIdentity,testKey)));
62  }
63 
64 protected:
70  void simpleTest()
71  {
72  string testString = "Lorem Ipsum";
73  string testKey = "TEST";
74  string testLine = testKey + "=" + testString + "\n";
75  istringstream testStream(testLine);
76 
77  regexConfProviderTest(testStream,testString,testKey);
78  }
79 
86  {
87  string testString = "Lorem Ipsum";
88  string testKey = "TEST";
89  string testLine =
90  testKey + "=" + "Lorem \n" +
91  testKey + "=__" + testKey + "__Ipsum\n";
92  istringstream testStream(testLine);
93 
94  regexConfProviderTest(testStream,testString,testKey);
95  }
96 
103  {
104  string testString = "Lorem Ipsum";
105  string testKey = "TEST";
106  string testLine =
107  string("SPACE= \n") +
108  "INFIX2=su\n" +
109  "INFIX1=rem__SPACE__I\n" +
110  testKey + "=Lo__INFIX1__p__INFIX2__m\n";
111  istringstream testStream(testLine);
112 
113  regexConfProviderTest(testStream,testString,testKey);
114  }
115 
123  {
124  string testString = "Lorem Ipsum";
125  string testKey = "TEST";
126  string testLine =
127  string("LOREM=Lorem__LOREM__ \n") +
128  testKey + "=__LOREM__Ipsum\n";
129  istringstream testStream(testLine);
130 
131  string testIdentity("testIdentity");
132 
133  RegexConfProvider rcp;
134 
135  // load parse test-stream
136  rcp.maybeLoad(testIdentity,testStream);
137 
138  // evaluate and verify, that recursion does not appear
139  CPPUNIT_ASSERT_MESSAGE("This should just terminate (the return value is not specified)",
140  rcp.getRegexValue(testIdentity,testKey));
141  }
142 };
143 
144 CPPUNIT_TEST_SUITE_REGISTRATION( regexConfProviderTestSuite );
Provide regex using conf file.
void regexConfProviderTest(istringstream &testStream, const string &testString, const string &testKey)
Test RegexConfProvider.
const char * getRegexValue(const std::string &name, const std::string &key)
Get the regex as string from the RegexMap.
void maybeLoad(const std::string &identity)
Check if identity already loaded in RegexMap, if not load them.