Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aaefa3c

Browse files
improving coverage for Json and Juss protocols...
1 parent 552f8ed commit aaefa3c

File tree

7 files changed

+259
-127
lines changed

7 files changed

+259
-127
lines changed

src/examples/Message.java

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package examples;
22

3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Objects;
6+
import java.util.Random;
7+
38
import org.ugp.serialx.juss.protocols.SelfSerializable;
49

510
/**
@@ -17,25 +22,47 @@ public class Message implements SelfSerializable
1722
protected String str;
1823
protected long date;
1924

25+
protected List<Message> replies;
26+
protected HashMap<String, Message> mappedReplies;
27+
28+
protected transient Object rand = new Random();
29+
2030
public Message(String str, int date)
2131
{
22-
this.str = str;
23-
this.date = date;
32+
this(str, date, (List<Message>) null);
33+
}
34+
35+
public Message(String str, int date, List<Message> replies)
36+
{
37+
setStr(str);
38+
setDate(date);
39+
setReplies(replies);
2440
}
2541

2642
@Override
27-
public boolean equals(Object obj) {
43+
public boolean equals(Object obj)
44+
{
2845
if (this == obj)
2946
return true;
3047
if (obj == null || getClass() != obj.getClass())
3148
return false;
3249
Message other = (Message) obj;
33-
return date == other.date && str.equals(other.str);
50+
return date == other.date &&
51+
Objects.equals(str, other.str) &&
52+
mappedReplies == null ? Objects.deepEquals(replies, other.replies) :
53+
Objects.deepEquals(mappedReplies, other.mappedReplies);
3454
}
3555

3656
@Override
37-
public String toString() {
38-
return "Message["+str+", "+date+"]";
57+
public String toString()
58+
{
59+
return "Message["+str+", "+date+", "+(mappedReplies == null ? replies : mappedReplies)+"]";
60+
}
61+
62+
@Override
63+
public Object[] serialize()
64+
{
65+
return new Object[] {str, date, replies};
3966
}
4067

4168
public String getStr() {
@@ -54,9 +81,27 @@ public void setDate(long date) {
5481
this.date = date;
5582
}
5683

57-
@Override
58-
public Object[] serialize()
59-
{
60-
return new Object[] {str, date};
84+
public List<Message> getReplies() {
85+
return replies;
86+
}
87+
88+
public void setReplies(List<Message> replies) {
89+
this.replies = replies;
90+
}
91+
92+
public HashMap<String, Message> getMappedReplies() {
93+
return mappedReplies;
94+
}
95+
96+
public void setMappedReplies(HashMap<String, Message> mappedReplies) {
97+
this.mappedReplies = mappedReplies;
98+
}
99+
100+
public Object getRand() {
101+
return rand;
102+
}
103+
104+
public void setRand(Object rand) {
105+
this.rand = rand;
61106
}
62107
}

src/examples/implementations/GeneralExample.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class GeneralExample
4848
public static final String TEST_3 = "some string";
4949

5050
public static final double TEST_4 = 5;
51-
public static final int TEST_4A = 6;
51+
public static final int TEST_4I = 6;
5252
public static final Scope TEST_5 = new Scope();
5353
public static final String TEST_6 = "HELLO_WORLD";
5454

@@ -107,7 +107,7 @@ public Class<? extends Random> applicableFor()
107107

108108
HashMap<String, Object> vars = new HashMap<>(); //Variables to serialize
109109
vars.put("yourMom", TEST_2);
110-
vars.put("num", TEST_4A);
110+
vars.put("num", TEST_4I);
111111

112112
int[][] ints = {{1, 2, 3}, {4, 5, 4}, {3, 2, 1}};
113113

@@ -169,22 +169,20 @@ public Class<? extends Random> applicableFor()
169169

170170
assertEquals(TEST_4, deserializer.getScope(4).getScope("neastedTest").getDouble("tst4"), 0);
171171
assertEquals(deserializer.getScope(4).getScope(Utils.splitValues("test neastedTest", ' ')).getParent(2), deserializer.getScope(4));
172-
assertEquals(((Scope) deserializer.getScope(4).getSubScope(0).<List<?>>get(0).get(3)).getSubScope(0).toObject(List.class).size(), TEST_5.into(Collection.class).size());
172+
assertEquals(((Scope) deserializer.getScope(4).getSubScope(0).<List<?>>get(0).get(3)).getSubScope(0).<List<?>>toObject(List.class).size(), TEST_5.<Collection<?>>into(Collection.class).size());
173173
assertTrue(deserializer.clone() instanceof JussSerializer);
174174
assertTrue(deserializer.filter(obj -> obj.equals(new Scope("true").getBool(0))).get(0));
175175

176176
assertEquals(TEST_3, deserializer.get(0));
177177
assertEquals(list, deserializer.get(2));
178178
assertEquals(new Bar(TEST_1), deserializer.<Object>get(5));
179179
assertArrayEquals(ints, Scope.from(deserializer.get(3)).toArray());
180-
assertEquals(-TEST_4A, deserializer.getByte(-3));
180+
assertEquals(-TEST_4I, deserializer.getByte(-3));
181181

182182
assertTrue(deserializer.<Boolean>get("_boolTst1") && deserializer.<Boolean>cloneOf("_boolTst2") && deserializer.getBool("_boolTst4") && !deserializer.containsVariable("_boolTst3"));
183183
assertEquals(Utils.multilpy(GenericScope.intoBidirectional(Scope.from(new Scope()) , null, Arrays.asList(97)).getChar(0), +1 +-6 / -2*(2+1)%- 100 + 1).toString(), deserializer.get(-2));
184184

185185
assertEquals(TEST_6, new Scope(deserializer).getString(-1));
186-
187-
// TODO Cover juss protocols
188186
}
189187

190188
//We can invoke static members in JUSS!
Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
package examples.implementations;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
36
import java.io.File;
47
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.HashMap;
510
import java.util.List;
11+
import java.util.Objects;
612

13+
import org.junit.Test;
14+
import org.ugp.serialx.LogProvider;
715
import org.ugp.serialx.Scope;
816
import org.ugp.serialx.Serializer;
917
import org.ugp.serialx.json.JsonSerializer;
1018
import org.ugp.serialx.juss.protocols.AutoProtocol;
19+
import org.ugp.serialx.juss.protocols.SelfSerializableProtocol;
1120
import org.ugp.serialx.protocols.SerializationProtocol;
1221

1322
import examples.Message;
@@ -19,44 +28,87 @@
1928
*
2029
* @since 1.3.7
2130
*/
22-
public class SerializingWithJson {
31+
public class SerializingWithJson
32+
{
33+
public static final String[] NO_SCOPE = new String[] {"str", "date", "replies"};
34+
35+
@Test
36+
public void test() throws Exception // SelfSerializable
37+
{
38+
SerializingWithJson.main(null);
39+
}
40+
41+
@Test
42+
public void test1() throws Exception // AutoProtocol
43+
{
44+
SerializingWithJson.main(NO_SCOPE);
45+
}
46+
47+
@Test
48+
public void test2() throws Exception // AutoProtocol
49+
{
50+
SerializingWithJson.main(new String[] {"str", "date"});
51+
}
52+
53+
@Test
54+
public void test3() throws Exception // AutoProtocol
55+
{
56+
SerializingWithJson.main(new String[] {"str", "date", "replies"});
57+
}
58+
59+
@Test
60+
public void test4() throws Exception // AutoProtocol
61+
{
62+
SerializingWithJson.main(new String[] {"str", "date", "mappedReplies"});
63+
}
2364

2465
public static void main(String[] args) throws Exception
2566
{
26-
/*
27-
* Registering AutoProtocol for Message class and setting it to serialize it as Scope!
28-
*
29-
* Registering additional Serialization protocol is therefore not necessary, we are doing this only to enforce the Scope format.
30-
*/
31-
SerializationProtocol.REGISTRY.add(new AutoProtocol<>(Message.class, true));
32-
33-
/*
34-
* Note that Message is also SelfSerializable which makes it eligible to be serialized with SelfSerializableProtocol as well!
35-
* You can try this by uncommenting this line and commenting line above, notice how data format will change slightly due to different serialization technique that SelfSerializable uses!
36-
*/
37-
// SerializationProtocol.REGISTRY.add(new SelfSerializableProtocol(Message.class));
67+
LogProvider.instance.setReThrowException(true); // This is for testing purposes, so no error are allowed in this case, comment temporary when necessary...
68+
69+
SerializationProtocol.REGISTRY.add(
70+
SerializationProtocol.REGISTRY.removeIf(prot -> prot.applicableFor() == Message.class) ? // Change this accordingly while experimenting (add negation in front)...
71+
72+
/*
73+
* Registering AutoProtocol for Message class and setting it to serialize it as Scope, but feel free to experiment (except when args are NO_SCOPE in case you are running this as test)!
74+
*/
75+
new AutoProtocol<>(Message.class, args != NO_SCOPE /*true*/, args) :
76+
77+
/*
78+
* Note that Message is also SelfSerializable which makes it eligible to be serialized with SelfSerializableProtocol as well!
79+
* You can try this by changing the condition above, notice how data format will change slightly due to different serialization technique that SelfSerializable uses!
80+
*/
81+
new SelfSerializableProtocol(Message.class)
82+
);
3883

3984
File medium = new File("src/examples/implementations/messages.json"); // Json file to use...
4085

41-
// Content to serialzie (list of Messages)
86+
// Content to serialize (list of Messages)
4287
List<Message> messages = new ArrayList<>();
4388
messages.add(new Message("Hi!", 1));
4489
messages.add(new Message("My name is Json.", 2));
4590
messages.add(new Message("And I am data format!", 3));
91+
if (Objects.deepEquals(args, new String[] {"str", "date", "mappedReplies"}))
92+
messages.get(2).setMappedReplies(Scope.mapKvArray(new HashMap<>(), "entry1", new Message("Hello to you as well from map!", 12)));
93+
else
94+
{
95+
messages.get(0).setReplies(new ArrayList<>());
96+
messages.get(1).setReplies(new ArrayList<>(Arrays.asList(new Message("Hello to you as well!", 11))));
97+
}
4698

4799
/*
48100
* Creating new JsonSerializer object and putting out Messages into it.
49101
*/
50102
Serializer serializer = new JsonSerializer(null, messages);
51103
serializer.SerializeTo(medium); // Serializing to given file.
52104

53-
System.out.println("-------- Serialized sucessfully! --------");
105+
System.out.println("-------- Serialization complete! --------");
54106

55107
/*
56108
* Deserializing our Messages from given file.
57109
*/
58110
Serializer deserializer = JsonSerializer.from(medium);
59-
111+
60112
// Mapping deserialized Scopes, or "json objects" in this case, back into our original messages!
61113
List<Message> deserializedMessages = deserializer.map(jsonObj -> {
62114
try
@@ -69,8 +121,21 @@ public static void main(String[] args) throws Exception
69121
}
70122
});
71123

72-
System.out.println("-------- Deserialized sucessfully! --------");
124+
System.out.println("-------- Deserialization complete! --------");
125+
System.out.println(deserializedMessages + "\n");
126+
127+
/* Test if original content was successfully serialized as well as deserialized! */
128+
if (Objects.deepEquals(args, new String[] {"str", "date"}))
129+
{
130+
for (int i = 0; i < messages.size(); i++)
131+
{
132+
assertTrue(deserializedMessages.get(i).getDate() == messages.get(i).getDate());
133+
assertTrue(Objects.equals(deserializedMessages.get(i).getStr(), messages.get(i).getStr()));
134+
}
135+
136+
return;
137+
}
73138

74-
System.out.println(messages.equals(deserializedMessages)); // True means that original content was successfully serialized as well as deserialized!
139+
assertEquals(messages, deserializedMessages);
75140
}
76141
}

