Skip to content

Package: DciMarshalXStreamExample

DciMarshalXStreamExample

nameinstructionbranchcomplexitylinemethod
DciMarshalXStreamExample()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
alternateSyntax()
M: 33 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
codeThatUsesMarshalling()
M: 0 C: 161
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 25
100%
M: 0 C: 1
100%
codeThatUsesMarshalling2()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
run()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
runWithXStreamContext1()
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
runWithXStreamContext2()
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.thesefoolishthings.examples.dci.marshal.xstream;
28:
29: import javax.annotation.Nonnull;
30: import javax.inject.Inject;
31: import java.io.ByteArrayInputStream;
32: import java.io.ByteArrayOutputStream;
33: import java.io.IOException;
34: import java.nio.file.Path;
35: import it.tidalwave.util.AsExtensions;
36: import it.tidalwave.util.Id;
37: import it.tidalwave.role.ContextManager;
38: import it.tidalwave.thesefoolishthings.examples.dci.marshal.role.XStreamContext1;
39: import it.tidalwave.thesefoolishthings.examples.dci.marshal.role.XStreamContext2;
40: import it.tidalwave.thesefoolishthings.examples.person.ListOfPersons;
41: import it.tidalwave.thesefoolishthings.examples.person.Person;
42: import lombok.experimental.ExtensionMethod;
43: import lombok.extern.slf4j.Slf4j;
44: import static it.tidalwave.thesefoolishthings.examples.dci.marshal.role.Loadable._Loadable_;
45: import static it.tidalwave.thesefoolishthings.examples.dci.marshal.role.Savable._Savable_;
46: import static java.nio.charset.StandardCharsets.UTF_8;
47: import static it.tidalwave.role.io.Marshallable._Marshallable_;
48: import static it.tidalwave.role.io.Unmarshallable._Unmarshallable_;
49:
50: /***********************************************************************************************************************
51: *
52: * Note: this code uses {@code @ExtensionMethod} from Lombok to enhance datum classes with the
53: * {@link it.tidalwave.util.As#as(Class)}, but it is not strictly required.
54: *
55: * @author Fabrizio Giudici
56: *
57: **********************************************************************************************************************/
58: @ExtensionMethod(AsExtensions.class) @Slf4j
59: public class DciMarshalXStreamExample
60: {
61: @Inject @Nonnull
62: private ContextManager contextManager;
63:
64: public void run()
65: throws IOException
66: {
67: runWithXStreamContext1();
68: runWithXStreamContext2();
69: }
70:
71: private void runWithXStreamContext1()
72: throws IOException
73: {
74: // START SNIPPET: xstreamcontext-contextmanager
75: var xStreamContext1 = new XStreamContext1();
76:
77: try
78: {
79: contextManager.addLocalContext(xStreamContext1);
80: codeThatUsesMarshalling();
81: }
82: finally
83: {
84: contextManager.removeLocalContext(xStreamContext1);
85: }
86: // END SNIPPET: xstreamcontext-contextmanager
87: }
88:
89: private void runWithXStreamContext2()
90: throws IOException
91: {
92: // START SNIPPET: xstreamcontext-contextmanager2
93: try (var binder = contextManager.binder(new XStreamContext2()))
94: {
95: codeThatUsesMarshalling();
96: }
97: // END SNIPPET: xstreamcontext-contextmanager2
98: }
99:
100: private void alternateSyntax()
101: throws IOException
102: {
103: contextManager.runEWithContexts(this::codeThatUsesMarshalling, new XStreamContext2());
104: var s = contextManager.runEWithContexts(this::codeThatUsesMarshalling2, new XStreamContext2());
105: log.info("{}", s);
106: }
107:
108: private void codeThatUsesMarshalling()
109: throws IOException
110: {
111: var path1 = Path.of("target/Person.xml");
112: var path2 = Path.of("target/People.xml");
113: // START SNIPPET: xstreamcontext-example1
114: var joe = new Person(new Id("1"), "Joe", "Smith");
115: var luke = new Person(new Id("2"), "Luke", "Skywalker");
116:
117: var marshalledPersons = "";
118: var marshalledPerson = "";
119:
120: try (var os = new ByteArrayOutputStream())
121: {
122: joe.as(_Marshallable_).marshal(os);
123: log.info("******** (joe as Marshallable) marshalled: {}\n", marshalledPerson = os.toString(UTF_8));
124: }
125:
126: try (var os = new ByteArrayOutputStream())
127: {
128: ListOfPersons.of(joe, luke).as(_Marshallable_).marshal(os);
129: log.info("******** (listOfPersons as Marshallable) marshalled: {}\n", marshalledPersons = os.toString(UTF_8));
130: }
131: // END SNIPPET: xstreamcontext-example1
132:
133: // START SNIPPET: xstreamcontext-example2
134: try (var is = new ByteArrayInputStream(marshalledPerson.getBytes(UTF_8)))
135: {
136: var person = Person.prototype().as(_Unmarshallable_).unmarshal(is);
137: log.info("******** Unmarshalled person: {}\n", person);
138: }
139:
140: try (var is = new ByteArrayInputStream(marshalledPersons.getBytes(UTF_8)))
141: {
142: var listOfPersons = ListOfPersons.empty().as(_Unmarshallable_).unmarshal(is);
143: log.info("******** Unmarshalled persons: {}\n", listOfPersons);
144: }
145: // END SNIPPET: xstreamcontext-example2
146:
147: // START SNIPPET: xstreamcontext-savable-loadable
148: joe.as(_Savable_).saveTo(path1);
149: ListOfPersons.of(joe, luke).as(_Savable_).saveTo(path2);
150: var p = Person.prototype().as(_Loadable_).loadFrom(path1);
151: var lp = ListOfPersons.empty().as(_Loadable_).loadFrom(path2);
152: // END SNIPPET: xstreamcontext-savable-loadable
153: log.info("******** Loaded person: {}\n", p);
154: log.info("******** Loaded persons: {}\n", lp);
155: }
156:
157: @Nonnull
158: private String codeThatUsesMarshalling2()
159: throws IOException
160: {
161: codeThatUsesMarshalling();
162: return "foo bar";
163: }
164: }