Skip to content

Commit 3cbdd49

Browse files
committed
updated Jacoco dependency version, improved tree of exceptions, covered some areas not-covered by tests before
1 parent ed4df18 commit 3cbdd49

File tree

8 files changed

+119
-29
lines changed

8 files changed

+119
-29
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
<plugin>
9999
<groupId>org.jacoco</groupId>
100100
<artifactId>jacoco-maven-plugin</artifactId>
101-
<version>0.7.2.201409121644</version>
101+
<version>0.7.4.201502262128</version>
102102
<executions>
103103
<execution>
104104
<goals>

src/main/java/com/igormaznitsa/jbbp/compiler/JBBPCompiledBlock.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.igormaznitsa.jbbp.compiler.varlen.JBBPIntegerValueEvaluator;
1919
import com.igormaznitsa.jbbp.exceptions.JBBPException;
20+
import com.igormaznitsa.jbbp.exceptions.JBBPIllegalArgumentException;
2021
import com.igormaznitsa.jbbp.utils.JBBPUtils;
2122
import java.util.*;
2223

@@ -251,6 +252,6 @@ public int findFieldOffsetForPath(final String fieldPath) {
251252
return f.getFieldOffsetInCompiledBlock();
252253
}
253254
}
254-
throw new JBBPException("Unknown field path [" + fieldPath + ']');
255+
throw new JBBPIllegalArgumentException("Unknown field path [" + fieldPath + ']');
255256
}
256257
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2015 Igor Maznitsa.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.igormaznitsa.jbbp.exceptions;
17+
18+
/**
19+
* The Exception can be thrown for transport errors
20+
*
21+
* @since 1.1.1
22+
*/
23+
public class JBBPIOException extends JBBPException {
24+
private static final long serialVersionUID = 1151192250682443895L;
25+
26+
public JBBPIOException(final String message) {
27+
super(message);
28+
}
29+
30+
public JBBPIOException(final String message, final Throwable cause) {
31+
super(message, cause);
32+
}
33+
34+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2015 Igor Maznitsa.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.igormaznitsa.jbbp.exceptions;
17+
18+
/**
19+
* The Exception can be thrown when detected incompatible or unusable parameter.
20+
*
21+
* @since 1.1.1
22+
*/
23+
public class JBBPIllegalArgumentException extends JBBPIOException {
24+
private static final long serialVersionUID = 2811626713945893782L;
25+
26+
public JBBPIllegalArgumentException(String message) {
27+
super(message);
28+
}
29+
30+
public JBBPIllegalArgumentException(String message, Throwable cause) {
31+
super(message, cause);
32+
}
33+
34+
}

src/main/java/com/igormaznitsa/jbbp/io/AbstractMappedClassFieldObserver.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.igormaznitsa.jbbp.io;
1717

1818
import com.igormaznitsa.jbbp.exceptions.JBBPException;
19+
import com.igormaznitsa.jbbp.exceptions.JBBPIllegalArgumentException;
1920
import com.igormaznitsa.jbbp.mapper.Bin;
2021
import com.igormaznitsa.jbbp.mapper.BinType;
2122
import com.igormaznitsa.jbbp.model.*;
@@ -152,12 +153,12 @@ protected void processObject(final Object obj, final Field field, final Object c
152153
if (binAnno == null) {
153154
binAnno = f.getDeclaringClass().getAnnotation(Bin.class);
154155
if (binAnno == null) {
155-
throw new JBBPException("Can't find any Bin annotation to use for " + f + " field");
156+
throw new JBBPIllegalArgumentException("Can't find any Bin annotation to use for " + f + " field");
156157
}
157158
}
158159

159160
if (binAnno.custom() && customFieldProcessor == null) {
160-
throw new IllegalArgumentException("The Class '" + obj.getClass().getName() + "' contains the field '" + f.getName() + "\' which is a custom one, you must provide a JBBPCustomFieldWriter instance to save the field.");
161+
throw new JBBPIllegalArgumentException("The Class '" + obj.getClass().getName() + "' contains the field '" + f.getName() + "\' which is a custom one, you must provide a JBBPCustomFieldWriter instance to save the field.");
161162
}
162163

163164
processObjectField(obj, f, binAnno, customFieldProcessor);
@@ -477,7 +478,7 @@ else if (fieldType.getComponentType() == double.class) {
477478
}
478479

479480
/**
480-
* INside auxiliary method to read object field value.
481+
* Inside auxiliary method to read object field value.
481482
*
482483
* @param obj an object which field is read
483484
* @param field a field to be read
@@ -490,7 +491,7 @@ private static Object readFieldValue(final Object obj, final Field field) {
490491
return field.get(obj);
491492
}
492493
catch (Exception ex) {
493-
throw new JBBPException("Can't read falue of a field [" + field + ']', ex);
494+
throw new JBBPException("Can't get falue from field [" + field + ']', ex);
494495
}
495496
}
496497

src/main/java/com/igormaznitsa/jbbp/io/JBBPOut.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.igormaznitsa.jbbp.io;
1717

18-
import com.igormaznitsa.jbbp.exceptions.JBBPException;
18+
import com.igormaznitsa.jbbp.exceptions.JBBPIOException;
1919
import com.igormaznitsa.jbbp.mapper.Bin;
2020
import com.igormaznitsa.jbbp.model.*;
2121
import com.igormaznitsa.jbbp.utils.JBBPUtils;
@@ -960,7 +960,7 @@ protected void onFieldLong(final Object obj, final Field field, final Bin annota
960960
try{
961961
this.Long(value);
962962
}catch(IOException ex){
963-
throw new JBBPException("Can't write long value", ex);
963+
throw new JBBPIOException("Can't write long value", ex);
964964
}
965965
}
966966

@@ -970,7 +970,7 @@ protected void onFieldInt(final Object obj, final Field field, final Bin annotat
970970
this.Int(value);
971971
}
972972
catch (IOException ex) {
973-
throw new JBBPException("Can't write int value", ex);
973+
throw new JBBPIOException("Can't write int value", ex);
974974
}
975975
}
976976

@@ -980,7 +980,7 @@ protected void onFieldShort(final Object obj, final Field field, final Bin annot
980980
this.Short(value);
981981
}
982982
catch (IOException ex) {
983-
throw new JBBPException("Can't write short value", ex);
983+
throw new JBBPIOException("Can't write short value", ex);
984984
}
985985
}
986986

@@ -990,7 +990,7 @@ protected void onFieldByte(final Object obj, final Field field, final Bin annota
990990
this.Byte(value);
991991
}
992992
catch (IOException ex) {
993-
throw new JBBPException("Can't write byte value", ex);
993+
throw new JBBPIOException("Can't write byte value", ex);
994994
}
995995
}
996996

@@ -1000,7 +1000,7 @@ protected void onFieldBool(final Object obj, final Field field, final Bin annota
10001000
this.Bool(value, annotation.bitOrder());
10011001
}
10021002
catch (IOException ex) {
1003-
throw new JBBPException("Can't write bool value", ex);
1003+
throw new JBBPIOException("Can't write bool value", ex);
10041004
}
10051005
}
10061006

@@ -1010,7 +1010,7 @@ protected void onFieldBits(final Object obj, final Field field, final Bin annota
10101010
this.Bits(bitNumber, value);
10111011
}
10121012
catch (IOException ex) {
1013-
throw new JBBPException("Can't write bit value", ex);
1013+
throw new JBBPIOException("Can't write bit value", ex);
10141014
}
10151015
}
10161016

@@ -1021,7 +1021,7 @@ protected void onFieldCustom(final Object obj, final Field field, final Bin anno
10211021
writer.writeCustomField(this, this.outStream, obj, field, annotation, value);
10221022
}
10231023
catch (IOException ex) {
1024-
throw new JBBPException("Can't write custom field", ex);
1024+
throw new JBBPIOException("Can't write custom field", ex);
10251025
}
10261026
}
10271027

src/main/java/com/igormaznitsa/jbbp/utils/JBBPTextWriter.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package com.igormaznitsa.jbbp.utils;
1717

18-
import com.igormaznitsa.jbbp.exceptions.JBBPException;
18+
import com.igormaznitsa.jbbp.exceptions.JBBPIOException;
1919
import com.igormaznitsa.jbbp.io.*;
2020
import com.igormaznitsa.jbbp.mapper.Bin;
2121
import java.io.*;
@@ -114,7 +114,7 @@ protected void onArrayEnd(final Object obj, final Field field, final Bin annotat
114114
HR();
115115
}
116116
catch (IOException ex) {
117-
throw new JBBPException("Can't log array field", ex);
117+
throw new JBBPIOException("Can't log array field", ex);
118118
}
119119
finally {
120120
arrayCounter--;
@@ -135,7 +135,7 @@ protected void onArrayStart(final Object obj, final Field field, final Bin annot
135135
IndentInc();
136136
}
137137
catch (IOException ex) {
138-
throw new JBBPException("Can't log array field", ex);
138+
throw new JBBPIOException("Can't log array field", ex);
139139
}
140140
finally {
141141
arrayCounter++;
@@ -152,7 +152,7 @@ protected void onStructEnd(final Object obj, final Field field, final Bin annota
152152
this.arrayCounter = this.counterStack.pop();
153153
}
154154
catch (IOException ex) {
155-
throw new JBBPException("Can't log struct field", ex);
155+
throw new JBBPIOException("Can't log struct field", ex);
156156
}
157157
}
158158

@@ -167,7 +167,7 @@ protected void onStructStart(final Object obj, final Field field, final Bin anno
167167
IndentInc();
168168
}
169169
catch (IOException ex) {
170-
throw new JBBPException("Can't log short field", ex);
170+
throw new JBBPIOException("Can't log short field", ex);
171171
}
172172
}
173173

@@ -180,7 +180,7 @@ protected void onFieldLong(final Object obj, final Field field, final Bin annota
180180
}
181181
}
182182
catch (IOException ex) {
183-
throw new JBBPException("Can't log short field", ex);
183+
throw new JBBPIOException("Can't log short field", ex);
184184
}
185185
}
186186

@@ -193,7 +193,7 @@ protected void onFieldInt(final Object obj, final Field field, final Bin annotat
193193
}
194194
}
195195
catch (IOException ex) {
196-
throw new JBBPException("Can't log int field", ex);
196+
throw new JBBPIOException("Can't log int field", ex);
197197
}
198198
}
199199

@@ -206,7 +206,7 @@ protected void onFieldShort(final Object obj, final Field field, final Bin annot
206206
}
207207
}
208208
catch (IOException ex) {
209-
throw new JBBPException("Can't log short field", ex);
209+
throw new JBBPIOException("Can't log short field", ex);
210210
}
211211
}
212212

@@ -219,7 +219,7 @@ protected void onFieldByte(final Object obj, final Field field, final Bin annota
219219
}
220220
}
221221
catch (IOException ex) {
222-
throw new JBBPException("Can't log byte field", ex);
222+
throw new JBBPIOException("Can't log byte field", ex);
223223
}
224224
}
225225

@@ -232,7 +232,7 @@ protected void onFieldBool(final Object obj, final Field field, final Bin annota
232232
}
233233
}
234234
catch (IOException ex) {
235-
throw new JBBPException("Can't log boolean field", ex);
235+
throw new JBBPIOException("Can't log boolean field", ex);
236236
}
237237
}
238238

@@ -245,7 +245,7 @@ protected void onFieldBits(final Object obj, final Field field, final Bin annota
245245
}
246246
}
247247
catch (IOException ex) {
248-
throw new JBBPException("Can't log bit field", ex);
248+
throw new JBBPIOException("Can't log bit field", ex);
249249
}
250250
}
251251

@@ -271,7 +271,7 @@ protected void onFieldCustom(final Object obj, final Field field, final Bin anno
271271
}
272272
}
273273
catch (IOException ex) {
274-
throw new JBBPException("Can't log custom field", ex);
274+
throw new JBBPIOException("Can't log custom field", ex);
275275
}
276276
}
277277

src/test/java/com/igormaznitsa/jbbp/io/JBBPOutTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.igormaznitsa.jbbp.io;
1717

18+
import com.igormaznitsa.jbbp.exceptions.JBBPIllegalArgumentException;
1819
import static com.igormaznitsa.jbbp.io.JBBPOut.*;
1920
import com.igormaznitsa.jbbp.mapper.Bin;
2021
import com.igormaznitsa.jbbp.mapper.BinType;
@@ -36,8 +37,12 @@ public void testBeginBin() throws Exception {
3637
assertArrayEquals(new byte[]{(byte) 0x80}, BeginBin(JBBPBitOrder.MSB0).Byte(1).End().toByteArray());
3738
assertArrayEquals(new byte[]{(byte) 0x80}, BeginBin(1).Byte(0x80).End().toByteArray());
3839

39-
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
40-
assertSame(buffer, BeginBin(buffer).End());
40+
final ByteArrayOutputStream buffer1 = new ByteArrayOutputStream();
41+
assertSame(buffer1, BeginBin(buffer1).End());
42+
43+
final ByteArrayOutputStream buffer2 = new ByteArrayOutputStream();
44+
BeginBin(buffer2,JBBPByteOrder.LITTLE_ENDIAN, JBBPBitOrder.MSB0).Short(1234).End();
45+
assertArrayEquals(new byte[]{(byte)0x4b, (byte)0x20}, buffer2.toByteArray());
4146
}
4247

4348
@Test
@@ -720,6 +725,21 @@ class Test {
720725
assertArrayEquals(new byte[]{1, (byte) 0x40, 3}, JBBPOut.BeginBin().Bin(new Test((byte) 1, (byte) 2, (byte) 3)).End().toByteArray());
721726
}
722727

728+
@Test
729+
public void testBin_Byte_StringAsByteArray() throws Exception {
730+
assertArrayEquals(new byte[0], JBBPOut.BeginBin().Byte("", JBBPBitOrder.LSB0).End().toByteArray());
731+
assertArrayEquals(new byte[0], JBBPOut.BeginBin().Byte("", JBBPBitOrder.MSB0).End().toByteArray());
732+
assertArrayEquals(new byte[]{65,66,67,68}, JBBPOut.BeginBin().Byte("ABCD", JBBPBitOrder.LSB0).End().toByteArray());
733+
assertArrayEquals(new byte[]{(byte)130,66,(byte)194,34}, JBBPOut.BeginBin().Byte("ABCD", JBBPBitOrder.MSB0).End().toByteArray());
734+
}
735+
736+
@Test
737+
public void testBin_Byte_StringAsShortArray() throws Exception {
738+
assertArrayEquals(new byte[0], JBBPOut.BeginBin().Short("", JBBPBitOrder.LSB0).End().toByteArray());
739+
assertArrayEquals(new byte[0], JBBPOut.BeginBin().Short("", JBBPBitOrder.MSB0).End().toByteArray());
740+
assertArrayEquals(new byte[]{0x04, 0x10, 0x04, 0x11, 0x04, 0x12, 0x04, 0x13, 0x04, 0x14}, JBBPOut.BeginBin().Short("АБВГД", JBBPBitOrder.LSB0).End().toByteArray());
741+
assertArrayEquals(new byte[]{0x08, 0x20, (byte)0x88, 0x20, (byte)0x48, 0x20, (byte)0xC8, 0x20, (byte)0x28, 0x20}, JBBPOut.BeginBin().Short("АБВГД", JBBPBitOrder.MSB0).End().toByteArray());
742+
}
723743

724744
@Bin
725745
private static class TestWithStaticField {
@@ -1180,7 +1200,7 @@ class Test {
11801200
assertEquals(2, JBBPOut.BeginBin().Bin(new Test((byte)12,(byte)24)).End().toByteArray().length);
11811201
}
11821202

1183-
@Test(expected = IllegalArgumentException.class)
1203+
@Test(expected = JBBPIllegalArgumentException.class)
11841204
public void testBin_CustomField_ErrorBecauseNoCustomWriter() throws Exception {
11851205
class Test {
11861206

0 commit comments

Comments
 (0)