-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch sampler to use more of Spring to make the code simplier
- Loading branch information
Krzysztof Suszynski
committed
May 24, 2019
1 parent
8047231
commit e13a0be
Showing
35 changed files
with
695 additions
and
540 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,14 @@ | |
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import pl.wavesoftware.plugs.tools.packager.api.digest.ProjectDigest; | ||
import pl.wavesoftware.plugs.tools.packager.api.model.Project; | ||
import pl.wavesoftware.plugs.tools.packager.sample.PackagerSamplerContext; | ||
import pl.wavesoftware.plugs.tools.packager.sample.SamplerContext; | ||
import pl.wavesoftware.plugs.tools.packager.sample.project.Projects; | ||
import pl.wavesoftware.plugs.tools.packager.testing.SamplerExtension; | ||
import pl.wavesoftware.plugs.tools.packager.sample.project.SimpleProject; | ||
|
||
import java.io.IOException; | ||
|
||
|
@@ -32,14 +35,18 @@ | |
* @author <a href="mailto:[email protected]">Krzysztof Suszynski</a> | ||
* @since 0.1.0 | ||
*/ | ||
@ExtendWith(SamplerExtension.class) | ||
@ExtendWith(SpringExtension.class) | ||
@ContextConfiguration(classes = PackagerSamplerContext.class) | ||
class ProjectDigestImplTest { | ||
|
||
@Autowired | ||
private SamplerContext context; | ||
|
||
@Test | ||
void digest(SamplerContext context) throws IOException { | ||
void digest() throws IOException { | ||
// given | ||
ProjectDigest digester = new ProjectDigestImpl(); | ||
Project project = context.get(Projects.SIMPLE); | ||
Project project = context.get(SimpleProject.class); | ||
|
||
// when | ||
CharSequence digest = digester.digest(project); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...les/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/AbstractSamplerContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2019 Wave Software | ||
* | ||
* 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 pl.wavesoftware.plugs.tools.packager.sample; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
abstract class AbstractSamplerContext implements SamplerContext { | ||
private static final Logger LOGGER = | ||
LoggerFactory.getLogger(AbstractSamplerContext.class); | ||
private final Map<Class<? extends Sampler<?>>, Object> samples = | ||
new HashMap<>(); | ||
|
||
@Override | ||
public <T> T get(Class<? extends Sampler<T>> spec) { | ||
@SuppressWarnings("unchecked") | ||
T val = (T) samples.computeIfAbsent(spec, ignored -> createNew(spec)); | ||
return val; | ||
} | ||
|
||
@Override | ||
public <T> T createNew(Class<? extends Sampler<T>> spec) { | ||
T sample = getSampler(spec).create(); | ||
if (LOGGER.isDebugEnabled()) { | ||
LOGGER.debug( | ||
"Created sample {} for spec {}", | ||
Integer.toHexString(sample.hashCode()), spec | ||
); | ||
} | ||
return sample; | ||
} | ||
|
||
@Override | ||
public void destroy() { | ||
samples.clear(); | ||
} | ||
|
||
protected abstract <T> Sampler<T> getSampler(Class<? extends Sampler<T>> spec); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...r-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/RandomSourceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (c) 2019 Wave Software | ||
* | ||
* 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 pl.wavesoftware.plugs.tools.packager.sample; | ||
|
||
import io.vavr.Lazy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.core.env.Environment; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Random; | ||
import java.util.zip.CRC32; | ||
|
||
final class RandomSourceImpl implements RandomSource { | ||
private static final Logger LOGGER = | ||
LoggerFactory.getLogger(RandomSourceImpl.class); | ||
private static final String SAMPLER_SEED_PROPERTY = "sampler.seed"; | ||
|
||
private final RandomSeedSupplier seedSupplier = new RandomSeedSupplier(); | ||
private final Environment environment; | ||
private final Lazy<Random> randomLazy = Lazy.of(this::doGet); | ||
|
||
RandomSourceImpl(Environment environment) { | ||
this.environment = environment; | ||
} | ||
|
||
@Override | ||
public Random get() { | ||
return randomLazy.get(); | ||
} | ||
|
||
private Random doGet() { | ||
String seed = environment.getProperty( | ||
SAMPLER_SEED_PROPERTY, | ||
seedSupplier.get().toString() | ||
); | ||
CRC32 crc32 = new CRC32(); | ||
crc32.update(seed.getBytes(StandardCharsets.UTF_8)); | ||
long randomSeed = crc32.getValue(); | ||
LOGGER.info( | ||
"Using seed: {}. To re-execute same exact case, set " + | ||
"environmental variable `export SAMPLER_SEED={}` or Java's " + | ||
"property `-D{}={}` with that seed value.", | ||
seed, | ||
seed, | ||
SAMPLER_SEED_PROPERTY, seed | ||
); | ||
return new Random(randomSeed); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
...ckager-samples/src/main/java/pl/wavesoftware/plugs/tools/packager/sample/SamplerBean.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.