1+ package org .scijava .ops .matcher ;
2+
3+ import java .util .function .Function ;
4+
5+ import org .junit .Assert ;
6+ import org .junit .Test ;
7+ import org .scijava .ops .function .Computers ;
8+ import org .scijava .ops .AbstractTestEnvironment ;
9+ import org .scijava .ops .OpDependency ;
10+ import org .scijava .ops .OpField ;
11+ import org .scijava .ops .core .Op ;
12+ import org .scijava .ops .core .OpCollection ;
13+ import org .scijava .param .Parameter ;
14+ import org .scijava .plugin .Plugin ;
15+ import org .scijava .struct .ItemIO ;
16+
17+ @ Plugin (type = OpCollection .class )
18+ public class DefaultMatchingErrorTest extends AbstractTestEnvironment {
19+
20+ @ OpField (names = "test.duplicateOp" )
21+ public final Function <Double , Double > duplicateA = (in ) -> in ;
22+
23+ @ OpField (names = "test.duplicateOp" )
24+ public final Function <Double , Double > duplicateB = (in ) -> in ;
25+
26+ @ Test
27+ public void duplicateErrorRegressionTest () {
28+ try {
29+ ops .op ("test.duplicateOp" ).inType (Double .class ).outType (Double .class )
30+ .function ();
31+ Assert .fail ();
32+ }
33+ catch (IllegalArgumentException e ) {
34+ Assert .assertTrue (e .getMessage ().startsWith (
35+ "org.scijava.ops.matcher.OpMatchingException: " +
36+ "Multiple 'test.duplicateOp/java.util.function.Function<java.lang.Double, java.lang.Double>' " +
37+ "ops of priority 0.0:" ));
38+ }
39+ }
40+
41+ @ Test
42+ public void missingDependencyRegressionTest () {
43+ try {
44+ ops .op ("test.missingDependencyOp" ).input (1. ).outType (Double .class )
45+ .apply ();
46+ Assert .fail ("Expected DependencyMatchingException" );
47+ }
48+ catch (IllegalArgumentException e ) {
49+ Throwable cause = e .getCause ();
50+ Assert .assertTrue (cause instanceof DependencyMatchingException );
51+ String message = cause .getMessage ();
52+ Assert .assertTrue (message .contains ("Name: \" test.nonexistingOp\" " ));
53+ }
54+ }
55+
56+ /**
57+ * Assert the DependencyMatchingException contains both dependencies in the chain
58+ */
59+ @ Test
60+ public void missingNestedDependencyRegressionTest () {
61+ try {
62+ ops .op ("test.outsideOp" ).input (1. ).outType (Double .class ).apply ();
63+ Assert .fail ("Expected DependencyMatchingException" );
64+ }
65+ catch (IllegalArgumentException e ) {
66+ Throwable cause = e .getCause ();
67+ Assert .assertTrue (cause instanceof DependencyMatchingException );
68+ String message = cause .getMessage ();
69+ Assert .assertTrue (message .contains ("Name: \" test.missingDependencyOp\" " ));
70+ Assert .assertTrue (message .contains ("Name: \" test.nonexistingOp\" " ));
71+ }
72+ }
73+
74+ @ Test
75+ public void missingDependencyViaAdaptationTest () {
76+ Double [] d = new Double [0 ];
77+ try {
78+ ops .op ("test.adaptMissingDep" ).input (d ).outType (Double [].class ).apply ();
79+ Assert .fail ("Expected DependencyMatchingException" );
80+ } catch (IllegalArgumentException e ) {
81+ Throwable cause = e .getCause ();
82+ Assert .assertTrue (cause instanceof DependencyMatchingException );
83+ String message = cause .getMessage ();
84+ Assert .assertTrue (message .contains ("adapted" ));
85+ Assert .assertTrue (message .contains ("Name: \" test.nonexistingOp\" " ));
86+
87+ }
88+ }
89+
90+ }
91+
92+ @ Plugin (type = Op .class , name = "test.furtherOutsideOp" )
93+ @ Parameter (key = "in" )
94+ @ Parameter (key = "out" , itemIO = ItemIO .OUTPUT )
95+ class FurtherDependentOp implements Function <Double , Double > {
96+
97+ @ OpDependency (name = "test.outsideOp" )
98+ private Function <Double , Double > op ;
99+
100+ @ Override
101+ public Double apply (Double t ) {
102+ return op .apply (t );
103+ }
104+
105+ }
106+
107+ @ Plugin (type = Op .class , name = "test.outsideOp" )
108+ @ Parameter (key = "in" )
109+ @ Parameter (key = "out" , itemIO = ItemIO .OUTPUT )
110+ class DependentOp implements Function <Double , Double > {
111+
112+ @ OpDependency (name = "test.missingDependencyOp" )
113+ private Function <Double , Double > op ;
114+
115+ @ Override
116+ public Double apply (Double t ) {
117+ return op .apply (t );
118+ }
119+
120+ }
121+
122+ @ Plugin (type = Op .class , name = "test.missingDependencyOp" )
123+ @ Parameter (key = "in" )
124+ @ Parameter (key = "out" , itemIO = ItemIO .OUTPUT )
125+ class MissingDependencyOp implements Function <Double , Double > {
126+
127+ @ OpDependency (name = "test.nonexistingOp" )
128+ private Function <Double , Double > op ;
129+
130+ @ Override
131+ public Double apply (Double t ) {
132+ return op .apply (t );
133+ }
134+
135+ }
136+
137+ @ Plugin (type = Op .class , name = "test.adaptMissingDep" )
138+ @ Parameter (key = "in" )
139+ @ Parameter (key = "out" , itemIO = ItemIO .OUTPUT )
140+ class MissingDependencyOpArr1 implements Computers .Arity1 <Double [], Double []> {
141+
142+ @ OpDependency (name = "test.nonexistingOp" )
143+ private Function <Double , Double > op ;
144+
145+ @ Override
146+ public void compute (Double [] t , Double [] out ) {
147+ }
148+
149+ }
150+
151+ @ Plugin (type = Op .class , name = "test.adaptMissingDep" )
152+ @ Parameter (key = "in" )
153+ @ Parameter (key = "out" , itemIO = ItemIO .OUTPUT )
154+ class MissingDependencyOpArr2 implements Function <Double , Double > {
155+
156+ @ OpDependency (name = "test.nonexistingOp" )
157+ private Function <Double , Double > op ;
158+
159+ @ Override
160+ public Double apply (Double t ) {
161+ return op .apply (t );
162+ }
163+
164+ }
0 commit comments