|
9 | 9 | import java.io.*; |
10 | 10 | import java.util.HashMap; |
11 | 11 | import java.util.Map; |
| 12 | +import java.util.function.Function; |
12 | 13 |
|
13 | 14 | import org.json.*; |
14 | 15 | import org.junit.Rule; |
@@ -132,4 +133,144 @@ public void testMethodTwo_replaceAll() |
132 | 133 | } |
133 | 134 | } |
134 | 135 |
|
| 136 | + @Test |
| 137 | + public void testMethodThree_replaceKey() |
| 138 | + { |
| 139 | + System.out.println("\nIn Test Method three"); |
| 140 | + String expectedStr = "<?xml version=\"1.0\"?>\n" + |
| 141 | + "<RJ_catalog>\n" + |
| 142 | + " <RJ_book id=\"bk101\">\n" + |
| 143 | + " <RJ_author>Gambardella, Matthew</RJ_author>\n" + |
| 144 | + " <RJ_title>XML Developer's Guide</RJ_title>\n" + |
| 145 | + " <RJ_genre>Computer</RJ_genre>\n" + |
| 146 | + " <RJ_price>44.95</RJ_price>\n" + |
| 147 | + " <RJ_publish_date>2000-10-01</RJ_publish_date>\n" + |
| 148 | + " <RJ_description>An in-depth look at creating applications\n" + |
| 149 | + " with XML.</RJ_description>\n" + |
| 150 | + " </RJ_book>\n" + |
| 151 | + " <RJ_book id=\"bk102\">\n" + |
| 152 | + " <RJ_author>Ralls, Kim</RJ_author>\n" + |
| 153 | + " <RJ_title>Midnight Rain</RJ_title>\n" + |
| 154 | + " <RJ_genre>Fantasy</RJ_genre>\n" + |
| 155 | + " <RJ_price>5.95</RJ_price>\n" + |
| 156 | + " <RJ_publish_date>2000-12-16</RJ_publish_date>\n" + |
| 157 | + " <RJ_description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</RJ_description>\n" + |
| 158 | + " </RJ_book>\n" + |
| 159 | + " <RJ_book id=\"bk103\">\n" + |
| 160 | + " <RJ_author>Corets, Eva</RJ_author>\n" + |
| 161 | + " <RJ_title>Maeve Ascendant</RJ_title>\n" + |
| 162 | + " <RJ_genre>Fantasy</RJ_genre>\n" + |
| 163 | + " <RJ_price>5.95</RJ_price>\n" + |
| 164 | + " <RJ_publish_date>2000-11-17</RJ_publish_date>\n" + |
| 165 | + " <RJ_description>After the collapse of a nanotechnology\n" + |
| 166 | + " society in England, the young survivors lay the\n" + |
| 167 | + " foundation for a new society.</RJ_description>\n" + |
| 168 | + " </RJ_book>\n" + |
| 169 | + " <RJ_book id=\"bk104\">\n" + |
| 170 | + " <RJ_author>Corets, Eva</RJ_author>\n" + |
| 171 | + " <RJ_title>Oberon's Legacy</RJ_title>\n" + |
| 172 | + " <RJ_genre>Fantasy</RJ_genre>\n" + |
| 173 | + " <RJ_price>5.95</RJ_price>\n" + |
| 174 | + " <RJ_publish_date>2001-03-10</RJ_publish_date>\n" + |
| 175 | + " <RJ_description>In post-apocalypse England, the mysterious\n" + |
| 176 | + " agent known only as Oberon helps to create a new life\n" + |
| 177 | + " for the inhabitants of London. Sequel to Maeve\n" + |
| 178 | + " Ascendant.</RJ_description>\n" + |
| 179 | + " </RJ_book>\n" + |
| 180 | + " <RJ_book id=\"bk105\">\n" + |
| 181 | + " <RJ_author>Corets, Eva</RJ_author>\n" + |
| 182 | + " <RJ_title>The Sundered Grail</RJ_title>\n" + |
| 183 | + " <RJ_genre>Fantasy</RJ_genre>\n" + |
| 184 | + " <RJ_price>5.95</RJ_price>\n" + |
| 185 | + " <RJ_publish_date>2001-09-10</RJ_publish_date>\n" + |
| 186 | + " <RJ_description>The two daughters of Maeve, half-sisters,\n" + |
| 187 | + " battle one another for control of England. Sequel to\n" + |
| 188 | + " Oberon's Legacy.</RJ_description>\n" + |
| 189 | + " </RJ_book>\n" + |
| 190 | + " <RJ_book id=\"bk106\">\n" + |
| 191 | + " <RJ_author>Randall, Cynthia</RJ_author>\n" + |
| 192 | + " <RJ_title>Lover Birds</RJ_title>\n" + |
| 193 | + " <RJ_genre>Romance</RJ_genre>\n" + |
| 194 | + " <RJ_price>4.95</RJ_price>\n" + |
| 195 | + " <RJ_publish_date>2000-09-02</RJ_publish_date>\n" + |
| 196 | + " <RJ_description>When Carla meets Paul at an ornithology\n" + |
| 197 | + " conference, tempers fly as feathers get ruffled.</RJ_description>\n" + |
| 198 | + " </RJ_book>\n" + |
| 199 | + " <RJ_book id=\"bk107\">\n" + |
| 200 | + " <RJ_author>Thurman, Paula</RJ_author>\n" + |
| 201 | + " <RJ_title>Splish Splash</RJ_title>\n" + |
| 202 | + " <RJ_genre>Romance</RJ_genre>\n" + |
| 203 | + " <RJ_price>4.95</RJ_price>\n" + |
| 204 | + " <RJ_publish_date>2000-11-02</RJ_publish_date>\n" + |
| 205 | + " <RJ_description>A deep sea diver finds true love twenty\n" + |
| 206 | + " thousand leagues beneath the sea.</RJ_description>\n" + |
| 207 | + " </RJ_book>\n" + |
| 208 | + " <RJ_book id=\"bk108\">\n" + |
| 209 | + " <RJ_author>Knorr, Stefan</RJ_author>\n" + |
| 210 | + " <RJ_title>Creepy Crawlies</RJ_title>\n" + |
| 211 | + " <RJ_genre>Horror</RJ_genre>\n" + |
| 212 | + " <RJ_price>4.95</RJ_price>\n" + |
| 213 | + " <RJ_publish_date>2000-12-06</RJ_publish_date>\n" + |
| 214 | + " <RJ_description>An anthology of horror stories about roaches,\n" + |
| 215 | + " centipedes, scorpions and other insects.</RJ_description>\n" + |
| 216 | + " </RJ_book>\n" + |
| 217 | + " <RJ_book id=\"bk109\">\n" + |
| 218 | + " <RJ_author>Kress, Peter</RJ_author>\n" + |
| 219 | + " <RJ_title>Paradox Lost</RJ_title>\n" + |
| 220 | + " <RJ_genre>Science Fiction</RJ_genre>\n" + |
| 221 | + " <RJ_price>6.95</RJ_price>\n" + |
| 222 | + " <RJ_publish_date>2000-11-02</RJ_publish_date>\n" + |
| 223 | + " <RJ_description>After an inadvertant trip through a Heisenberg\n" + |
| 224 | + " Uncertainty Device, James Salway discovers the problems\n" + |
| 225 | + " of being quantum.</RJ_description>\n" + |
| 226 | + " </RJ_book>\n" + |
| 227 | + " <RJ_book id=\"bk110\">\n" + |
| 228 | + " <RJ_author>O'Brien, Tim</RJ_author>\n" + |
| 229 | + " <RJ_title>Microsoft .NET: The Programming Bible</RJ_title>\n" + |
| 230 | + " <RJ_genre>Computer</RJ_genre>\n" + |
| 231 | + " <RJ_price>36.95</RJ_price>\n" + |
| 232 | + " <RJ_publish_date>2000-12-09</RJ_publish_date>\n" + |
| 233 | + " <RJ_description>Microsoft's .NET initiative is explored in\n" + |
| 234 | + " detail in this deep programmer's reference.</RJ_description>\n" + |
| 235 | + " </RJ_book>\n" + |
| 236 | + " <RJ_book id=\"bk111\">\n" + |
| 237 | + " <RJ_author>O'Brien, Tim</RJ_author>\n" + |
| 238 | + " <RJ_title>MSXML3: A Comprehensive Guide</RJ_title>\n" + |
| 239 | + " <RJ_genre>Computer</RJ_genre>\n" + |
| 240 | + " <RJ_price>36.95</RJ_price>\n" + |
| 241 | + " <RJ_publish_date>2000-12-01</RJ_publish_date>\n" + |
| 242 | + " <RJ_description>The Microsoft MSXML3 parser is covered in\n" + |
| 243 | + " detail, with attention to XML DOM interfaces, XSLT processing,\n" + |
| 244 | + " SAX and more.</RJ_description>\n" + |
| 245 | + " </RJ_book>\n" + |
| 246 | + " <RJ_book id=\"bk112\">\n" + |
| 247 | + " <RJ_author>Galos, Mike</RJ_author>\n" + |
| 248 | + " <RJ_title>Visual Studio 7: A Comprehensive Guide</RJ_title>\n" + |
| 249 | + " <RJ_genre>Computer</RJ_genre>\n" + |
| 250 | + " <RJ_price>49.95</RJ_price>\n" + |
| 251 | + " <RJ_publish_date>2001-04-16</RJ_publish_date>\n" + |
| 252 | + " <RJ_description>Microsoft Visual Studio 7 is explored in depth,\n" + |
| 253 | + " looking at how Visual Basic, Visual C++, C#, and ASP+ are\n" + |
| 254 | + " integrated into a comprehensive development\n" + |
| 255 | + " environment.</RJ_description>\n" + |
| 256 | + " </RJ_book>\n" + |
| 257 | + "</RJ_catalog>"; |
| 258 | + |
| 259 | + expectedStr = XML.toJSONObject(expectedStr).toString(); |
| 260 | + try { |
| 261 | + //Define the function |
| 262 | + Function<String, String> keyTransformer= (x) -> ("RJ_"+x); |
| 263 | + FileReader filereader = new FileReader("src/test/resources/Catalog.xml"); |
| 264 | + JSONObject jo = XML.toJSONObject(filereader, keyTransformer); |
| 265 | + |
| 266 | + String actualStr = jo.toString(); |
| 267 | + //System.out.println(actualStr); |
| 268 | + assertEquals(expectedStr, actualStr); |
| 269 | + |
| 270 | + } catch (FileNotFoundException e) { |
| 271 | + System.out.println("File not found"); |
| 272 | + e.printStackTrace(); |
| 273 | + } |
| 274 | + } |
| 275 | + |
135 | 276 | } |
0 commit comments