diff --git a/bundles/org.eclipse.passage.lic.api/src/org/eclipse/passage/lic/internal/api/conditions/mining/ConditionTransport.java b/bundles/org.eclipse.passage.lic.api/src/org/eclipse/passage/lic/internal/api/conditions/mining/ConditionTransport.java index 4c481176d..64ce2cdff 100644 --- a/bundles/org.eclipse.passage.lic.api/src/org/eclipse/passage/lic/internal/api/conditions/mining/ConditionTransport.java +++ b/bundles/org.eclipse.passage.lic.api/src/org/eclipse/passage/lic/internal/api/conditions/mining/ConditionTransport.java @@ -14,14 +14,14 @@ import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.Collection; import org.eclipse.passage.lic.internal.api.conditions.Condition; import org.eclipse.passage.lic.internal.api.registry.Service; /** - * Persistence interface for {@link Condition}(s). + * Encapsulates knowledge how to retrieve {@link Condition}(s) from an input + * source of the configured {@linkplain ContentType}. */ public interface ConditionTransport extends Service { @@ -31,10 +31,4 @@ public interface ConditionTransport extends Service { */ Collection read(InputStream input) throws IOException; - /** - * Writes {@link Condition}(s) to the given {@link OutputStream}. Stream remains - * open. - */ - void write(Collection conditions, OutputStream output) throws IOException; - } diff --git a/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/ConditionPack.java b/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/ConditionPack.java index e436a0086..0c10820d3 100644 --- a/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/ConditionPack.java +++ b/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/ConditionPack.java @@ -21,11 +21,11 @@ * Intentionally ugly. */ @SuppressWarnings("restriction") -final class ConditionPack { +public final class ConditionPack { Collection conditions; - ConditionPack(Collection conditions) { + public ConditionPack(Collection conditions) { this.conditions = conditions; } diff --git a/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/JsonConditionTransport.java b/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/JsonConditionTransport.java index 9866292eb..701d0faed 100644 --- a/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/JsonConditionTransport.java +++ b/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/JsonConditionTransport.java @@ -14,9 +14,7 @@ import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.Collection; -import java.util.Objects; import org.eclipse.passage.lic.internal.api.conditions.Condition; import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport; @@ -37,12 +35,4 @@ public Collection read(InputStream input) throws IOException { return new JsonObjectMapper().get().readValue(input, ConditionPack.class).conditions; } - @SuppressWarnings("resource") - @Override - public void write(Collection conditions, OutputStream output) throws IOException { - Objects.requireNonNull(conditions, "JsonConditionTransport::write::conditions"); //$NON-NLS-1$ - Objects.requireNonNull(output, "JsonConditionTransport::write::output"); //$NON-NLS-1$ - output.write(new JsonObjectMapper().get().writeValueAsBytes(new ConditionPack(conditions))); - } - } diff --git a/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/JsonObjectMapper.java b/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/JsonObjectMapper.java index 065f5ae14..7c4427e70 100644 --- a/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/JsonObjectMapper.java +++ b/bundles/org.eclipse.passage.lic.json/src/org/eclipse/passage/lic/internal/json/tobemoved/JsonObjectMapper.java @@ -25,7 +25,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule; @SuppressWarnings("restriction") -class JsonObjectMapper implements Supplier { +public final class JsonObjectMapper implements Supplier { @Override public ObjectMapper get() { diff --git a/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/XmiConditionTransport.java b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/XmiConditionTransport.java index b5744670a..0fa8789a1 100644 --- a/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/XmiConditionTransport.java +++ b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/XmiConditionTransport.java @@ -50,9 +50,13 @@ public Iterable readConditions(InputStream input) throws IOE return extracted; } + /** + * FIXME: beware: on reading we expect LicensePackDescriptor to be a root for + * conditions but on reading we put conditions directly into contents. Will not + * work both sides. Totally deprecated. + */ @Override - public void writeConditions(Iterable conditions, OutputStream output) - throws IOException { + public void writeConditions(Iterable conditions, OutputStream output) throws IOException { Resource resource = new XMIResourceImpl(); EList contents = resource.getContents(); for (LicensingCondition conditionDescriptor : conditions) { diff --git a/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/XmiConditionTransport.java b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/XmiConditionTransport.java index a6ae3f235..bf51d40ab 100644 --- a/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/XmiConditionTransport.java +++ b/bundles/org.eclipse.passage.lic.licenses.migration/src/org/eclipse/passage/lic/internal/licenses/migration/tobemoved/XmiConditionTransport.java @@ -14,21 +14,15 @@ import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.Collection; import java.util.HashMap; import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import org.eclipse.emf.common.util.EList; -import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; -import org.eclipse.passage.lic.api.conditions.LicensingCondition; -import org.eclipse.passage.lic.base.conditions.LicensingConditions; import org.eclipse.passage.lic.internal.api.conditions.Condition; import org.eclipse.passage.lic.internal.api.conditions.EvaluationType; -import org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed; import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport; import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType; import org.eclipse.passage.lic.internal.base.conditions.BaseCondition; @@ -75,32 +69,4 @@ private Condition condition(LicenseGrantDescriptor descriptor) { descriptor.getConditionExpression())); } - @Override - public void write(Collection conditions, OutputStream output) throws IOException { - Resource resource = new XMIResourceImpl(); - EList contents = resource.getContents(); - for (Condition condition : conditions) { - LicensingCondition conditionDescriptor = condition(condition); - if (conditionDescriptor instanceof EObject) { - EObject eObject = (EObject) conditionDescriptor; - contents.add(eObject); - } - } - resource.save(output, new HashMap<>()); - } - - // FIXME: severe error here: none of'em are EObject! Find a way to construct - // true-emf LicenseGrantDescriptors - private LicensingCondition condition(Condition condition) { - LicensingCondition lic = LicensingConditions.create(// - condition.feature(), // - condition.versionMatch().version(), // - condition.versionMatch().rule().identifier(), // - ((ValidityPeriodClosed) condition.validityPeriod()).from(), // - ((ValidityPeriodClosed) condition.validityPeriod()).to(), // - condition.evaluationInstructions().type().identifier(), // - condition.evaluationInstructions().expression()); - throw new UnsupportedOperationException(); - } - } diff --git a/tests/org.eclipse.passage.lic.api.tests/src/org/eclipse/passage/lic/api/tests/conditions/mining/ConditionTransportContractTest.java b/tests/org.eclipse.passage.lic.api.tests/src/org/eclipse/passage/lic/api/tests/conditions/mining/ConditionTransportContractTest.java index 5ad819bf0..6d4fd69b9 100644 --- a/tests/org.eclipse.passage.lic.api.tests/src/org/eclipse/passage/lic/api/tests/conditions/mining/ConditionTransportContractTest.java +++ b/tests/org.eclipse.passage.lic.api.tests/src/org/eclipse/passage/lic/api/tests/conditions/mining/ConditionTransportContractTest.java @@ -17,7 +17,6 @@ import static org.junit.Assume.assumeTrue; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Collection; import java.util.HashSet; @@ -25,6 +24,7 @@ import java.util.stream.Collectors; import org.eclipse.passage.lic.internal.api.conditions.Condition; +import org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed; import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport; import org.junit.Test; @@ -36,11 +36,6 @@ public final void identifiedByContentType() { assertNotNull(transport().id()); } - @Test(expected = NullPointerException.class) - public final void conditionsAreMandatory() throws IOException { - transport().write(null, new ByteArrayOutputStream()); - } - @Test public final void readYourOwnWritings() throws IOException { // given @@ -48,33 +43,38 @@ public final void readYourOwnWritings() throws IOException { assumeTrue("Too few: supply at least two test conditions for transportation", sedentaries.size() > 1); //$NON-NLS-1$ ConditionTransport transport = transport(); // when - String serialized = write(transport, sedentaries); - Collection nomads = read(transport, serialized); + Collection nomads = read(transport, serialized(sedentaries)); // then assertEquals(new HashSet<>(textual(sedentaries)), new HashSet<>(textual(nomads))); } - private String write(ConditionTransport transport, Collection conditions) throws IOException { - ByteArrayOutputStream output = new ByteArrayOutputStream(); - transport.write(conditions, output); - return new String(output.toByteArray()); - } - private Collection read(ConditionTransport transport, String source) throws IOException { return transport.read(new ByteArrayInputStream(source.getBytes())); } - private Set textual(Collection conditions) { - return conditions.stream()// + private Set textual(Collection condition) { + return condition.stream()// .map(this::textual) // .collect(Collectors.toSet()); } + private String textual(Condition condition) { + return new StringBuilder()// + .append(condition.feature())// + .append(condition.versionMatch().version())// + .append(condition.versionMatch().rule().identifier())// + .append(((ValidityPeriodClosed) condition.validityPeriod()).from())// + .append(((ValidityPeriodClosed) condition.validityPeriod()).to())// + .append(condition.evaluationInstructions().type().identifier())// + .append(condition.evaluationInstructions().expression()) // + .toString(); + } + /** - * Provide any textual representation of a condition that distinguishes two - * instances with differing content. + * Supply source for reading which is expected to be parsed to + * {@code conditions()} */ - protected abstract String textual(Condition condition); + protected abstract String serialized(Collection condition); /** * Produce new instance of a transport under test diff --git a/tests/org.eclipse.passage.lic.api.tests/src/org/eclipse/passage/lic/api/tests/fakes/FakeConditionTransport.java b/tests/org.eclipse.passage.lic.api.tests/src/org/eclipse/passage/lic/api/tests/fakes/FakeConditionTransport.java index 89b91a175..c9f879be1 100644 --- a/tests/org.eclipse.passage.lic.api.tests/src/org/eclipse/passage/lic/api/tests/fakes/FakeConditionTransport.java +++ b/tests/org.eclipse.passage.lic.api.tests/src/org/eclipse/passage/lic/api/tests/fakes/FakeConditionTransport.java @@ -14,7 +14,6 @@ import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.Collection; import org.eclipse.passage.lic.internal.api.conditions.Condition; @@ -34,9 +33,4 @@ public Collection read(InputStream input) throws IOException { throw new UnsupportedOperationException(); } - @Override - public void write(Collection conditions, OutputStream output) throws IOException { - throw new UnsupportedOperationException(); - } - } diff --git a/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/conditions/mining/HeadOnlyTransport.java b/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/conditions/mining/HeadOnlyTransport.java index 0e62bd0b0..56b6bebe8 100644 --- a/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/conditions/mining/HeadOnlyTransport.java +++ b/tests/org.eclipse.passage.lic.base.tests/src/org/eclipse/passage/lic/internal/base/tests/conditions/mining/HeadOnlyTransport.java @@ -16,7 +16,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; -import java.io.OutputStream; import java.util.ArrayList; import java.util.Collection; import java.util.Date; @@ -62,11 +61,6 @@ public Collection read(InputStream input) throws IOException { return conditions; } - @Override - public void write(Collection conditions, OutputStream output) throws IOException { - throw new UnsupportedOperationException(); - } - private VersionMatch commonVersion() { return new BaseVersionMatch("1.0.0", new MatchingRuleDefault()); //$NON-NLS-1$ } diff --git a/tests/org.eclipse.passage.lic.json.tests/src/org/eclipse/passage/lic/json/tests/tobemoved/JsonConditionTransportTest.java b/tests/org.eclipse.passage.lic.json.tests/src/org/eclipse/passage/lic/json/tests/tobemoved/JsonConditionTransportTest.java index 26a5ada80..1b2e2f60f 100644 --- a/tests/org.eclipse.passage.lic.json.tests/src/org/eclipse/passage/lic/json/tests/tobemoved/JsonConditionTransportTest.java +++ b/tests/org.eclipse.passage.lic.json.tests/src/org/eclipse/passage/lic/json/tests/tobemoved/JsonConditionTransportTest.java @@ -16,30 +16,12 @@ import org.eclipse.passage.lic.api.tests.conditions.mining.ConditionTransportContractTest; import org.eclipse.passage.lic.internal.api.conditions.Condition; -import org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed; import org.eclipse.passage.lic.internal.api.conditions.mining.ConditionTransport; import org.eclipse.passage.lic.internal.json.tobemoved.JsonConditionTransport; @SuppressWarnings("restriction") public final class JsonConditionTransportTest extends ConditionTransportContractTest { - @Override - protected String textual(Condition condition) { - return new StringBuffer() // - .append(condition.feature()).append('|') // - .append(condition.versionMatch().version()).append('|') // - .append(condition.versionMatch().rule().identifier()).append('|') // - .append(textual(((ValidityPeriodClosed) condition.validityPeriod()))).append('|') // - .append(condition.evaluationInstructions().type().identifier()).append('|') // - .append(condition.evaluationInstructions().expression()) // - .toString(); - - } - - private String textual(ValidityPeriodClosed period) { - return String.format("%d-%d", period.from().getTime(), period.to().getTime()); //$NON-NLS-1$ - } - @Override protected ConditionTransport transport() { return new JsonConditionTransport(); @@ -50,4 +32,9 @@ protected Collection conditions() { return new TestConditions().conditions(); } + @Override + protected String serialized(Collection condition) { + return new TestConditions().textual(); + } + } diff --git a/tests/org.eclipse.passage.lic.json.tests/src/org/eclipse/passage/lic/json/tests/tobemoved/TestConditions.java b/tests/org.eclipse.passage.lic.json.tests/src/org/eclipse/passage/lic/json/tests/tobemoved/TestConditions.java index 805533516..f9e83cb98 100644 --- a/tests/org.eclipse.passage.lic.json.tests/src/org/eclipse/passage/lic/json/tests/tobemoved/TestConditions.java +++ b/tests/org.eclipse.passage.lic.json.tests/src/org/eclipse/passage/lic/json/tests/tobemoved/TestConditions.java @@ -12,6 +12,11 @@ *******************************************************************************/ package org.eclipse.passage.lic.json.tests.tobemoved; +import static org.junit.Assert.fail; +import static org.junit.Assume.assumeNoException; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.Date; @@ -26,6 +31,10 @@ import org.eclipse.passage.lic.internal.base.conditions.MatchingRuleCompatible; import org.eclipse.passage.lic.internal.base.conditions.MatchingRuleEquivalent; import org.eclipse.passage.lic.internal.base.conditions.MatchingRulePerfect; +import org.eclipse.passage.lic.internal.json.tobemoved.ConditionPack; +import org.eclipse.passage.lic.internal.json.tobemoved.JsonObjectMapper; + +import com.fasterxml.jackson.core.JsonProcessingException; @SuppressWarnings("restriction") public final class TestConditions { @@ -50,4 +59,17 @@ Collection conditions() { return conditions; } + String textual() { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + try { + output.write(new JsonObjectMapper().get().writeValueAsBytes(new ConditionPack(conditions))); + return new String(output.toByteArray(), "UTF-8"); //$NON-NLS-1$ + } catch (JsonProcessingException e) { + fail("Is not intendd to fail on valid data"); //$NON-NLS-1$ + } catch (IOException e) { + assumeNoException(e); // file system misbehavior must not fail test + } + throw new RuntimeException("unreachable"); //$NON-NLS-1$ + } + }