Skip to content

Commit 7a8d3c8

Browse files
author
Devendra
committed
changing string escape method
1 parent 899c5e4 commit 7a8d3c8

22 files changed

Lines changed: 827 additions & 19 deletions

File tree

‎java/libs/commons-lang3-3.4.jar‎

424 KB
Binary file not shown.

‎java/libs/lcrypto-jdk15.jar‎

-798 KB
Binary file not shown.

‎java/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
</plugins>
8484
</build>
8585
<dependencies>
86+
<dependency>
87+
<groupId>org.apache.commons</groupId>
88+
<artifactId>commons-lang3</artifactId>
89+
<version>3.4</version>
90+
</dependency>
8691
<dependency>
8792
<groupId>org.json</groupId>
8893
<artifactId>json</artifactId>

‎java/src1/com/pubnub/api/PubnubUtil.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import org.json.JSONException;
1111
import org.json.JSONObject;
1212

13+
import org.apache.commons.lang3.StringEscapeUtils;
14+
15+
1316
/**
1417
* PubnubUtil class provides utility methods like urlEncode etc
1518
*
@@ -18,6 +21,10 @@
1821
*/
1922
public class PubnubUtil extends PubnubUtilCore {
2023

24+
public static String escapeJava(String a) {
25+
return StringEscapeUtils.escapeJava(a);
26+
}
27+
2128
public static String stringEscapeSlashes(String s, String a, String b) {
2229
return s.replace(a, b);
2330
}

‎java/srcPubnubApi/srcCore/com/pubnub/api/PubnubCore.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,15 @@ protected Object _publish(Hashtable args, boolean sync) {
198198
if (storeInHistory != null && storeInHistory.length() > 0)
199199
parameters.put("store", storeInHistory);
200200

201-
202201
final Callback callback = getWrappedCallback(cb);
203202

203+
204+
204205
if (this.CIPHER_KEY.length() > 0) {
205206
// Encrypt Message
206207
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY, this.IV);
207208
try {
209+
208210
if (message instanceof String) {
209211
msgStr = "\"" + msgStr + "\"";
210212
}
@@ -216,8 +218,12 @@ protected Object _publish(Hashtable args, boolean sync) {
216218
}
217219
} else {
218220
if (message instanceof String) {
221+
/*
222+
msgStr = PubnubUtil.stringReplaceAll(msgStr, "\\n", "\\\\n");
219223
msgStr = PubnubUtil.stringEscapeSlashes(msgStr, "\\", "\\\\");
220224
msgStr = PubnubUtil.stringReplaceAll(msgStr, "\"", "\\\\\"");
225+
*/
226+
msgStr = PubnubUtil.escapeJava(msgStr);
221227
msgStr = "\"" + msgStr + "\"";
222228
}
223229
}

‎scala/scala-pubnub-tests/pom.xml‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
</dependency>
3131

3232
<!-- Test -->
33+
<dependency>
34+
<groupId>org.apache.commons</groupId>
35+
<artifactId>commons-lang3</artifactId>
36+
<version>3.4</version>
37+
</dependency>
3338
<dependency>
3439
<groupId>junit</groupId>
3540
<artifactId>junit</artifactId>
@@ -161,9 +166,9 @@
161166
<reportsDirectory>${project.build.directory}/surefire-reports</reportsDirectory>
162167
<junitxml>.</junitxml>
163168
<filereports>WDF TestSuite.txt</filereports>
164-
<!--<tagsToInclude>com.pubnub.api.tests.NewTest</tagsToInclude>-->
169+
<tagsToInclude>com.pubnub.api.tests.PublishTest</tagsToInclude>-->
165170
<systemProperties>
166-
<org.slf4j.simpleLogger.defaultLogLevel></org.slf4j.simpleLogger.defaultLogLevel>
171+
<org.slf4j.simpleLogger.defaultLogLevel>debug</org.slf4j.simpleLogger.defaultLogLevel>
167172
</systemProperties>
168173
</configuration>
169174
<executions>

‎scala/scala-pubnub-tests/scala-pubnub-tests.iml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<orderEntry type="inheritedJdk" />
1818
<orderEntry type="sourceFolder" forTests="false" />
1919
<orderEntry type="library" name="Maven: org.slf4j:slf4j-nop:1.7.13" level="project" />
20+
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.4" level="project" />
2021
<orderEntry type="library" name="Maven: org.scala-lang:scala-library:2.10.0" level="project" />
2122
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
2223
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />

‎scala/scala-pubnub-tests/src/tests/scala/com/pubnub/api/tests/junit.scala‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ class PublishResult {
2323
var result = 0;
2424
}
2525

26+
/*
2627
@Test
2728
class PublishTest extends AwaitilitySupport {
2829
29-
/*
30+
3031
@Test
3132
def subscribe() = {
3233
var pubnub = new Pubnub("demo", "demo")
@@ -48,8 +49,9 @@ class PublishTest extends AwaitilitySupport {
4849
await atMost(3000, MILLISECONDS) until { pr.result == 1 }
4950
5051
}
51-
*/
52+
5253
5354
}
5455
56+
*/
5557

‎scala/scala-pubnub-tests/src/tests/scala/com/pubnub/api/tests/publish.scala‎

Lines changed: 131 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import scala.util.Random
3636
object PublishTest extends Tag("com.pubnub.api.tests.PublishTest")
3737

3838
@RunWith(classOf[JUnitRunner])
39-
class PublishSpec extends fixture.FunSpec with AwaitilitySupport {
39+
class PublishSpec1 extends fixture.FunSpec with AwaitilitySupport {
4040

4141
var PUBLISH_KEY = ""
4242
var SUBSCRIBE_KEY = ""
@@ -45,7 +45,7 @@ class PublishSpec extends fixture.FunSpec with AwaitilitySupport {
4545
var SSL = false
4646
var RANDOM = new Random()
4747

48-
48+
var TIMEOUT = 20000
4949

5050
type FixtureParam = PubnubTestConfig
5151

@@ -70,11 +70,10 @@ class PublishSpec extends fixture.FunSpec with AwaitilitySupport {
7070

7171
describe("Publish()") {
7272

73-
it("should be able to publish String with double quotes successfully") { pubnubTestConfig =>
73+
it("should be able to publish String with double quotes successfully", PublishTest) { pubnubTestConfig =>
7474

7575
var pubnub = pubnubTestConfig.pubnub
76-
//var channel = "channel-" + getRandom()
77-
var channel = "ab"
76+
var channel = "channel-" + getRandom()
7877
var message = "message-" + "\"hi\""
7978
var testObj = new PnTest(3)
8079

@@ -96,7 +95,6 @@ class PublishSpec extends fixture.FunSpec with AwaitilitySupport {
9695
override def successCallback(channel: String, message1: Object) {
9796
pubnub.unsubscribe(channel)
9897
testObj.test(true)
99-
println(message1)
10098
assertTrue(message1.toString, (message1.getClass()).equals(message.getClass()))
10199
assertTrue(message1.equals(message))
102100
}
@@ -106,15 +104,96 @@ class PublishSpec extends fixture.FunSpec with AwaitilitySupport {
106104
}
107105
});
108106

109-
await atMost(5000, MILLISECONDS) until { testObj.checksRemaining() == 0 }
107+
await atMost(TIMEOUT, MILLISECONDS) until { testObj.checksRemaining() == 0 }
108+
109+
}
110+
111+
112+
it("should be able to publish JSON Array successfully", PublishTest) { pubnubTestConfig =>
113+
114+
var pubnub = pubnubTestConfig.pubnub
115+
var channel = "channel-" + getRandom()
116+
var message = new JSONArray();
117+
message.put("a")
118+
message.put("b")
119+
var testObj = new PnTest(3)
120+
121+
pubnub.subscribe(channel, new Callback {
122+
override def connectCallback(channel: String, message1: Object): Unit = {
123+
testObj.test(true)
124+
pubnub.publish(channel, message, new Callback {
125+
126+
override def successCallback(channel: String, message: Object) {
127+
testObj.test(true)
128+
}
129+
130+
override def errorCallback(channel: String, error: PubnubError) {
131+
assertTrue(error.toString, false)
132+
}
133+
})
134+
135+
}
136+
override def successCallback(channel: String, message1: Object) {
137+
pubnub.unsubscribe(channel)
138+
testObj.test(true)
139+
assertTrue(message1.toString + " : " + message1.getClass(), (message1.getClass()).equals(message.getClass()))
140+
assertTrue(message1.asInstanceOf[JSONArray].getString(0).equals("a"))
141+
assertTrue(message1.asInstanceOf[JSONArray].getString(1).equals("b"))
142+
}
143+
override def errorCallback(channel: String, error: PubnubError) {
144+
pubnub.unsubscribe(channel)
145+
assertTrue(error.toString, false)
146+
}
147+
});
148+
149+
await atMost(TIMEOUT, MILLISECONDS) until { testObj.checksRemaining() == 0 }
150+
151+
}
152+
153+
it("should be able to publish JSON Array literal string successfully", PublishTest) { pubnubTestConfig =>
154+
155+
var pubnub = pubnubTestConfig.pubnub
156+
var channel = "channel-" + getRandom()
157+
var message = "[\"a\",\"b\"]"
158+
var testObj = new PnTest(3)
159+
160+
pubnub.subscribe(channel, new Callback {
161+
override def connectCallback(channel: String, message1: Object): Unit = {
162+
testObj.test(true)
163+
pubnub.publish(channel, message, new Callback {
164+
165+
override def successCallback(channel: String, message: Object) {
166+
testObj.test(true)
167+
}
168+
169+
override def errorCallback(channel: String, error: PubnubError) {
170+
assertTrue(error.toString, false)
171+
}
172+
})
173+
174+
}
175+
override def successCallback(channel: String, message1: Object) {
176+
pubnub.unsubscribe(channel)
177+
testObj.test(true)
178+
assertTrue(message1.toString + " : " + message1.getClass(), (message1.getClass()).equals(message.getClass()))
179+
assertTrue(message1.toString + " : " + message.toString + "[" + message1.getClass() + "]", message1.equals(message))
180+
181+
}
182+
override def errorCallback(channel: String, error: PubnubError) {
183+
pubnub.unsubscribe(channel)
184+
assertTrue(error.toString, false)
185+
}
186+
});
187+
188+
await atMost(TIMEOUT, MILLISECONDS) until { testObj.checksRemaining() == 0 }
110189

111190
}
112191

113-
it("should be able to publish JSON Object successfully", PublishTest ) { pubnubTestConfig =>
192+
193+
it("should be able to publish JSON Object successfully", PublishTest) { pubnubTestConfig =>
114194

115195
var pubnub = pubnubTestConfig.pubnub
116-
//var channel = "channel-" + getRandom()
117-
var channel = "ab"
196+
var channel = "channel-" + getRandom()
118197
var message = new JSONObject();
119198
message.put("a", "b")
120199
var testObj = new PnTest(3)
@@ -146,15 +225,14 @@ class PublishSpec extends fixture.FunSpec with AwaitilitySupport {
146225
}
147226
});
148227

149-
await atMost(10000, MILLISECONDS) until { testObj.checksRemaining() == 0 }
228+
await atMost(TIMEOUT, MILLISECONDS) until { testObj.checksRemaining() == 0 }
150229

151230
}
152231

153232
it("should be able to publish JSON Object literal string successfully", PublishTest) { pubnubTestConfig =>
154233

155234
var pubnub = pubnubTestConfig.pubnub
156-
//var channel = "channel-" + getRandom()
157-
var channel = "ab"
235+
var channel = "channel-" + getRandom()
158236
var message = "{\"a\":\"b\"}"
159237
var testObj = new PnTest(3)
160238

@@ -186,11 +264,50 @@ class PublishSpec extends fixture.FunSpec with AwaitilitySupport {
186264
}
187265
});
188266

189-
await atMost(10000, MILLISECONDS) until { testObj.checksRemaining() == 0 }
267+
await atMost(TIMEOUT, MILLISECONDS) until { testObj.checksRemaining() == 0 }
190268

191269
}
192270

193271

272+
it("should be able to publish string with \\n successfully", PublishTest) { pubnubTestConfig =>
273+
274+
var pubnub = pubnubTestConfig.pubnub
275+
var channel = "channel-" + getRandom()
276+
var message = "[1,\n2]"
277+
var testObj = new PnTest(3)
278+
279+
pubnub.subscribe(channel, new Callback {
280+
override def connectCallback(channel: String, message1: Object): Unit = {
281+
testObj.test(true)
282+
pubnub.publish(channel, message, new Callback {
283+
284+
override def successCallback(channel: String, message: Object) {
285+
testObj.test(true)
286+
}
287+
288+
override def errorCallback(channel: String, error: PubnubError) {
289+
assertTrue(error.toString, false)
290+
}
291+
})
292+
293+
}
294+
override def successCallback(channel: String, message1: Object) {
295+
pubnub.unsubscribe(channel)
296+
testObj.test(true)
297+
assertTrue(message1.toString + " : " + message1.getClass(), (message1.getClass()).equals(message.getClass()))
298+
assertTrue(message1.toString + " : " + message.toString + "[" + message1.getClass() + "]", message1.equals(message))
299+
300+
}
301+
override def errorCallback(channel: String, error: PubnubError) {
302+
pubnub.unsubscribe(channel)
303+
assertTrue(error.toString, false)
304+
}
305+
});
306+
307+
await atMost(TIMEOUT, MILLISECONDS) until { testObj.checksRemaining() == 0 }
308+
309+
}
310+
194311

195312
}
196313
}

0 commit comments

Comments
 (0)