Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
Open sidebar
Rajiv Prabhakar
cava
Commits
71581a14
Commit
71581a14
authored
Jun 24, 2017
by
Rajiv Prabhakar
Browse files
v1.6.0 release:
- Added Validatec array-length-check, string-length-check and string-matches-check
parent
47d97c54
Pipeline
#9294738
failed with stage
in 1 minute and 11 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
182 additions
and
3 deletions
+182
-3
pom.xml
pom.xml
+1
-1
src/main/java/org/rajivprab/cava/Validatec.java
src/main/java/org/rajivprab/cava/Validatec.java
+88
-0
src/test/java/org/rajivprab/cava/ValidatecTest.java
src/test/java/org/rajivprab/cava/ValidatecTest.java
+93
-2
No files found.
pom.xml
View file @
71581a14
...
...
@@ -6,7 +6,7 @@
<groupId>
com.rajivprab
</groupId>
<artifactId>
cava
</artifactId>
<version>
1.
5
.0
</version>
<version>
1.
6
.0
</version>
<name>
Cava: Clean Java
</name>
<description>
A library that enables users to write minimal, clean and simple Java code
</description>
...
...
src/main/java/org/rajivprab/cava/Validatec.java
View file @
71581a14
...
...
@@ -4,6 +4,7 @@ import com.google.common.base.Strings;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.Map
;
...
...
@@ -71,6 +72,93 @@ public class Validatec {
return
"Size check failed. Expecting: "
+
size
+
", found: "
+
collection
;
}
// ---------- Array.length
public
static
void
length
(
Object
[]
array
,
int
length
)
{
length
(
array
,
length
,
getArrayLengthMessage
(
array
,
length
));
}
public
static
void
length
(
Object
[]
array
,
int
length
,
String
message
)
{
length
(
array
,
length
,
message
,
DEFAULT_EXCEPTION
);
}
public
static
void
length
(
Object
[]
array
,
int
length
,
Class
<?
extends
RuntimeException
>
exceptionType
)
{
length
(
array
,
length
,
getArrayLengthMessage
(
array
,
length
),
exceptionType
);
}
public
static
void
length
(
Object
[]
array
,
int
length
,
String
message
,
Class
<?
extends
RuntimeException
>
type
)
{
length
(
array
,
length
,
()
->
throwException
(
message
,
type
));
}
public
static
void
length
(
Object
[]
array
,
int
length
,
Runnable
runnable
)
{
if
(
array
==
null
||
array
.
length
!=
length
)
{
log
.
error
(
getArrayLengthMessage
(
array
,
length
));
runnable
.
run
();
}
}
private
static
String
getArrayLengthMessage
(
Object
[]
array
,
int
length
)
{
return
"Array length check failed. Expecting: "
+
length
+
", found: "
+
Arrays
.
toString
(
array
);
}
// ---------- String.length
public
static
void
length
(
String
string
,
int
length
)
{
length
(
string
,
length
,
getStringLengthMessage
(
string
,
length
));
}
public
static
void
length
(
String
string
,
int
length
,
String
message
)
{
length
(
string
,
length
,
message
,
DEFAULT_EXCEPTION
);
}
public
static
void
length
(
String
string
,
int
length
,
Class
<?
extends
RuntimeException
>
exceptionType
)
{
length
(
string
,
length
,
getStringLengthMessage
(
string
,
length
),
exceptionType
);
}
public
static
void
length
(
String
string
,
int
length
,
String
message
,
Class
<?
extends
RuntimeException
>
exceptionType
)
{
length
(
string
,
length
,
()
->
throwException
(
message
,
exceptionType
));
}
public
static
void
length
(
String
string
,
int
length
,
Runnable
runnable
)
{
if
(
string
==
null
||
string
.
length
()
!=
length
)
{
log
.
error
(
getStringLengthMessage
(
string
,
length
));
runnable
.
run
();
}
}
private
static
String
getStringLengthMessage
(
String
string
,
int
length
)
{
return
"String length check failed. Expecting: "
+
length
+
", found: "
+
string
;
}
// ---------- String.matches
public
static
void
matches
(
String
string
,
String
regex
)
{
matches
(
string
,
regex
,
getStringMatchesMessage
(
string
,
regex
));
}
public
static
void
matches
(
String
string
,
String
regex
,
String
message
)
{
matches
(
string
,
regex
,
message
,
DEFAULT_EXCEPTION
);
}
public
static
void
matches
(
String
string
,
String
regex
,
Class
<?
extends
RuntimeException
>
exceptionType
)
{
matches
(
string
,
regex
,
getStringMatchesMessage
(
string
,
regex
),
exceptionType
);
}
public
static
void
matches
(
String
string
,
String
regex
,
String
message
,
Class
<?
extends
RuntimeException
>
exceptionType
)
{
matches
(
string
,
regex
,
()
->
throwException
(
message
,
exceptionType
));
}
public
static
void
matches
(
String
string
,
String
regex
,
Runnable
runnable
)
{
if
(
regex
==
null
||
string
==
null
||
!
string
.
matches
(
regex
))
{
log
.
error
(
getStringMatchesMessage
(
string
,
regex
));
runnable
.
run
();
}
}
private
static
String
getStringMatchesMessage
(
String
string
,
String
regex
)
{
return
"String matches check failed. Expecting: "
+
regex
+
", found: "
+
string
;
}
// ---------- Less than - Numbers
public
static
<
T
>
void
lessThan
(
Comparable
<
T
>
one
,
T
two
)
{
...
...
src/test/java/org/rajivprab/cava/ValidatecTest.java
View file @
71581a14
...
...
@@ -108,7 +108,7 @@ public class ValidatecTest extends TestBase {
Validatec
.
size
(
ImmutableList
.
of
(
1
,
2
),
3
,
ArithmeticException
.
class
);
Assert
.
fail
();
}
catch
(
ArithmeticException
e
)
{
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"Size check failed. Expecting: 3"
);
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"Size check failed. Expecting: 3
, found: [1, 2]
"
);
}
}
...
...
@@ -118,7 +118,98 @@ public class ValidatecTest extends TestBase {
Validatec
.
size
(
null
,
3
,
ArithmeticException
.
class
);
Assert
.
fail
();
}
catch
(
ArithmeticException
e
)
{
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"Size check failed. Expecting: 3"
);
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"Size check failed. Expecting: 3, found: null"
);
}
}
// ----- Array.length
@Test
public
void
matchesArrayLength_allGood
()
{
Validatec
.
length
(
new
Integer
[]{
1
,
2
},
2
);
}
@Test
public
void
doesNotMatchArrayLength_throwDefaultException
()
{
try
{
Validatec
.
length
(
new
Character
[]{
'a'
,
'b'
},
3
,
ArithmeticException
.
class
);
Assert
.
fail
();
}
catch
(
ArithmeticException
e
)
{
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"Array length check failed. Expecting: 3, found: [a, b]"
);
}
}
@Test
public
void
nullArray_throwExceptionGiven
()
{
try
{
Validatec
.
length
((
Object
[])
null
,
3
,
ArithmeticException
.
class
);
Assert
.
fail
();
}
catch
(
ArithmeticException
e
)
{
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"Array length check failed. Expecting: 3, found: null"
);
}
}
// ----- String.length
@Test
public
void
matchesStringLength_allGood
()
{
Validatec
.
length
(
"12"
,
2
);
}
@Test
public
void
doesNotMatchStringLength_throwDefaultException
()
{
try
{
Validatec
.
length
(
"ab"
,
3
,
ArithmeticException
.
class
);
Assert
.
fail
();
}
catch
(
ArithmeticException
e
)
{
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"String length check failed. Expecting: 3, found: ab"
);
}
}
@Test
public
void
nullString_throwExceptionGiven
()
{
try
{
Validatec
.
length
((
String
)
null
,
3
,
ArithmeticException
.
class
);
Assert
.
fail
();
}
catch
(
ArithmeticException
e
)
{
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"String length check failed. Expecting: 3, found: null"
);
}
}
// ----- String.matches
@Test
public
void
matchesString_allGood
()
{
Validatec
.
matches
(
"abc"
,
"[cab]+"
);
}
@Test
public
void
doesNotMatchString_throwDefaultException
()
{
try
{
Validatec
.
matches
(
"abc"
,
"[ab]+"
,
ArithmeticException
.
class
);
Assert
.
fail
();
}
catch
(
ArithmeticException
e
)
{
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"String matches check failed. Expecting: [ab]+, found: abc"
);
}
}
@Test
public
void
nullString_failMatch_throwExceptionGiven
()
{
try
{
Validatec
.
matches
(
null
,
"[abc]+"
,
ArithmeticException
.
class
);
Assert
.
fail
();
}
catch
(
ArithmeticException
e
)
{
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"String matches check failed. Expecting: [abc]+, found: null"
);
}
}
@Test
public
void
nullRegex_failMatch_throwExceptionGiven
()
{
try
{
Validatec
.
matches
(
"abc"
,
null
,
ArithmeticException
.
class
);
Assert
.
fail
();
}
catch
(
ArithmeticException
e
)
{
Truth
.
assertThat
(
e
).
hasMessageThat
().
contains
(
"String matches check failed. Expecting: null, found: abc"
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment