Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Hedtke, Moritz
AuD 2021 H05 Tests
Commits
ca15a7d1
Verified
Commit
ca15a7d1
authored
Jun 12, 2021
by
Daniel Mangold
Browse files
Added update mechanism for config file
parent
f4bd7c91
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/test/java/h05/Config.java
View file @
ca15a7d1
package
h05
;
import
java.
util.List
;
import
java.util.
Map
;
import
java.util.
Random
;
import
java.
lang.reflect.Field
;
import
java.util.
*
;
import
java.util.
stream.Collectors
;
/**
* This class contains constants that are used as settings for the tests.
...
...
@@ -11,6 +11,8 @@ import java.util.Random;
@SuppressWarnings
({
"JavadocReference"
,
"unused"
})
public
class
Config
{
// >>>## UPDATE MARKER, DO NOT REMOVE, ONLY MODIFY THE LINES BELOW
/**
* Whether this file exists. Used by Utils.java to determine whether or not the config file is missing
*/
...
...
@@ -65,4 +67,14 @@ public class Config {
* @see h05.provider.RandomTreeProvider
*/
public
static
final
int
MAX_TREE_DEPTH
=
5
;
// ##<<< UPDATE MARKER, DO NOT REMOVE, DO NOT CHANGE ANYTHING BELOW THIS LINE
/**
* Returns a set of the names of all fields in this class
* @return a set of all fields defined in this class
*/
public
static
Set
<
String
>
getConfigs
()
{
return
Arrays
.
stream
(
Config
.
class
.
getDeclaredFields
()).
map
(
Field:
:
getName
).
collect
(
Collectors
.
toUnmodifiableSet
());
}
}
src/test/java/h05/Utils.java
View file @
ca15a7d1
...
...
@@ -29,7 +29,7 @@ public class Utils {
public
static
final
Map
<
Class
<?>,
Boolean
>
CLASS_CORRECT
=
new
HashMap
<>();
public
static
final
Map
<
Method
,
Boolean
>
METHOD_CORRECT
=
new
HashMap
<>();
private
static
final
String
LOCAL_VERSION
=
"
1.1
"
,
ASSIGNMENT_ID
=
"H05"
;
private
static
final
String
LOCAL_VERSION
=
"
2.0.0
"
,
ASSIGNMENT_ID
=
"H05"
;
private
static
final
Map
<
TestType
.
Type
,
String
>
METHOD_LOOKUP
=
Map
.
of
(
TestType
.
Type
.
CLASS
,
"checkClass"
,
TestType
.
Type
.
INTERFACE
,
"checkInterface"
...
...
@@ -208,6 +208,7 @@ public class Utils {
*/
private
static
boolean
checkForUpdates
()
{
HttpResponse
<
String
>
response
=
getHttpResource
(
".test_version"
);
boolean
persistentChanges
=
false
;
if
(
response
==
null
||
response
.
statusCode
()
!=
200
)
{
System
.
err
.
println
(
"Unable to fetch version from repository"
);
...
...
@@ -216,7 +217,6 @@ public class Utils {
try
{
JSONObject
remoteData
=
new
JSONObject
(
response
.
body
());
boolean
persistentChanges
=
false
;
Version
localVersion
=
new
Version
(
LOCAL_VERSION
),
remoteVersion
=
new
Version
(
remoteData
.
getString
(
"version"
));
...
...
@@ -269,11 +269,51 @@ public class Utils {
}
}
return
persistentChanges
;
}
catch
(
IOException
|
InterruptedException
|
NoSuchAlgorithmException
e
)
{
if
(
AUTO_UPDATE
&&
(
Boolean
)
getConfigOrDefault
(
"EXISTS"
,
false
))
{
JSONObject
configData
=
remoteData
.
getJSONObject
(
"config"
);
String
configStub
=
configData
.
getString
(
"stub"
);
StringBuilder
configFileContents
=
new
StringBuilder
(
" // >>>## UPDATE MARKER, DO NOT REMOVE, ONLY MODIFY THE LINES BELOW\n\n"
);
//noinspection unchecked
Set
<
String
>
existingFields
=
(
Set
<
String
>)
Class
.
forName
(
"h05.Config"
).
getDeclaredMethod
(
"getConfigs"
).
invoke
(
null
);
try
(
BufferedReader
reader
=
new
BufferedReader
(
new
FileReader
(
configData
.
getString
(
"file"
))))
{
boolean
insert
=
false
;
for
(
String
line
=
reader
.
readLine
();
!
line
.
matches
(
"\\w*// ##<<<.*"
);
line
=
reader
.
readLine
())
{
if
(
line
.
matches
(
"\\w*// >>>##.*"
))
{
reader
.
readLine
();
insert
=
true
;
}
if
(
insert
)
configFileContents
.
append
(
line
);
}
}
for
(
Map
.
Entry
<
String
,
Object
>
constantEntry
:
configData
.
getJSONObject
(
"constants"
).
toMap
().
entrySet
())
{
String
fieldName
=
constantEntry
.
getKey
(),
fieldData
=
(
String
)
constantEntry
.
getValue
();
if
(!
existingFields
.
contains
(
fieldName
))
configFileContents
.
append
(
fieldData
).
append
(
"\n\n"
);
}
configFileContents
.
append
(
" // ##<<< UPDATE MARKER, DO NOT REMOVE, DO NOT CHANGE ANYTHING BELOW THIS LINE\n\n"
);
for
(
Map
.
Entry
<
String
,
Object
>
methodEntry
:
configData
.
getJSONObject
(
"methods"
).
toMap
().
entrySet
())
configFileContents
.
append
((
String
)
methodEntry
.
getValue
()).
append
(
"\n\n"
);
persistentChanges
=
true
;
try
(
BufferedWriter
writer
=
new
BufferedWriter
(
new
FileWriter
(
configData
.
getString
(
"file"
))))
{
writer
.
write
(
configStub
.
replaceFirst
(
">>>##<<<"
,
configFileContents
.
toString
()));
}
}
}
catch
(
IOException
|
InterruptedException
|
NoSuchAlgorithmException
|
ReflectiveOperationException
e
)
{
e
.
printStackTrace
();
return
true
;
}
return
persistentChanges
;
}
/**
...
...
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