src/examples/implementations/SimpleQuerying.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static void main(String[] args) throws Exception
136136
assertEquals(content.getScope("ppl").valuesCount(), ages.size());
137137
assertEquals(filtered.getScope("serialx"), content.getScope("serialx"));
138138

139-
assertTrue(residents.toObject(List.class).size() > 0); //Should not be 0
139+
assertTrue(residents.<List<?>>toObject(List.class).size() > 0); //Should not be 0
140140
assertTrue(remappedValues.size() > 0);
141141

142142
JsonSerializer test = new JsonSerializer();
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
[
22
{
33
"str" : "Hi!",
4-
"date" : 1
4+
"date" : 1,
5+
"mappedReplies" : null
56
},
67
{
78
"str" : "My name is Json.",
8-
"date" : 2
9+
"date" : 2,
10+
"mappedReplies" : null
911
},
1012
{
1113
"str" : "And I am data format!",
12-
"date" : 3
14+
"date" : 3,
15+
"mappedReplies" : {
16+
"entry1" : {
17+
"str" : "Hello to you as well from map!",
18+
"date" : 12,
19+
"mappedReplies" : null
20+
}
21+
}
1322
}
1423
]

src/examples/implementations/test.juss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//Date created: 07-12-2024 at 21:57:29 CEST
1+
//Date created: 07-18-2024 at 23:24:38 CEST
22

