Toggle navigation
Toggle navigation
This project
Loading...
Sign in
홍길동
/
onos
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Thomas Vachuska
2014-11-11 11:46:49 -0800
Browse Files
Options
Browse Files
Download
Plain Diff
Committed by
Gerrit Code Review
2014-11-11 11:46:49 -0800
Commit
702dae890582b90672dfb00ec8270faf786ad0fe
702dae89
2 parents
fc274c97
1e20711a
Merge "Unit tests for the DefaultFlowRule class"
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
201 additions
and
8 deletions
core/api/src/main/java/org/onlab/onos/net/flow/DefaultFlowRule.java
core/net/src/test/java/org/onlab/onos/net/flow/DefaultFlowRuleTest.java
utils/junit/src/main/java/org/onlab/junit/ImmutableClassChecker.java
core/api/src/main/java/org/onlab/onos/net/flow/DefaultFlowRule.java
View file @
702dae8
...
...
@@ -58,7 +58,7 @@ public class DefaultFlowRule implements FlowRule {
}
public
DefaultFlowRule
(
DeviceId
deviceId
,
TrafficSelector
selector
,
TrafficTreatment
treat
e
ment
,
int
priority
,
ApplicationId
appId
,
TrafficTreatment
treatment
,
int
priority
,
ApplicationId
appId
,
int
timeout
,
boolean
permanent
)
{
if
(
priority
<
FlowRule
.
MIN_PRIORITY
)
{
...
...
@@ -68,7 +68,7 @@ public class DefaultFlowRule implements FlowRule {
this
.
deviceId
=
deviceId
;
this
.
priority
=
priority
;
this
.
selector
=
selector
;
this
.
treatment
=
treat
e
ment
;
this
.
treatment
=
treatment
;
this
.
appId
=
appId
.
id
();
this
.
timeout
=
timeout
;
this
.
permanent
=
permanent
;
...
...
core/net/src/test/java/org/onlab/onos/net/flow/DefaultFlowRuleTest.java
0 → 100644
View file @
702dae8
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
onlab
.
onos
.
net
.
flow
;
import
org.junit.Test
;
import
org.onlab.onos.net.DeviceId
;
import
org.onlab.onos.net.intent.IntentTestsMocks
;
import
com.google.common.testing.EqualsTester
;
import
static
org
.
hamcrest
.
MatcherAssert
.
assertThat
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
onlab
.
junit
.
ImmutableClassChecker
.
assertThatClassIsImmutableBaseClass
;
import
static
org
.
onlab
.
onos
.
net
.
NetTestTools
.
did
;
import
static
org
.
onlab
.
onos
.
net
.
NetTestTools
.
APP_ID
;
/**
* Unit tests for the default flow rule class.
*/
public
class
DefaultFlowRuleTest
{
private
static
final
IntentTestsMocks
.
MockSelector
SELECTOR
=
new
IntentTestsMocks
.
MockSelector
();
private
static
final
IntentTestsMocks
.
MockTreatment
TREATMENT
=
new
IntentTestsMocks
.
MockTreatment
();
final
FlowRule
flowRule1
=
new
MockFlowRule
(
1
);
final
FlowRule
sameAsFlowRule1
=
new
MockFlowRule
(
1
);
final
FlowRule
flowRule2
=
new
MockFlowRule
(
2
);
final
DefaultFlowRule
defaultFlowRule1
=
new
DefaultFlowRule
(
flowRule1
);
final
DefaultFlowRule
sameAsDefaultFlowRule1
=
new
DefaultFlowRule
(
sameAsFlowRule1
);
final
DefaultFlowRule
defaultFlowRule2
=
new
DefaultFlowRule
(
flowRule2
);
private
static
class
MockFlowRule
implements
FlowRule
{
int
priority
;
MockFlowRule
(
int
priority
)
{
this
.
priority
=
priority
;
}
@Override
public
FlowId
id
()
{
return
FlowId
.
valueOf
(
1
);
}
@Override
public
short
appId
()
{
return
0
;
}
@Override
public
int
priority
()
{
return
priority
;
}
@Override
public
DeviceId
deviceId
()
{
return
did
(
"1"
);
}
@Override
public
TrafficSelector
selector
()
{
return
SELECTOR
;
}
@Override
public
TrafficTreatment
treatment
()
{
return
TREATMENT
;
}
@Override
public
int
timeout
()
{
return
0
;
}
@Override
public
boolean
isPermanent
()
{
return
false
;
}
}
/**
* Checks that the DefaultFlowRule class is immutable but can be inherited
* from.
*/
@Test
public
void
testImmutability
()
{
assertThatClassIsImmutableBaseClass
(
DefaultFlowRule
.
class
);
}
/**
* Tests the equals, hashCode and toString methods using Guava EqualsTester.
*/
@Test
public
void
testEquals
()
{
new
EqualsTester
()
.
addEqualityGroup
(
defaultFlowRule1
,
sameAsDefaultFlowRule1
)
.
addEqualityGroup
(
defaultFlowRule2
)
.
testEquals
();
}
/**
* Tests creation of a DefaultFlowRule using a FlowRule constructor.
*/
@Test
public
void
testCreationFromFlowRule
()
{
assertThat
(
defaultFlowRule1
.
deviceId
(),
is
(
flowRule1
.
deviceId
()));
assertThat
(
defaultFlowRule1
.
appId
(),
is
(
flowRule1
.
appId
()));
assertThat
(
defaultFlowRule1
.
id
(),
is
(
flowRule1
.
id
()));
assertThat
(
defaultFlowRule1
.
isPermanent
(),
is
(
flowRule1
.
isPermanent
()));
assertThat
(
defaultFlowRule1
.
priority
(),
is
(
flowRule1
.
priority
()));
assertThat
(
defaultFlowRule1
.
selector
(),
is
(
flowRule1
.
selector
()));
assertThat
(
defaultFlowRule1
.
treatment
(),
is
(
flowRule1
.
treatment
()));
assertThat
(
defaultFlowRule1
.
timeout
(),
is
(
flowRule1
.
timeout
()));
}
/**
* Tests creation of a DefaultFlowRule using a FlowId constructor.
*/
@Test
public
void
testCreationWithFlowId
()
{
final
DefaultFlowRule
rule
=
new
DefaultFlowRule
(
did
(
"1"
),
SELECTOR
,
TREATMENT
,
22
,
33
,
44
,
false
);
assertThat
(
rule
.
deviceId
(),
is
(
did
(
"1"
)));
assertThat
(
rule
.
id
().
value
(),
is
(
33L
));
assertThat
(
rule
.
isPermanent
(),
is
(
false
));
assertThat
(
rule
.
priority
(),
is
(
22
));
assertThat
(
rule
.
selector
(),
is
(
SELECTOR
));
assertThat
(
rule
.
treatment
(),
is
(
TREATMENT
));
assertThat
(
rule
.
timeout
(),
is
(
44
));
}
/**
* Tests the creation of a DefaultFlowRule using an AppId constructor.
*/
@Test
public
void
testCreationWithAppId
()
{
final
DefaultFlowRule
rule
=
new
DefaultFlowRule
(
did
(
"1"
),
SELECTOR
,
TREATMENT
,
22
,
APP_ID
,
44
,
false
);
assertThat
(
rule
.
deviceId
(),
is
(
did
(
"1"
)));
assertThat
(
rule
.
isPermanent
(),
is
(
false
));
assertThat
(
rule
.
priority
(),
is
(
22
));
assertThat
(
rule
.
selector
(),
is
(
SELECTOR
));
assertThat
(
rule
.
treatment
(),
is
(
TREATMENT
));
assertThat
(
rule
.
timeout
(),
is
(
44
));
}
}
utils/junit/src/main/java/org/onlab/junit/ImmutableClassChecker.java
View file @
702dae8
...
...
@@ -43,9 +43,9 @@ public class ImmutableClassChecker {
* @param clazz the class to check
* @return true if the given class is a properly specified immutable class.
*/
private
boolean
isImmutableClass
(
Class
<?>
clazz
)
{
private
boolean
isImmutableClass
(
Class
<?>
clazz
,
boolean
allowNonFinalClass
)
{
// class must be declared final
if
(!
Modifier
.
isFinal
(
clazz
.
getModifiers
()))
{
if
(!
allowNonFinalClass
&&
!
Modifier
.
isFinal
(
clazz
.
getModifiers
()))
{
failureReason
=
"a class that is not final"
;
return
false
;
}
...
...
@@ -113,16 +113,43 @@ public class ImmutableClassChecker {
}
/**
* Assert that the given class adheres to the
utility
class rules.
* Assert that the given class adheres to the
immutable
class rules.
*
* @param clazz the class to check
*
* @throws java.lang.AssertionError if the class is not a
valid
*
utility
class
* @throws java.lang.AssertionError if the class is not a
n
*
immutable
class
*/
public
static
void
assertThatClassIsImmutable
(
Class
<?>
clazz
)
{
final
ImmutableClassChecker
checker
=
new
ImmutableClassChecker
();
if
(!
checker
.
isImmutableClass
(
clazz
))
{
if
(!
checker
.
isImmutableClass
(
clazz
,
false
))
{
final
Description
toDescription
=
new
StringDescription
();
final
Description
mismatchDescription
=
new
StringDescription
();
checker
.
describeTo
(
toDescription
);
checker
.
describeMismatch
(
mismatchDescription
);
final
String
reason
=
"\n"
+
"Expected: is \""
+
toDescription
.
toString
()
+
"\"\n"
+
" but : was \""
+
mismatchDescription
.
toString
()
+
"\""
;
throw
new
AssertionError
(
reason
);
}
}
/**
* Assert that the given class adheres to the immutable class rules, but
* is not declared final. Classes that need to be inherited from cannot be
* declared final.
*
* @param clazz the class to check
*
* @throws java.lang.AssertionError if the class is not an
* immutable class
*/
public
static
void
assertThatClassIsImmutableBaseClass
(
Class
<?>
clazz
)
{
final
ImmutableClassChecker
checker
=
new
ImmutableClassChecker
();
if
(!
checker
.
isImmutableClass
(
clazz
,
true
))
{
final
Description
toDescription
=
new
StringDescription
();
final
Description
mismatchDescription
=
new
StringDescription
();
...
...
Please
register
or
login
to post a comment