-
Notifications
You must be signed in to change notification settings - Fork 23
/
UperEncoderExample1BasicTest.java
170 lines (145 loc) · 5.14 KB
/
UperEncoderExample1BasicTest.java
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
package net.gcdc.asn1.uper;
import static org.junit.Assert.assertEquals;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
import net.gcdc.asn1.datatypes.Asn1BigInteger;
import net.gcdc.asn1.datatypes.Asn1Optional;
import net.gcdc.asn1.datatypes.Asn1SequenceOf;
import net.gcdc.asn1.datatypes.Asn1String;
import net.gcdc.asn1.datatypes.CharacterRestriction;
import net.gcdc.asn1.datatypes.RestrictedString;
import net.gcdc.asn1.datatypes.Sequence;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UperEncoderExample1BasicTest {
private final static Logger logger = LoggerFactory.getLogger(UperEncoder.class);
/**
* Example from the Standard on UPER.
<pre>
PersonnelRecord ::= [APPLICATION 0] IMPLICIT SET {
name Name,
title [0] VisibleString,
number EmployeeNumber,
dateOfHire [1] Date,
nameOfSpouse [2] Name,
children [3] IMPLICIT
SEQUENCE OF ChildInformation DEFAULT {} }
ChildInformation ::= SET {
name Name,
dateOfBirth [0] Date}
Name ::= [APPLICATION 1] IMPLICIT SEQUENCE {
givenName VisibleString,
initial VisibleString,
familyName VisibleString }
EmployeeNumber ::= [APPLICATION 2] IMPLICIT INTEGER
Date ::= [APPLICATION 3] IMPLICIT VisibleString -- YYYYMMDD
</pre>
*/
@Sequence
public static class PersonenelRecord {
Name name;
EmployeeNumber number;
@RestrictedString(CharacterRestriction.VisibleString)
String title;
Date dateOfHire;
Name nameOfSpouse;
@Asn1Optional SequenceOfChildInformation sequenceOfChildInformation = new SequenceOfChildInformation();
public PersonenelRecord() {
this(new Name(), new EmployeeNumber(), "", new Date(), new Name(), new SequenceOfChildInformation());
}
public PersonenelRecord(
Name name,
EmployeeNumber number,
String title,
Date dateOfHire,
Name nameOfSpouse,
SequenceOfChildInformation sequenceOfChildInformation
) {
this.name = name;
this.number = number;
this.title = title;
this.dateOfHire = dateOfHire;
this.nameOfSpouse = nameOfSpouse;
this.sequenceOfChildInformation = sequenceOfChildInformation;
}
}
@Sequence
public static class Name {
@RestrictedString(CharacterRestriction.VisibleString)
String givenName;
@RestrictedString(CharacterRestriction.VisibleString)
String initial;
@RestrictedString(CharacterRestriction.VisibleString)
String familyName;
public Name() { this("", "", ""); }
public Name(String givenName, String initial, String familyName) {
this.givenName = givenName;
this.initial = initial;
this.familyName = familyName;
}
}
public static class EmployeeNumber extends Asn1BigInteger {
public EmployeeNumber() { this(0); }
public EmployeeNumber(long value) { this(BigInteger.valueOf(value)); }
public EmployeeNumber(BigInteger value) { super(value); }
}
@RestrictedString(CharacterRestriction.VisibleString)
public static class Date extends Asn1String {
public Date() { this(""); }
public Date(String value) { super(value); }
}
@Sequence
public static class ChildInformation {
Name name;
Date dateOfBirth;
public ChildInformation() { this(new Name(), new Date()); }
public ChildInformation(Name name, Date dateOfBirth) {
this.name = name;
this.dateOfBirth = dateOfBirth;
}
}
public static class SequenceOfChildInformation extends Asn1SequenceOf<ChildInformation> {
public SequenceOfChildInformation() { super(); }
public SequenceOfChildInformation(Collection<ChildInformation> coll) { super(coll); }
}
@Test public void test() throws IllegalArgumentException, IllegalAccessException {
PersonenelRecord record = new PersonenelRecord(
new Name(
"John",
"P",
"Smith"
),
new EmployeeNumber(51),
"Director",
new Date("19710917"),
new Name(
"Mary",
"T",
"Smith"),
new SequenceOfChildInformation(Arrays.asList(
new ChildInformation(
new Name(
"Ralph",
"T",
"Smith"
),
new Date("19571111")
),
new ChildInformation(
new Name(
"Susan",
"B",
"Jones"
),
new Date("19590717")
)
))
);
byte[] encoded = UperEncoder.encode(record);
logger.debug("data hex: {}", UperEncoder.hexStringFromBytes(encoded));
assertEquals("824ADFA3700D005A7B74F4D0026611134F2CB8FA6FE410C5CB762C1CB16E09370F2F20350169EDD3D340102D2C3B386801A80B4F6E9E9A0218B96ADD8B162C4169F5E787700C20595BF765E610C5CB572C1BB16E",
UperEncoder.hexStringFromBytes(encoded));
}
}