33
//Scope serialization summary:
44
//2 variables!
@@ -7,9 +7,9 @@
77
num = 6; //Primitive data type: "6" the integer value! Stored by "num" variable!
88
yourMom = "has an event horizon... //lol"; //Object of java.lang.String: "has an event horizon... //lol"! Stored by "yourMom" variable!
99
"some string"; //Object of java.lang.String: "some string"!
10-
java.util.Random 68284896517770L; //Object of java.util.Random: "java.util.Random@52b1beb6" serialized using examples.implementations.GeneralExample$1[java.util.Random]!
10+
java.util.Random 68284896517770L; //Object of java.util.Random: "java.util.Random@18df8434" serialized using examples.implementations.GeneralExample$1[java.util.Random]!
1111
ArrayList 0 0 T 1 3 0 5 T 5 6 {java.util.LinkedList 0 0 T 1 3 0 5 T 5 6}; //Object of java.util.ArrayList: "[0, 0, true, 1, 3, 0, 5, true, 5, 6, [0, 0, true, 1, 3, 0, 5, true, 5, 6]]" serialized using org.ugp.serialx.protocols.ListProtocol[java.util.Collection]!
12-
(1 2 3) (4 5 4) (3 2 1); //Primitive array [[I@273e7444 converted by org.ugp.serialx.juss.converters.ArrayConverter
12+
(1 2 3) (4 5 4) (3 2 1); //Primitive array [[I@65c7a252 converted by org.ugp.serialx.juss.converters.ArrayConverter
1313
{
1414
//Scope serialization summary:
1515
//1 variables!

0 commit comments

Comments
 (0)