Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.hierarchicaldata;

import junit.framework.*;

import org.litesoft.commonfoundation.typeutils.*;

public class HierarchicalDataSimpleFactoryTest extends TestCase
{
    public static TestSuite suite()
    {
        return new TestSuite( HierarchicalDataSimpleFactoryTest.class );
    }

    public HierarchicalDataSimpleFactoryTest( String name )
    {
        super( name );
    }

    public static void main( String[] args )
    {
        junit.textui.TestRunner.run( suite() );
    }

    private void validateBadName_createRootNode( HierarchicalDataSink pSink, String pName )
    {
        try
        {
            pSink.createRootNode( pName );
            fail( "Allowed createRootNode of Bad Name '" + pName + "'" );
        }
        catch ( IllegalArgumentException expected )
        {
            //
        }
    }

    private void validateBadName_createChildNode( HierarchicalDataSink.Node pNode, String pName )
    {
        try
        {
            pNode.createChildNode( pName );
            fail( "Allowed createChildNode of Bad Name '" + pName + "'" );
        }
        catch ( IllegalArgumentException expected )
        {
            //
        }
    }

    private void validateBadName_addAttribute( HierarchicalDataSink.Node pNode, String pName )
    {
        try
        {
            pNode.setAttribute( pName, "A" );
            fail( "Allowed addAttribute of Bad Name '" + pName + "'" );
        }
        catch ( IllegalArgumentException expected )
        {
            //
        }
    }

    public void testSink()
            throws Exception
    {
        StringBuilder actual = new StringBuilder();
        HierarchicalDataSink sink = HierarchicalDataSimpleFactory.INSTANCE.createSink( actual );

        validateBadName_createRootNode( sink, null );
        validateBadName_createRootNode( sink, "" );
        validateBadName_createRootNode( sink, "  " );
        validateBadName_createRootNode( sink, "Bad-Name" );
        validateBadName_createRootNode( sink, "Bad Name" );
        validateBadName_createRootNode( sink, "Bad|Name" );
        validateBadName_createRootNode( sink, "_BadName" );
        validateBadName_createRootNode( sink, "9BadName" );

        HierarchicalDataSink.Node node = sink.createRootNode( " ABC  " );

        validateBadName_createChildNode( node, null );
        validateBadName_createChildNode( node, "" );
        validateBadName_createChildNode( node, "  " );
        validateBadName_createChildNode( node, "Bad-Name" );
        validateBadName_createChildNode( node, "Bad Name" );
        validateBadName_createChildNode( node, "Bad|Name" );
        validateBadName_createChildNode( node, "_BadName" );
        validateBadName_createChildNode( node, "9BadName" );

        try
        {
            sink.createRootNode( "ShouldError" );
            fail( "Allowed 2nd createRootNode" );
        }
        catch ( IllegalStateException expected )
        {
        }

        validateBadName_addAttribute( node, null );
        validateBadName_addAttribute( node, "" );
        validateBadName_addAttribute( node, "  " );
        validateBadName_addAttribute( node, "Bad-Name" );
        validateBadName_addAttribute( node, "Bad Name" );
        validateBadName_addAttribute( node, "Bad|Name" );
        validateBadName_addAttribute( node, "_BadName" );
        validateBadName_addAttribute( node, "9BadName" );

        node.setAttribute( "b", "B" );
        node.setAttribute( "a", "A" );
        node.setAttribute( "c", "C" );
        HierarchicalDataSink.Node node1 = node.createChildNode( "D.E.F_" );

        try
        {
            node.setAttribute( "Should", "Error" );
            fail( "Allowed addAttribute after createChildNode" );
        }
        catch ( IllegalStateException expected )
        {
        }

        node1.setAttribute( "e", "E" );
        HierarchicalDataSink.Node node11 = node1.createChildNode( "GHI" );
        node11.setAttribute( " g_  ", "G" );
        node11.done();
        node1.done();
        HierarchicalDataSink.Node node2 = node.createChildNode( "DEF2" );
        node2.setAttribute( "f", "F" );
        node2.done();

        try
        {
            node2.setAttribute( "Should", "Error" );
            fail( "Allowed addAttribute after done" );
        }
        catch ( IllegalStateException expected )
        {
        }
        try
        {
            node2.createChildNode( "ShouldError" );
            fail( "Allowed createChildNode after done" );
        }
        catch ( IllegalStateException expected )
        {
        }

        node.done();

        assertEquals( "" + //
                      "ABC(a=A b=B c=C)\n" + //
                      ".D.E.F_(e=E)\n" + //
                      "..GHI(g_=G)\n" + //
                      ".DEF2(f=F)\n" + //
                      "", actual.toString() );
    }

