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:
37   /**
38    * \brief Test RegexConfProvider
39    * \test
40    * -# Create new RegexConfProvider
41    * -# Load test data from testStream
42    * -# Get data using testKey and compare against testString
43    * \param testStream Stream to load data from
44    * \param testString String to check result against
45    * \param testKey    Key to check result against
46    */
regexConfProviderTest(istringstream & testStream,const string & testString,const string & testKey)47   void regexConfProviderTest (istringstream& testStream,
48                     const string& testString,
49                     const string& testKey)
50   {
51     string testIdentity("testIdentity");
52 
53     RegexConfProvider rcp;
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:
65   /**
66    * \test
67    * -# Create simple test stream with 'key=value'
68    * -# Check with regexConfProviderTest()
69    */
simpleTest()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 
80   /**
81    * \test
82    * -# Create test stream with key inside value
83    * -# Check with regexConfProviderTest()
84    */
simpleReplacementTest()85   void simpleReplacementTest()
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 
97   /**
98    * \test
99    * -# Create test stream with multiple pairs
100    * -# Check with regexConfProviderTest()
101    */
multipleReplacementTest()102   void multipleReplacementTest()
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 
116   /**
117    * \test
118    * -# Create ambiguous test stream
119    * -# Load in RegexConfProvider
120    * -# Try to retrieve value
121    */
testForInfiniteRecursion()122   void testForInfiniteRecursion()
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 );
145