Skip to content

Commit b6bbe6a

Browse files
committed
#50 finalized on renameable datasource
1 parent 1320cd1 commit b6bbe6a

File tree

4 files changed

+75
-123
lines changed

4 files changed

+75
-123
lines changed

src/main/java/org/simplejavamail/mailer/internal/datasource/NamedDataSource.java

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/main/java/org/simplejavamail/mailer/internal/mailsender/MimeMessageHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.simplejavamail.email.AttachmentResource;
88
import org.simplejavamail.email.Email;
99
import org.simplejavamail.email.Recipient;
10-
import org.simplejavamail.mailer.internal.datasource.NamedDataSource;
1110

1211
import javax.activation.DataHandler;
1312
import javax.activation.DataSource;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.simplejavamail.mailer.internal.mailsender;
2+
3+
import javax.activation.DataSource;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
7+
8+
import static org.simplejavamail.internal.util.MiscUtil.valueNullOrEmpty;
9+
10+
/**
11+
* Allows given datasource to be renamed (from {@link javax.activation.DataHandler} perspective).
12+
*
13+
* @author Lukas Kosina
14+
*/
15+
class NamedDataSource implements DataSource {
16+
17+
/**
18+
* Original data source used for attachment.
19+
*/
20+
private final DataSource dataSource;
21+
22+
/**
23+
* The new name (optional), which will be applied as email attachment.
24+
*/
25+
private final String name;
26+
27+
/**
28+
* Used for wrapping data source in parameter. Method {@link NamedDataSource#getName()} will
29+
* not use the original name, but it will use the name in the parameter instead (if provided).
30+
*
31+
* @param dataSource wrapped data source
32+
* @param name new name of data source
33+
*/
34+
public NamedDataSource(String name, DataSource dataSource) {
35+
this.dataSource = dataSource;
36+
this.name = name;
37+
}
38+
39+
/**
40+
* @return {@link DataSource#getInputStream()}
41+
*/
42+
@Override
43+
public InputStream getInputStream()
44+
throws IOException {
45+
return dataSource.getInputStream();
46+
}
47+
48+
/**
49+
* @return {@link DataSource#getOutputStream()}
50+
*/
51+
@Override
52+
public OutputStream getOutputStream()
53+
throws IOException {
54+
return dataSource.getOutputStream();
55+
}
56+
57+
/**
58+
* @return {@link DataSource#getContentType()}
59+
*/
60+
@Override
61+
public String getContentType() {
62+
return dataSource.getContentType();
63+
}
64+
65+
/**
66+
* {@link #name} if provided, {@link DataSource#getName()} of the original datasource otherwise.
67+
*
68+
* @return name of data source
69+
*/
70+
@Override
71+
public String getName() {
72+
return !valueNullOrEmpty(name) ? name : dataSource.getName();
73+
}
74+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.simplejavamail.mailer.internal.datasource;
1+
package org.simplejavamail.mailer.internal.mailsender;
22

33
import org.junit.Before;
44
import org.junit.Test;
@@ -29,53 +29,24 @@ public void renameWillWork() throws Exception {
2929
verifyZeroInteractions(dataSource);
3030
}
3131

32-
@Test
33-
public void originalNameWillWork() throws Exception {
34-
DataSource testDataSource = new NamedDataSource(dataSource);
35-
assertThat(testDataSource.getName()).isEqualTo("testName");
36-
verify(dataSource).getName();
37-
}
38-
3932
@Test
4033
public void inputStreamWillBeTheSame1() throws Exception {
4134
DataSource testDataSource = new NamedDataSource("newName", dataSource);
4235
testDataSource.getInputStream();
4336
verify(dataSource).getInputStream();
4437
}
4538

46-
@Test
47-
public void inputStreamWillBeTheSame2() throws Exception {
48-
DataSource testDataSource = new NamedDataSource(dataSource);
49-
testDataSource.getInputStream();
50-
verify(dataSource).getInputStream();
51-
}
52-
5339
@Test
5440
public void outputStreamWillBeTheSame1() throws Exception {
5541
DataSource testDataSource = new NamedDataSource("newName", dataSource);
5642
testDataSource.getOutputStream();
5743
verify(dataSource).getOutputStream();
5844
}
5945

60-
@Test
61-
public void outputStreamWillBeTheSame2() throws Exception {
62-
DataSource testDataSource = new NamedDataSource(dataSource);
63-
testDataSource.getOutputStream();
64-
verify(dataSource).getOutputStream();
65-
}
66-
6746
@Test
6847
public void contentTypeStreamWillBeTheSame1() throws Exception {
6948
DataSource testDataSource = new NamedDataSource("newName", dataSource);
7049
testDataSource.getContentType();
7150
verify(dataSource).getContentType();
7251
}
73-
74-
@Test
75-
public void contentTypeStreamWillBeTheSame2() throws Exception {
76-
DataSource testDataSource = new NamedDataSource(dataSource);
77-
testDataSource.getContentType();
78-
verify(dataSource).getContentType();
79-
}
80-
8152
}

0 commit comments

Comments
 (0)