    public void testSource()
            throws Exception
    {
        String sourceString = "" + //
                              "ABC(a=A b=B c=C)\n" + //
                              ".DEF(e=E)\n" + //
                              "..GHI(g=G)\n" + //
                              ".DEF2(f=F)\n" + //
                              "";
        HierarchicalDataSource source = HierarchicalDataSimpleFactory.INSTANCE.createSource( sourceString );

        HierarchicalDataSource.Node node = source.getRootNode();

        try
        {
            source.getRootNode();
            fail( "Allowed 2nd getRootNode" );
        }
        catch ( IllegalStateException expected )
        {
        }

        assertEquals( "ABC", node.getName() );
        assertEquals( "a\nb\nc\n", Strings.linesToString( node.getAttributeNames() ) );

        assertEquals( "A", node.getAttributeValue( "a" ) );
        assertEquals( "B", node.getAttributeValue( "b" ) );
        assertEquals( "C", node.getAttributeValue( "c" ) );

        HierarchicalDataSource.Node node1 = node.getNextChild();
        assertEquals( "DEF", node1.getName() );
        assertEquals( "e\n", Strings.linesToString( node1.getAttributeNames() ) );

        assertEquals( "E", node1.getAttributeValue( "e" ) );

        HierarchicalDataSource.Node node11 = node1.getNextChild();
        assertEquals( "GHI", node11.getName() );
        assertEquals( "g\n", Strings.linesToString( node11.getAttributeNames() ) );

        assertEquals( "G", node11.getAttributeValue( "g" ) );

        assertNull( node11.getNextChild() );

        assertNull( node1.getNextChild() );

        HierarchicalDataSource.Node node2 = node.getNextChild();
        assertEquals( "DEF2", node2.getName() );
        assertEquals( "f\n", Strings.linesToString( node2.getAttributeNames() ) );

        assertEquals( "F", node2.getAttributeValue( "f" ) );

        assertNull( node2.getNextChild() );

        assertNull( node.getNextChild() );
    }

    public void testSource2()
            throws Exception
    {
        String sourceString = "" + //
                              "ABC()\n" + //
                              ".  DEF  (e_.=E)\n" + //
                              "..GHI(  g  =  G   )\n" + //
                              "";
        HierarchicalDataSource source = HierarchicalDataSimpleFactory.INSTANCE.createSource( sourceString );

        HierarchicalDataSource.Node node = source.getRootNode();

        assertEquals( "ABC", node.getName() );
        String[] names = node.getAttributeNames();
        assertEquals( 0, names.length );

        HierarchicalDataSource.Node node1 = node.getNextChild();
        assertEquals( "DEF", node1.getName() );
        assertEquals( "e_.\n", Strings.linesToString( node1.getAttributeNames() ) );

        assertEquals( "E", node1.getAttributeValue( "e_." ) );

        HierarchicalDataSource.Node node11 = node1.getNextChild();
        assertEquals( "GHI", node11.getName() );
        assertEquals( "g\n", Strings.linesToString( node11.getAttributeNames() ) );

        assertEquals( "G", node11.getAttributeValue( "g" ) );

        assertNull( node11.getNextChild() );

        assertNull( node1.getNextChild() );

        assertNull( node.getNextChild() );
    }

    public void testRoundTrip()
            throws Exception
    {
        chkRoundTrip( "ABC(a=A b=B c=C)\n" + //
                      ".DEF(e=E)\n" + //
                      "..GHI(g=G)\n" + //
                      ".DEF2(f=F)\n" + //
                      "" );
        chkRoundTrip( "ABC(a='  \\r\\n\\\\'' \\x001F')\n" );
    }

    private void chkRoundTrip( String pSourceString )
            throws Exception
    {
        StringBuilder zSinkBuffer = new StringBuilder();

        HierarchicalDataFactory factory = HierarchicalDataSimpleFactory.INSTANCE;

        HierarchicalDataSource.Node sourceNode = factory.createSource( pSourceString ).getRootNode();
        HierarchicalDataSink.Node sinkNode = factory.createSink( zSinkBuffer ).createRootNode( sourceNode.getName() );

        copy( sourceNode, sinkNode );

        String zSinkString = zSinkBuffer.toString();

        assertEquals( pSourceString, zSinkString );
    }

    private void copy( HierarchicalDataSource.Node pFrom, HierarchicalDataSink.Node pTo )
    {
        String[] names = pFrom.getAttributeNames();
        if ( names != null )
        {
            for ( String name : names )
            {
                pTo.setAttribute( name, pFrom.getAttributeValue( name ) );
            }
        }

        for ( HierarchicalDataSource.Node child; null != (child = pFrom.getNextChild()); )
        {
            copy( child, pTo.createChildNode( child.getName() ) );
        }
        pTo.done();
    }
}

Commits for litesoft/trunk/Java/core/Server/tests/org/litesoft/core/hierarchicaldata/HierarchicalDataSimpleFactoryTest.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

803 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:08:34 +0000
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000